pgsql: Improve error for RETURNING with system columns under rules

From: Michael Paquier <michael(at)paquier(dot)xyz>
To: pgsql-committers(at)lists(dot)postgresql(dot)org
Subject: pgsql: Improve error for RETURNING with system columns under rules
Date: 2026-08-20 22:51:52
Message-ID: E1wxBby-00000001Q20-45G0@gemulon.postgresql.org
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-committers

Improve error for RETURNING with system columns under rules

When a DML query's RETURNING list references the system column of a
target table, and the query was rewritten by a rule with its own
RETURNING clause, ReplaceVarsFromTargetList() failed with an internal
error "could not find replacement targetlist entry for attno -1",
since the rule's RETURNING list only has entries for user columns.

Such queries cannot work: the rewritten query need not scan the original
target relation at all, so a rule's RETURNING list has no way to provide
values for that relation's system columns.

This is a life improvement change as the error reported is just more
relevant for the user and this has never worked since RETURNING is
reported. No backpatch is done, based on the lack of complaints over
the ages.

Reported-by: Zheng Wang <hackerzheng666(at)gmail(dot)com>
Author: Zsolt Parragi <zsolt(dot)parragi(at)percona(dot)com>
Discussion: https://postgr.es/m/19632-9155d9baec763c8c@postgresql.org

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/d39762b8c79a8a825be4af0a22b048a18483afa3

Modified Files
--------------
src/backend/rewrite/rewriteManip.c | 13 +++++++++++++
src/test/regress/expected/returning.out | 7 +++++++
src/test/regress/sql/returning.sql | 5 +++++
3 files changed, 25 insertions(+)

Browse pgsql-committers by date

  From Date Subject
Next Message Michael Paquier 2026-08-20 23:33:33 pgsql: ecpg: Add missing NULL check in ecpg_store_input() following OOM
Previous Message Michael Paquier 2026-08-20 22:05:49 pgsql: test_aio: Fix broken error recovery assertions in 001_aio