1
0
mirror of https://github.com/postgres/postgres.git synced 2025-07-17 06:41:09 +03:00

Fix SPI's handling of errors during transaction commit.

SPI_commit previously left it up to the caller to recover from any error
occurring during commit.  Since that's complicated and requires use of
low-level xact.c facilities, it's not too surprising that no caller got
it right.  Let's move the responsibility for cleanup into spi.c.  Doing
that requires redefining SPI_commit as starting a new transaction, so
that it becomes equivalent to SPI_commit_and_chain except that you get
default transaction characteristics instead of preserving the prior
transaction's characteristics.  We can make this pretty transparent
API-wise by redefining SPI_start_transaction() as a no-op.  Callers
that expect to do something in between might be surprised, but
available evidence is that no callers do so.

Having made that API redefinition, we can fix this mess by having
SPI_commit[_and_chain] trap errors and start a new, clean transaction
before re-throwing the error.  Likewise for SPI_rollback[_and_chain].
Some cleanup is also needed in AtEOXact_SPI, which was nowhere near
smart enough to deal with SPI contexts nested inside a committing
context.

While plperl and pltcl need no changes beyond removing their now-useless
SPI_start_transaction() calls, plpython needs some more work because it
hadn't gotten the memo about catching commit/rollback errors in the
first place.  Such an error resulted in longjmp'ing out of the Python
interpreter, which leaks Python stack entries at present and is reported
to crash Python 3.11 altogether.  Add the missing logic to catch such
errors and convert them into Python exceptions.

This is a back-patch of commit 2e517818f.  That's now aged long enough
to reduce the concerns about whether it will break something, and we
do need to ensure that supported branches will work with Python 3.11.

Peter Eisentraut and Tom Lane

Discussion: https://postgr.es/m/3375ffd8-d71c-2565-e348-a597d6e739e3@enterprisedb.com
Discussion: https://postgr.es/m/17416-ed8fe5d7213d6c25@postgresql.org
This commit is contained in:
Tom Lane
2022-06-22 12:12:00 -04:00
parent 9a3aab0f2a
commit 293f5c5f49
17 changed files with 537 additions and 144 deletions

View File

@ -55,8 +55,11 @@ for i in range(0, 10):
return 1
$$;
SELECT transaction_test2();
ERROR: invalid transaction termination
CONTEXT: PL/Python function "transaction_test2"
ERROR: spiexceptions.InvalidTransactionTermination: invalid transaction termination
CONTEXT: Traceback (most recent call last):
PL/Python function "transaction_test2", line 5, in <module>
plpy.commit()
PL/Python function "transaction_test2"
SELECT * FROM test1;
a | b
---+---
@ -70,7 +73,7 @@ plpy.execute("CALL transaction_test1()")
return 1
$$;
SELECT transaction_test3();
ERROR: spiexceptions.InvalidTransactionTermination: invalid transaction termination
ERROR: spiexceptions.InvalidTransactionTermination: spiexceptions.InvalidTransactionTermination: invalid transaction termination
CONTEXT: Traceback (most recent call last):
PL/Python function "transaction_test3", line 2, in <module>
plpy.execute("CALL transaction_test1()")
@ -88,7 +91,7 @@ plpy.execute("DO LANGUAGE plpythonu $x$ plpy.commit() $x$")
return 1
$$;
SELECT transaction_test4();
ERROR: spiexceptions.InvalidTransactionTermination: invalid transaction termination
ERROR: spiexceptions.InvalidTransactionTermination: spiexceptions.InvalidTransactionTermination: invalid transaction termination
CONTEXT: Traceback (most recent call last):
PL/Python function "transaction_test4", line 2, in <module>
plpy.execute("DO LANGUAGE plpythonu $x$ plpy.commit() $x$")
@ -100,8 +103,11 @@ s.enter()
plpy.commit()
$$;
WARNING: forcibly aborting a subtransaction that has not been exited
ERROR: cannot commit while a subtransaction is active
CONTEXT: PL/Python anonymous code block
ERROR: spiexceptions.InvalidTransactionTermination: cannot commit while a subtransaction is active
CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 4, in <module>
plpy.commit()
PL/Python anonymous code block
-- commit inside cursor loop
CREATE TABLE test2 (x int);
INSERT INTO test2 VALUES (0), (1), (2), (3), (4);
@ -191,5 +197,54 @@ SELECT * FROM pg_cursors;
------+-----------+-------------+-----------+---------------+---------------
(0 rows)
-- check handling of an error during COMMIT
CREATE TABLE testpk (id int PRIMARY KEY);
CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED);
DO LANGUAGE plpythonu $$
# this insert will fail during commit:
plpy.execute("INSERT INTO testfk VALUES (0)")
plpy.commit()
plpy.warning('should not get here')
$$;
ERROR: spiexceptions.ForeignKeyViolation: insert or update on table "testfk" violates foreign key constraint "testfk_f1_fkey"
DETAIL: Key (f1)=(0) is not present in table "testpk".
CONTEXT: Traceback (most recent call last):
PL/Python anonymous code block, line 4, in <module>
plpy.commit()
PL/Python anonymous code block
SELECT * FROM testpk;
id
----
(0 rows)
SELECT * FROM testfk;
f1
----
(0 rows)
DO LANGUAGE plpythonu $$
# this insert will fail during commit:
plpy.execute("INSERT INTO testfk VALUES (0)")
try:
plpy.commit()
except Exception as e:
plpy.info('sqlstate: %s' % (e.sqlstate))
# these inserts should work:
plpy.execute("INSERT INTO testpk VALUES (1)")
plpy.execute("INSERT INTO testfk VALUES (1)")
$$;
INFO: sqlstate: 23503
SELECT * FROM testpk;
id
----
1
(1 row)
SELECT * FROM testfk;
f1
----
1
(1 row)
DROP TABLE test1;
DROP TABLE test2;

