Re: arrays returned in text format - Mailing list pgsql-general

From Konstantin Izmailov
Subject Re: arrays returned in text format
Date
Msg-id CAAw-MsdVVaVB8LFUvbpjgfsPyWn8A5a3PdHr=EXs-bSkAZdrHA@mail.gmail.com
Whole thread Raw
In response to Re: arrays returned in text format  (Tom Lane <tgl@sss.pgh.pa.us>)
Responses Re: arrays returned in text format  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-general
Oops, I forgot to mention that we slightly modified libpq to request resulting fields formats (since Postgres protocol v3 supports this). See our additions in Bold:

PQexec(PGconn *conn, const char *query, int resultFormatCount, const int* resultFormats)
{
    if (!PQexecStart(conn))
        return NULL;
    if (!PQsendQuery(conn, query, resultFormatCount, resultFormats))
        return NULL;
    return PQexecFinish(conn, 0);
}

where PQsendQuery passes requested format in the Bind message:

        /* construct the Bind message */
        if (pqPutMsgStart('B', false, conn) < 0 ||
            pqPuts("", conn) < 0 ||
            pqPuts(""/* use unnamed statement */, conn) < 0)
            goto sendFailed;

        /* no parameters formats */
        if (pqPutInt(0, 2, conn) < 0)
            goto sendFailed;

        if (pqPutInt(0, 2, conn) < 0)
            goto sendFailed;

        if (pqPutInt(resultFormatCount, 2, conn) < 0)
            goto sendFailed;

        for (i = 0; i < resultFormatCount; i++)
        {
            if (pqPutInt(resultFormats[i], 2, conn) < 0)
                goto sendFailed;
        }


The above is being used for about 10 years in our variant of libpq. It works for everything except for the case with ARRAY.

Thank you for the quick reply!


On Fri, Mar 4, 2016 at 10:03 PM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
Konstantin Izmailov <pgfizm@gmail.com> writes:
> I'm using libpq to read array values, and I noticed that sometimes the
> values are returned in Binary and sometimes - in Text format.

> 1. Returned in Binary format:
>    int formats[1] = { 1 }; // request binary format
>    res = PQexec(conn, "SELECT rgField FROM aTable", 1, formats);
>    assert(PQfformat(res, 0) == 1);  // this is OK

> 2. Returned in Text format:
>    res = PQexec(conn, "SELECT ARRAY[1,2,3]", 1, formats);
>    assert(PQfformat(res, 0) == 1);  // this fails???

Um, that is not the call signature of PQexec(), nor of any of its
variants.

                        regards, tom lane

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: arrays returned in text format
Next
From: Tom Lane
Date:
Subject: Re: arrays returned in text format