8000 Rethink the dependencies recorded for FieldSelect/FieldStore nodes. · koderP/postgres@be203c3 · GitHub
[go: up one dir, main page]

Skip to content

Commit be203c3

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 7102efd commit be203c3

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
@@ -2454,6 +2454,23 @@ Table "public.test_tbl2_subclass"
24542454
Inherits: test_tbl2
24552455

24562456
DROP TABLE test_tbl2_subclass;
2457+
CREATE TYPE test_typex AS (a int, b text);
2458+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
2459+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
2460+
ERROR: cannot drop composite type test_typex column a because other objects depend on it
2461+
DETAIL: constraint test_tblx_y_check on table test_tblx depends on composite type test_typex column a
2462+
HINT: Use DROP ... CASCADE to drop the dependent objects too.
2463+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
2464+
NOTICE: drop cascades to constraint test_tblx_y_check on table test_tblx
2465+
\d test_tblx
2466+
Table "public.test_tblx"
2467+
Column | Type | Modifiers
2468+
--------+------------+-----------
2469+
x | integer |
2470+
y | test_typex |
2471+
2472+
DROP TABLE test_tblx;
2473+
DROP TYPE test_typex;
24572474
-- This test isn't that interesting on its own, but the purpose is to leave
24582475
-- behind a table to test pg_upgrade with. The table has a composite type
24592476
-- 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
@@ -1558,6 +1558,14 @@ ALTER TYPE test_type2 RENAME ATTRIBUTE a TO aa CASCADE;
15581558

15591559
DROP TABLE test_tbl2_subclass;
15601560

1561+
CREATE TYPE test_typex AS (a int, b text);
1562+
CREATE TABLE test_tblx (x int, y test_typex check ((y).a > 0));
1563+
ALTER TYPE test_typex DROP ATTRIBUTE a; -- fails
1564+
ALTER TYPE test_typex DROP ATTRIBUTE a CASCADE;
1565+
\d test_tblx
1566+
DROP TABLE test_tblx;
1567+
DROP TYPE test_typex;
1568+
15611569
-- This test isn't that interesting on its own, but the purpose is to leave
15621570
-- behind a table to test pg_upgrade with. The table has a composite type
15631571
-- column in it, and the composite type has a dropped attribute.

0 commit comments

Comments
 (0)
0