Doubt regarding query parameter metadata - Mailing list pgsql-interfaces

From Giovani Garcia
Subject Doubt regarding query parameter metadata
Date
Msg-id eCpNID63M_eEUgOvB_39vy2LH_FaR9YS3hoUp7fYHbsKOQp9OzOfGglSQV3Cs50ajXJK3DBpZ-fma6qCawNyfX2Py3jnP7IOhr4FTUcbGBk=@protonmail.com
Whole thread Raw
Responses Re: Doubt regarding query parameter metadata  (Tom Lane <tgl@sss.pgh.pa.us>)
List 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

pgsql-interfaces by date:

Previous
From: Ashutosh Sharma
Date:
Subject: Re: #include present twice in define.pgc
Next
From: Tom Lane
Date:
Subject: Re: Doubt regarding query parameter metadata