首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >创建PostgreSQL内部C函数的副本并将其作为用户定义的函数加载

创建PostgreSQL内部C函数的副本并将其作为用户定义的函数加载
EN

Stack Overflow用户
提问于 2013-12-02 13:53:35
回答 1查看 716关注 0票数 4

我想要创建位于int2_avg_accum中的C函数的修改版本。我想我可以(作为开始)编译numeric.h并将int2_avg_accum加载为用户定义的函数。

我所做的:

  1. PG_MODULE_MAGIC添加到numeric.h中(如描述的这里)
  2. int2_avg_accum重命名为int2_avg_accum2 in numeric.h
  3. 如文档所描述的那样编译了numeric.h (没有错误,没有警告) (cc -fpic -Ipg_config -包含服务器-c numeric.c,然后是cc -shared -o numeric.so numeric.o)
  4. 在PostgreSQL中创建一个函数:

代码语言:javascript
运行
复制
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中看到了以下内容

代码语言:javascript
运行
复制
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的工作副本,我需要做的是不同的工作

EN

Stack Overflow用户

回答已采纳

发布于 2013-12-03 21:46:22

psql客户端询问您是否希望重新连接的原因是,正如注释所示,后端正在分段错误。

从这样的崩溃中收集核心转储并使用调试器进行检查是可能的(例如。要找出它到底在哪里崩溃。但是,我最好的猜测是,它正在崩溃,因为您将一个大文件编写成postgresql的核心组件,单独编译它,并尝试将其作为扩展模块加载。

C文件包含大量函数、静态变量和数据结构,您只想复制其中的一个函数。所有这些函数、变量等都已存在于正在运行的postgresql系统中。当您编译数值.c的版本并加载它时,您要添加的新函数将引用库中的函数和变量,而不是使用主postgresql程序中的函数和变量。它可能是引用未正确初始化的数据结构,从而导致其崩溃。

我建议您从一个空白文件开始,并只从int2_avg_accum函数中复制数值(重命名为您所做的)。如果该函数正在postgresql中调用其他函数,或者引用变量,它将使用主postgresql二进制文件中的函数和变量,这正是您想要的。您可以包含原始的数值.h来获取所有外部函数的声明。

在如何将函数定义为内部函数与将其加载为动态加载模块时需要如何定义函数之间还有其他一些区别:

  • 您需要通过添加宏来指定使用V1调用约定: PG_FUNCTION_INFO_V1(int2_avg_accum2); 如果缺少,也会导致分段错误,因为postgresql将假定版本0调用约定,这与函数定义不匹配!
  • 正如您所指出的,您必须包括PG_MODOULE_MAGIC。

对我有用的完整文件是:

代码语言:javascript
运行
复制
#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);
}

编撰:

代码语言:javascript
运行
复制
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,您可能需要根据设置调整路径。

票数 3
EN
查看全部 1 条回答
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/20329959

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档