plpython: Add SPI cursor support

Add a function plpy.cursor that is similar to plpy.execute but uses an
SPI cursor to avoid fetching the entire result set into memory.

Jan Urbański, reviewed by Steve Singer
This commit is contained in:
Peter Eisentraut
2011-12-05 19:52:15 +02:00
parent e6d9e2106f
commit 89e850e6fd
9 changed files with 1251 additions and 3 deletions

View File

@ -382,3 +382,73 @@ SELECT * FROM subtransaction_tbl;
(0 rows)
DROP TABLE subtransaction_tbl;
-- cursor/subtransactions interactions
CREATE FUNCTION cursor_in_subxact() RETURNS int AS $$
with plpy.subtransaction():
cur = plpy.cursor("select * from generate_series(1, 20) as gen(i)")
cur.fetch(10)
fetched = cur.fetch(10);
return int(fetched[5]["i"])
$$ LANGUAGE plpythonu;
ERROR: could not compile PL/Python function "cursor_in_subxact"
DETAIL: SyntaxError: invalid syntax (line 3)
CREATE FUNCTION cursor_aborted_subxact() RETURNS int AS $$
try:
with plpy.subtransaction():
cur = plpy.cursor("select * from generate_series(1, 20) as gen(i)")
cur.fetch(10);
plpy.execute("select no_such_function()")
except plpy.SPIError:
fetched = cur.fetch(10)
return int(fetched[5]["i"])
return 0 # not reached
$$ LANGUAGE plpythonu;
ERROR: could not compile PL/Python function "cursor_aborted_subxact"
DETAIL: SyntaxError: invalid syntax (line 4)
CREATE FUNCTION cursor_plan_aborted_subxact() RETURNS int AS $$
try:
with plpy.subtransaction():
plpy.execute('create temporary table tmp(i) '
'as select generate_series(1, 10)')
plan = plpy.prepare("select i from tmp")
cur = plpy.cursor(plan)
plpy.execute("select no_such_function()")
except plpy.SPIError:
fetched = cur.fetch(5)
return fetched[2]["i"]
return 0 # not reached
$$ LANGUAGE plpythonu;
ERROR: could not compile PL/Python function "cursor_plan_aborted_subxact"
DETAIL: SyntaxError: invalid syntax (line 4)
CREATE FUNCTION cursor_close_aborted_subxact() RETURNS boolean AS $$
try:
with plpy.subtransaction():
cur = plpy.cursor('select 1')
plpy.execute("select no_such_function()")
except plpy.SPIError:
cur.close()
return True
return False # not reached
$$ LANGUAGE plpythonu;
ERROR: could not compile PL/Python function "cursor_close_aborted_subxact"
DETAIL: SyntaxError: invalid syntax (line 4)
SELECT cursor_in_subxact();
ERROR: function cursor_in_subxact() does not exist
LINE 1: SELECT cursor_in_subxact();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT cursor_aborted_subxact();
ERROR: function cursor_aborted_subxact() does not exist
LINE 1: SELECT cursor_aborted_subxact();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT cursor_plan_aborted_subxact();
ERROR: function cursor_plan_aborted_subxact() does not exist
LINE 1: SELECT cursor_plan_aborted_subxact();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
SELECT cursor_close_aborted_subxact();
ERROR: function cursor_close_aborted_subxact() does not exist
LINE 1: SELECT cursor_close_aborted_subxact();
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.