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