This is about Postgres 9.6...
I have a very simple 1-arg function, in C, that I want to return the ctid of the record passed in. Example:
CREATE OR REPLACE FUNCTION foo(record) RETURNS tid LANGUAGE c IMMUTABLE STRICT AS 'my_extension';
Its implementation is simply:
Datum foo(PG_FUNCTION_ARGS) {
HeapTupleHeader td = PG_GETARG_HEAPTUPLEHEADER(0);
PG_RETURN_POINTER(&td->t_ctid);
}
What I'm seeing is that the ctid returned from this function isn't always correct:
# select ctid, foo(table) from table limit 10;
ctid | foo
-------+-----------
(0,1) | (19195,1) -- not correct!
(0,2) | (0,2)
(0,3) | (0,3)
(0,4) | (0,4)
(0,5) | (0,5)
(0,6) | (0,6)
(0,7) | (0,7)
(1,1) | (1,1)
(1,2) | (1,2)
(1,3) | (1,3)
(10 rows)
I've spent hours tracing through the PG sources trying to figure out why and/or find a different way to do this, but I've got nothing.
Of the various examples in the PG sources that use PG_GETARG_HEAPTUPLEHEADER, or otherwise deal with a HeapTupleHeader, I can't find any that want to access a system column, so I'm starting to think I'm just doing it wrong entirely.
Can anyone point me in the right direction?
Thanks for your time!
eric