Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 8be1022

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 69fc2ca commit 8be1022

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 = 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
@@ -2682,6 +2682,23 @@ Typed table of type: test_type2
26822682
Inherits: test_tbl2
26832683

26842684
DROP TABLE test_tbl2_subclass;
2685+
CREATE TYPE test_typex AS (a int, b text);
2686+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
2687+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
2688+
ERROR: cannot drop composite type test_typex column a because other objects depend on it
2689+
DETAIL: constraint test_tblx_y_check on table test_tblx depends on composite type test_typex column a
2690+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
2691+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
2692+
NOTICE: drop cascades to constraint test_tblx_y_check on table test_tblx
2693+
\d test_tblx
2694+
Table "public.test_tblx"
2695+
Column | Type | Collation | Nullable | Default
2696+
--------+------------+-----------+----------+---------
2697+
x | integer | | |
2698+
y | test_typex | | |
2699+
2700+
DROP TABLE test_tblx;
2701+
DROP TYPE test_typex;
26852702
-- This test isn't that interesting on its own, but the purpose is to leave
26862703
-- behind a table to test pg_upgrade with. The table has a composite type
26872704
-- 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
@@ -1698,6 +1698,14 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
16981698

16991699
DROP TABLE test_tbl2_subclass;
17001700

1701+
CREATE TYPE test_typex AS (a int, b text);
1702+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
1703+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
1704+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
1705+
\d test_tblx
1706+
DROP TABLE test_tblx;
1707+
DROP TYPE test_typex;
1708+
17011709
-- This test isn't that interesting on its own, but the purpose is to leave
17021710
-- behind a table to test pg_upgrade with. The table has a composite type
17031711
-- column in it, and the composite type has a dropped attribute.

0 commit comments

Comments
 (0)