public inbox for [email protected]
help / color / mirror / Atom feedFrom: Giovani Garcia <[email protected]>
To: [email protected] <[email protected]>
Subject: Doubt regarding query parameter metadata
Date: Thu, 01 Apr 2021 03:33:40 +0000
Message-ID: <eCpNID63M_eEUgOvB_39vy2LH_FaR9YS3hoUp7fYHbsKOQp9OzOfGglSQV3Cs50ajXJK3DBpZ-fma6qCawNyfX2Py3jnP7IOhr4FTUcbGBk=@protonmail.com> (raw)
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
view thread (4+ messages) latest in thread
reply
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients using the --to and --cc options:
reply via email
To: [email protected]
Cc: [email protected], [email protected]
Subject: Re: Doubt regarding query parameter metadata
In-Reply-To: <eCpNID63M_eEUgOvB_39vy2LH_FaR9YS3hoUp7fYHbsKOQp9OzOfGglSQV3Cs50ajXJK3DBpZ-fma6qCawNyfX2Py3jnP7IOhr4FTUcbGBk=@protonmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox