| From: | Kirill Reshke <reshkekirill(at)gmail(dot)com> |
|---|---|
| To: | Mads Westberg <mads(at)bolighub(dot)dk> |
| Cc: | pgsql-bugs(at)lists(dot)postgresql(dot)org |
| Subject: | Re: JIT inlining: "tupdesc reference ... is not owned by resource owner Portal" (inlined code uses a cloned tupdesc_resowner_desc) |
| Date: | 2026-12-01 16:57:09 |
| Message-ID: | CALdSSPg-DWabwzteCXM7bZ5UR7wBcJYKfCFEtCdj31dy8_v3kw@mail.gmail.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
On Tue, 1 Dec 2026 at 19:51, Mads Westberg <mads(at)bolighub(dot)dk> wrote:
>
> 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
Hi!
I can reproduce this
The thing is, JIT compilation can sometimes function inline. While
doing so, it can copy some variables (globals in LLVM terms). In this
case there is file-local constant "static const ResourceOwnerDesc
tupdesc_resowner_desc". If the address of this variable is significant
when JIT imports it, it creates a second object with a different
address
ResourceOwnerRemember() also takes its address for memory management,
which then disagrees with inlined code copied address when we are in
ResourceOwnerForget(). ResourceOwner later fails to find the
remembered reference.
Attached disables inlining of functions whose addresses are
significant (hasGlobalUnnamedAddr) , which would make JIT less
efficient, but I didn't check if impact is measurable.
The check rejects only a specific function from inlining if it depends
on a file-local constant that cannot be safely cloned, not all JIT
inlining,
--
Best regards,
Kirill Reshke
| Attachment | Content-Type | Size |
|---|---|---|
| fix.patch | application/octet-stream | 4.2 KB |
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Nathan Bossart | 2026-12-01 17:38:12 | Re: BUG #19688: pg_dump --schema scans all sequences in PostgreSQL 18, causing severe performance regression |
| Previous Message | Matthias van de Meent | 2026-12-01 16:48:04 | Re: BUG #19519: REPACK can fail due to missing chunk for toast value |