pg.ddx.io pgsql-interfaces@postgresql.org mailing list archive
help / color / mirror / Atom feedInserting data a UDT in binary format using LIBPQ
3+ messages / 2 participants
[nested] [flat]
* Inserting data a UDT in binary format using LIBPQ
@ 2007-01-09 17:30 Brijesh Shrivastav <Bshrivastav@esri.com>
0 siblings, 1 reply; 3+ messages in thread
From: Brijesh Shrivastav @ 2007-01-09 17:30 UTC (permalink / raw)
To: pgsql-interfaces; +Cc: Brijesh Shrivastav <Bshrivastav@esri.com>
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));
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Inserting data a UDT in binary format using LIBPQ
@ 2007-01-09 21:17 Tom Lane <tgl@sss.pgh.pa.us>
parent: Brijesh Shrivastav <Bshrivastav@esri.com>
0 siblings, 0 replies; 3+ messages in thread
From: Tom Lane @ 2007-01-09 21:17 UTC (permalink / raw)
To: Brijesh Shrivastav <Bshrivastav@esri.com>; +Cc: pgsql-interfaces
"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
^ permalink raw reply [nested|flat] 3+ messages in thread
* Re: Inserting data a UDT in binary format using LIBPQ
@ 2007-01-09 21:51 Brijesh Shrivastav <Bshrivastav@esri.com>
0 siblings, 0 replies; 3+ messages in thread
From: Brijesh Shrivastav @ 2007-01-09 21:51 UTC (permalink / raw)
To: Tom Lane <tgl@sss.pgh.pa.us>; +Cc: pgsql-interfaces
FUNC_BINARY_FORMAT was similar to varlena struct and I thought
this is what you would get back from a detoasted argument in
recieve function but I guess it was a wrong assumption. I will
try StringInfo structure instead.
Thanks for the pointer.
Brijesh
> -----Original Message-----
> From: Tom Lane [mailto:tgl@sss.pgh.pa.us]
> Sent: Tuesday, January 09, 2007 1:17 PM
> To: Brijesh Shrivastav
> Cc: pgsql-interfaces@postgresql.org
> Subject: Re: [INTERFACES] Inserting data a UDT in binary format using
> LIBPQ
>
>
> "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
>
>
^ permalink raw reply [nested|flat] 3+ messages in thread
end of thread, other threads:[~2007-01-09 21:51 UTC | newest]
Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2007-01-09 17:30 Inserting data a UDT in binary format using LIBPQ Brijesh Shrivastav <Bshrivastav@esri.com>
2007-01-09 21:17 ` Re: Inserting data a UDT in binary format using LIBPQ Tom Lane <tgl@sss.pgh.pa.us>
2007-01-09 21:51 Re: Inserting data a UDT in binary format using LIBPQ Brijesh Shrivastav <Bshrivastav@esri.com>
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox