为什么我的PostgreSQL C函数崩溃了?

时间:2011-09-09 14:33:22

标签: c postgresql

我们正在使用两个遗留聚合函数,它们连接文本值,由条形(|)或逗号(,)提交。这些函数本质上是string_agg()的非常慢的实现,它出现在postgresql 9.0中。

为了获得string_agg()的性能而不必修改使用我们的bar_list()comma_list()函数的代码,我试图为那些基本上为ape的函数创建C实现{ {1}}。

不幸的是,我不是C程序员。

无论如何,在postgresql源代码中的string_agg()中找到string_agg()源后,我开始重用该代码:

varlena.c

当我编译它时,它似乎成功完成,没有错误:

#include "postgres.h"                                 
#include <ctype.h>
#include <syslog.h>
#include <sys/types.h>
#include <unistd.h>
#include "lib/stringinfo.h"
#include "fmgr.h"
#include "utils/builtins.h"

PG_MODULE_MAGIC;

/*
Compile this module in the following way:

   gcc -I $(pg_config --includedir-server) -fpic -c vs_funcs.c;
   gcc -shared -o $(pg_config --pkglibdir)/vs_funcs.so vs_funcs.o;

This will also place the compiled module in the correct location.
*/

// Code below taken directly or modified from varlena.c in postgresl source

/*
 * appendStringInfoText
 *
 * Append a text to str.
 * Like appendStringInfoString(str, text_to_cstring(t)) but faster.
*/
static void
appendStringInfoText(StringInfo str, const text *t) 
{
    appendBinaryStringInfo(str, VARDATA_ANY(t), VARSIZE_ANY_EXHDR(t));
}

