From: PG Bug reporting form <noreply@postgresql.org>
To: pgsql-bugs@lists.postgresql.org
Cc: marcos.castedo@anachronics.com
Subject: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails
Date: Thu, 12 Nov 2026 23:50:19 +0000
Message-ID: <19899-3e21845d988f0c0f@postgresql.org> (raw)
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
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: noreply@postgresql.org, marcos.castedo@anachronics.com
Subject: Re: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails
In-Reply-To: <19899-3e21845d988f0c0f@postgresql.org>
References: <19899-3e21845d988f0c0f@postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox