Skip to content

Commit

Permalink
Revert "[6X_STABLE] Fix bug: PLPY function causes master process rese…
Browse files Browse the repository at this point in the history
…t. (#16864)"

This reverts commit 45d6ba8.
  • Loading branch information
beeender committed Jan 19, 2024
1 parent 45d6ba8 commit fe8860a
Show file tree
Hide file tree
Showing 6 changed files with 13 additions and 125 deletions.
1 change: 0 additions & 1 deletion src/backend/access/transam/xact.c
Original file line number Diff line number Diff line change
Expand Up @@ -5043,7 +5043,6 @@ void
BeginInternalSubTransaction(char *name)
{
TransactionState s = CurrentTransactionState;
SIMPLE_FAULT_INJECTOR("begin_internal_sub_transaction");

if (Gp_role == GP_ROLE_DISPATCH)
{
Expand Down
45 changes: 0 additions & 45 deletions src/pl/plpython/expected/plpython_subtransaction.out
Original file line number Diff line number Diff line change
Expand Up @@ -476,48 +476,3 @@ CONTEXT: Traceback (most recent call last):
PL/Python function "cursor_close_aborted_subxact", line 7, in <module>
cur.close()
PL/Python function "cursor_close_aborted_subxact"
-- error report test in subtransaction begin
-- prepare function
CREATE OR REPLACE FUNCTION test_func() RETURNS SETOF int AS
$$
plpy.execute("select pg_backend_pid()")

for i in range(0, 5):
yield (i)

$$ LANGUAGE plpython3u;
-- inject fault and wait for trigger
select gp_inject_fault_infinite('begin_internal_sub_transaction', 'error', 1);
gp_inject_fault_infinite
--------------------------
Success:
(1 row)

SELECT test_func();
ERROR: function "test_func" error fetching next item from iterator
DETAIL: spiexceptions.FaultInject: fault triggered, fault name:'begin_internal_sub_transaction' fault type:'error'
CONTEXT: Traceback (most recent call last):
PL/Python function "test_func"
select gp_wait_until_triggered_fault('begin_internal_sub_transaction', 1, 1);
gp_wait_until_triggered_fault
-------------------------------
Success:
(1 row)

select gp_inject_fault('begin_internal_sub_transaction', 'reset', 1);
gp_inject_fault
-----------------
Success:
(1 row)

SELECT test_func();
test_func
-----------
0
1
2
3
4
(5 rows)

DROP FUNCTION test_func();
12 changes: 4 additions & 8 deletions src/pl/plpython/plpy_cursorobject.c
Original file line number Diff line number Diff line change
Expand Up @@ -116,8 +116,7 @@ PLy_cursor_query(const char *query)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -206,8 +205,7 @@ PLy_cursor_plan(PyObject *ob, PyObject *args)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -355,8 +353,7 @@ PLy_cursor_iternext(PyObject *self)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -425,8 +422,7 @@ PLy_cursor_fetch(PyObject *self, PyObject *args)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down
57 changes: 8 additions & 49 deletions src/pl/plpython/plpy_spi.c
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,7 @@ PLy_spi_prepare(PyObject *self, PyObject *args)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -255,8 +254,7 @@ PLy_spi_execute_plan(PyObject *ob, PyObject *list, int64 limit)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -369,8 +367,7 @@ PLy_spi_execute_query(char *query, int64 limit)
oldcontext = CurrentMemoryContext;
oldowner = CurrentResourceOwner;

if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
return NULL;
PLy_spi_subtransaction_begin(oldcontext, oldowner);

PG_TRY();
{
Expand Down Expand Up @@ -501,9 +498,7 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int64 rows, int status)
* MemoryContext oldcontext = CurrentMemoryContext;
* ResourceOwner oldowner = CurrentResourceOwner;
*
* if(!PLy_spi_subtransaction_begin(oldcontext, oldowner))
* return NULL;
*
* PLy_spi_subtransaction_begin(oldcontext, oldowner);
* PG_TRY();
* {
* <call SPI functions>
Expand All @@ -520,48 +515,12 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, int64 rows, int status)
* These utilities take care of restoring connection to the SPI manager and
* setting a Python exception in case of an abort.
*/
bool
void
PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner)
{
PG_TRY();
{
/* Start subtransaction (could fail) */
BeginInternalSubTransaction(NULL);
/* Want to run inside function's memory context */
MemoryContextSwitchTo(oldcontext);
}
PG_CATCH();
{
ErrorData *edata;
PLyExceptionEntry *entry;
PyObject *exc;

/* Ensure we restore original context and owner */
MemoryContextSwitchTo(oldcontext);
CurrentResourceOwner = oldowner;

/* Save error info */
edata = CopyErrorData();
FlushErrorState();

/* Look up the correct exception */
entry = hash_search(PLy_spi_exceptions, &(edata->sqlerrcode),
HASH_FIND, NULL);

/*
* This could be a custom error code, if that's the case fallback to
* SPIError
*/
exc = entry ? entry->exc : PLy_exc_spi_error;
/* Make Python raise the exception */
PLy_spi_exception_set(exc, edata);
FreeErrorData(edata);

return false;
}
PG_END_TRY();

return true;
BeginInternalSubTransaction(NULL);
/* Want to run inside function's memory context */
MemoryContextSwitchTo(oldcontext);
}

void
Expand Down
2 changes: 1 addition & 1 deletion src/pl/plpython/plpy_spi.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ typedef struct PLyExceptionEntry
} PLyExceptionEntry;

/* handling of SPI operations inside subtransactions */
extern bool PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner);
extern void PLy_spi_subtransaction_begin(MemoryContext oldcontext, ResourceOwner oldowner);
extern void PLy_spi_subtransaction_commit(MemoryContext oldcontext, ResourceOwner oldowner);
extern void PLy_spi_subtransaction_abort(MemoryContext oldcontext, ResourceOwner oldowner);

Expand Down
21 changes: 0 additions & 21 deletions src/pl/plpython/sql/plpython_subtransaction.sql
Original file line number Diff line number Diff line change
Expand Up @@ -294,24 +294,3 @@ SELECT cursor_in_subxact();
SELECT cursor_aborted_subxact();
SELECT cursor_plan_aborted_subxact();
SELECT cursor_close_aborted_subxact();

-- error report test in subtransaction begin
-- prepare function
CREATE OR REPLACE FUNCTION test_func() RETURNS SETOF int AS
$$
plpy.execute("select pg_backend_pid()")

for i in range(0, 5):
yield (i)

$$ LANGUAGE plpythonu;

-- inject fault and wait for trigger
select gp_inject_fault_infinite('begin_internal_sub_transaction', 'error', 1);
SELECT test_func();
select gp_wait_until_triggered_fault('begin_internal_sub_transaction', 1, 1);
select gp_inject_fault('begin_internal_sub_transaction', 'reset', 1);

SELECT test_func();

DROP FUNCTION test_func();

0 comments on commit fe8860a

Please sign in to comment.