pl/python long-lived allocations in datum->dict transformation - Mailing list pgsql-hackers

From Jan Urbański
Subject pl/python long-lived allocations in datum->dict transformation
Date
Msg-id 4F2ED053.1010904@wulczer.org
Whole thread Raw
Responses Re: pl/python long-lived allocations in datum->dict transformation  (Tom Lane <tgl@sss.pgh.pa.us>)
List pgsql-hackers
Consider this:

create table arrays as select array[random(), random(), random(),
random(), random(), random()] as a from generate_series(1, 1000000);

create or replace function plpython_outputfunc() returns void as $$
c = plpy.cursor('select a from arrays')
for row in c:
    pass
$$ language plpythonu;

When running the function, every datum will get transformed into a
Python dict, which includes calling the type's output function,
resulting in a memory allocation. The memory is allocated in the SPI
context, so it accumulates until the function is finished.

This is annoying for functions that plough through large tables, doing
some calculation. Attached is a patch that does the conversion of
PostgreSQL Datums into Python dict objects in a scratch memory context
that gets reset every time.

Cheers,
Jan

Attachment

pgsql-hackers by date:

Previous
From: Jeff Davis
Date:
Subject: Re: initdb and fsync
Next
From: Jan Urbański
Date:
Subject: plpgsql leaking memory when stringifying datums