Thread: How to manipulate tuples in C functions?

How to manipulate tuples in C functions?

From
"a"
Date:
Hey all:

As far as I know, composite type and rows (trigger functions) are passed between PostgreSQL and C functions as tuple. However, I rarely find things relating on how to manipulate a tuple under C from documentation. 

The only thing I can find from the doc is very simple stuff like 'GetAttributeByName' or 'GetAttributeByNum' to retrieve data, or use a 'BlessTupleDesc' to initialize a 'TupleDesc' variable and 'heap_form_tuple' to return a 'HeapTuple' and return a 'HeapTupleGetDatum(HeapTuple tuple)' to convert to a 'Datum' type.

However not only the illustration of the previous listed functions are not fully described (e.g. return and input of each functions) but also any complex situation are stated. What if I have a composite type inside a composite type? What if I have a array inside a composite type? What if I have a variable length type?

Furthermore, to write a compatible procedures, I would need at least 2 more information: 1, The total number of entry inside a tuple; 2, The total space the tuple took.

So if anyone can tell me how to answer the above question? Or simply give an example of iterator that go through all entries of a tuple? Thank you so much!! 

Re: How to manipulate tuples in C functions?

From
Tom Lane
Date:
"=?ISO-8859-1?B?YQ==?=" <372660931@qq.com> writes:
> As far as I know, composite type and rows (trigger functions) are passed between PostgreSQL and C functions as tuple.
However,I rarely find things relating on how to manipulate a tuple under C from documentation.  

A lot of low-level stuff like that is only really documented in the server
source code.  You shouldn't hesitate to look around in the server source
and crib from functions that do something related to what you want.

> So if anyone can tell me how to answer the above question? Or simply give an example of iterator that go through all
entriesof a tuple? Thank you so much!! 

I'd suggest record_out() in src/backend/utils/adt/rowtypes.c as a
prototypical example of disassembling an arbitrary tuple passed as
a composite-type argument.  (Note that the environment for trigger
functions is a bit different, mostly for historical reasons.)

            regards, tom lane