BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails
The following bug has been logged on the website:
Bug reference: 19899
Logged by: Marcos Castedo
Email address: marcos.castedo@anachronics.com
PostgreSQL version: 19rc2
Operating system: Debian GNU/Linux (x86_64)
Description:
When the conversion of a column value to a Python object fails inside
Python, the backend
terminates with signal 11 and the postmaster restarts all sessions. The
expected result is an ERROR
in the calling session.
The steps below read a row with plpy.execute and make the conversion of one
column fail on demand
by starting the server with a lower memory limit; the limit is only a way to
trigger the failure.
The conversion is done by PLyDict_FromTuple, which also builds row arguments
and
TD["new"]/TD["old"]; only the plpy.execute case was run.
Steps to reproduce:
1. In a new cluster:
CREATE EXTENSION plpython3u;
CREATE TABLE big (id int, b bytea);
INSERT INTO big SELECT 1, convert_to(repeat('a', 300000000), 'UTF8');
2. Restart the server under an address-space limit that leaves room for the
server to detoast the
value but not for Python to copy it. Here a backend with Python loaded uses
about 188 MB, and the
limit was 188 MB + 450 MB:
ulimit -v 653312
pg_ctl -D <datadir> -l <logfile> start
The numbers depend on the build and platform; the limit has to leave room
for the detoasted value
but not for a second copy of it.
3. Then:
CREATE FUNCTION g() RETURNS int AS $$
r = plpy.execute("SELECT b FROM big")
return len(r[0]["b"])
$$ LANGUAGE plpython3u;
SELECT g();
server closed the connection unexpectedly
Server log (19rc2):
LOG: client backend (PID 548414) was terminated by signal 11: Segmentation
fault
LOG: terminating any other active server processes
LOG: all server processes terminated; reinitializing
17.12 shows the same with the same limit:
LOG: server process (PID 546767) was terminated by signal 11: Segmentation
fault
On 19rc2, with a higher limit (188 MB + 1200 MB) SELECT g() returns
300000000, and with a lower
one (188 MB + 100 MB) the server fails first, while detoasting, with an
ordinary out-of-memory
ERROR and the session survives.
Backtrace from the core of the crashing backend (19rc2, same run as the log
above):
#0 Py_INCREF (op=0x0) at ./Include/refcount.h:282
#1 _Py_NewRef (obj=0x0) at ./Include/refcount.h:543
#2 PyDict_SetItem (op=..., key=..., value=0x0) at Objects/dictobject.c:2727
#3 PyDict_SetItemString (v=..., key="b", item=0x0) at
Objects/dictobject.c:5094
#4 PLyDict_FromTuple (...) at src/pl/plpython/plpy_typeio.c
#5 PLy_input_from_tuple (...) at src/pl/plpython/plpy_typeio.c
#6 PLy_spi_execute_fetch_result (tuptable=..., rows=1, status=5) at
src/pl/plpython/plpy_spi.c:412
#7 PLy_spi_execute_query (query="SELECT b FROM big", limit=0) at
src/pl/plpython/plpy_spi.c:324
PG Bug reporting form <noreply@postgresql.org> writes:
When the conversion of a column value to a Python object fails inside
Python, the backend
terminates with signal 11 and the postmaster restarts all sessions. The
expected result is an ERROR
in the calling session.
The immediate cause in this example seems to be
(1) PyBytes_FromStringAndSize returns NULL after a malloc failure
(2) We blindly pass that NULL to PyDict_SetItemString
(3) PyDict_SetItemString doesn't cope with that and dumps core.
It looks to me like plpy_typeio.c is just full of bugs like this,
because it doesn't have any coherent strategy for detecting or
dealing with failures. There are a few places that check for
NULL results and throw PLy_elog(ERROR), but not many. I think
we'd first want to decide whether to check immediately after
each call to a Python function, or try to centralize it a bit.
I wonder now about the rest of plpython, too.
I don't care enough about PL/Python to work on this myself,
but perhaps someone else will pick it up.
regards, tom lane
пн, 16 нояб. 2026 г. в 22:53, Tom Lane <tgl@sss.pgh.pa.us>:
I think
we'd first want to decide whether to check immediately after
each call to a Python function, or try to centralize it a bit.
Hi Tom!
I centralized the problematic spots in plpy_typeio - a small step toward
order.
I'll look at the other places in plpython a bit later.
Thoughts?
--
Regards,
Rachitskiy Andrey
On Mon, Nov 16, 2026 at 12:53 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
The immediate cause in this example seems to be
(1) PyBytes_FromStringAndSize returns NULL after a malloc failure
(2) We blindly pass that NULL to PyDict_SetItemString
(3) PyDict_SetItemString doesn't cope with that and dumps core.It looks to me like plpy_typeio.c is just full of bugs like this,
because it doesn't have any coherent strategy for detecting or
dealing with failures. There are a few places that check for
NULL results and throw PLy_elog(ERROR), but not many. I think
we'd first want to decide whether to check immediately after
each call to a Python function, or try to centralize it a bit.
Here's what my LLM says:
The problem here is that PL/Python's input conversion functions follow
the Python convention of returning NULL, with an exception set, when
Python can't build the object, and none of the backend code that
calls them checks. Here the copy of the bytea fails with MemoryError,
and PLyDict_FromTuple stores the NULL straight into the dictionary.
d0aa965c0a (Peter Eisentraut, 2017-10-31) stated the rule: code called
from Python returns NULL, and code called from PostgreSQL raises an
error. The input conversions are only ever called from backend code,
which was supposed to turn the NULL into an error and never does.
Marcos is right that row-type arguments and TD["new"] go through the
same code, and they crash too, as does an array element. A scalar
argument is worse: argument setup treats a NULL result as an SQL NULL,
so the function quietly runs with None and returns a wrong answer.
The attached patch makes the two entry points, PLy_input_convert and
PLy_input_from_tuple, throw an error on a NULL result, as the
numeric-to-Decimal conversion already does, and makes argument setup
use None only for an SQL NULL. Every case then fails with an ordinary
ERROR and the session survives. A plpython3u function that lowers
RLIMIT_AS on its own backend reproduces this without a restart; that's
the attached setup.sql.
--
Robert Haas
Databricks
Hi Robert,
Thanks for the patch.
Every case then fails with an ordinary ERROR and the session survives
I tested your patch and triggers and function calls can still crash.
PLy_trigger_build_args() puts unchecked results into the TD
dictionary. PLy_procedure_call() crashes when building the argument
list fails.
V1-0001 is your patch without change
V2-0002 adds checks for those spots and it fixed ^^^
Thanks,
Shihao
Attachments:
t259248_5v2-0001-Fix-PL-Python-crashes-when-a-value-can-t-be-conve.patchapplication/octet-stream; name=v2-0001-Fix-PL-Python-crashes-when-a-value-can-t-be-conve.patchDownload+40-10
v2-0002-plpython-Check-for-failures-while-setting-up-func.patchapplication/octet-stream; name=v2-0002-plpython-Check-for-failures-while-setting-up-func.patchDownload+52-37
вт, 17 нояб. 2026 г. в 05:55, shihao zhong <zhong950419@gmail.com>:
Hi Robert,
Thanks for the patch.
Every case then fails with an ordinary ERROR and the session survives
I tested your patch and triggers and function calls can still crash.
PLy_trigger_build_args() puts unchecked results into the TD
dictionary. PLy_procedure_call() crashes when building the argument
list fails.V1-0001 is your patch without change
V2-0002 adds checks for those spots and it fixed ^^^Thanks,
Shihao
I also found quite a few more places where fixes can be made and I'm
attaching v3. Reworked the error handling somewhat in 0001, introduced
shared functions, and fixed plpy_typeio. In 0002 I fixed other problematic
spots in pl_python. In my view, the code has become much better.
Attachments:
t259248_6v3-0002-Check-the-remaining-Python-API-failures-in-PL-Pyt.patchtext/x-patch; charset=US-ASCII; name=v3-0002-Check-the-remaining-Python-API-failures-in-PL-Pyt.patchDownload+89-33
v3-0001-Fix-unchecked-NULL-returns-in-PL-Python-object-co.patchtext/x-patch; charset=US-ASCII; name=v3-0001-Fix-unchecked-NULL-returns-in-PL-Python-object-co.patchDownload+343-111
Hi Andrey,
Thanks for your patches.
In 0002 I fixed other problematic spots in pl_python.
Thanks for the v3. I ran the same allocation failure tests and it
doesn't crash. It also fixes a crash in PLy_procedure_compile() that my
v2 missed.
Thanks,
Shihao