Thread: Recreate C functions?

Recreate C functions?

From
Elliot Chance
Date:
Hi,

I seem to be having trouble recreating C functions, the source:

=== test.c ====
#include <postgres.h>
#include <fmgr.h>

PG_MODULE_MAGIC;

int32 plpgsql_test(text* s) {
    return 100;
}
=== end ===

Then compile:
$ cc -fpic -c -I/usr/include/pgsql/server test.c
$ ld -shared -o funcs.so test.o

Then create the function:
CREATE FUNCTION plpgsql_test(text) RETURNS integer
     AS '/storage/Scripts/plpgsql/funcs', 'plpgsql_test'
     LANGUAGE C STRICT IMMUTABLE;

So far it works:
> select plpgsql_test('abc');
100

The problem is if I change test.c like:
return 200;

Then recompile, drop and create the function it still returns 100? However if I compile it as funcs2.so it will then
createthe function with the correct 200 result ... what am I doing wrong? 

Thanks,
Elliot


Re: Recreate C functions?

From
Pavel Stehule
Date:
2010/12/14 Elliot Chance <elliotchance@gmail.com>:
> Hi,
>
> I seem to be having trouble recreating C functions, the source:
>
> === test.c ====
> #include <postgres.h>
> #include <fmgr.h>
>
> PG_MODULE_MAGIC;
>
> int32 plpgsql_test(text* s) {
>    return 100;
> }
> === end ===
>
> Then compile:
> $ cc -fpic -c -I/usr/include/pgsql/server test.c
> $ ld -shared -o funcs.so test.o
>
> Then create the function:
> CREATE FUNCTION plpgsql_test(text) RETURNS integer
>     AS '/storage/Scripts/plpgsql/funcs', 'plpgsql_test'
>     LANGUAGE C STRICT IMMUTABLE;
>
> So far it works:
>> select plpgsql_test('abc');
> 100
>
> The problem is if I change test.c like:
> return 200;
>
> Then recompile, drop and create the function it still returns 100? However if I compile it as funcs2.so it will then
createthe function with the correct 200 result ... what am I doing wrong? 
>

you need reload library. I don't know, if it is really necessary, but
I restart server when I reinstall (after recompilation) some code.

Regards

Pavel Stehule

> Thanks,
> Elliot
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

Re: Recreate C functions?

From
Pavel Stehule
Date:
Hello

it's maybe offtopic - you use a deprecated API

better to use V1 interface.

Regards

Pavel Stehule

2010/12/14 Elliot Chance <elliotchance@gmail.com>:
> Hi,
>
> I seem to be having trouble recreating C functions, the source:
>
> === test.c ====
> #include <postgres.h>
> #include <fmgr.h>
>
> PG_MODULE_MAGIC;
>
> int32 plpgsql_test(text* s) {
>    return 100;
> }
> === end ===
>
> Then compile:
> $ cc -fpic -c -I/usr/include/pgsql/server test.c
> $ ld -shared -o funcs.so test.o
>
> Then create the function:
> CREATE FUNCTION plpgsql_test(text) RETURNS integer
>     AS '/storage/Scripts/plpgsql/funcs', 'plpgsql_test'
>     LANGUAGE C STRICT IMMUTABLE;
>
> So far it works:
>> select plpgsql_test('abc');
> 100
>
> The problem is if I change test.c like:
> return 200;
>
> Then recompile, drop and create the function it still returns 100? However if I compile it as funcs2.so it will then
createthe function with the correct 200 result ... what am I doing wrong? 
>
> Thanks,
> Elliot
>
>
> --
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
> To make changes to your subscription:
> http://www.postgresql.org/mailpref/pgsql-general
>

Re: Recreate C functions?

From
Elliot Chance
Date:
OK, my fault, in the docs:

"After it is used for the first time, a dynamically loaded object file is retained in memory. Future calls in the same session to the function(s) in that file will only incur the small overhead of a symbol table lookup. If you need to force a reload of an object file, for example after recompiling it, begin a fresh session."

So this works for testing:
psql -U fts -c "select plpgsql_test('abc');"

On 14/12/2010, at 7:52 PM, Pavel Stehule wrote:

Hello

it's maybe offtopic - you use a deprecated API

better to use V1 interface.

Regards

Pavel Stehule

2010/12/14 Elliot Chance <elliotchance@gmail.com>:
Hi,

I seem to be having trouble recreating C functions, the source:

=== test.c ====
#include <postgres.h>
#include <fmgr.h>

PG_MODULE_MAGIC;

int32 plpgsql_test(text* s) {
   return 100;
}
=== end ===

Then compile:
$ cc -fpic -c -I/usr/include/pgsql/server test.c
$ ld -shared -o funcs.so test.o

Then create the function:
CREATE FUNCTION plpgsql_test(text) RETURNS integer
    AS '/storage/Scripts/plpgsql/funcs', 'plpgsql_test'
    LANGUAGE C STRICT IMMUTABLE;

So far it works:
select plpgsql_test('abc');
100

The problem is if I change test.c like:
return 200;

Then recompile, drop and create the function it still returns 100? However if I compile it as funcs2.so it will then create the function with the correct 200 result ... what am I doing wrong?

Thanks,
Elliot


--
Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-general


Re: Recreate C functions?

From
Pavel Stehule
Date:
2010/12/14 Elliot Chance <elliotchance@gmail.com>:
> OK, my fault, in the docs:
> "After it is used for the first time, a dynamically loaded object file is
> retained in memory. Future calls in the same session to the function(s) in
> that file will only incur the small overhead of a symbol table lookup. If
> you need to force a reload of an object file, for example after recompiling
> it, begin a fresh session."
> So this works for testing:
> psql -U fts -c "select plpgsql_test('abc');"
> On 14/12/2010, at 7:52 PM, Pavel Stehule wrote:
>

good to know.

regards

Pavel

> Hello
>
> it's maybe offtopic - you use a deprecated API
>
> better to use V1 interface.
>
> Regards
>
> Pavel Stehule
>
> 2010/12/14 Elliot Chance <elliotchance@gmail.com>:
>
> Hi,
>
> I seem to be having trouble recreating C functions, the source:
>
> === test.c ====
>
> #include <postgres.h>
>
> #include <fmgr.h>
>
> PG_MODULE_MAGIC;
>
> int32 plpgsql_test(text* s) {
>
>    return 100;
>
> }
>
> === end ===
>
> Then compile:
>
> $ cc -fpic -c -I/usr/include/pgsql/server test.c
>
> $ ld -shared -o funcs.so test.o
>
> Then create the function:
>
> CREATE FUNCTION plpgsql_test(text) RETURNS integer
>
>     AS '/storage/Scripts/plpgsql/funcs', 'plpgsql_test'
>
>     LANGUAGE C STRICT IMMUTABLE;
>
> So far it works:
>
> select plpgsql_test('abc');
>
> 100
>
> The problem is if I change test.c like:
>
> return 200;
>
> Then recompile, drop and create the function it still returns 100? However
> if I compile it as funcs2.so it will then create the function with the
> correct 200 result ... what am I doing wrong?
>
> Thanks,
>
> Elliot
>
>
> --
>
> Sent via pgsql-general mailing list (pgsql-general@postgresql.org)
>
> To make changes to your subscription:
>
> http://www.postgresql.org/mailpref/pgsql-general
>
>
>