View File

@ -44,8 +44,6 @@ static PyObject *PLy_fatal(PyObject *self, PyObject *args, PyObject *kw);
static PyObject *PLy_quote_literal(PyObject *self, PyObject *args);
static PyObject *PLy_quote_nullable(PyObject *self, PyObject *args);
static PyObject *PLy_quote_ident(PyObject *self, PyObject *args);
static PyObject *PLy_commit(PyObject *self, PyObject *args);
static PyObject *PLy_rollback(PyObject *self, PyObject *args);
/* A list of all known exceptions, generated from backend/utils/errcodes.txt */
@ -582,31 +580,3 @@ PLy_output(volatile int level, PyObject *self, PyObject *args, PyObject *kw)
*/
Py_RETURN_NONE;
}
static PyObject *
PLy_commit(PyObject *self, PyObject *args)
{
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
SPI_commit();
SPI_start_transaction();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
Py_RETURN_NONE;
}
static PyObject *
PLy_rollback(PyObject *self, PyObject *args)
{
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
SPI_rollback();
SPI_start_transaction();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
Py_RETURN_NONE;
}

View File

@ -462,6 +462,100 @@ PLy_spi_execute_fetch_result(SPITupleTable *tuptable, uint64 rows, int status)
return (PyObject *) result;
}
PyObject *
PLy_commit(PyObject *self, PyObject *args)
{
MemoryContext oldcontext = CurrentMemoryContext;
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
PG_TRY();
{
SPI_commit();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
}
PG_CATCH();
{
ErrorData *edata;
PLyExceptionEntry *entry;
PyObject *exc;
/* Save error info */
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
/* 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 NULL;
}
PG_END_TRY();
Py_RETURN_NONE;
}
PyObject *
PLy_rollback(PyObject *self, PyObject *args)
{
MemoryContext oldcontext = CurrentMemoryContext;
PLyExecutionContext *exec_ctx = PLy_current_execution_context();
PG_TRY();
{
SPI_rollback();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
}
PG_CATCH();
{
ErrorData *edata;
PLyExceptionEntry *entry;
PyObject *exc;
/* Save error info */
MemoryContextSwitchTo(oldcontext);
edata = CopyErrorData();
FlushErrorState();
/* was cleared at transaction end, reset pointer */
exec_ctx->scratch_ctx = NULL;
/* 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 NULL;
}
PG_END_TRY();
Py_RETURN_NONE;
}
/*
* Utilities for running SPI functions in subtransactions.
*

View File

@ -13,6 +13,9 @@ extern PyObject *PLy_spi_prepare(PyObject *self, PyObject *args);
extern PyObject *PLy_spi_execute(PyObject *self, PyObject *args);
extern PyObject *PLy_spi_execute_plan(PyObject *ob, PyObject *list, long limit);
extern PyObject *PLy_commit(PyObject *self, PyObject *args);
extern PyObject *PLy_rollback(PyObject *self, PyObject *args);
typedef struct PLyExceptionEntry
{
int sqlstate; /* hash key, must be first */

View File

@ -148,5 +148,35 @@ SELECT * FROM test1;
SELECT * FROM pg_cursors;
-- check handling of an error during COMMIT
CREATE TABLE testpk (id int PRIMARY KEY);
CREATE TABLE testfk(f1 int REFERENCES testpk DEFERRABLE INITIALLY DEFERRED);
DO LANGUAGE plpythonu $$
# this insert will fail during commit:
plpy.execute("INSERT INTO testfk VALUES (0)")
plpy.commit()
plpy.warning('should not get here')
$$;
SELECT * FROM testpk;
SELECT * FROM testfk;
DO LANGUAGE plpythonu $$
# this insert will fail during commit:
plpy.execute("INSERT INTO testfk VALUES (0)")
try:
plpy.commit()
except Exception as e:
plpy.info('sqlstate: %s' % (e.sqlstate))
# these inserts should work:
plpy.execute("INSERT INTO testpk VALUES (1)")
plpy.execute("INSERT INTO testfk VALUES (1)")
$$;
SELECT * FROM testpk;
SELECT * FROM testfk;
DROP TABLE test1;
DROP TABLE test2;