How can I see the PostgreSQL column type from a RowDescription message?

1.2k views Asked by At

I'm implementing a driver to PostgreSQL and gets an RowDescription message and a few DataRow messages in response to a database query. But how do I get the type for the columns returned? E.g. the first column should be an int and the second an varchar(20).

Here are some printouts from RowDescription:

[RowDescription] 2 rows
[Row] id
tableObjId: 16393
attrNr: 1
objId: 23
dataTypeSz: 4
typeModifier: -1
formatCode: 0
[Row] name
tableObjId: 16393
attrNr: 2
objId: 1043
dataTypeSz: -1
typeModifier: 24
formatCode: 0

and for the DataRow:

[DataRow] 2 columns
data: 2
data: Jonas
[DataRow] 2 columns
data: 76
data: Anders

Any suggestions? Is this anything I have to lookup in any systemtables?

1

There are 1 answers

0
araqnid On BEST ANSWER

objId is the reference to pg_type.oid for the column type. 23 is int4 and 1043 is varchar (select * from pg_type where oid in (23,1043))

The type modifier for the second column should indicate the qualifier on varchar, although I'm not quite sure why it's 24 rather than 20. Probably because that's the length of the varlena structure returned, which has a 32-bit length prepended. Anyway, that's how typmod works: if you do select pg_catalog.format_type(1043, 24), you'll see that the output is character varying(20). The documentation for PQfmod says that the typmod interpretation is type-specific.