From: Noah Misch <noah@leadboat.com>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Enforce view row visibility for INSERT ... ON CONFLICT target ro
Date: Mon, 09 Nov 2026 14:21:51 +0000
Message-ID: <E1xQQFr-000000003Mi-0hL4@gemulon.postgresql.org> (raw)
Enforce view row visibility for INSERT ... ON CONFLICT target rows.
The WHERE condition of an automatically updatable view restricts the
rows that an UPDATE, DELETE, or MERGE through the view can affect to
those that are visible through it. INSERT ... ON CONFLICT DO UPDATE
through such a view did not follow this rule. The conflicting row is
found through the arbiter index rather than by scanning the view, and
the view's WHERE condition was never applied to it. DO UPDATE therefore
updated the row whether or not the view showed it, and evaluated its SET
expressions, its WHERE clause, and RETURNING against it; ON CONFLICT DO
SELECT read it the same way. This odd set of behaviors was documented,
but it created a security issue, described further down.
Fix by having rewriteTargetView() emit the view's WHERE quals as a
WithCheckOption of new kind WCO_VIEW_CONFLICT_CHECK, which
ExecOnConflictUpdate() and ExecOnConflictSelect() check against the
conflicting row, innermost view first and before the ON CONFLICT WHERE
clause is evaluated. Using a new WCO kind for this is perhaps a little
unorthodox, but doing so provides exactly what is needed here: it raises
an error when a conflicting row doesn't satisfy the view's quals. This
makes sense because ON CONFLICT DO UPDATE guarantees that each row
proposed for insertion is either inserted or updated, neither of which
is feasible here. The arbiter index belongs to the base relation, so
the conflicting row makes insertion the wrong thing to do. The view's
WHERE condition puts the conflicting row beyond the reach of an update
through the view, but it doesn't make sense to act as if there's no row
to update, because when that happens we're supposed to take the
insertion path instead (that's what upsert promises). This situation is
fundamentally irreconcilable, so throwing an error is all we can do.
This fixes a security bug. A user whose privileges on a view allow an
upsert through it, but who holds none on its base relation, can modify
every row that the view hides, one at a time, by guessing their keys, as
if the view had no WHERE condition. The same user can read those rows,
through the conflict action's WHERE clause, its SET expressions, and
RETURNING. This historical behavior is hard to defend in general, and
impossible to defend in cases where the target view happens to be a
security barrier view.
The problem dates to commit 168d5805e, which added ON CONFLICT and
documented its original behavior with updatable views. ON CONFLICT DO
SELECT was added to Postgres 19 by commit 88327092f, and it inherited
that behavior.
An upsert through a view that relied on reaching rows the view hides now
fails. This should be called out as an incompatibility in the release
notes for the next set of minor releases.
Reported-by: Qing Xu <emyiqing@gmail.com>
Reported-by: Carl Andersen <carl@spearbit.com>
Reported-by: Zsolt Parragi <zsolt.parragi@percona.com>
Reported-by: shihao zhong <zhong950419@gmail.com>
Author: Peter Geoghegan <pg@bowt.ie>
Author: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: Robert Haas <robertmhaas@gmail.com>
Security: CVE-2026-16237
Backpatch-through: 14
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/315c4e4e85f3fae5ebc3a62d69b5400138d307f9
Author: Peter Geoghegan <pg@bowt.ie>
Modified Files
--------------
doc/src/sgml/ref/create_view.sgml | 13 +-
src/backend/executor/execMain.c | 6 +
src/backend/executor/nodeModifyTable.c | 20 ++
src/backend/rewrite/rewriteHandler.c | 78 +++++---
src/include/nodes/parsenodes.h | 4 +
src/test/regress/expected/rowsecurity.out | 18 ++
src/test/regress/expected/updatable_views.out | 263 ++++++++++++++++++++++++++
src/test/regress/sql/rowsecurity.sql | 15 ++
src/test/regress/sql/updatable_views.sql | 209 ++++++++++++++++++++
9 files changed, 600 insertions(+), 26 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: noah@leadboat.com
Subject: Re: pgsql: Enforce view row visibility for INSERT ... ON CONFLICT target ro
In-Reply-To: <E1xQQFr-000000003Mi-0hL4@gemulon.postgresql.org>
References: <E1xQQFr-000000003Mi-0hL4@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox