Thread: question about libpq

question about libpq

From
Jay Campbell
Date:
Hello all.  I am trying to use libpq to store some large blocks of text
in a postgresql table, and I can't figure out how to make it behave
like I want.  Any advice would be very welcome.

I have a large block of text coming in on STDIN, and I would like to
dump it all to a given field.  In a perfect world, I could just do
this:

PQmagicFunction(conn, my_row_oid, my_field_number, STDIN);

...and it would grab the data from STDIN and append it to the given
field in the given row, until EOF.  Now, since PQmagicFunction doesn't
exist (as far as I know), I could buffer it and do the following:

char buffer[1024];
PQaddFromCharacterBufferToField(conn, my_row_oid, my_field_number,
buffer, 1024);

but again, I haven't seen PQaddFromCharacterBufferToField(PGconn *,
Oid, int, char *, int) in any of the header files.  :)

...as it stands, i'm having to escape the buffer contents and do a
PQexec 'update' command that appends the 1024 escaped characters to the
field, every time the buffer fills.  (which could be thousands of times
as the filesize could be in the megabytes)  Perhaps I'm doing it right,
but it sure feels icky.  I keep getting this feeling that there is a
better way (or two).

can someone point me in the right direction please?  If my explanation
didn't make sense, please tell me that too.

Thanks in advance,
-jay


Re: question about libpq

From
Joe Conway
Date:
Jay Campbell wrote:
> can someone point me in the right direction please?  If my explanation
> didn't make sense, please tell me that too.

See PQexecParams():
http://www.postgresql.org/docs/current/interactive/libpq-exec.html#LIBPQ-EXEC-MAIN

I think that's probably what you're looking for.

Joe


Re: question about libpq

From
Tom Lane
Date:
Joe Conway <mail@joeconway.com> writes:
> Jay Campbell wrote:
>> can someone point me in the right direction please?  If my explanation
>> didn't make sense, please tell me that too.

> See PQexecParams():

Another possibility, if you're intent on streaming the data in small
chunks, is to store it in a "large object".  The
lo_open/lo_write/lo_close functionality seems extremely close to what
you are asking for.  Unfortunately we have no comparable API at present
for regular text or bytea fields ...

            regards, tom lane