Home > OS >  How to reload a postgres C extension?
How to reload a postgres C extension?

Time:02-20

I've created a C extension defining the shell of a function that I can call from SQL code in Postgresql 12.3. I'm using PGXS to build and install the extension. I can build, install and call the function, but if I make changes and reinstall, the changes don't show.

The make file is:

MODULES = bar
EXTENSION = bar
DATA = bar--0.0.1.sql
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

bar.control is

comment = 'Simple bar function'
default_version = '0.0.1'
relocatable = true
module_pathname = '$libdir/bar'

bar--0.0.1.sql is:

CREATE OR REPLACE FUNCTION
bar(jsonb) RETURNS int AS 'MODULE_PATHNAME','bar'
LANGUAGE C STRICT;

bar.c is:

#include "postgres.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(bar);

Datum bar(PG_FUNCTION_ARGS)
{
    PG_RETURN_INT32(101);
}

I run make install to do the install. And then drop extension bar; create extension bar; but the changes won't show when I call the function. The only way I can install a new version is to restart postgres. What step am I missing?

CodePudding user response:

Well, I discovered that if I just create a new connection to the DB the changes will show. That isn't so bad, I suppose. Does that have to do with how postgres uses fork() to create a new process for each connection?

CodePudding user response:

When you execute your function, the shared library gets loaded into your backend process and stays there. Replacing the shared library on disk does not modify the in-memory copy loaded into your process. You have to start a new database connection and execute the function again to get the new version.

  • Related