Fix handling of container types in find_composite_type_dependencies.
authorTom Lane
Wed, 9 Aug 2017 21:03:10 +0000 (17:03 -0400)
committerTom Lane
Wed, 9 Aug 2017 21:03:10 +0000 (17:03 -0400)
find_composite_type_dependencies correctly found columns that are of
the specified type, and columns that are of arrays of that type, but
not columns that are domains or ranges over the given type, its array
type, etc.  The most general way to handle this seems to be to assume
that any type that is directly dependent on the specified type can be
treated as a container type, and processed recursively (allowing us
to handle nested cases such as ranges over domains over arrays ...).
Since a type's array type already has such a dependency, we can drop
the existing special case for the array type.

The very similar logic in get_rels_with_domain was likewise a few
bricks shy of a load, as it supposed that a directly dependent type
could *only* be a sub-domain.  This is already wrong for ranges over
domains, and it'll someday be wrong for arrays over domains.

Add test cases illustrating the problems, and back-patch to all
supported branches.

Discussion: https://postgr.es/m/15268.1502309024@sss.pgh.pa.us

src/backend/commands/tablecmds.c
src/backend/commands/typecmds.c
src/test/regress/expected/domain.out
src/test/regress/sql/domain.sql

index 471a44bf0d9d6e3700b93dadd14d3e48a6f1b98f..5541fcbd54eefd135f310c1046be2408c65954de 100644 (file)
@@ -4135,13 +4135,18 @@ ATTypedTableRecursion(List **wqueue, Relation rel, AlterTableCmd *cmd,
 /*
  * find_composite_type_dependencies
  *
- * Check to see if a composite type is being used as a column in some
- * other table (possibly nested several levels deep in composite types!).
+ * Check to see if the type "typeOid" is being used as a column in some table
+ * (possibly nested several levels deep in composite types, arrays, etc!).
  * Eventually, we'd like to propagate the check or rewrite operation
- * into other such tables, but for now, just error out if we find any.
+ * into such tables, but for now, just error out if we find any.
  *
- * Caller should provide either a table name or a type name (not both) to
- * report in the error message, if any.
+ * Caller should provide either the associated relation of a rowtype,
+ * or a type name (not both) for use in the error message, if any.
+ *
+ * Note that "typeOid" is not necessarily a composite type; it could also be
+ * another container type such as an array or range, or a domain over one of
+ * these things.  The name of this function is therefore somewhat historical,
+ * but it's not worth changing.
  *
  * We assume that functions and views depending on the type are not reasons
  * to reject the ALTER.  (How safe is this really?)
@@ -4154,11 +4159,13 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
    ScanKeyData key[2];
    SysScanDesc depScan;
    HeapTuple   depTup;
-   Oid         arrayOid;
+
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
 
    /*
-    * We scan pg_depend to find those things that depend on the rowtype. (We
-    * assume we can ignore refobjsubid for a rowtype.)
+    * We scan pg_depend to find those things that depend on the given type.
+    * (We assume we can ignore refobjsubid for a type.)
     */
    depRel = heap_open(DependRelationId, AccessShareLock);
 
@@ -4180,8 +4187,22 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
        Relation    rel;
        Form_pg_attribute att;
 
-       /* Ignore dependees that aren't user columns of relations */
-       /* (we assume system columns are never of rowtypes) */
+       /* Check for directly dependent types */
+       if (pg_depend->classid == TypeRelationId)
+       {
+           /*
+            * This must be an array, domain, or range containing the given
+            * type, so recursively check for uses of this type.  Note that
+            * any error message will mention the original type not the
+            * container; this is intentional.
+            */
+           find_composite_type_dependencies(pg_depend->objid,
+                                            origRelation, origTypeName);
+           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)
            continue;
@@ -4237,14 +4258,6 @@ find_composite_type_dependencies(Oid typeOid, Relation origRelation,
    systable_endscan(depScan);
 
    relation_close(depRel, AccessShareLock);
-
-   /*
-    * If there's an array type for the rowtype, must check for uses of it,
-    * too.
-    */
-   arrayOid = get_array_type(typeOid);
-   if (OidIsValid(arrayOid))
-       find_composite_type_dependencies(arrayOid, origRelation, origTypeName);
 }
 
 
index 34235bc46aa9d650d10932847b7d4ff41fb753f0..0a8763c103fea18d680cd7af600137f516fe5aa2 100644 (file)
@@ -2716,10 +2716,9 @@ validateDomainConstraint(Oid domainoid, char *ccbin)
  * risk by using the weakest suitable lock (ShareLock for most callers).
  *
  * XXX the API for this is not sufficient to support checking domain values
- * that are inside composite types or arrays.  Currently we just error out
- * if a composite type containing the target domain is stored anywhere.
- * There are not currently arrays of domains; if there were, we could take
- * the same approach, but it'd be nicer to fix it properly.
+ * that are inside container types, such as composite types, arrays, or
+ * ranges.  Currently we just error out if a container type containing the
+ * target domain is stored anywhere.
  *
  * Generally used for retrieving a list of tests when adding
  * new constraints to a domain.
@@ -2728,6 +2727,7 @@ static List *
 get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
 {
    List       *result = NIL;
+   char       *domainTypeName = format_type_be(domainOid);
    Relation    depRel;
    ScanKeyData key[2];
    SysScanDesc depScan;
@@ -2735,6 +2735,9 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
 
    Assert(lockmode != NoLock);
 
+   /* since this function recurses, it could be driven to stack overflow */
+   check_stack_depth();
+
    /*
     * We scan pg_depend to find those things that depend on the domain. (We
     * assume we can ignore refobjsubid for a domain.)
@@ -2761,20 +2764,32 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
        Form_pg_attribute pg_att;
        int         ptr;
 
-       /* Check for directly dependent types --- must be domains */
+       /* Check for directly dependent types */
        if (pg_depend->classid == TypeRelationId)
        {
-           Assert(get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN);
-
-           /*
-            * Recursively add dependent columns to the output list.  This is
-            * a bit inefficient since we may fail to combine RelToCheck
-            * entries when attributes of the same rel have different derived
-            * domain types, but it's probably not worth improving.
-            */
-           result = list_concat(result,
-                                get_rels_with_domain(pg_depend->objid,
-                                                     lockmode));
+           if (get_typtype(pg_depend->objid) == TYPTYPE_DOMAIN)
+           {
+               /*
+                * This is a sub-domain, so recursively add dependent columns
+                * to the output list.  This is a bit inefficient since we may
+                * fail to combine RelToCheck entries when attributes of the
+                * same rel have different derived domain types, but it's
+                * probably not worth improving.
+                */
+               result = list_concat(result,
+                                    get_rels_with_domain(pg_depend->objid,
+                                                         lockmode));
+           }
+           else
+           {
+               /*
+                * Otherwise, it is some container type using the domain, so
+                * fail if there are any columns of this type.
+                */
+               find_composite_type_dependencies(pg_depend->objid,
+                                                NULL,
+                                                domainTypeName);
+           }
            continue;
        }
 
@@ -2811,7 +2826,7 @@ get_rels_with_domain(Oid domainOid, LOCKMODE lockmode)
            if (OidIsValid(rel->rd_rel->reltype))
                find_composite_type_dependencies(rel->rd_rel->reltype,
                                                 NULL,
-                                                format_type_be(domainOid));
+                                                domainTypeName);
 
            /*
             * Otherwise, we can ignore relations except those with both
index d2f9730d66ee627e11e6d08c0005a13808a7fd6a..6e2eaaf75098df97a3dd1c4e1bf5f73c4db1dc30 100644 (file)
@@ -662,11 +662,28 @@ insert into ddtest2 values(row(-1));
 alter domain posint add constraint c1 check(value >= 0);
 ERROR:  cannot alter type "posint" because column "ddtest2.f1" uses it
 drop table ddtest2;
+-- Likewise for domains within arrays of composite
 create table ddtest2(f1 ddtest1[]);
 insert into ddtest2 values('{(-1)}');
 alter domain posint add constraint c1 check(value >= 0);
 ERROR:  cannot alter type "posint" because column "ddtest2.f1" uses it
 drop table ddtest2;
+-- Likewise for domains within domains over array of composite
+create domain ddtest1d as ddtest1[];
+create table ddtest2(f1 ddtest1d);
+insert into ddtest2 values('{(-1)}');
+alter domain posint add constraint c1 check(value >= 0);
+ERROR:  cannot alter type "posint" because column "ddtest2.f1" uses it
+drop table ddtest2;
+drop domain ddtest1d;
+-- Doesn't work for ranges, either
+create type rposint as range (subtype = posint);
+create table ddtest2(f1 rposint);
+insert into ddtest2 values('(-1,3]');
+alter domain posint add constraint c1 check(value >= 0);
+ERROR:  cannot alter type "posint" because column "ddtest2.f1" uses it
+drop table ddtest2;
+drop type rposint;
 alter domain posint add constraint c1 check(value >= 0);
 create domain posint2 as posint check (value % 2 = 0);
 create table ddtest2(f1 posint2);
index c149efc347c862e9ee41d2da31a8b05d89cd6380..b2ea440a933e5e33220026ee873210be19afad03 100644 (file)
@@ -451,11 +451,28 @@ insert into ddtest2 values(row(-1));
 alter domain posint add constraint c1 check(value >= 0);
 drop table ddtest2;
 
+-- Likewise for domains within arrays of composite
 create table ddtest2(f1 ddtest1[]);
 insert into ddtest2 values('{(-1)}');
 alter domain posint add constraint c1 check(value >= 0);
 drop table ddtest2;
 
+-- Likewise for domains within domains over array of composite
+create domain ddtest1d as ddtest1[];
+create table ddtest2(f1 ddtest1d);
+insert into ddtest2 values('{(-1)}');
+alter domain posint add constraint c1 check(value >= 0);
+drop table ddtest2;
+drop domain ddtest1d;
+
+-- Doesn't work for ranges, either
+create type rposint as range (subtype = posint);
+create table ddtest2(f1 rposint);
+insert into ddtest2 values('(-1,3]');
+alter domain posint add constraint c1 check(value >= 0);
+drop table ddtest2;
+drop type rposint;
+
 alter domain posint add constraint c1 check(value >= 0);
 
 create domain posint2 as posint check (value % 2 = 0);