我有一个用户定义的类型 mytype :
CREATE TYPE mytype AS (
value1 integer
);
我想使用C函数返回mytype。这是我写的:
#include "postgres.h"
#include "fmgr.h"
#include "funcapi.h"
#include "catalog/pg_type.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(testfunc);
Datum
testfunc(PG_FUNCTION_ARGS) {
TupleDesc tupdesc;
Datum values[1];
bool nulls[1];
MemSet(values, 0, sizeof(values));
MemSet(nulls, 0, sizeof(nulls));
tupdesc = CreateTemplateTupleDesc(1, false);
TupleDescInitEntry(tupdesc, (AttrNumber) 1, "value1", INT4OID, -1, 0);
tupdesc = BlessTupleDesc(tupdesc);
values[0] = Int32GetDatum(100);
HeapTuple rettuple = heap_form_tuple( tupdesc, values, nulls );
PG_RETURN_DATUM( HeapTupleGetDatum( rettuple ) );
}
SQL参考:
CREATE OR REPLACE FUNCTION testfunc() RETURNS mytype AS 'path/to/library.so', 'testfunc' LANGUAGE C STRICT;
现在,当我尝试测试我的功能时,它会崩溃连接:
postgres=# SELECT testfunc();
SSL SYSCALL error: EOF detected
The connection to the server was lost. Attempting reset: Failed.
请告诉我我在哪里做错了。
服务器日志:
2017-01-12 18:47:45 IST LOG: server process (PID 25208) was terminated by signal 11: Segmentation fault
2017-01-12 18:47:45 IST DETAIL: Failed process was running: SELECT testfunc();
2017-01-12 18:47:45 IST LOG: terminating any other active server processes
2017-01-12 18:47:45 IST WARNING: terminating connection because of crash of another server process
2017-01-12 18:47:45 IST 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.
2017-01-12 18:47:45 IST HINT: In a moment you should be able to reconnect to the database and repeat your command.
2017-01-12 18:47:45 IST FATAL: the database system is in recovery mode
2017-01-12 18:47:45 IST LOG: all server processes terminated; reinitializing
2017-01-12 18:47:45 IST LOG: database system was interrupted; last known up at 2017-01-12 18:45:10 IST
2017-01-12 18:47:45 IST FATAL: the database system is in recovery mode
2017-01-12 18:47:45 IST LOG: database system was not properly shut down; automatic recovery in progress
2017-01-12 18:47:46 IST LOG: record with zero length at 0/1C692E8
2017-01-12 18:47:46 IST LOG: redo is not required
2017-01-12 18:47:46 IST LOG: MultiXact member wraparound protections are now enabled
2017-01-12 18:47:46 IST LOG: database system is ready to accept connections
2017-01-12 18:47:46 IST LOG: autovacuum launcher started