我正在尝试将以下SQL函数重写为c等效函数(尝试使其稍微快一点):CREATE OR REPLACE FUNCTION dat2(time_key integer) RETURNS date AS$BODY$BEGIN RETURN case when time_key > 0 then '2006-12-31'::date + time_key end as result;END;$BODY$ LANGUAGE plpgsql IMMUTABLE STRICT COST 100;我想我可以修改现有的操作符,做一些类似的事情:#include "postgres.h"#include "fmgr.h"#include "utils/date.h"#ifdef PG_MODULE_MAGICPG_MODULE_MAGIC;#endifPG_FUNCTION_INFO_V1(dwd);Datumdwd(PG_FUNCTION_ARGS){ /* DateADT dateVal = PG_GETARG_DATEADT(0); */ DateADT dateVal = PG_GETARG_DATEADT(2006-12-31); int32 days = PG_GETARG_INT32(0); PG_RETURN_DATEADT(dateVal + days);}如果我编译我的函数并将其转换为date函数,我可以在PostgreSQL中创建int4函数:create or replace function dwd(int) returns date as'/usr/lib/postgresql/9.3/lib/dwd', 'dwd' language c cost 1;我得到.so的dwd,但我期望2000-01-01。显然select dwd(0);有问题。如何在这个c函数中定义日期常量? (adsbygoogle = window.adsbygoogle || []).push({}); 最佳答案 现在它工作了。原来DateADT是2000-01-01以来的天数(整数)。c函数:#include "postgres.h"#include "fmgr.h"#include "utils/date.h"#ifdef PG_MODULE_MAGICPG_MODULE_MAGIC;#endifPG_FUNCTION_INFO_V1(dwd);Datumdwd(PG_FUNCTION_ARGS){ int32 days = PG_GETARG_INT32(0); if (days > 0) { DateADT dateVal = 2556; PG_RETURN_DATEADT(dateVal + days); } else { PG_RETURN_NULL(); }}性能测试: drop table if exists tmp; create table tmp as select dat2(gs) from generate_series(1,1000000) gs; -- Query returned successfully: 1000000 rows affected, 4101 ms execution time. drop table if exists tmp; create table tmp as select dwd(gs) from generate_series(1,1000000) gs; -- Query returned successfully: 1000000 rows affected, 1527 ms execution time.在我的搜索过程中,我发现this对PostgreSQL中的c函数非常有用。 (adsbygoogle = window.adsbygoogle || []).push({}); 10-08 03:20