pgsql: Calculate extraUpdatedCols in query rewriter, not parser. - Mailing list pgsql-committers

From Tom Lane
Subject pgsql: Calculate extraUpdatedCols in query rewriter, not parser.
Date
Msg-id E1kXpXY-0002qU-MZ@gemulon.postgresql.org
Whole thread Raw
List pgsql-committers
Calculate extraUpdatedCols in query rewriter, not parser.

It's unsafe to do this at parse time because addition of generated
columns to a table would not invalidate stored rules containing
UPDATEs on the table ... but there might now be dependent generated
columns that were not there when the rule was made.  This also fixes
an oversight that rewriteTargetView failed to update extraUpdatedCols
when transforming an UPDATE on an updatable view.  (Since the new
calculation is downstream of that, rewriteTargetView doesn't actually
need to do anything; but before, there was a demonstrable bug there.)

In v13 and HEAD, this leads to easily-visible bugs because (since
commit c6679e4fc) we won't recalculate generated columns that aren't
listed in extraUpdatedCols.  In v12 this bitmap is mostly just used
for trigger-firing decisions, so you'd only notice a problem if a
trigger cared whether a generated column had been updated.

I'd complained about this back in May, but then forgot about it
until bug #16671 from Michael Paul Killian revived the issue.

Back-patch to v12 where this field was introduced.  If existing
stored rules contain any extraUpdatedCols values, they'll be
ignored because the rewriter will overwrite them, so the bug will
be fixed even for existing rules.  (But note that if someone were
to update to 13.1 or 12.5, store some rules with UPDATEs on tables
having generated columns, and then downgrade to a prior minor version,
they might observe issues similar to what this patch fixes.  That
seems unlikely enough to not be worth going to a lot of effort to fix.)

Discussion: https://postgr.es/m/10206.1588964727@sss.pgh.pa.us
Discussion: https://postgr.es/m/16671-2fa55851859fb166@postgresql.org

Branch
------
master

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

Modified Files
--------------
src/backend/optimizer/plan/setrefs.c          |  6 ++--
src/backend/parser/analyze.c                  | 33 ---------------------
src/backend/replication/logical/worker.c      |  5 ++--
src/backend/rewrite/rewriteHandler.c          | 41 +++++++++++++++++++++++++++
src/include/nodes/parsenodes.h                | 16 +++++++----
src/include/parser/analyze.h                  |  2 --
src/include/rewrite/rewriteHandler.h          |  3 ++
src/test/regress/expected/updatable_views.out | 35 +++++++++++++++++++++++
src/test/regress/sql/updatable_views.sql      | 21 ++++++++++++++
9 files changed, 115 insertions(+), 47 deletions(-)


pgsql-committers by date:

Previous
From: Tom Lane
Date:
Subject: pgsql: Don't use custom OID symbols in pg_proc.dat.
Next
From: Tom Lane
Date:
Subject: pgsql: Use mode "r" for popen() in psql's evaluate_backtick().