如何在postgres中使用float8用户定义函数

时间:2018-01-08 13:31:46

标签: c postgresql stored-functions

我比postgres服务器编程更新,我想实现双+ 1; 这是https://www.postgresql.org/docs/9.6/static/xfunc-c.html的代码副本。

#include "postgres.h"
#include <string.h>
#include "utils/geo_decls.h"

#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif

float8 *
add_one_float8(float8 *arg)
{
    float8    *result = (float8 *) palloc(sizeof(float8));

    *result = *arg + 1.0;//if replace *arg with 1.5, it return normal(2.5)

    return result;
}

我成功创建了函数。每次调用函数时,该部分都被postgres杀死。

然后我尝试用float8替换float8 * arg,然后用语句

调用function
select add_one(1.5);

它返回1; 似乎arg是0.然后我用以下代码替换它

float8
add_one_float8(float8 arg)
{
    if(arg>0){
        return arg+2;
    }else if(arg==0)
        return arg+3;
    else
        return arg+4;
}

然后它返回2; 我不知道如何在postgres中处理float8或double,有人知道如何解决它或我错过了必要的步骤吗?谢谢提前。

顺便说一句,postgresql-9.5和9.6我试试,结果是一样的。平台是linux,centos7

1 个答案:

答案 0 :(得分:0)

the documentation中所述,您需要使用PG_GETARG_FLOAT8(0)来获取参数,并使用PG_RETURN_FLOAT8来返回结果。

相关问题