JIT inlining: "tupdesc reference ... is not owned by resource owner Portal" (inlined code uses a cloned tupdesc_resowner_desc)

From: Mads Westberg <mads(at)bolighub(dot)dk>
To: pgsql-bugs(at)lists(dot)postgresql(dot)org
Subject: JIT inlining: "tupdesc reference ... is not owned by resource owner Portal" (inlined code uses a cloned tupdesc_resowner_desc)
Date: 2026-12-01 13:16:32
Message-ID: CAKrZDnX+fU5Wo4Umd7zyoN+zUahFXxV568C7FKTWggthLZBK1g@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Hi,

With JIT inlining and optimization enabled, a query that builds a
named-composite ROW()
containing an out-of-line (external TOAST) value can fail with

ERROR: tupdesc reference 0x... is not owned by resource owner Portal

Reproduced with the PGDG Ubuntu 24.04 packages on x86_64, all using LLVM
19.1.1:
17.12-1.pgdg24.04+1, 18.6-1.pgdg24.04+2 and 18.7-1.pgdg24.04+2. We first
hit it in production
on 18.7.

Reproduction: run the setup, then the query in a NEW session. In the
session that ran the
setup, the query passes for us.

-- setup
CREATE TABLE p (id bigint, a text, b jsonb, c int) PARTITION BY RANGE (id);
CREATE TABLE p1 PARTITION OF p FOR VALUES FROM (MINVALUE) TO (MAXVALUE);
INSERT INTO p SELECT g, 'row ' || g, jsonb_build_object('n', g), g % 7
FROM generate_series(1, 100000) g;
-- one incompressible value > 2 kB, stored out of line
INSERT INTO p SELECT 0, 'big', jsonb_build_object('blob',
(SELECT string_agg(md5(x::text), '') FROM generate_series(1, 200) x)), 0;
ANALYZE p;

-- new session
SET max_parallel_workers_per_gather = 0;
SET jit = on;
SET jit_above_cost = 0;
SET jit_inline_above_cost = 0;
SET jit_optimize_above_cost = 0;
SELECT sum(hashtextextended(ROW(
https://www.google.com/url?q=http://p.id&source=gmail&ust=1796217392816000&sa=E,
p.a, p.b, p.c)::p::text, 0)),
sum(hashtextextended(p::text, 0))
FROM p;

On 17.12 and 18.7 this fails every time (6/6 and 3/3 runs). On 18.6 the
same script passes,
but it fails with small changes that only alter the shape of the JIT module
(for example
enable_seqscan = off). It passes with jit_inline_above_cost = -1, with
jit_optimize_above_cost = -1, with jit = off, or when the big value is
replaced by a small one.

Analysis (line numbers from REL_18_STABLE):

- Since the PG 17 resource-owner rework, ResourceOwnerForget() matches an
entry by value *and*
by kind pointer. For tupdescs, the kind is &tupdesc_resowner_desc, a
file-local
"static const ResourceOwnerDesc" (tupdesc.c:38).
- llvmjit_inline.cpp's function_inlinable() (around lines 610-651) clones
file-local constant
globals into the module being compiled, so that functions referencing them
can be inlined.
Inlined code therefore passes the address of a *copy* of
tupdesc_resowner_desc.
- ExecEvalRow() -> HeapTupleGetDatum() -> HeapTupleHeaderGetDatum()
(execTuples.c:2478). For a
tuple with external fields this pins the rowtype with
lookup_rowtype_tupdesc(), calls
toast_flatten_tuple_to_datum(), and then calls ReleaseTupleDesc().
lookup_rowtype_tupdesc()
is never inlined, because its callee uses non-constant statics. So the pin
is remembered
under the real kind.
- If DecrTupleDescRefCount() was imported into the module (in our case
apparently via
ExecEvalConvertRowtype for the whole-row reference p.*), LLVM can inline
the whole chain into
the expression. The release then calls ResourceOwnerForget() with the
cloned kind, finds no
entry and raises the error.

Evidence:
- backtrace_functions = 'ResourceOwnerForget' on 18.7 shows the error path
of
ResourceOwnerForget called directly from a JIT frame (no symbol), called
from ExecAgg
(advance_aggregates).
- With jit_dump_bitcode, every failing module's optimized bitcode contains
the global
tupdesc_resowner_desc and no remaining call to DecrTupleDescRefCount. Every
passing module
still calls DecrTupleDescRefCount externally and has no such global. That
held for all
27 runs we compared on a larger schema.

Which functions LLVM actually inlines depends on the module's contents, so
in a real workload
the error looks intermittent and comes and goes with plan changes.

The same hazard seems to apply to any ResourceOwnerDesc defined as a
file-local
static const (catcache, relcache, plancache, snapmgr, fd, dsm, ...),
wherever remember and
forget can end up on different sides of the JIT boundary. Possible fixes:
give these
descriptors external linkage (as bufmgr.c already does for
buffer_pin_resowner_desc), or
have the inliner refuse to clone constant globals whose address escapes, or
refuse to inline
functions that take such an address.

Our workaround is ALTER SYSTEM SET jit_inline_above_cost = -1.

Thanks,
Mads Westberg

Responses

Browse pgsql-bugs by date

  From Date Subject
Next Message Kirill Reshke 2026-12-01 13:33:45 Re: BUG #19963: INNER JOIN ON qual evaluated as scan filter even when the inner input is empty
Previous Message Pavan 2026-12-01 12:10:26 Re: BUG #19952: psql \d+ omits nondefault replica identity for partitioned tables