Memory for BYTEA returned by C function is not released until connection is dropped

Поиск
Список
Период
Сортировка
От John Leiseboer
Тема Memory for BYTEA returned by C function is not released until connection is dropped
Дата
Msg-id 1EC7FC2A429FF940B43E21AC8D1EECD057F521D7@qbrick.homeip.net
обсуждение исходный текст
Ответы Re: Memory for BYTEA returned by C function is not released until connection is dropped
Список pgsql-general
I have written a number of functions in C that return BYTEA type. I have compiled and run on both Windows and Linux,
32-bitand 64-bit, PostgreSQL versions 9.3 and 9.4. 

My functions return BYTEA data to the caller. The problem is that memory usage grows until there is no memory left on
thehost, at which point an error is returned. If I drop the connection (e.g. by quitting from pqsql), the memory is
returned.

I wrote the following minimal function to test palloc() and BYTEA return behaviour, and found that this minimal program
alsoexhibits the unbounded memory growth problem. 


C source code:

PG_FUNCTION_INFO_V1(test_palloc);
Datum test_palloc()
{
    bytea *test_ret;
    int test_len = 1024;

    test_ret = (bytea *)palloc(test_len + VARHDRSZ);
    SET_VARSIZE(test_ret, test_len + VARHDRSZ);
    PG_RETURN_BYTEA_P(test_ret);
}

Function definition:

CREATE OR REPLACE FUNCTION test_palloc() RETURNS BYTEA AS E'<path to shared library>', test_palloc' LANGUAGE C
IMMUTABLESTRICT; 

psql commands to reproduce the problem:

\o out.txt
SELECT ids.*, test_palloc() FROM GENERATE_SERIES(1, 1000000) ids;

At the completion of the above command, host memory will have been consumed but not released back to the system. After
quittingpsql (\q), memory is released. 

Is this expected behaviour or a bug? Am I doing something wrong? How can I return a BYTEA type from a C library
functionwithout having to drop the connection in order to recover the allocated memory that is returned to the caller? 

Regards,
John



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

Предыдущее
От: Melvin Davidson
Дата:
Сообщение: Re: clone_schema function
Следующее
От: Tom Lane
Дата:
Сообщение: Re: Memory for BYTEA returned by C function is not released until connection is dropped