BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails

From: PG Bug reporting form <noreply(at)postgresql(dot)org>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Cc: marcos(dot)castedo(at)anachronics(dot)com
Subject: BUG #19899: PL/Python backend crashes (SIGSEGV) when converting a column value to a Python object fails
Date: 2026-11-12 23:50:19
Message-ID: 19899-3e21845d988f0c0f@postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

The following bug has been logged on the website:

Bug reference: 19899
Logged by: Marcos Castedo
Email address: marcos(dot)castedo(at)anachronics(dot)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

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Daniel Gustafsson 2026-11-13 00:00:07 Re: BUG #19702: decode() accepts Base64 payload after terminal padding
Previous Message PG Bug reporting form 2026-11-12 23:42:08 BUG #19898: int4_avg_accum_inv wraps below INT64_MIN instead of raising an error