Re: [DEPARSE] EXPLAIN prints another ModifyTable's RETURNING aliases in a SubPlan

From: Ayush Tiwari <ayushtiwari(dot)slg01(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: assam258(at)gmail(dot)com, pgsql-hackers <pgsql-hackers(at)postgresql(dot)org>, Dean Rasheed <dean(dot)a(dot)rasheed(at)gmail(dot)com>
Subject: Re: [DEPARSE] EXPLAIN prints another ModifyTable's RETURNING aliases in a SubPlan
Date: 2026-10-21 14:21:01
Message-ID: CAJTYsWWsORQbyViXmsvT4-NWkjuykkjjk2c+gQtq+vw2PJON-A@mail.gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

While looking at 1999fe227df, I noticed that the new Assert(refname)
in get_variable() can be hit through a rule (v v edge case, fuzz testing):

CREATE TABLE t (a int);
CREATE VIEW v AS SELECT * FROM t;
CREATE RULE v_ins AS ON INSERT TO v DO INSTEAD
INSERT INTO t VALUES (new.a) RETURNING WITH (NEW AS n) n.a;
EXPLAIN (VERBOSE, COSTS OFF)
INSERT INTO v AS new VALUES (1) RETURNING *;

TRAP: failed Assert("refname"), File: "ruleutils.c", Line: 7747

IIUC, the INSERT already uses "new" as its alias, so it has no NEW
alias for RETURNING, and rewriteRuleAction() then copies that empty
alias over the rule's "n". The same seems to happen for OLD, with an
UPDATE rule and UPDATE v AS old.

The attached patch just keeps the rule's alias in that case, and
EXPLAIN then shows "Output: n.a". Thoughts?

Regards,
Ayush

Attachment Content-Type Size
v1-0001-Preserve-RETURNING-aliases-when-rewriting-rules.patch application/octet-stream 1.8 KB

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Tom Lane 2026-10-21 14:36:47 Re: [PG19] psql backslash commands fail with translated messages and a non UTF-8 client encoding
Previous Message Markos Fountoulakis 2026-10-21 14:15:56 [PATCH] Fix argument in pg_indexam_progress_phasename()