Thread: Problems converting between C string and Datum

Problems converting between C string and Datum

From
"Jack Orenstein"
Date:
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

Re: Problems converting between C string and Datum

From
Tom Lane
Date:
"Jack Orenstein" <jack.orenstein@gmail.com> writes:
> 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)))));

That looks OK as far as it goes, so I speculate the problem is in
something you didn't show us.

> This crashes doing the ereport:

If you are doing backend C code you should certainly learn how to use
gdb to narrow things down more than that.  A stack trace would be much
more informative than "server closed the connection unexpectedly".

            regards, tom lane

Re: Problems converting between C string and Datum

From
Martijn van Oosterhout
Date:
On Wed, Sep 20, 2006 at 11:19:29AM -0400, Jack Orenstein wrote:
> 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:

<snip>

They look OK. For examples see the PG_STR_GET_TEXT() and
PG_TEXT_GET_STR() macros in utils/adt/varlena.c.

> 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.

Obviously something is going wrong. Perhaps you should enable coredumps
and use gdb to examine them, or attach gdb to the running backend to
catch the error.

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

Try elog(), but the details are also controlled by the client also,
perhaps you can reduce the verbosity there also?

Have a nice day,
--
Martijn van Oosterhout   <kleptog@svana.org>   http://svana.org/kleptog/
> From each according to his ability. To each according to his ability to litigate.

Attachment