Doubt regarding query parameter metadata

From: Giovani Garcia <giovani(dot)garcia(at)protonmail(dot)com>
To: "pgsql-interfaces(at)lists(dot)postgresql(dot)org" <pgsql-interfaces(at)lists(dot)postgresql(dot)org>
Subject: Doubt regarding query parameter metadata
Date: 2021-04-01 03:33:40
Message-ID: eCpNID63M_eEUgOvB_39vy2LH_FaR9YS3hoUp7fYHbsKOQp9OzOfGglSQV3Cs50ajXJK3DBpZ-fma6qCawNyfX2Py3jnP7IOhr4FTUcbGBk=@protonmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-interfaces

Hello everyone,

I'm using the PQprepare and PQdescribePrepared functions of the libpq in order to retrieve query parameter metadata and encode certain values based on the types of columns I'm dealing with.

The issue I'm finding is that the Oid returned for a VARCHAR column is TEXTOID (25) instead of VARCHAROID (1043).
From what I read, a VARCHAR column with unspecified size behaves in the same way a TEXT column does, and I don't know if this could possibly affect the Oid of a column or not.
But that's not the case here: the VARCHAR column has a specified size of 20.

Here's the CREATE TABLE in question:

CREATE TABLE oid_test (
key CHAR(10),
value VARCHAR(20)
);

And the resulting data on pg_attribute table:

postgres=# select attname, attlen, atttypid, attcollation, atttypmod from pg_attribute where attrelid = 'oid_test'::regclass;
attname | attlen | atttypid | attcollation | atttypmod
----------+--------+----------+--------------+-----------
tableoid | 4 | 26 | 0 | -1
cmax | 4 | 29 | 0 | -1
xmax | 4 | 28 | 0 | -1
cmin | 4 | 29 | 0 | -1
xmin | 4 | 28 | 0 | -1
ctid | 6 | 27 | 0 | -1
key | -1 | 1042 | 100 | 14
value | -1 | 1043 | 100 | 24
(8 rows)

Now, when I run the following program (simplified for brevity), I get 25 (TEXTOID) as the Oid of the query param instead of the expected 1043 (VARCHAROID).
Could anyone help me understand why?

#include <libpq-fe.h>

int main() {

int n = 1;

PGconn* conn = PQconnectdb("postgresql://127.0.0.1:5432/postgres");

PGresult* prepared = PQprepare(conn,

"oid-test",

"SELECT key FROM oid_test WHERE value = $1",

n,

NULL);

PGresult* metadata = PQdescribePrepared(conn, "oid-test");

printf("%d\n", PQparamtype(metadata, 0));

PQfinish(conn);

}

I tried reading the source code of Postgresql and libpq to make some sense of it, but I'm a newbie and couldn't get to the bottom of things.

I appreciate any help, thanks in advance!
Giovani

Responses

Browse pgsql-interfaces by date

  From Date Subject
Next Message Tom Lane 2021-04-01 14:14:20 Re: Doubt regarding query parameter metadata
Previous Message Luiz Fernandes 2021-03-09 16:49:58 Re: How to find whether a cursor can scroll with libpq