aboutsummaryrefslogtreecommitdiff
path: root/src/backend/commands/tablecmds.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/backend/commands/tablecmds.c')
-rw-r--r--src/backend/commands/tablecmds.c57
1 files changed, 49 insertions, 8 deletions
diff --git a/src/backend/commands/tablecmds.c b/src/backend/commands/tablecmds.c
index 5d0dcfa3e6e..5c596499d1b 100644
--- a/src/backend/commands/tablecmds.c
+++ b/src/backend/commands/tablecmds.c
@@ -5962,6 +5962,7 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
{
Form_pg_depend pg_depend = (Form_pg_depend) GETSTRUCT(depTup);
Relation rel;
+ TupleDesc tupleDesc;
Form_pg_attribute att;
/* Check for directly dependent types */
@@ -5978,18 +5979,58 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
continue;
}
- /* Else, ignore dependees that aren't user columns of relations */
- /* (we assume system columns are never of interesting types) */
- if (pg_depend->classid != RelationRelationId ||
- pg_depend->objsubid <= 0)
+ /* Else, ignore dependees that aren't relations */
+ if (pg_depend->classid != RelationRelationId)
continue;
rel = relation_open(pg_depend->objid, AccessShareLock);
- att = TupleDescAttr(rel->rd_att, pg_depend->objsubid - 1);
+ tupleDesc = RelationGetDescr(rel);
- if (rel->rd_rel->relkind == RELKIND_RELATION ||
- rel->rd_rel->relkind == RELKIND_MATVIEW ||
- rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE)
+ /*
+ * If objsubid identifies a specific column, refer to that in error
+ * messages. Otherwise, search to see if there's a user column of the
+ * type. (We assume system columns are never of interesting types.)
+ * The search is needed because an index containing an expression
+ * column of the target type will just be recorded as a whole-relation
+ * dependency. If we do not find a column of the type, the dependency
+ * must indicate that the type is transiently referenced in an index
+ * expression but not stored on disk, which we assume is OK, just as
+ * we do for references in views. (It could also be that the target
+ * type is embedded in some container type that is stored in an index
+ * column, but the previous recursion should catch such cases.)
+ */
+ if (pg_depend->objsubid > 0 && pg_depend->objsubid <= tupleDesc->natts)
+ att = TupleDescAttr(tupleDesc, pg_depend->objsubid - 1);
+ else
+ {
+ att = NULL;
+ for (int attno = 1; attno <= tupleDesc->natts; attno++)
+ {
+ att = TupleDescAttr(tupleDesc, attno - 1);
+ if (att->atttypid == typeOid && !att->attisdropped)
+ break;
+ att = NULL;
+ }
+ if (att == NULL)
+ {
+ /* No such column, so assume OK */
+ relation_close(rel, AccessShareLock);
+ continue;
+ }
+ }
+
+ /*
+ * We definitely should reject if the relation has storage. If it's
+ * partitioned, then perhaps we don't have to reject: if there are
+ * partitions then we'll fail when we find one, else there is no
+ * stored data to worry about. However, it's possible that the type
+ * change would affect conclusions about whether the type is sortable
+ * or hashable and thus (if it's a partitioning column) break the
+ * partitioning rule. For now, reject for partitioned rels too.
+ */
+ if (RELKIND_HAS_STORAGE(rel->rd_rel->relkind) ||
+ rel->rd_rel->relkind == RELKIND_PARTITIONED_TABLE ||
+ rel->rd_rel->relkind == RELKIND_PARTITIONED_INDEX)
{
if (origTypeName)
ereport(ERROR,