// This is largely copied from the code for the string_agg function
#define MAKE_STR_AGG(AGG_NAME, AGG_DELIM) \
        static StringInfo \
        make_ ## AGG_NAME ##_state(FunctionCallInfo fcinfo) \
        { \
            StringInfo  state; \
            MemoryContext aggcontext; \
            MemoryContext oldcontext; \
            if (!AggCheckCallContext(fcinfo, &aggcontext)) \
            { \
                elog(ERROR, "#AGG_NAME called in non-aggregate context"); \
            } \
            oldcontext = MemoryContextSwitchTo(aggcontext); \
            state = makeStringInfo(); \
            MemoryContextSwitchTo(oldcontext); \
            return state; \
        } \
        PG_FUNCTION_INFO_V1(AGG_NAME ## _transfn); \
        Datum \
        AGG_NAME ## _transfn(PG_FUNCTION_ARGS) \
        { \
        { \
            StringInfo  state; \
            state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0); \
            if (!PG_ARGISNULL(1)) \
            { \
                if (state == NULL) \
                    state = make_ ## AGG_NAME ## _state(fcinfo); \
                else if (!PG_ARGISNULL(2)) \
                    appendStringInfoText(state, string_to_text(#AGG_DELIM)); \
                appendStringInfoText(state, PG_GETARG_TEXT_PP(1)); \
            } \
            PG_RETURN_POINTER(state); \
        }\
        PG_FUNCTION_INFO_V1(AGG_NAME ## _finalfn); \
        Datum \
        AGG_NAME ## _finalfn(PG_FUNCTION_ARGS) \
        { \
            StringInfo  state; \
            Assert(AggCheckCallContext(fcinfo, NULL)); \
            state = PG_ARGISNULL(0) ? NULL : (StringInfo) PG_GETARG_POINTER(0); \
            if (state != NULL) \
                PG_RETURN_TEXT_P(cstring_to_text(state->data)); \
            else \
                PG_RETURN_NULL(); \
        }

/*
 * bar_list - Concatenates values and returns string.
 *
 * Syntax: bar_list(value text) RETURNS text
 *
 * Note: Any NULL values are ignored. The first-call delimiter isn't
 * actually used at all, and on subsequent calls the delimiter precedes
 * the associated value.
 */
MAKE_STR_AGG(bar_list, '|');

/*
 * comma_list - Concatenates values and returns string.
 *
 * Syntax: comma_list(value text) RETURNS text
 *
 * Note: Any NULL values are ignored. The first-call delimiter isn't
 * actually used at all, and on subsequent calls the delimiter precedes
 * the associated value.
 */
MAKE_STR_AGG(comma_list, ',');      
产生的lib中的

符号:

vezult@cruella-devel:~/Development/VistaShare/oct_git/utilities$ make
gcc -fpic -c -I/usr/include/postgresql/9.0/server vs_funcs.c
gcc -shared -o vs_funcs.so vs_funcs.o;

我在postgres中创建了这个函数:

vezult@cruella-devel:~/Development/VistaShare/oct_git/utilities$ nm vs_funcs.so 
                 U AggCheckCallContext
                 U CurrentMemoryContext
00000000000009dc t MemoryContextSwitchTo
0000000000000ec0 r Pg_magic_data.8975
0000000000000a06 T Pg_magic_func
0000000000201138 a _DYNAMIC
00000000002012e8 a _GLOBAL_OFFSET_TABLE_
                 w _Jv_RegisterClasses
0000000000201118 d __CTOR_END__
0000000000201110 d __CTOR_LIST__
0000000000201128 d __DTOR_END__
0000000000201120 d __DTOR_LIST__
0000000000001108 r __FRAME_END__
0000000000201130 d __JCR_END__
0000000000201130 d __JCR_LIST__
0000000000201348 A __bss_start
                 w __cxa_finalize@@GLIBC_2.2.5
0000000000000de0 t __do_global_ctors_aux
0000000000000930 t __do_global_dtors_aux
0000000000201340 d __dso_handle
0000000000000ea0 r __func__.9026
0000000000000e80 r __func__.9096
                 w __gmon_start__
0000000000201348 A _edata
0000000000201358 A _end
0000000000000e18 T _fini
0000000000000868 T _init
                 U appendBinaryStringInfo
0000000000000a13 t appendStringInfoText
0000000000000bec T bar_list_finalfn
0000000000000b34 T bar_list_transfn
0000000000000910 t call_gmon_start
0000000000000d82 T comma_list_finalfn
0000000000000cca T comma_list_transfn
0000000000201348 b completed.6341
                 U cstring_to_text
0000000000201350 b dtor_idx.6343
                 U elog_finish
                 U elog_start
00000000000009b0 t frame_dummy
                 U makeStringInfo
0000000000000aab t make_bar_list_state
0000000000000c41 t make_comma_list_state
0000000000000e9c r my_finfo.9039
0000000000000e98 r my_finfo.9072
0000000000000e78 r my_finfo.9109
0000000000000e74 r my_finfo.9142
                 U pg_detoast_datum_packed
0000000000000bdf T pg_finfo_bar_list_finalfn
0000000000000b27 T pg_finfo_bar_list_transfn
0000000000000d75 T pg_finfo_comma_list_finalfn
0000000000000cbd T pg_finfo_comma_list_transfn

当我运行它时,它会因为分段错误而崩溃:

CREATE FUNCTION bar_list_transfn(text, text) RETURNS text AS 'vs_funcs', 'bar_list_transfn' language 'C';

所以,在我看来,我的代码应该与内置函数非常相似,所以我不确定它为什么不起作用。老实说,我不知道编译时警告是否重要,或者真正的问题是否在其他地方。

我做错了什么?

2 个答案:

答案 0 :(得分:4)

看起来您的SQL代码使用了错误的类型。从源代码(src/include/catalog/pg_proc.h):

DATA(insert OID = 3535 (  string_agg_transfn        PGNSP PGUID 12 1 0 0 0 f f f f f i 3 0 2281 "2281 25 25" _null_ _null_ _null_ _null_ string_agg_transfn _null_ _null_ _null_ ));
DESCR("aggregate transition function");
DATA(insert OID = 3536 (  string_agg_finalfn        PGNSP PGUID 12 1 0 0 0 f f f f f i 1 0 25 "2281" _null_ _null_ _null_ _null_ string_agg_finalfn _null_ _null_ _null_ ));
DESCR("aggregate final function");
DATA(insert OID = 3538 (  string_agg                PGNSP PGUID 12 1 0 0 0 t f f f f i 2 0 25 "25 25" _null_ _null_ _null_ _null_ aggregate_dummy _null_ _null_ _null_ ));
DESCR("concatenate aggregate input into a string");

特别是:

... string_agg_transfn ... 2281 "2281 25 25" ...
... string_agg_finalfn ... 25   "2281"       ...
... string_agg         ... 25   "25 25"      ...

第一个OID是返回类型。后续的OID是参数类型。可以通过查询目录来解码这些OID:

joey=# SELECT oid, typname FROM pg_type WHERE oid IN (25, 2281);
 oid  | typname  
------+----------
   25 | text
 2281 | internal
(2 rows)

因此,正确的SQL语句应该是:

CREATE FUNCTION bar_list_transfn(internal, text)
RETURNS internal
AS 'vs_funcs', 'bar_list_transfn'
LANGUAGE 'C';

CREATE FUNCTION bar_list_finalfn(internal)
RETURNS text
AS 'vs_funcs', 'bar_list_finalfn'
LANGUAGE 'C';

CREATE AGGREGATE bar_list_agg (
    BASETYPE  = text,             -- item type
    SFUNC     = bar_list_transfn, -- transition function
    STYPE     = internal,         -- state type
    FINALFUNC = bar_list_finalfn  -- final calculation function
);

答案 1 :(得分:0)

如果您只想创建一个围绕array_agg()的包装器,那么您可以在没有C的情况下完成。

这个答案How to concatenate strings of a string field in a PostgreSQL 'group by' query?解释了如何使用纯SQL创建自己的聚合(并且不编写单行C代码)

我不知道这是否有资格获得“高绩效”。