BUG #11469: Recursive plpython function results in KeyError for next function

From: rahiyer(at)gmail(dot)com
To: pgsql-bugs(at)postgresql(dot)org
Subject: BUG #11469: Recursive plpython function results in KeyError for next function
Date: 2014-09-22 17:47:15
Message-ID: 20140922174715.17174.44694@wrigleys.postgresql.org
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 11469
Logged by: Rahul Iyer
Email address: rahiyer(at)gmail(dot)com
PostgreSQL version: 9.3.5
Operating system: Mac osx 10.9.4
Description:

The problem occurs when a plpython function calls itself using plpy.execute.
The next plpython function results in a KeyError corresponding to the last
argument of the recursive function. The problem does not occur if a string
representation of 'globals()' is created. Minimal repro steps have been
provided below.

-----------------------------------------------
CREATE OR REPLACE FUNCTION recur_test(a int) RETURNS VOID AS $$
if a:
plpy.execute("select recur_test({0})".format(a-1))
$$ LANGUAGE PLPYTHONU;

CREATE OR REPLACE FUNCTION test() RETURNS TEXT AS $$
import sys
return 'test'
$$ LANGUAGE PLPYTHONU;

CREATE OR REPLACE FUNCTION test2() RETURNS TEXT AS $$
g = str(globals())
import sys
return 'test'
$$ LANGUAGE PLPYTHONU;
-----------------------------------------------

-- Postgres version

-- Both functions work as expected
# SELECT test();
test
------
test
(1 row)

# SELECT test2();
test2
-------
test
(1 row)

-- recursive function call
# SELECT recur_test(2);
recur_test
------------

(1 row)

# SELECT test();
ERROR: XX000: KeyError: 'a'
CONTEXT: Traceback (most recent call last):
PL/Python function "test", line 2, in <module>
import sys
PL/Python function "test"
LOCATION: PLy_elog, plpy_elog.c:106

-- second call does not give error
# SELECT test();
test
------
test
(1 row)

# SELECT recur_test(2);
recur_test
------------

(1 row)

-- no error in this case
# SELECT test2();
test2
-------
test
(1 row)

---------------------------------------------------

Browse pgsql-bugs by date

  From Date Subject
Next Message Nelson Page 2014-09-22 18:45:02 Re: BUG #11457: The below query crashes 9.3.5, but not 9.3.4
Previous Message Heikki Linnakangas 2014-09-22 16:38:20 Re: [BUGS] Re: [BUGS] Re: BUG #11431: Failing to backup and restore a Windows postgres database, with Norwegian Bokmål locale.