Обсуждение: Inserting data a UDT in binary format using LIBPQ

Поиск
Список
Период
Сортировка

Inserting data a UDT in binary format using LIBPQ

От
"Brijesh Shrivastav"
Дата:
Dear PostgreSQL Gurus,

I need your help. I have created a UDT that has both text
and binary functions (func_IN and func_recv) to recieve
text and binary data. I have also added some other functions
for new type that takes binary and text input. Everything
seems to be working fine if I am using psql. I can load and query
data. However, I cannot seem to insert data using LibPQ. It calls
func_recv functions on the server side but doesn't get the
data it expects. I have created the type be of variable length
and am 'detoasting' the data inside recv function to make sure
data input is properly parsed.

Included below is a snippet of code cut and places from various
functions that I am using to insert. On server side when I try to print
input data size and values I get incorrect results.

I didn't find much documentation but thought this should have
worked. Please point any thing that I didn't do right. Also, another
function that takes binary input in the very same manner and can be
invoked at sql level seems to work just fine.

Thanks,
Brijesh

Client side code:
********************************************** sprintf(m_psql," Insert into %s (UDTCOL) VALUES "   " ($1)",
TEST_TABLE);
  m_paramTypes[0] = 50415; //OID for the new type from pg_type table m_paramValues[0] = "A text string to test the data
input"; m_paramSizes[0] = strlen(m_paramValues[0]); m_paramFormats[0] = PG_BINARY_FORMAT;  m_res = PQprepare(m_pgconn,
m_stmtName,m_psql,1,m_paramTypes); if (PQresultStatus(m_res) != PGRES_COMMAND_OK) {   ....   .... } 
m_res = PQexecPrepared(m_pgconn,                        m_stmtName,                        1,
m_paramValues,                       (const int  *) m_paramSizes,                        (const int *) m_paramFormats,
                     m_resFormat);  
 if (PQresultStatus(m_res) != PGRES_COMMAND_OK &&     PQresultStatus(m_res) != PGRES_TUPLES_OK) {
 }

Server side code:
**********************************************

Datum func_Recv(PG_FUNCTION_ARGS)
{    FUNC_BINARY_FORMAT *input     = (FUNC_BINARY_FORMAT *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));

   elog(NOTICE," Binary DATA_LEN = %d\n",VARSIZE(input));   elog(NOTICE," Binary DATA CONTENT= %s\n",VARDATA(input));


Re: Inserting data a UDT in binary format using LIBPQ

От
Tom Lane
Дата:
"Brijesh Shrivastav" <Bshrivastav@esri.com> writes:
> Datum func_Recv(PG_FUNCTION_ARGS)
> {
>      FUNC_BINARY_FORMAT *input 
>         = (FUNC_BINARY_FORMAT *)PG_DETOAST_DATUM(PG_GETARG_DATUM(0));

Why would you expect that the input to a receive function would be
already in the datatype's internal format?

I'm too lazy to go check the code right now, but my recollection is that
what you get is a reference to a StringInfo holding the data message
received from the client.  You should probably not touch the StringInfo
directly if you can avoid it, but use the convenience functions that are
provided for receive functions to use.  Look at the code for existing
receive functions for datatypes similar to yours.
        regards, tom lane