我剛剛開始用Postgres外部C函數進行測試。當我傳入一個數字並返回它時,該函數可以正常工作。 (例)Postgres C函數 - 傳遞和返回數字
示例函數
PG_FUNCTION_INFO_V1(numericTesting);
Datum
numericTesting(PG_FUNCTION_ARGS)
{
Numeric p = PG_GETARG_NUMERIC(0);
PG_RETURN_NUMERIC(p);
}
然而,當我嘗試做傳遞的變量的任何數學函數,它不會編譯。我得到
error: invalid operands to binary *
示例函數
PG_FUNCTION_INFO_V1(numericTesting);
Datum
numericTesting(PG_FUNCTION_ARGS)
{
Numeric p = PG_GETARG_NUMERIC(0);
PG_RETURN_NUMERIC(p * .5);
}
是什麼原因造成的?我猜數字數據類型需要一些函數來允許數學。我試過使用:PG_RETURN_NUMERIC(DatumGetNumeric(p * .5))但結果相同。
該版本可行,但需要使用兩個數字。需要弄清楚如何從一個Integer或Float8實例化一個Numeric我想。 PG_FUNCTION_INFO_V1(numericTesting); Datum numericTesting(PG_FUNCTION_ARGS) { Numeric p = PG_GETARG_NUMERIC(0);數字答案= DatumGetNumeric(DirectFunctionCall2(numeric_mul,NumericGetDatum(p_latitude),NumericGetDatum(p_latitude))); PG_RETURN_NUMERIC(answer); } – user739866