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

Commite086197

Browse files
committed
Fix aboriginal mistake in plpython's set-returning-function support.
We must stay in the function's SPI context until done calling the iteratorthat returns the set result. Otherwise, any attempt to invoke SPI featuresin the python code called by the iterator will malfunction. Diagnosis andpatch by Jan Urbanski, per bug report from Jean-Baptiste Quenot.Back-patch to 8.2; there was no support for SRFs in previous versions ofplpython.
1 parent102aeed commite086197

File tree

3 files changed

+48
-8
lines changed

3 files changed

+48
-8
lines changed

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

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,14 @@ class producer:
3131
return self.icontent
3232
return producer(count, content)
3333
$$ LANGUAGE plpythonu;
34+
CREATE FUNCTION test_setof_spi_in_iterator() RETURNS SETOF text AS
35+
$$
36+
for s in ('Hello', 'Brave', 'New', 'World'):
37+
plpy.execute('select 1')
38+
yield s
39+
plpy.execute('select 2')
40+
$$
41+
LANGUAGE plpythonu;
3442
-- Test set returning functions
3543
SELECT test_setof_as_list(0, 'list');
3644
test_setof_as_list
@@ -107,3 +115,12 @@ SELECT test_setof_as_iterator(2, null);
107115

108116
(2 rows)
109117

118+
SELECT test_setof_spi_in_iterator();
119+
test_setof_spi_in_iterator
120+
----------------------------
121+
Hello
122+
Brave
123+
New
124+
World
125+
(4 rows)
126+

‎src/pl/plpython/plpython.c

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -984,7 +984,10 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
984984
{
985985
if (!proc->is_setof||proc->setof==NULL)
986986
{
987-
/* Simple type returning function or first time for SETOF function */
987+
/*
988+
* Simple type returning function or first time for SETOF function:
989+
* actually execute the function.
990+
*/
988991
plargs=PLy_function_build_args(fcinfo,proc);
989992
plrv=PLy_procedure_call(proc,"args",plargs);
990993
if (!proc->is_setof)
@@ -999,14 +1002,10 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
9991002
}
10001003

10011004
/*
1002-
* Disconnect from SPI manager and then create the return values datum
1003-
* (if the input function does a palloc for it this must not be
1004-
* allocated in the SPI memory context because SPI_finish would free
1005-
* it).
1005+
* If it returns a set, call the iterator to get the next return item.
1006+
* We stay in the SPI context while doing this, because PyIter_Next()
1007+
* calls back into Python code which might contain SPI calls.
10061008
*/
1007-
if (SPI_finish()!=SPI_OK_FINISH)
1008-
elog(ERROR,"SPI_finish failed");
1009-
10101009
if (proc->is_setof)
10111010
{
10121011
boolhas_error= false;
@@ -1063,11 +1062,24 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
10631062
(errcode(ERRCODE_DATA_EXCEPTION),
10641063
errmsg("error fetching next item from iterator")));
10651064

1065+
/* Disconnect from the SPI manager before returning */
1066+
if (SPI_finish()!=SPI_OK_FINISH)
1067+
elog(ERROR,"SPI_finish failed");
1068+
10661069
fcinfo->isnull= true;
10671070
return (Datum)NULL;
10681071
}
10691072
}
10701073

1074+
/*
1075+
* Disconnect from SPI manager and then create the return values datum
1076+
* (if the input function does a palloc for it this must not be
1077+
* allocated in the SPI memory context because SPI_finish would free
1078+
* it).
1079+
*/
1080+
if (SPI_finish()!=SPI_OK_FINISH)
1081+
elog(ERROR,"SPI_finish failed");
1082+
10711083
plerrcontext.callback=plpython_return_error_callback;
10721084
plerrcontext.previous=error_context_stack;
10731085
error_context_stack=&plerrcontext;

‎src/pl/plpython/sql/plpython_setof.sql

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,15 @@ class producer:
3535
return producer(count, content)
3636
$$ LANGUAGE plpythonu;
3737

38+
CREATEFUNCTIONtest_setof_spi_in_iterator() RETURNS SETOFtextAS
39+
$$
40+
for sin ('Hello','Brave','New','World'):
41+
plpy.execute('select 1')
42+
yield s
43+
plpy.execute('select 2')
44+
$$
45+
LANGUAGE plpythonu;
46+
3847

3948
-- Test set returning functions
4049
SELECT test_setof_as_list(0,'list');
@@ -51,3 +60,5 @@ SELECT test_setof_as_iterator(0, 'list');
5160
SELECT test_setof_as_iterator(1,'list');
5261
SELECT test_setof_as_iterator(2,'list');
5362
SELECT test_setof_as_iterator(2,null);
63+
64+
SELECT test_setof_spi_in_iterator();

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp