Search code examples
cpostgresqllibpq

Get (integer[]) array value from the query result of PostgreSQL using libpq library


after executing query I run below functions but don't know how to convert value to c integer array.

how to get integer array from pptRawValue where COLUMN_VALUE_IN_BINARY_FORMAT when I looked at memory i can see the value are present in pptRawValue but not able to get that values in integer array in c code?

please help.

long lColumnNum = PQfnumber(ptQueryRes, pstrColumnName);
long lFormat = PQfformat(ptQueryRes, lColumnNum);
*pptRawValue = PQgetvalue(ptQueryRes, lRowNum, lColumnNum);
*plValueLength = PQgetlength(ptQueryRes, lRowNum, lColumnNum);
int iarray[];

Solution

  • Found the way to extract values from received binary array

    The received binary array has below format. I have actually verified it.

    This didn't quite agree with what I found but it gave me a starting point. Here is what I found and hope experts will correct me if I am wrong:

    The first 20 bytes seem to have the following information about the array:

    --first 4 bytes don't know what it is but it is always 1
          This is may be format number as Laurenz Albe mentioned above.
           Reference:https://www.postgresql.org/docs/current/static/protocol-overview.html#PROTOCOL-FORMAT-CODES
    --second 4 bytes " " " " " " " " " 0
    --third 4 bytes oid of the datatype in the array
    --4th 4 bytes number of elements in the array
    --5th 4 bytes dimension of the array

    Form here we have the actual data. Each datum is preceeded with a 4 byte integer indicating the number of bytes the next element occupies. Knowing the number of elements one can parse the memory and get access to the elements.

    Please fallow the link: https://www.postgresql.org/message-id/attachment/13504/arrayAccess.txt