Fix composite type dependency check for index expressions

check_composite_type_dependency() previously assumed that an index
expression's return type always becomes the index column's storage
type, and therefore didn't check the return type of an index
expression separately.  However, that's not true for an index access
method or operator class that uses a separate storage key type, such
as in hash indexes (amkeytype = int4), GIN array_ops indexes
(opckeytype = anyelement), or GiST multirange_ops and BRIN
range_inclusion_ops indexes (opckeytype = anyrange).  When altering a
composite type (or adding a constraint to a domain via
get_rels_with_domain()) returned by an index expression on such an
index, you would reach the "We've covered all reasons for this
dependency to exist, so why are we here?" codepath.  On
assertion-enabled builds, that trips an assertion, and on
non-assertion builds, the ALTER is rejected.

ANALYZE on such indexes stores datums of the expression's result type
in pg_statistic, and the index contents might not be sensible with the
new type anyway, so storing the dependency and rejecting the ALTER is
correct, but we should avoid the assertion.

To fix, make CheckTypeDependencyInSingleRelExpr() also check the
expression's result type.  Commit d98d99ba17b made
recordDependencyOnSingleRelExpr() record a dependency on it, so it
makes sense for CheckTypeDependencyInSingleRelExpr() to check it.  The
caller that checked PARTITION BY expressions checked for the return
type explicitly, so now that CheckTypeDependencyInSingleRelExpr() does
it, that caller can be simplified.

In passing, fix the OID passed to CheckTypeDependencyInSingleRelExpr()
in the index expression checks.  They should pass the indexed table's
OID (indrelid) rather than the index's own OID, matching the
recordDependencyOnSingleRelExpr() calls in index_create().  That's
harmless, as the OID passed doesn't actually affect
CheckTypeDependencyInSingleRelExpr() currently, but let's be tidy.

Author: Zhijie Hou <[email protected]>
Reviewed-by: Chao Li <[email protected]>
Discussion: https://postgr.es/m/[email protected]
Backpatch-through: 15

Branch
------
master

Details
-------
https://git.postgresql.org/pg/commitdiff/3b60740cc25ed5595980c469ce0403c6a1533d62

Modified Files
--------------
src/backend/catalog/dependency.c                | 21 +++++++
src/backend/commands/tablecmds.c                | 84 ++++++++-----------------
src/test/regress/expected/alter_column_type.out |  8 +++
src/test/regress/sql/alter_column_type.sql      |  6 ++
4 files changed, 62 insertions(+), 57 deletions(-)

Reply via email to