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

时间:2022-09-15 19:19:42

I am newer to postgres server programing and I want to achieve double+1; here is the code copy from https://www.postgresql.org/docs/9.6/static/xfunc-c.html.

我对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;
}

I successfully create function.Everytimes I call function,the section was killed by postgres.

我成功地创建函数。每次我调用函数,这个部分就会被postgres杀死。

Then I try to replace float8 *arg with float8 and then call function with statement

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

select add_one(1.5);

it return 1; it seems to arg is 0.And then I replace it with the following code

它返回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;
}

and then it return 2; I have no idea how to handle float8 or double in postgres,does anybody know how to fix it or what neccessary step I missed?Thanks advance.

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

By the way, both postgresql-9.5 and 9.6 I tries,the result is the same.Platform is linux,centos7

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

1 个解决方案

#1


0  

As described in the documentation, you need to use PG_GETARG_FLOAT8(0) to get the argument and PG_RETURN_FLOAT8 to return the result.

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

#1


0  

As described in the documentation, you need to use PG_GETARG_FLOAT8(0) to get the argument and PG_RETURN_FLOAT8 to return the result.

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