From 234ec9a3a40f7356a7c64ff8fd7ad4b2ed5d02e7 Mon Sep 17 00:00:00 2001 From: Aman Ganapathy Date: Thu, 3 Dec 2026 18:51:21 +0000 Subject: [PATCH v1 1/2] Fix ALTER COLUMN TYPE when a dependent object is dropped concurrently. RememberAllDependentForRebuilding() looks up the objects that depend on the column without locking them. Views, SQL functions, OWNED BY sequences and publication WHERE clauses can be dropped without a lock on the table, so if one is dropped after its pg_depend entry was found, the lookup fails with an internal error such as "could not find tuple for rule", or the error detail shows "(null)" instead of the function. Skip such objects: once dropped, they no longer depend on the column. Discussion: https://postgr.es/m/19968-d24000922918f552@postgresql.org --- src/backend/commands/tablecmds.c | 33 ++++++++++++++++++++++++++------ 1 file changed, 27 insertions(+), 6 deletions(-) diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c index 9755943..2dc8ed7 100644 --- a/src/backend/commands/tablecmds.c +++ b/src/backend/commands/tablecmds.c @@ -16225,6 +16225,9 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, { char relKind = get_rel_relkind(foundObject.objectId); + if (relKind == '\0') + break; + if (relKind == RELKIND_INDEX || relKind == RELKIND_PARTITIONED_INDEX) { @@ -16266,12 +16269,18 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, * AT_SetExpression. */ if (subtype == AT_AlterColumnType) + { + char *objdesc = getObjectDescription(&foundObject, true); + + if (objdesc == NULL) + break; + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot alter type of a column used by a function or procedure"), errdetail("%s depends on column \"%s\".", - getObjectDescription(&foundObject, false), - colName))); + objdesc, colName))); + } break; case RewriteRelationId: @@ -16281,12 +16290,18 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, * function bodies. FIXME someday. */ if (subtype == AT_AlterColumnType) + { + char *objdesc = getObjectDescription(&foundObject, true); + + if (objdesc == NULL) + break; + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot alter type of a column used by a view or rule"), errdetail("%s depends on column \"%s\".", - getObjectDescription(&foundObject, false), - colName))); + objdesc, colName))); + } break; case TriggerRelationId: @@ -16379,12 +16394,18 @@ RememberAllDependentForRebuilding(AlteredTableInfo *tab, AlterTableType subtype, * clause. Same issues as above. FIXME someday. */ if (subtype == AT_AlterColumnType) + { + char *objdesc = getObjectDescription(&foundObject, true); + + if (objdesc == NULL) + break; + ereport(ERROR, (errcode(ERRCODE_FEATURE_NOT_SUPPORTED), errmsg("cannot alter type of a column used by a publication WHERE clause"), errdetail("%s depends on column \"%s\".", - getObjectDescription(&foundObject, false), - colName))); + objdesc, colName))); + } break; default: -- 2.47.3