Thread: writing & flushing C extensions

writing & flushing C extensions

From
"John P. Looney"
Date:
 I'm trying to write a few functions in C for postgres. But, when I change
some code, I'm never sure that it'll be flushed when I do a;

 DROP FUNCTION test();
 CREATE FUNCTION test() RETURNS text AS '/var/lib/pgsql/functions/test.so' LANGUAGE 'c';

 Is there a way to make sure ? Even restarting postgres doesn't always
work (or else my makefiles are broken...)

Kate

--
_______________________________________
John Looney             Chief Scientist
a n t e f a c t o     t: +353 1 8586004
www.antefacto.com     f: +353 1 8586014


Re: writing & flushing C extensions

From
Peter Eisentraut
Date:
John P. Looney writes:

>  I'm trying to write a few functions in C for postgres. But, when I change
> some code, I'm never sure that it'll be flushed when I do a;
>
>  DROP FUNCTION test();
>  CREATE FUNCTION test() RETURNS text AS '/var/lib/pgsql/functions/test.so' LANGUAGE 'c';
>
>  Is there a way to make sure ? Even restarting postgres doesn't always
> work (or else my makefiles are broken...)

While in general this is platform-dependent, recreating the function at
least *could* work in many cases.  Restarting the server should work in
any case.  So possibly your build process needs adjustment.

--
Peter Eisentraut   peter_e@gmx.net   http://funkturm.homeip.net/~peter


Re: writing & flushing C extensions

From
"John P. Looney"
Date:
On Mon, Oct 15, 2001 at 11:02:10AM -0400, Tom Lane mentioned:
> >  DROP FUNCTION test();
> >  CREATE FUNCTION test() RETURNS text AS '/var/lib/pgsql/functions/test.so' LANGUAGE 'c';
> Actually, you can be pretty sure that it *won't* be flushed; once a
> backend has loaded a dynamic link library, the library stays loaded
> till the backend exits.  Dropping/recreating individual function
> definitions that point at the library makes no difference.
>
> I believe the only way to get an updated copy of a library without
> starting a new backend is to execute the LOAD command, which will
> unload and reload the library.

 Ah, excellent. Thanks.

 Just out of curiousity - does "LOAD" also flush other libraries that the
postgres extension is linked against ?

John

--
_______________________________________
John Looney             Chief Scientist
a n t e f a c t o     t: +353 1 8586004
www.antefacto.com     f: +353 1 8586014


Re: writing & flushing C extensions

From
Tom Lane
Date:
"John P. Looney" <john@antefacto.com> writes:
>  I'm trying to write a few functions in C for postgres. But, when I change
> some code, I'm never sure that it'll be flushed when I do a;

>  DROP FUNCTION test();
>  CREATE FUNCTION test() RETURNS text AS '/var/lib/pgsql/functions/test.so' LANGUAGE 'c';

Actually, you can be pretty sure that it *won't* be flushed; once a
backend has loaded a dynamic link library, the library stays loaded
till the backend exits.  Dropping/recreating individual function
definitions that point at the library makes no difference.

I believe the only way to get an updated copy of a library without
starting a new backend is to execute the LOAD command, which will
unload and reload the library.

>  Is there a way to make sure ? Even restarting postgres doesn't always
> work (or else my makefiles are broken...)

Better take a hard look at your makefiles then... a fresh backend
definitely will load the current copy of the .so file on first reference
to any function in it.

You didn't say what platform you are on, but on some platforms it's
not allowed to delete or overwrite a shared library file that is
currently open for execution.  If yours is like this then the makefile's
attempt to install the new .so might be failing.  Best solution that
I know of is to move the old .so out of the way, install the new,
then attempt to delete the old.

            regards, tom lane

Re: writing & flushing C extensions

From
Tom Lane
Date:
"John P. Looney" <john@antefacto.com> writes:
>  Just out of curiousity - does "LOAD" also flush other libraries that the
> postgres extension is linked against ?

Good question.  It'd depend on what dlclose() (or local equivalent) does
on your platform.  My guess is "probably not", since to do so
the dynamic loader would need to keep a lot of state about which
libraries had been loaded as a result of dependencies and whether any
other libraries still loaded had dependencies on them too.

            regards, tom lane