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

Commitadd0ea8

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 parent3134d88 commitadd0ea8

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
@@ -985,7 +985,10 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
985985
{
986986
if (!proc->is_setof||proc->setof==NULL)
987987
{
988-
/* Simple type returning function or first time for SETOF function */
988+
/*
989+
* Simple type returning function or first time for SETOF function:
990+
* actually execute the function.
991+
*/
989992
plargs=PLy_function_build_args(fcinfo,proc);
990993
plrv=PLy_procedure_call(proc,"args",plargs);
991994
if (!proc->is_setof)
@@ -1000,14 +1003,10 @@ PLy_function_handler(FunctionCallInfo fcinfo, PLyProcedure *proc)
10001003
}
10011004

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

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

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