I would like to create modified version of C function int2_avg_accum located in src/backend/utils/adt/numeric.c. I thought I can (as a start) just compile numeric.h and load int2_avg_accum as user defined function.
What I did:
- Added
PG_MODULE_MAGICtonumeric.h(as described here) - Renamed
int2_avg_accumtoint2_avg_accum2innumeric.h - Compiled
numeric.has described in the documentation (no errors, no warnings) (cc -fpic -Ipg_config --includedir-server-c numeric.cand thencc -shared -o numeric.so numeric.o) - Created a function in PostgreSQL:
.
create or replace function int2_avg_accum2(bigint[], smallint)
returns bigint[] as
'/usr/lib/postgresql/9.1/lib/numeric', 'int2_avg_accum2'
language c
cost 1;
alter function int2_avg_accum2(bigint[], smallint)
owner to postgres;
When I try to run select int2_avg_accum2(array[1::bigint,1],1::smallint); I get only message (in pgAdmin): "Do you want to attempt to reconnect to the database?". No other messages or errors.
When I call the function I see the following in /var/log/postgresql/postgresql-9.1-main.log:
2013-12-03 09:52:02 CET LOG: server process (PID 3366) was terminated by signal 11: Segmentation fault
2013-12-03 09:52:02 CET LOG: terminating any other active server processes
2013-12-03 09:52:02 CET WARNING: terminating connection because of crash of another server process
2013-12-03 09:52:02 CET DETAIL: The postmaster has commanded this server process to roll back the current transaction and exit, because another server process exited abnormally and possibly corrupted shared memory.
2013-12-03 09:52:02 CET HINT: In a moment you should be able to reconnect to the database and repeat your command.
2013-12-03 09:52:02 CET LOG: all server processes terminated; reinitializing
2013-12-03 09:52:02 CET LOG: database system was interrupted; last known up at 2013-12-03 09:50:53 CET
2013-12-03 09:52:02 CET LOG: database system was not properly shut down; automatic recovery in progress
2013-12-03 09:52:02 CET LOG: record with zero length at 0/B483EA0
2013-12-03 09:52:02 CET LOG: redo is not required
2013-12-03 09:52:03 CET LOG: autovacuum launcher started
2013-12-03 09:52:03 CET LOG: database system is ready to accept connections
What I have to do differently in order to get working copy of int2_avg_accum?
numeric.cYou are correct, backend is segfaulting. I added log details. Any other hints?