Skip to content

Commit 6784d7a

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 f0392e6 commit 6784d7a

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
@@ -1716,10 +1716,24 @@ find_expr_references_walker(Node *node,
17161716
else if (IsA(node, FieldSelect))
17171717
{
17181718
FieldSelect *fselect = (FieldSelect *) node;
1719+
Oid argtype = getBaseType(exprType((Node *) fselect->arg));
1720+
Oid reltype = get_typ_typrelid(argtype);
17191721

1720-
/* result type might not appear anywhere else in expression */
1721-
add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
1722-
context->addrs);
1722+
/*
1723+
* We need a dependency on the specific column named in FieldSelect,
1724+
* assuming we can identify the pg_class OID for it. (Probably we
1725+
* always can at the moment, but in future it might be possible for
1726+
* argtype to be RECORDOID.) If we can make a column dependency then
1727+
* we shouldn't need a dependency on the column's type; but if we
1728+
* can't, make a dependency on the type, as it might not appear
1729+
* anywhere else in the expression.
1730+
*/
1731+
if (OidIsValid(reltype))
1732+
add_object_address(OCLASS_CLASS, reltype, fselect->fieldnum,
1733+
context->addrs);
1734+
else
1735+
add_object_address(OCLASS_TYPE, fselect->resulttype, 0,
1736+
context->addrs);
17231737
/* the collation might not be referenced anywhere else, either */
17241738
if (OidIsValid(fselect->resultcollid) &&
17251739
fselect->resultcollid != DEFAULT_COLLATION_OID)
@@ -1729,10 +1743,20 @@ find_expr_references_walker(Node *node,
17291743
else if (IsA(node, FieldStore))
17301744
{
17311745
FieldStore *fstore = (FieldStore *) node;
1746+
Oid reltype = get_typ_typrelid(fstore->resulttype);
17321747

1733-
/* result type might not appear anywhere else in expression */
1734-
add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
1735-
context->addrs);
1748+
/* similar considerations to FieldSelect, but multiple column(s) */
1749+
if (OidIsValid(reltype))
1750+
{
1751+
ListCell *l;
1752+
1753+
foreach(l, fstore->fieldnums)
1754+
add_object_address(OCLASS_CLASS, reltype, lfirst_int(l),
1755+
context->addrs);
1756+
}
1757+
else
1758+
add_object_address(OCLASS_TYPE, fstore->resulttype, 0,
1759+
context->addrs);
17361760
}
17371761
else if (IsA(node, RelabelType))
17381762
{

src/test/regress/expected/alter_table.out

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2706,6 +2706,23 @@ Typed table of type: test_type2
27062706
Inherits: test_tbl2
27072707

27082708
DROP TABLE test_tbl2_subclass;
2709+
CREATE TYPE test_typex AS (a int, b text);
2710+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
2711+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
2712+
ERROR: cannot drop composite type test_typex column a because other objects depend on it
2713+
DETAIL: constraint test_tblx_y_check on table test_tblx depends on composite type test_typex column a
2714+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
2715+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
2716+
NOTICE: drop cascades to constraint test_tblx_y_check on table test_tblx
2717+
\d test_tblx
2718+
Table "public.test_tblx"
2719+
Column | Type | Collation | Nullable | Default
2720+
--------+------------+-----------+----------+---------
2721+
x | integer | | |
2722+
y | test_typex | | |
2723+
2724+
DROP TABLE test_tblx;
2725+
DROP TYPE test_typex;
27092726
-- This test isn't that interesting on its own, but the purpose is to leave
27102727
-- behind a table to test pg_upgrade with. The table has a composite type
27112728
-- 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
@@ -1714,6 +1714,14 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
17141714

17151715
DROP TABLE test_tbl2_subclass;
17161716

1717+
CREATE TYPE test_typex AS (a int, b text);
1718+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
1719+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
1720+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
1721+
\d test_tblx
1722+
DROP TABLE test_tblx;
1723+
DROP TYPE test_typex;
1724+
17171725
-- This test isn't that interesting on its own, but the purpose is to leave
17181726
-- behind a table to test pg_upgrade with. The table has a composite type
17191727
-- column in it, and the composite type has a dropped attribute.

0 commit comments

Comments
 (0)