PostgreSQL FunctionCallInfoData结构体

FunctionCallInfoData是实际传给C Language function函数的数据,它默认是隐藏的,实际上每一个C Language function函数默认都有一个FunctionCallInfoData.它的定义如下:

typedef struct FunctionCallInfoData
{
    FmgrInfo   *flinfo;         /*函数描述指针*/
    fmNodePtr   context;        /*函数调用的上下文信息*/
    fmNodePtr   resultinfo;     /*返回结果的额外信息*/
    Oid         fncollation;    /*函数的Oid*/
    bool        isnull;         /*返回结果是否为NULL,如果返回值为NULL,必须设置为true*/
    short       nargs;          /*函数参数的数量*/
    Datum       arg[FUNC_MAX_ARGS]; /*函数的所有参数*/
    bool        argnull[FUNC_MAX_ARGS]; /*某一项参数是否为NULL*/
} FunctionCallInfoData;

一个简单的示例函数,包含两个参数

Datum sum_int(FunctionCallInfoData *fcinfo){
    int a1,a2;
    if (fcinfo->argnull[0] || fcinfo->argnull[1]){
        fcinfo->isnull = true;
        return (Datum) 0;
    }
    a1 = DatumGetInt32(fcinfo->arg[0]);
    a2 = DatumGetInt32(fcinfo->arg[1]);
    fcinfo->isnull = false;
    return Int32GetDatum(a1 + a2);
}

我们经常使用的函数定义,等同上面的函数

Datum sum_int(PG_FUNCTION_ARGS){
    int a1 = PG_GETARG_INT32(0);
    int a2 = PG_GETARG_INT32(1);
    PG_RETURN_INT32(a1 + a2);
}

宏定义如下

#define PG_FUNCTION_ARGS   FuncationCallInfo fcinfo
#define PG_NARGS()         (fcinfo->nargs)
#define PG_ARGISNULL(n)    (fcinfo->argnull[n])
#define PG_GETARG_DATUM(n) (fcinfo->arg[n])
#define PG_GETARG_INT32(n) DatumGetInt32(PG_GETARG_DATUM(n))
#define PG_RETURN_INT32(x) return Int32GetDatum(x)
#define PG_RETURN_NULL()   do { fcinfo->isnull = true; return (Datum) 0; } while (0)

创建函数

CREATE FUNCTION sum_int(int, int)
RETURNS int AS 'pg_kmcb','add'
LANGUAGE C;
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

kmblack1

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值