greenplum-db/plcontainer

ERROR when try/catch inside a subtransaction block

BaiShaoqi opened this issue · 2 comments

Expected behavior

NOTICE:  caught
CONTEXT:  PL/Python function "try_catch_inside_subtransaction"
 try_catch_inside_subtransaction
---------------------------------

(1 row)

 i
---
 1
(1 row)

NOTICE:  caught
CONTEXT:  PL/Python function "pk_violation_inside_subtransaction"
 pk_violation_inside_subtransaction
------------------------------------

(1 row)

 i
---
 1
(1 row)

Actual behavior

WARNING:  forcibly aborting a subtransaction that has not been exited
ERROR:  PL/Container client exception occurred:
DETAIL:  Error receiving subtransaction exit error message


 i
---
(0 rows)

WARNING:  forcibly aborting a subtransaction that has not been exited
ERROR:  PL/Container client exception occurred:
DETAIL:  Error receiving subtransaction exit error message

 i
---
(0 rows)

Step to reproduce the behavior

CREATE TABLE subtransaction_tbl (
    i integer
);

CREATE FUNCTION try_catch_inside_subtransaction() RETURNS void
AS $$
# container: plc_python_shared
with plpy.subtransaction():
     plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     try:
         plpy.execute("INSERT INTO subtransaction_tbl VALUES ('a')")
     except plpy.SPIError:
         plpy.notice("caught")
$$ LANGUAGE plcontainer;

SELECT try_catch_inside_subtransaction();
SELECT * FROM subtransaction_tbl;
TRUNCATE subtransaction_tbl;

ALTER TABLE subtransaction_tbl ADD PRIMARY KEY (i);

CREATE FUNCTION pk_violation_inside_subtransaction() RETURNS void
AS $$
# container: plc_python_shared
with plpy.subtransaction():
     plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     try:
         plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
     except plpy.SPIError:
         plpy.notice("caught")
$$ LANGUAGE plcontainer;

SELECT pk_violation_inside_subtransaction();
SELECT * FROM subtransaction_tbl;
DROP TABLE subtransaction_tbl;

We have created an issue in Pivotal Tracker to manage this. Unfortunately, the Pivotal Tracker project is private so you may be unable to view the contents of the story.

The labels on this github issue will be updated when the story is started.

The RCA of this issue is the result of execution of plpy.execute is not return back to Container through a message, but error out directly.
In fact try except logic in container should be responsible for whether the plpy.execute should error out or not.

try:
    plpy.execute("INSERT INTO subtransaction_tbl VALUES (1)")
except plpy.SPIError:
    plpy.notice("caught")