Last active
July 31, 2021 00:28
-
-
Save radekg/db6325415d13d2aa1d18e531b6a3240c to your computer and use it in GitHub Desktop.
YugabyteDB Postgres extension build infrastructure with an extension template
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
FROM postgres:11.2 | |
RUN apt-get update \ | |
&& apt-get install -y build-essential \ | |
libpq5=11.12-1.pgdg90+1 \ | |
libpq-dev=11.12-1.pgdg90+1 \ | |
postgresql-server-dev-11 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
-- Complain if script is sourced in psql, rather than via CREATE EXTENSION. | |
\echo Use "CREATE EXTENSION example" to load this file.\quit |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "postgres.h" | |
#include "miscadmin.h" | |
#include "nodes/parsenodes.h" | |
#include "tcop/utility.h" | |
#define PG13_GTE (PG_VERSION_NUM >= 130000) | |
// Hook function: | |
static ProcessUtility_hook_type prev_hook = NULL; | |
// Required macro for extension libraries to work: | |
PG_MODULE_MAGIC; | |
void _PG_init(void); | |
void _PG_fini(void); | |
/* | |
* IO: Hook logic. | |
*/ | |
static void | |
example_hook(PlannedStmt *pstmt, | |
const char *queryString, | |
ProcessUtilityContext context, | |
ParamListInfo params, | |
QueryEnvironment *queryEnv, | |
DestReceiver *dest, | |
#if PG13_GTE | |
QueryCompletion *completionTag | |
#else | |
char *completionTag | |
#endif | |
) | |
{ | |
// Get the utility statement from the planned statement | |
Node *utility_stmt = pstmt->utilityStmt; | |
if (!superuser()) | |
{ | |
switch (utility_stmt->type) { | |
// I don't do anything... | |
default: | |
ereport(LOG, (errmsg("statement type: %d", utility_stmt->type))); | |
break; | |
} | |
} | |
// Chain to previously defined hooks | |
if (prev_hook) | |
prev_hook(pstmt, queryString, | |
context, params, queryEnv, | |
dest, completionTag); | |
else | |
standard_ProcessUtility(pstmt, queryString, | |
context, params, queryEnv, | |
dest, completionTag); | |
} | |
/* | |
* IO: module load callback | |
*/ | |
void | |
_PG_init(void) | |
{ | |
// Store the previous hook | |
prev_hook = ProcessUtility_hook; | |
// Set our hook | |
ProcessUtility_hook = example_hook; | |
ereport(LOG, (errmsg("example extension initialized"))); | |
} | |
/* | |
* IO: module unload callback | |
* This is just for completion. Right now postgres doesn't call _PG_fini, see: | |
* https://github.com/postgres/postgres/blob/master/src/backend/utils/fmgr/dfmgr.c#L388-L402 | |
*/ | |
void | |
_PG_fini(void) | |
{ | |
ProcessUtility_hook = prev_hook; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
shared_preload_libraries = example |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
comment = 'Example library' | |
default_version = '0.1.0' | |
relocatable = false |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
EXTENSION = example | |
DATA = $(wildcard sql/*--*.sql) | |
MODULE_big = example | |
OBJS = src/example.o | |
TESTS = $(wildcard test/sql/*.sql) | |
REGRESS = $(patsubst test/sql/%.sql,%,$(TESTS)) | |
REGRESS_OPTS = --use-existing --inputdir=test | |
# Tell pg_config to pass us the PostgreSQL extensions makefile(PGXS) | |
# and include it into our own Makefile through the standard "include" directive. | |
PG_CONFIG = pg_config | |
PGXS := $(shell $(PG_CONFIG) --pgxs) | |
include $(PGXS) | |
# Custom targets: | |
CURRENT_DIR=$(dir $(realpath $(firstword $(MAKEFILE_LIST)))) | |
.PHONY: ext-infra | |
ext-infra: | |
docker build -t \ | |
postgres-extensions-builder:11.2 . | |
.PHONY: ext-clean | |
ext-clean: | |
docker run --rm \ | |
-v $(shell pwd):/extension \ | |
-ti postgres-extensions-builder:11.2 \ | |
/bin/bash -c 'cd /extension && make clean' | |
.PHONY: ext-build | |
ext-build: | |
docker run --rm \ | |
-v $(shell pwd):/extension \ | |
-ti postgres-extensions-builder:11.2 \ | |
/bin/bash -c 'cd /extension && make' | |
.PHONY: ext-run | |
ext-run: | |
mkdir -p ${CURRENT_DIR}/.tmp/pgdata \ | |
&& docker run --rm \ | |
-v $(shell pwd):/extension \ | |
-v $(shell pwd)/.tmp/pgdata:/pgdata \ | |
-e POSTGRES_PASSWORD=builder \ | |
-e PGDATA=/pgdata \ | |
-p 5432:5432 \ | |
-ti postgres-extensions-builder:11.2 \ | |
/bin/bash -c 'cd /extension && make install && /docker-entrypoint.sh -c shared_preload_libraries=example' | |
.PHONY: ext-run-clean | |
ext-run-clean: | |
rm -rf ${CURRENT_DIR}/.tmp \ | |
&& mkdir -p ${CURRENT_DIR}/.tmp/pgdata \ | |
&& docker run --rm \ | |
-v $(shell pwd):/extension \ | |
-v $(shell pwd)/.tmp/pgdata:/pgdata \ | |
-e POSTGRES_PASSWORD=builder \ | |
-e PGDATA=/pgdata \ | |
-p 5432:5432 \ | |
-ti postgres-extensions-builder:11.2 \ | |
/bin/bash -c 'cd /extension && make clean && make && make install && /docker-entrypoint.sh -c shared_preload_libraries=example' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment