How to return a large String with C

Поиск
Список
Период
Сортировка
От Stefan Niantschur
Тема How to return a large String with C
Дата
Msg-id 200802171340.29916.sniantschur@web.de
обсуждение исходный текст
Ответы Re: How to return a large String with C
Список 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));
(...)

В списке pgsql-general по дате отправления:

Предыдущее
От: Greg Smith
Дата:
Сообщение: Re: Query output into a space delimited/location sensitive file
Следующее
От: Jorge Godoy
Дата:
Сообщение: Re: the feasibility of sending email from stored procedure in Postgres