Search Postgresql Archives

How to return a large String with C

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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));
(...)

---------------------------(end of broadcast)---------------------------
TIP 6: explain analyze is your friend

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[Index of Archives]     [Postgresql Jobs]     [Postgresql Admin]     [Postgresql Performance]     [Linux Clusters]     [PHP Home]     [PHP on Windows]     [Kernel Newbies]     [PHP Classes]     [PHP Books]     [PHP Databases]     [Postgresql & PHP]     [Yosemite]
  Powered by Linux