BUG #11469: Recursive plpython function results in KeyError for next function - Mailing list pgsql-bugs

From rahiyer@gmail.com
Subject BUG #11469: Recursive plpython function results in KeyError for next function
Date
Msg-id 20140922174715.17174.44694@wrigleys.postgresql.org
Whole thread Raw
List pgsql-bugs
The following bug has been logged on the website:

Bug reference:      11469
Logged by:          Rahul Iyer
Email address:      rahiyer@gmail.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)

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

pgsql-bugs by date:

Previous
From: Stephen Frost
Date:
Subject: Re: BUG #11350: ALTER SYSTEM is not DDL?
Next
From: Nelson Page
Date:
Subject: Re: BUG #11457: The below query crashes 9.3.5, but not 9.3.4