Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit1d2fe56

Browse files
committed
Fix PL/Python for recursion and interleaved set-returning functions.
PL/Python failed if a PL/Python function was invoked recursively via SPI,since arguments are passed to the function in its global dictionary(a horrible decision that's far too ancient to undo) and it would deletethose dictionary entries on function exit, leaving the outer recursionlevel(s) without any arguments. Not deleting them would be little better,since the outer levels would then see the innermost level's arguments.Since PL/Python uses ValuePerCall mode for evaluating set-returningfunctions, it's possible for multiple executions of the same SRF to beinterleaved within a query. PL/Python failed in such a case, becauseit stored only one iterator per function, directly in the function'sPLyProcedure struct. Moreover, one interleaved instance of the SRFwould see argument values that should belong to another.Hence, invent code for saving and restoring the argument entries. To fixthe recursion case, we only need to save at recursive entry and restoreat recursive exit, so the overhead in non-recursive cases is negligible.To fix the SRF case, we have to save when suspending a SRF and restorewhen resuming it, which is potentially not negligible; but fortunatelythis is mostly a matter of manipulating Python object refcounts andshould not involve much physical data copying.Also, store the Python iterator and saved argument values in a structureassociated with the SRF call site rather than the function itself. Thisrequires adding a memory context deletion callback to ensure that the SRFstate is cleaned up if the calling query exits before running the SRF tocompletion. Without that we'd leak a refcount to the iterator object insuch a case, resulting in session-lifespan memory leakage. (In thepre-existing code, there was no memory leak because there was only oneiterator pointer, but what would happen is that the previous iteratorwould be resumed by the next query attempting to use the SRF. Hardly thesemantics we want.)We can buy back some of whatever overhead we've added by getting rid ofPLy_function_delete_args(), which seems a useless activity: there is noneed to delete argument entries from the global dictionary on exit,since the next time anyone would see the global dict is on the nextfresh call of the PL/Python function, at which time we'd overwrite thoseentries with new arg values anyway.Also clean up some really ugly coding in the SRF implementation, includingsuch gems as returning directly out of a PG_TRY block. (The only reasonthat failed to crash hard was that all existing call sites immediatelyexited their own PG_TRY blocks, popping the dangling longjmp pointer beforethere was any chance of it being used.)In principle this is a bug fix; but it seems a bit too invasive relative toits value for a back-patch, and besides the fix depends on memory contextcallbacks so it could not go back further than 9.5 anyway.Alexey Grishchenko and Tom Lane
1 parent11c8669 commit1d2fe56

File tree

7 files changed

+401
-73
lines changed

7 files changed

+401
-73
lines changed

‎src/pl/plpython/expected/plpython_setof.out

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,35 @@ SELECT test_setof_spi_in_iterator();
124124
World
125125
(4 rows)
126126

127+
-- set-returning function that modifies its parameters
128+
CREATE OR REPLACE FUNCTION ugly(x int, lim int) RETURNS SETOF int AS $$
129+
global x
130+
while x <= lim:
131+
yield x
132+
x = x + 1
133+
$$ LANGUAGE plpythonu;
134+
SELECT ugly(1, 5);
135+
ugly
136+
------
137+
1
138+
2
139+
3
140+
4
141+
5
142+
(5 rows)
143+
144+
-- interleaved execution of such a function
145+
SELECT ugly(1,3), ugly(7,8);
146+
ugly | ugly
147+
------+------
148+
1 | 7
149+
2 | 8
150+
3 | 7
151+
1 | 8
152+
2 | 7
153+
3 | 8
154+
(6 rows)
155+
127156
-- returns set of named-composite-type tuples
128157
CREATE OR REPLACE FUNCTION get_user_records()
129158
RETURNS SETOF users

‎src/pl/plpython/expected/plpython_spi.out

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,15 @@ for r in rv:
5757
return seq
5858
'
5959
LANGUAGE plpythonu;
60+
CREATE FUNCTION spi_recursive_sum(a int) RETURNS int
61+
AS
62+
'r = 0
63+
if a > 1:
64+
r = plpy.execute("SELECT spi_recursive_sum(%d) as a" % (a-1))[0]["a"]
65+
return a + r
66+
'
67+
LANGUAGE plpythonu;
68+
--
6069
-- spi and nested calls
6170
--
6271
select nested_call_one('pass this along');
@@ -112,6 +121,12 @@ SELECT join_sequences(sequences) FROM sequences
112121
----------------
113122
(0 rows)
114123

124+
SELECT spi_recursive_sum(10);
125+
spi_recursive_sum
126+
-------------------
127+
55
128+
(1 row)
129+
115130
--
116131
-- plan and result objects
117132
--

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp