How to get the 'ctid' from a record type?

From: Eric Ridge <eebbrr(at)gmail(dot)com>
To: "pgsql-hackers(at)postgresql(dot)org" <pgsql-hackers(at)postgresql(dot)org>
Subject: How to get the 'ctid' from a record type?
Date: 2017-03-11 00:48:03
Message-ID: CANcm6wb-=qQRYqHZe0b2JcYKbXhNkQWPY_37dPW_jHX_sbbJKw@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: 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

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andres Freund 2017-03-11 00:58:10 Indirect assignment code for array slices is dead code?
Previous Message Bruce Momjian 2017-03-11 00:42:25 Re: WIP: Faster Expression Processing v4