Skip to content

Commit 376ac92

Browse files
committed
Rethink the dependencies recorded for FieldSelect/FieldStore nodes.
On closer investigation, commits f3ea3e3 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
1 parent 5542922 commit 376ac92

File tree

3 files changed

+55
-6
lines changed

3 files changed

+55
-6
lines changed

src/backend/catalog/dependency.c

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1661,10 +1661,24 @@ find_expr_references_walker(Node *node,
16611661
else if (IsA(node, FieldSelect))
16621662
{
16631663
FieldSelect *fselect = (FieldSelect *) node;
1664+
Oid argtype = exprType((Node *) fselect->arg);
1665+
Oid reltype = get_typ_typrelid(argtype);
16641666

1665-
/* result type might not appear anywhere else in expression */
1666-
add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
1667-
context->addrs);
1667+
/*
1668+
* We need a dependency on the specific column named in FieldSelect,
1669+
* assuming we can identify the pg_class OID for it. (Probably we
1670+
* always can at the moment, but in future it might be possible for
1671+
* argtype to be RECORDOID.) If we can make a column dependency then
1672+
* we shouldn't need a dependency on the column's type; but if we
1673+
* can't, make a dependency on the type, as it might not appear
1674+
* anywhere else in the expression.
1675+
*/
1676+
if (OidIsValid(reltype))
1677+
add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
1678+
context->addrs);
1679+
else
1680+
add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
1681+
context->addrs);
16681682
/* the collation might not be referenced anywhere else, either */
16691683
if (OidIsValid(fselect->resultcollid) &&
16701684
fselect->resultcollid != DEFAULT_COLLATION_OID)
@@ -1674,10 +1688,20 @@ find_expr_references_walker(Node *node,
16741688
else if (IsA(node, FieldStore))
16751689
{
16761690
FieldStore *fstore = (FieldStore *) node;
1691+
Oid reltype = get_typ_typrelid(fstore->resulttype);
16771692

1678-
/* result type might not appear anywhere else in expression */
1679-
add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
1680-
context->addrs);
1693+
/* similar considerations to FieldSelect, but multiple column(s) */
1694+
if (OidIsValid(reltype))
1695+
{
1696+
ListCell *l;
1697+
1698+
foreach(l, fstore->fieldnums)
1699+
add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
1700+
context->addrs);
1701+
}
1702+
else
1703+
add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
1704+
context->addrs);
16811705
}
16821706
else if (IsA(node, RelabelType))
16831707
{

src/test/regress/expected/alter_table.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2572,6 +2572,23 @@ Table "public.test_tbl2_subclass"
25722572
Inherits: test_tbl2
25732573

25742574
DROP TABLE test_tbl2_subclass;
2575+
CREATE TYPE test_typex AS (a int, b text);
2576+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
2577+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
2578+
ERROR: cannot drop composite type test_typex column a because other objects depend on it
2579+
DETAIL: constraint test_tblx_y_check on table test_tblx depends on composite type test_typex column a
2580+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
2581+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
2582+
NOTICE: drop cascades to constraint test_tblx_y_check on table test_tblx
2583+
\d test_tblx
2584+
Table "public.test_tblx"
2585+
Column | Type | Modifiers
2586+
--------+------------+-----------
2587+
x | integer |
2588+
y | test_typex |
2589+
2590+
DROP TABLE test_tblx;
2591+
DROP TYPE test_typex;
25752592
-- This test isn't that interesting on its own, but the purpose is to leave
25762593
-- behind a table to test pg_upgrade with. The table has a composite type
25772594
-- column in it, and the composite type has a dropped attribute.

src/test/regress/sql/alter_table.sql

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1621,6 +1621,14 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
16211621

16221622
DROP TABLE test_tbl2_subclass;
16231623

1624+
CREATE TYPE test_typex AS (a int, b text);
1625+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
1626+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
1627+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
1628+
\d test_tblx
1629+
DROP TABLE test_tblx;
1630+
DROP TYPE test_typex;
1631+
16241632
-- This test isn't that interesting on its own, but the purpose is to leave
16251633
-- behind a table to test pg_upgrade with. The table has a composite type
16261634
-- column in it, and the composite type has a dropped attribute.

0 commit comments

Comments
 (0)