How to return a large String with C

From: Stefan Niantschur <sniantschur(at)web(dot)de>
To: pgsql-general(at)postgresql(dot)org
Subject: How to return a large String with C
Date: 2008-02-17 12:40:29
Message-ID: 200802171340.29916.sniantschur@web.de
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

Hi all,

I want to write a function in C which retrieves a large string from a table,
does some work on it and returns the result to the surrounding SELECT.
(e.g. SELECT my_c_func(text);)

So far I have been successfully doing calls to SPI, select the data from the
table and return it. However, this works only with string not larger than
page size of char[8192].

The strings I expect are much longer and this causes the backend to crash.
Printing the string via elog shows the correct content, returning the string
to the postmaster makes the backend crah.

How do I do this correctly?

The code snippet which works look like this:
PG_FUNCTION_INFO_V1(my_c_func);
Datum
my_c_func (PG_FUNCTION_ARGS)
(...)
char buf[8192];
if (ret > 0 && SPI_tuptable != NULL)
{
TupleDesc tupdesc = SPI_tuptable->tupdesc;
SPITupleTable *tuptable = SPI_tuptable;
int i,j;
for (i = 0; i < proc; i++)
{
HeapTuple tuple = tuptable->vals[i];
for (i = 1, buf[0] = 0; i <= tupdesc->natts;
i++) {
snprintf(buf + strlen (buf),
sizeof(buf) - strlen(buf), " %s%s",
SPI_getvalue(tuple, tupdesc,
i),
(i ==
tupdesc->natts) ? " " : " |");
appendStringInfo(&result_buf,
SPI_getvalue(tuple, tupdesc, i));
}
}
}
SPI_finish();
(... do some work here ...)
elog(INFO, Content: %s <==###", result_buf.data);
PG_RETURN_TEXT_P(GET_TEXT((char *)query_buf.data));
(...)

Responses

Browse pgsql-general by date

  From Date Subject
Next Message Jorge Godoy 2008-02-17 12:47:21 Re: the feasibility of sending email from stored procedure in Postgres
Previous Message Greg Smith 2008-02-17 07:22:21 Re: Query output into a space delimited/location sensitive file