Problems converting between C string and Datum - Mailing list pgsql-general

From Jack Orenstein
Subject Problems converting between C string and Datum
Date
Msg-id 7ecd811f0609200819n1eba1dc1i87073c0ef1c1719e@mail.gmail.com
Whole thread Raw
Responses Re: Problems converting between C string and Datum
Re: Problems converting between C string and Datum
List pgsql-general
Thanks for all your help with the memory management problems. Next
problem: I'm having problems converting from a char* to a Datum and back
again.

I have a char* which I need as a Datum, for use with a plan returned
from SPI_prepare, so I'm doing this:

    char* string;
    Datum d;
    ...
    d = DirectFunctionCall1(textin, CStringGetDatum(string));
    ...
    ereport(WARNING, (errmsg("string: %s",

DatumGetCString(DirectFunctionCall1(textout, d)))));

(The ereport is for debugging -- my code then proceeds to call
SPI_execute_plan.)

This crashes doing the ereport:

    psql:test.sql:23: server closed the connection unexpectedly
        This probably means the server terminated abnormally
        before or while processing the request.

So my questions are:

1) Is this code the way to convert char* to Datum and back again?

    DirectFunctionCall1(textin, CStringGetDatum(string))
    ...
    DatumGetCString(DirectFunctionCall1(textout, d))

I've put this code together based on examples I've found on the web
and in the postgres source.

2) Is there some neater way to generate debug output than ereport(WARNING, ...)?
The output is quite verbose, e.g.

    psql:test.sql:23: WARNING:  MY DEBUG OUTPUT
    CONTEXT:  SQL statement "insert into log select insert_ifs(  $1 ,
$2 ,  $3 ,  $4 ,  $5 ,  $6 ,  $7 )"
    PL/pgSQL function "regress" line 5 at SQL statement

for each line of output.


Jack Orenstein

pgsql-general by date:

Previous
From: Tom Lane
Date:
Subject: Re: pg_dump output containing CREATE TYPE does not restore with psql
Next
From: Jeff Davis
Date:
Subject: 8.2: select from an INSERT returning?