Accessing composite type elements

Started by Garfield Lewisalmost 4 years ago5 messagesgeneral
Jump to latest
#1Garfield Lewis
garfield.lewis@lzlabs.com

Hi All,

I’m not sure if this is the actual place for this but I guess I can start here. The question I have is, I’ve created a composite type like this:

CREATE TYPE myxml AS { encoding_ int4, xml_ xml };

In my client-side C code I am using PQgetvalue to pull in the data from the row/column. However, since it is a composite type, it is unclear to me how to get the individual members from the data. I have googled but I probably just am not googling the correct term because I cannot find any examples of this being done anywhere. A hex dump of the data gives me this:

0x0000000200000017000000046f0100000000008e000001433c637573746f6d6572696e666f20786d6c6e733d22687474703a2f2f6c7a6c6162732e637573742e636f6d22204369643d22543130303130303031223e3c6e616d653e5461626c6520584d4c…

I can tell that the green portion is my endcoding_ value and the blue section is the xml_ data.

My best guess right now is:

* 0x2 is the number of members
* 0x4 and 0x143 are the lengths of the individual members
* 0x17 and 0x8e are the OID for the member type

Is this the proper layout of these composite types? Can I go ahead and use this without possibly having it broken in the future? Are there any existing supported APIs that I can use instead to get this information?

Regards
G

#2Tom Lane
tgl@sss.pgh.pa.us
In reply to: Garfield Lewis (#1)
Re: Accessing composite type elements

Garfield Lewis <garfield.lewis@lzlabs.com> writes:

In my client-side C code I am using PQgetvalue to pull in the data from the row/column. However, since it is a composite type, it is unclear to me how to get the individual members from the data.

Binary representations are not too well documented :-(. However,
looking at record_send() helps here.

My best guess right now is:
* 0x2 is the number of members
* 0x4 and 0x143 are the lengths of the individual members
* 0x17 and 0x8e are the OID for the member type

Right, with the additional proviso that -1 "length" indicates a null
field value.

regards, tom lane

#3Garfield Lewis
garfield.lewis@lzlabs.com
In reply to: Tom Lane (#2)
Re: [EXT] Re: Accessing composite type elements

Binary representations are not too well documented :-(. However,
looking at record_send() helps here.

will have a look…

Right, with the additional proviso that -1 "length" indicates a null
field value.

Thx, Tom… never thought the null field…

--
Regards,
Garfield A. Lewis

#4Merlin Moncure
mmoncure@gmail.com
In reply to: Garfield Lewis (#3)
Re: [EXT] Re: Accessing composite type elements

On Thu, Jun 2, 2022 at 12:05 PM Garfield Lewis <garfield.lewis@lzlabs.com>
wrote:

Binary representations are not too well documented :-(. However,
looking at record_send() helps here.

will have a look…

Right, with the additional proviso that -1 "length" indicates a null
field value.

Thx, Tom… never thought the null field…

take a look at libpqtypes. it's client side extension library to libpq that
implements the binary protocol.

https://github.com/pgagarinov/libpqtypes

merlin

Show quoted text
#5Garfield Lewis
garfield.lewis@lzlabs.com
In reply to: Merlin Moncure (#4)
Re: [EXT] Re: Accessing composite type elements

take a look at libpqtypes. it's client side extension library to libpq that implements the binary protocol.

https://github.com/pgagarinov/libpqtypes

merlin

Thx, Merlin… I’ll have a look see…