If you are thinking about distributing yourPostgreSQL extension modules, setting up a portable build system for them can be fairly difficult. Therefore thePostgreSQL installation provides a build infrastructure for extensions, calledPGXS, so that simple extension modules can be built simply against an already installed server.PGXS is mainly intended for extensions that include C code, although it can be used for pure-SQL extensions too. Note thatPGXS is not intended to be a universal build system framework that can be used to build any software interfacing toPostgreSQL; it simply automates common build rules for simple server extension modules. For more complicated packages, you might need to write your own build system.
To use thePGXS infrastructure for your extension, you must write a simple makefile. In the makefile, you need to set some variables and include the globalPGXS makefile. Here is an example that builds an extension module namedisbn_issn, consisting of a shared library containing some C code, an extension control file, an SQL script, an include file (only needed if other modules might need to access the extension functions without going via SQL), and a documentation text file:
MODULES = isbn_issnEXTENSION = isbn_issnDATA = isbn_issn--1.0.sqlDOCS = README.isbn_issnHEADERS_isbn_issn = isbn_issn.hPG_CONFIG = pg_configPGXS := $(shell $(PG_CONFIG) --pgxs)include $(PGXS)
The last three lines should always be the same. Earlier in the file, you assign variables or add custommake rules.
Set one of these three variables to specify what is built:
The following variables can also be set:
EXTENSION#extension name(s); for each name you must provide an file, which will be installed intoextension.controlprefix/share/extension
MODULEDIR#subdirectory of into which DATA and DOCS files should be installed (if not set, default isprefix/shareextension ifEXTENSION is set, orcontrib if not)
DATA#random files to install intoprefix/share/$MODULEDIR
DATA_built#random files to install into, which need to be built firstprefix/share/$MODULEDIR
DATA_TSEARCH#random files to install underprefix/share/tsearch_data
DOCS#random files to install underprefix/doc/$MODULEDIR
HEADERSHEADERS_built#Files to (optionally build and) install under.prefix/include/server/$MODULEDIR/$MODULE_big
UnlikeDATA_built, files inHEADERS_built are not removed by theclean target; if you want them removed, also add them toEXTRA_CLEAN or add your own rules to do it.
HEADERS_$MODULEHEADERS_built_$MODULE#Files to install (after building if specified) under, whereprefix/include/server/$MODULEDIR/$MODULE$MODULE must be a module name used inMODULES orMODULE_big.
UnlikeDATA_built, files inHEADERS_built_$MODULE are not removed by theclean target; if you want them removed, also add them toEXTRA_CLEAN or add your own rules to do it.
It is legal to use both variables for the same module, or any combination, unless you have two module names in theMODULES list that differ only by the presence of a prefixbuilt_, which would cause ambiguity. In that (hopefully unlikely) case, you should use only theHEADERS_built_$MODULE variables.
SCRIPTS#script files (not binaries) to install intoprefix/bin
SCRIPTS_built#script files (not binaries) to install into, which need to be built firstprefix/bin
REGRESS#list of regression test cases (without suffix), see below
REGRESS_OPTS#additional switches to pass topg_regress
ISOLATION#list of isolation test cases, see below for more details
ISOLATION_OPTS#additional switches to pass topg_isolation_regress
TAP_TESTS#switch defining if TAP tests need to be run, see below
NO_INSTALL#don't define aninstall target, useful for test modules that don't need their build products to be installed
NO_INSTALLCHECK#don't define aninstallcheck target, useful e.g., if tests require special configuration, or don't usepg_regress
EXTRA_CLEAN#extra files to remove inmake clean
PG_CPPFLAGS#will be prepended toCPPFLAGS
PG_CFLAGS#will be appended toCFLAGS
PG_CXXFLAGS#will be appended toCXXFLAGS
PG_LDFLAGS#will be prepended toLDFLAGS
PG_LIBS#will be added toPROGRAM link line
SHLIB_LINK#will be added toMODULE_big link line
PG_CONFIG#path topg_config program for thePostgreSQL installation to build against (typically justpg_config to use the first one in yourPATH)
Put this makefile asMakefile in the directory which holds your extension. Then you can domake to compile, and thenmake install to install your module. By default, the extension is compiled and installed for thePostgreSQL installation that corresponds to the firstpg_config program found in yourPATH. You can use a different installation by settingPG_CONFIG to point to itspg_config program, either within the makefile or on themake command line.
You can select a separate directory prefix in which to install your extension's files, by setting themake variableprefix when executingmake install like so:
make install prefix=/usr/local/postgresql
This will install the extension control and SQL files into/usr/local/postgresql/share and the shared modules into/usr/local/postgresql/lib. If the prefix does not include the stringspostgres orpgsql, such as
make install prefix=/usr/local/extras
thenpostgresql will be appended to the directory names, installing the control and SQL files into/usr/local/extras/share/postgresql/extension and the shared modules into/usr/local/extras/lib/postgresql. Either way, you'll need to setextension_control_path anddynamic_library_path to enable thePostgreSQL server to find the files:
extension_control_path = '/usr/local/extras/share/postgresql:$system'dynamic_library_path = '/usr/local/extras/lib/postgresql:$libdir'
You can also runmake in a directory outside the source tree of your extension, if you want to keep the build directory separate. This procedure is also called aVPATH build. Here's how:
mkdir build_dircd build_dirmake -f /path/to/extension/source/tree/Makefilemake -f /path/to/extension/source/tree/Makefile install
Alternatively, you can set up a directory for a VPATH build in a similar way to how it is done for the core code. One way to do this is using the core scriptconfig/prep_buildtree. Once this has been done you can build by setting themake variableVPATH like this:
make VPATH=/path/to/extension/source/treemake VPATH=/path/to/extension/source/tree install
This procedure can work with a greater variety of directory layouts.
The scripts listed in theREGRESS variable are used for regression testing of your module, which can be invoked bymake installcheck after doingmake install. For this to work you must have a runningPostgreSQL server. The script files listed inREGRESS must appear in a subdirectory namedsql/ in your extension's directory. These files must have extension.sql, which must not be included in theREGRESS list in the makefile. For each test there should also be a file containing the expected output in a subdirectory namedexpected/, with the same stem and extension.out.make installcheck executes each test script withpsql, and compares the resulting output to the matching expected file. Any differences will be written to the fileregression.diffs indiff -c format. Note that trying to run a test that is missing its expected file will be reported as“trouble”, so make sure you have all expected files.
The scripts listed in theISOLATION variable are used for tests stressing behavior of concurrent session with your module, which can be invoked bymake installcheck after doingmake install. For this to work you must have a runningPostgreSQL server. The script files listed inISOLATION must appear in a subdirectory namedspecs/ in your extension's directory. These files must have extension.spec, which must not be included in theISOLATION list in the makefile. For each test there should also be a file containing the expected output in a subdirectory namedexpected/, with the same stem and extension.out.make installcheck executes each test script, and compares the resulting output to the matching expected file. Any differences will be written to the fileoutput_iso/regression.diffs indiff -c format. Note that trying to run a test that is missing its expected file will be reported as“trouble”, so make sure you have all expected files.
TAP_TESTS enables the use of TAP tests. Data from each run is present in a subdirectory namedtmp_check/. See alsoSection 31.4 for more details.
The easiest way to create the expected files is to create empty files, then do a test run (which will of course report differences). Inspect the actual result files found in theresults/ directory (for tests inREGRESS), oroutput_iso/results/ directory (for tests inISOLATION), then copy them toexpected/ if they match what you expect from the test.
If you see anything in the documentation that is not correct, does not match your experience with the particular feature or requires further clarification, please usethis form to report a documentation issue.