[HACKERS] How to get the 'ctid' from a record type? - Mailing list pgsql-hackers

From Eric Ridge
Subject [HACKERS] How to get the 'ctid' from a record type?
Date
Msg-id CANcm6wb-=qQRYqHZe0b2JcYKbXhNkQWPY_37dPW_jHX_sbbJKw@mail.gmail.com
Whole thread Raw
Responses Re: [HACKERS] How to get the 'ctid' from a record type?
List pgsql-hackers
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

pgsql-hackers by date:

Previous
From: Bruce Momjian
Date:
Subject: Re: [HACKERS] WIP: Faster Expression Processing v4
Next
From: Andres Freund
Date:
Subject: [HACKERS] Indirect assignment code for array slices is dead code?