[PATCH] plpython function causes server panic
Hao Zhang <zhrt1446384557@gmail.com>
From: Hao Zhang <zhrt1446384557@gmail.com>
To: pgsql-hackers@lists.postgresql.org
Date: 2023-11-30T07:15:55Z
Lists: pgsql-hackers
Attachments
- v1-0001-Fix-bug-plpython-function-causes-server-panic.patch (text/x-patch) patch v1-0001
Hi hackers,
I found a problem when executing the plpython function:
After the plpython function returns an error, in the same session, if we
continue to execute
plpython function, the server panic will be caused.
*Reproduce*
preparation
SET max_parallel_workers_per_gather=4;
SET parallel_setup_cost=1;
SET min_parallel_table_scan_size ='4kB';
CREATE TABLE t(i int);
INSERT INTO t SELECT generate_series(1, 10000);
CREATE EXTENSION plpython3u;
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 parallel safe;
execute the function twice in the same session
postgres=# SELECT test_func() from t where i>10 and i<100;
ERROR: error fetching next item from iterator
DETAIL: Exception: cannot start subtransactions during a parallel
operation
CONTEXT: Traceback (most recent call last):
PL/Python function "test_func"
postgres=# SELECT test_func();
server closed the connection unexpectedly
This probably means the server terminated abnormally
before or while processing the request.
The connection to the server was lost. Attempting reset: Failed.
The connection to the server was lost. Attempting reset: Failed.
*Analysis*
- There is an SPI call in test_func(): plpy.execute().
- Then the server will start a subtransaction by
PLy_spi_subtransaction_begin(); BUT! The Python processor does not know
whether an error happened during PLy_spi_subtransaction_begin().
- If there is an error that occurs in PLy_spi_subtransaction_begin(),
the SPI call will be terminated but the python error indicator won't be set
and the PyObject won't be free.
- Then the next plpython UDF in the same session will fail due to the
wrong Python environment.
*Solution*
Use try-catch to catch the error that occurs in
PLy_spi_subtransaction_begin(), and set the python error indicator.
With Regards
Hao Zhang
Commits
-
Allow "internal" subtransactions in parallel mode.
- 0075d78947e3 17.0 landed
-
Tighten up application of parallel mode checks.
- 94b4f7e2a635 9.6.0 cited