Now it works. It turns out that DateADT
is number of days (integer) since 2000-01-01.
c function:
#include "postgres.h"
#include "fmgr.h"
#include "utils/date.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(dwd);
Datum
dwd(PG_FUNCTION_ARGS)
{
int32 days = PG_GETARG_INT32(0);
if (days > 0) {
DateADT dateVal = 2556;
PG_RETURN_DATEADT(dateVal + days);
}
else {
PG_RETURN_NULL();
}
}
Performance test:
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.
During my search I have found this to be quite useful reference for c functions in PostgreSQL.