aboutsummaryrefslogtreecommitdiff
path: root/src/pl/plpython/sql/plpython_setof.sql
diff options
context:
space:
mode:
authorPeter Eisentraut <peter_e@gmx.net>2012-05-02 20:59:51 +0300
committerPeter Eisentraut <peter_e@gmx.net>2012-05-02 20:59:51 +0300
commit52aa334fcd5a9d230be7e8fb964d94c6c4e63dc7 (patch)
tree1bf7fe4cd01a6d127c6271c5d81b4e5199bf6107 /src/pl/plpython/sql/plpython_setof.sql
parente9605a039b60350003daf8a5b3c0c10993994b60 (diff)
downloadpostgresql-52aa334fcd5a9d230be7e8fb964d94c6c4e63dc7.tar.gz
postgresql-52aa334fcd5a9d230be7e8fb964d94c6c4e63dc7.zip
PL/Python: Fix crash in functions returning SETOF and using SPI
Allocate PLyResultObject.tupdesc in TopMemoryContext, because its lifetime is the lifetime of the Python object and it shouldn't be freed by some other memory context, such as one controlled by SPI. We trust that the Python object will clean up its own memory. Before, this would crash the included regression test case by trying to use memory that was already freed. reported by Asif Naeem, analysis by Tom Lane
Diffstat (limited to 'src/pl/plpython/sql/plpython_setof.sql')
-rw-r--r--src/pl/plpython/sql/plpython_setof.sql12
1 files changed, 12 insertions, 0 deletions
diff --git a/src/pl/plpython/sql/plpython_setof.sql b/src/pl/plpython/sql/plpython_setof.sql
index 80a8d5b4c1e..243f711dcc6 100644
--- a/src/pl/plpython/sql/plpython_setof.sql
+++ b/src/pl/plpython/sql/plpython_setof.sql
@@ -62,3 +62,15 @@ SELECT test_setof_as_iterator(2, 'list');
SELECT test_setof_as_iterator(2, null);
SELECT test_setof_spi_in_iterator();
+
+
+-- setof function with an SPI result set (used to crash because of
+-- memory management issues across multiple calls)
+
+CREATE OR REPLACE FUNCTION get_user_records()
+RETURNS SETOF users
+AS $$
+ return plpy.execute("SELECT * FROM users ORDER BY username")
+$$ LANGUAGE plpythonu;
+
+SELECT get_user_records();