Fix bogus dependency handling for GENERATED expressions.
authorTom Lane <tgl@sss.pgh.pa.us>
Mon, 21 Mar 2022 18:58:49 +0000 (14:58 -0400)
committerTom Lane <tgl@sss.pgh.pa.us>
Mon, 21 Mar 2022 18:58:49 +0000 (14:58 -0400)
commitcb02fcb4c95bae08adaca1202c2081cfc81a28b5
treeddefe5d7587999509e2bd6f4223a961a01b499ce
parent17f3bc09284e1b529cdf524bbba709af6493f30c
Fix bogus dependency handling for GENERATED expressions.

For GENERATED columns, we record all dependencies of the generation
expression as AUTO dependencies of the column itself.  This means
that the generated column is silently dropped if any dependency
is removed, even if CASCADE wasn't specified.  This is at least
a POLA violation, but I think it's actually based on a misreading
of the standard.  The standard does say that you can't drop a
dependent GENERATED column in RESTRICT mode; but that's buried down
in a subparagraph, on a different page from some pseudocode that
makes it look like an AUTO drop is being suggested.

Change this to be more like the way that we handle regular default
expressions, ie record the dependencies as NORMAL dependencies of
the pg_attrdef entry.  Also, make the pg_attrdef entry's dependency
on the column itself be INTERNAL not AUTO.  That has two effects:

* the column will go away, not just lose its default, if any
dependency of the expression is dropped with CASCADE.  So we
don't need any special mechanism to make that happen.

* it provides an additional cross-check preventing someone from
dropping the default expression without dropping the column.

catversion bump because of change in the contents of pg_depend
(which also requires a change in one information_schema view).

Per bug #17439 from Kevin Humphreys.  Although this is a longstanding
bug, it seems impractical to back-patch because of the need for
catalog contents changes.

Discussion: https://postgr.es/m/17439-7df4421197e928f0@postgresql.org
src/backend/catalog/information_schema.sql
src/backend/catalog/pg_attrdef.c
src/backend/commands/tablecmds.c
src/bin/pg_dump/pg_dump_sort.c
src/include/catalog/catversion.h
src/test/regress/expected/generated.out
src/test/regress/sql/generated.sql