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