gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:37:00 25 4
gpt4 key购买 nike

我是postgres服务器编程的新手,我想实现double+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,然后用语句调用函数

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

最佳答案

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

关于c - 如何在postgres c用户定义函数中使用float8,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48151370/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com