[PATCH] plpython function causes server panic

From: Hao Zhang <zhrt1446384557(at)gmail(dot)com>
To: pgsql-hackers(at)lists(dot)postgresql(dot)org
Subject: [PATCH] plpython function causes server panic
Date: 2023-11-30 07:15:55
Message-ID: CALY6Dr-2yLVeVPhNMhuBnRgOZo1UjoTETgtKBx1B2gUi8yy+3g@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

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

Attachment Content-Type Size
v1-0001-Fix-bug-plpython-function-causes-server-panic.patch text/x-patch 4.8 KB

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Hayato Kuroda (Fujitsu) 2023-11-30 07:28:05 RE: Is this a problem in GenericXLogFinish()?
Previous Message Peter Smith 2023-11-30 07:05:29 Re: Synchronizing slots from primary to standby