Rethink the dependencies recorded for FieldSelect/FieldStore nodes.
authorTom Lane
Fri, 27 Oct 2017 16:18:57 +0000 (12:18 -0400)
committerTom Lane
Fri, 27 Oct 2017 16:18:57 +0000 (12:18 -0400)
On closer investigation, commits f3ea3e3e8 et al were a few bricks
shy of a load.  What we need is not so much to lock down the result
type of a FieldSelect, as to lock down the existence of the column
it's trying to extract.  Otherwise, we can break it by dropping that
column.  The dependency on the result type is then held indirectly
through the column, and doesn't need to be recorded explicitly.

Out of paranoia, I left in the code to record a dependency on the
result type, but it's used only if we can't identify the pg_class OID
for the column.  That shouldn't ever happen right now, AFAICS, but
it seems possible that in future the input node could be marked as
being of type RECORD rather than some specific composite type.

Likewise for FieldStore.

Like the previous patch, back-patch to all supported branches.

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

src/backend/catalog/dependency.c
src/test/regress/expected/alter_table.out
src/test/regress/sql/alter_table.sql

index 5a78efc4b4c734af24f7f95a9dd04a3d368e96cc..cc5d3ca5df0e48f67399e60aa74df2263f012f16 100644 (file)
@@ -1661,10 +1661,24 @@ find_expr_references_walker(Node *node,
    else if (IsA(node, FieldSelect))
    {
        FieldSelect *fselect = (FieldSelect *) node;
+       Oid         argtype = exprType((Node *) fselect->arg);
+       Oid         reltype = get_typ_typrelid(argtype);
 
-       /* result type might not appear anywhere else in expression */
-       add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
-                          context->addrs);
+       /*
+        * We need a dependency on the specific column named in FieldSelect,
+        * assuming we can identify the pg_class OID for it.  (Probably we
+        * always can at the moment, but in future it might be possible for
+        * argtype to be RECORDOID.)  If we can make a column dependency then
+        * we shouldn't need a dependency on the column's type; but if we
+        * can't, make a dependency on the type, as it might not appear
+        * anywhere else in the expression.
+        */
+       if (OidIsValid(reltype))
+           add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
+                              context->addrs);
+       else
+           add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
+                              context->addrs);
        /* the collation might not be referenced anywhere else, either */
        if (OidIsValid(fselect->resultcollid) &&
            fselect->resultcollid != DEFAULT_COLLATION_OID)
@@ -1674,10 +1688,20 @@ find_expr_references_walker(Node *node,
    else if (IsA(node, FieldStore))
    {
        FieldStore *fstore = (FieldStore *) node;
+       Oid         reltype = get_typ_typrelid(fstore->resulttype);
 
-       /* result type might not appear anywhere else in expression */
-       add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
-                          context->addrs);
+       /* similar considerations to FieldSelect, but multiple column(s) */
+       if (OidIsValid(reltype))
+       {
+           ListCell   *l;
+
+           foreach(l, fstore->fieldnums)
+               add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
+                                  context->addrs);
+       }
+       else
+           add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
+                              context->addrs);
    }
    else if (IsA(node, RelabelType))
    {
index bb9a79505bdc5ee9c4ab43a4c779a1ee001c3933..6eab5d6e022a868869e93c976da4b7caded00dee 100644 (file)
@@ -2572,6 +2572,23 @@ Table "public.test_tbl2_subclass"
 Inherits: test_tbl2
 
 DROP TABLE test_tbl2_subclass;
+CREATE TYPE test_typex AS (a int, b text);
+CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
+ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
+ERROR:  cannot drop composite type test_typex column a because other objects depend on it
+DETAIL:  constraint test_tblx_y_check on table test_tblx depends on composite type test_typex column a
+HINT:  Use DROP ... CASCADE to drop the dependent objects too.
+ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
+NOTICE:  drop cascades to constraint test_tblx_y_check on table test_tblx
+\d test_tblx
+    Table "public.test_tblx"
+ Column |    Type    | Modifiers 
+--------+------------+-----------
+ x      | integer    | 
+ y      | test_typex | 
+
+DROP TABLE test_tblx;
+DROP TYPE test_typex;
 -- This test isn't that interesting on its own, but the purpose is to leave
 -- behind a table to test pg_upgrade with. The table has a composite type
 -- column in it, and the composite type has a dropped attribute.
index d947a3cea70e92b0ac9125c1c159f1716a1a1699..f1004b5a395420c3e2f09d1ca458325fe198704f 100644 (file)
@@ -1621,6 +1621,14 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
 
 DROP TABLE test_tbl2_subclass;
 
+CREATE TYPE test_typex AS (a int, b text);
+CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
+ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
+ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
+\d test_tblx
+DROP TABLE test_tblx;
+DROP TYPE test_typex;
+
 -- This test isn't that interesting on its own, but the purpose is to leave
 -- behind a table to test pg_upgrade with. The table has a composite type
 -- column in it, and the composite type has a dropped attribute.