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

创建PostgreSQL内部C函数的副本,并将其作为用户定义函数加载,postgresql,postgresql-9.1,Postgresql,Postgresql 9.1,我想创建位于src/backend/utils/adt/numeric.C中的C函数的修改版本int2\u avg\u acum。我想我可以(首先)编译numeric.h并将int2\u avg\u acum加载为用户定义的函数 我所做的: 将PG\u模块\u MAGIC添加到numeric.h(如上所述) 在numeric.h 按照文档中的描述编译numeric.h(无错误,无警告)(cc-fpic-Ipg_-config--includedir server-c numeric.c,然后cc

我想创建位于
src/backend/utils/adt/numeric.C
中的C函数的修改版本
int2\u avg\u acum
。我想我可以(首先)编译
numeric.h
并将
int2\u avg\u acum
加载为用户定义的函数

我所做的:

  • PG\u模块\u MAGIC
    添加到
    numeric.h
    (如上所述)
  • numeric.h
  • 按照文档中的描述编译
    numeric.h
    (无错误,无警告)(
    cc-fpic-I
    pg_-config--includedir server
    -c numeric.c
    ,然后
    cc-shared-o numeric.so numeric.o
  • 在PostgreSQL中创建了一个函数:
  • 当我尝试运行
    时,选择int2\u avg\u acum2(数组[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\u avg\u acum的工作副本,我必须采取不同的措施。

    根据注释,psql客户端询问您是否希望重新连接的原因是因为后端正在发生故障

    可以从这样的崩溃中收集一个内核转储,并使用调试器(如gdb)检查它,以找出崩溃的确切位置。然而,我最好的猜测是,它正在崩溃,因为您已经获取了一个作为postgresql核心组件编写的大文件,单独编译了它,并试图将其作为扩展模块加载

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

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

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

    • 您需要通过添加宏来指定正在使用V1调用约定:

      PG功能信息V1(int2平均值)

      如果缺少,这也将导致SEGFULTS,因为postgresql将采用版本0调用约定,这与函数定义不匹配

    • 正如你所说,你必须包括PG_MODOULE_魔术

    对我有用的完整文件是:

    #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上使用了Postgresql 9.2。您可能需要根据设置调整路径。

    您是编译了整个numeric.c,还是只提取了函数int2\u avg\u acum?它询问您是否要重新连接的原因很可能是因为后端正在分段故障-postgresql后端日志或/var/log/messages中有任何关于此的消息吗?@harmic我编译了整个
    numeric.c
    您是对的,后端正在分段故障。我添加了日志详细信息。还有其他提示吗?谢谢。我试图只编译
    int2\u avg\u acum
    。它编译正常,但仍然存在分段错误。我认为这是因为调用约定。只要我声明PG_FUNCTION_INFO_V1(int2_avg_accum2),我就可以使用它。我更新了答案,提供了更多的细节。酷,现在它可以工作了!添加
    PG_功能信息V1(int2平均值accum2)成功了。
    
    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