Memory for BYTEA returned by C function is not released until connection is dropped - Mailing list pgsql-general

From John Leiseboer
Subject Memory for BYTEA returned by C function is not released until connection is dropped
Date
Msg-id ME1PR01MB04046CB79227CC1861967234B0460@ME1PR01MB0404.ausprd01.prod.outlook.com
Whole thread Raw
Responses Re: Memory for BYTEA returned by C function is not released until connection is dropped
List 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 IMMUTABLE STRICT;

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 by date:

Previous
From: Ramesh T
Date:
Subject: postgres 9.3
Next
From: Pavel Stehule
Date:
Subject: Re: Memory for BYTEA returned by C function is not released until connection is dropped