PostgreSQL Extension Multiple SQL Files


I'm creating a custom extension for PostgreSQL. I can get the extension to create successfully, but my foo–0.1.sql is getting rather large and complex. I'd like to break it out into multiple files for ease of maintenance but I can't figure out from the docs how to do this.

It seems like I could go one of two routes:

  1. Use a built-in functionality with PG's extension building infrastructure to automatically use all of my .sql files.
  2. Use the Makefile to concatenate all of my .sql files when the extension is first built.

My questions are whether #1 is even possible (I can't see anything in the docs – it only references the single extension–version.sql file that is required).

Also, how would I do #2? I don't have much experience with Makefiles and can't quite figure it out.


Best Answer

You could probably achieve #1 with "\i" directives to import snippets. I wouldn't advise it, though, I'd use a Makefile to concatenate the desired chunks.

It's as simple as something like:

myext--1.0.sql: myext-types.sql myext-tables.sql myext-views.sql
    cat $< $@

Note that the indent must be a tab, not spaces.

There's lots of good info out there on writing basic Makefiles, so I won't go on further.