I have a user-defined type mytype :
CREATE TYPE mytype AS (
timestamps timestamp[],
last integer
);
I want to access values in timestamps field using C function. Here is what I have written :
#include "postgres.h"
#include "fmgr.h"
#include "executor/executor.h"
#include "datatype/timestamp.h"
#ifdef PG_MODULE_MAGIC
PG_MODULE_MAGIC;
#endif
PG_FUNCTION_INFO_V1(testfunc);
Datum
testfunc(PG_FUNCTION_ARGS) {
bool isnull;
HeapTupleHeader at = PG_GETARG_HEAPTUPLEHEADER(0);
Datum dt = GetAttributeByName(at, "timestamps", &isnull);
Timestamp ** tts = (Timestamp **) DatumGetPointer(dt);
PG_RETURN_DATUM(**tts);
}
SQL reference :
CREATE OR REPLACE FUNCTION testfunc(mytype) RETURNS timestamp AS 'path/to/library.so', 'testfunc' LANGUAGE C STRICT;
Now I am getting garbage result when I am trying to test:
postgres=# SELECT testfunc('("{""2017-01-11 00:00:00"", NULL}", 10)');
testfunc
---------------------------
2000-01-01 00:00:00.00001
(1 row)