我想要创建位于int2_avg_accum
中的C函数的修改版本。我想我可以(作为开始)编译numeric.h
并将int2_avg_accum
加载为用户定义的函数。
我所做的:
PG_MODULE_MAGIC
添加到numeric.h
中(如描述的这里)int2_avg_accum
重命名为int2_avg_accum2
in numeric.h
numeric.h
(没有错误,没有警告) (cc -fpic -I
pg_config -包含服务器-c numeric.c
,然后是cc -shared -o numeric.so numeric.o
)。
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;
当我尝试运行select int2_avg_accum2(array[1::bigint,1],1::smallint);
时,我只收到消息(在pgAdmin中):“您要尝试重新连接到数据库吗?”没有其他消息或错误。
当我调用这个函数时,我在/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
为了获得int2_avg_accum
的工作副本,我需要做的是不同的工作
发布于 2013-12-03 21:46:22
psql客户端询问您是否希望重新连接的原因是,正如注释所示,后端正在分段错误。
从这样的崩溃中收集核心转储并使用调试器进行检查是可能的(例如。要找出它到底在哪里崩溃。但是,我最好的猜测是,它正在崩溃,因为您将一个大文件编写成postgresql的核心组件,单独编译它,并尝试将其作为扩展模块加载。
C文件包含大量函数、静态变量和数据结构,您只想复制其中的一个函数。所有这些函数、变量等都已存在于正在运行的postgresql系统中。当您编译数值.c的版本并加载它时,您要添加的新函数将引用库中的函数和变量,而不是使用主postgresql程序中的函数和变量。它可能是引用未正确初始化的数据结构,从而导致其崩溃。
我建议您从一个空白文件开始,并只从int2_avg_accum函数中复制数值(重命名为您所做的)。如果该函数正在postgresql中调用其他函数,或者引用变量,它将使用主postgresql二进制文件中的函数和变量,这正是您想要的。您可以包含原始的数值.h来获取所有外部函数的声明。
在如何将函数定义为内部函数与将其加载为动态加载模块时需要如何定义函数之间还有其他一些区别:
对我有用的完整文件是:
#include "postgres.h"
#include "fmgr.h"
#include "utils/array.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
typedef struct Int8TransTypeData
{
int64 count;
int64 sum;
} Int8TransTypeData;
PG_FUNCTION_INFO_V1(int2_avg_accum2);
Datum
int2_avg_accum2(PG_FUNCTION_ARGS)
{
ArrayType *transarray;
int16 newval = PG_GETARG_INT16(1);
Int8TransTypeData *transdata;
/*
* If we're invoked as an aggregate, we can cheat and modify our first
* parameter in-place to reduce palloc overhead. Otherwise we need to make
* a copy of it before scribbling on it.
*/
if (AggCheckCallContext(fcinfo, NULL))
transarray = PG_GETARG_ARRAYTYPE_P(0);
else
transarray = PG_GETARG_ARRAYTYPE_P_COPY(0);
if (ARR_HASNULL(transarray) ||
ARR_SIZE(transarray) != ARR_OVERHEAD_NONULLS(1) + sizeof(Int8TransTypeData))
elog(ERROR, "expected 2-element int8 array");
transdata = (Int8TransTypeData *) ARR_DATA_PTR(transarray);
transdata->count++;
transdata->sum += newval;
PG_RETURN_ARRAYTYPE_P(transarray);
}
编撰:
gcc -I/usr/pgsql-9.2/include/server -fPIC -c my_avg_accum.c
gcc -shared -o my_avg_accum.so my_avg_accum.o
我在Centos 6上使用PostgreSQL9.2,您可能需要根据设置调整路径。
https://stackoverflow.com/questions/20329959
复制相似问题