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

Commit 2159196

Browse files
committed
Turns out new IN implementation has got some problems in an UPDATE or
DELETE with inherited target table. Fix it; add a regression test. Also, correct ancient misspelling of 'inherited'.
1 parent 147fbf9 commit 2159196

File tree

8 files changed

+80
-20
lines changed

8 files changed

+80
-20
lines changed

src/backend/nodes/copyfuncs.c

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
* Portions Copyright (c) 1994, Regents of the University of California
1616
*
1717
* IDENTIFICATION
18-
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.244 2003/02/16 02:30:37 tgl Exp $
18+
* $Header: /cvsroot/pgsql/src/backend/nodes/copyfuncs.c,v 1.245 2003/03/05 20:01:01 tgl Exp $
1919
*
2020
*-------------------------------------------------------------------------
2121
*/
@@ -1481,12 +1481,16 @@ _copyQuery(Query *from)
14811481
COPY_NODE_FIELD(limitCount);
14821482
COPY_NODE_FIELD(setOperations);
14831483
COPY_INTLIST_FIELD(resultRelations);
1484+
COPY_NODE_FIELD(in_info_list);
1485+
COPY_SCALAR_FIELD(hasJoinRTEs);
14841486

14851487
/*
1486-
* We do not copy the planner internal fields: base_rel_list,
1487-
* other_rel_list, join_rel_list, equi_key_list, in_info_list,
1488-
* query_pathkeys, hasJoinRTEs. That would get us into copying
1489-
* RelOptInfo/Path trees, which we don't want to do.
1488+
* We do not copy the other planner internal fields: base_rel_list,
1489+
* other_rel_list, join_rel_list, equi_key_list, query_pathkeys.
1490+
* That would get us into copying RelOptInfo/Path trees, which we don't
1491+
* want to do. It is necessary to copy in_info_list and hasJoinRTEs
1492+
* for the benefit of inheritance_planner(), which may try to copy a
1493+
* Query in which these are already set.
14901494
*/
14911495

14921496
return newnode;

src/backend/nodes/equalfuncs.c

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
* Portions Copyright (c) 1994, Regents of the University of California
1919
*
2020
* IDENTIFICATION
21-
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.187 2003/02/16 02:30:37 tgl Exp $
21+
* $Header: /cvsroot/pgsql/src/backend/nodes/equalfuncs.c,v 1.188 2003/03/05 20:01:02 tgl Exp $
2222
*
2323
*-------------------------------------------------------------------------
2424
*/
@@ -576,12 +576,14 @@ _equalQuery(Query *a, Query *b)
576576
COMPARE_NODE_FIELD(limitCount);
577577
COMPARE_NODE_FIELD(setOperations);
578578
COMPARE_INTLIST_FIELD(resultRelations);
579+
COMPARE_NODE_FIELD(in_info_list);
580+
COMPARE_SCALAR_FIELD(hasJoinRTEs);
579581

580582
/*
581-
* We do not check the internal-to-the-planner fields: base_rel_list,
582-
* other_rel_list, join_rel_list, equi_key_list, in_info_list,
583-
* query_pathkeys, hasJoinRTEs. They might not be set yet, and in any
584-
* case they should be derivable from the other fields.
583+
* We do not check the other planner internal fields: base_rel_list,
584+
* other_rel_list, join_rel_list, equi_key_list, query_pathkeys.
585+
* They might not be set yet, and in any case they should be derivable
586+
* from the other fields.
585587
*/
586588
return true;
587589
}

src/backend/optimizer/path/allpaths.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.97 2003/02/15 20:12:40 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/path/allpaths.c,v 1.98 2003/03/05 20:01:03 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -115,7 +115,7 @@ set_base_rel_pathlists(Query *root)
115115
/* RangeFunction --- generate a separate plan for it */
116116
set_function_pathlist(root, rel, rte);
117117
}
118-
else if ((inheritlist = expand_inherted_rtentry(root, rti, true))
118+
else if ((inheritlist = expand_inherited_rtentry(root, rti, true))
119119
!= NIL)
120120
{
121121
/* Relation is root of an inheritance tree, process specially */

src/backend/optimizer/plan/planner.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.149 2003/03/05 18:38:14 tgl Exp $
11+
* $Header: /cvsroot/pgsql/src/backend/optimizer/plan/planner.c,v 1.150 2003/03/05 20:01:03 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -276,8 +276,8 @@ subquery_planner(Query *parse, double tuple_fraction)
276276
* grouping_planner.
277277
*/
278278
if (parse->resultRelation &&
279-
(lst = expand_inherted_rtentry(parse, parse->resultRelation, false))
280-
!= NIL)
279+
(lst = expand_inherited_rtentry(parse, parse->resultRelation,
280+
false)) != NIL)
281281
plan = inheritance_planner(parse, lst);
282282
else
283283
plan = grouping_planner(parse, tuple_fraction);

src/backend/optimizer/prep/prepunion.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414
*
1515
*
1616
* IDENTIFICATION
17-
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.90 2003/02/09 06:56:27 tgl Exp $
17+
* $Header: /cvsroot/pgsql/src/backend/optimizer/prep/prepunion.c,v 1.91 2003/03/05 20:01:03 tgl Exp $
1818
*
1919
*-------------------------------------------------------------------------
2020
*/
@@ -642,7 +642,7 @@ find_all_inheritors(Oid parentrel)
642642
}
643643

644644
/*
645-
* expand_inherted_rtentry
645+
* expand_inherited_rtentry
646646
* Check whether a rangetable entry represents an inheritance set.
647647
* If so, add entries for all the child tables to the query's
648648
* rangetable, and return an integer list of RT indexes for the
@@ -666,7 +666,7 @@ find_all_inheritors(Oid parentrel)
666666
* XXX probably should convert the result type to Relids?
667667
*/
668668
List *
669-
expand_inherted_rtentry(Query *parse, Index rti, bool dup_parent)
669+
expand_inherited_rtentry(Query *parse, Index rti, bool dup_parent)
670670
{
671671
RangeTblEntry *rte = rt_fetch(rti, parse->rtable);
672672
Oid parentOID;

src/include/optimizer/prep.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2002, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $Id: prep.h,v 1.37 2003/02/09 23:57:19 tgl Exp $
10+
* $Id: prep.h,v 1.38 2003/03/05 20:01:04 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -52,7 +52,7 @@ extern Plan *plan_set_operations(Query *parse);
5252

5353
extern List *find_all_inheritors(Oid parentrel);
5454

55-
extern List *expand_inherted_rtentry(Query *parse, Index rti,
55+
extern List *expand_inherited_rtentry(Query *parse, Index rti,
5656
bool dup_parent);
5757

5858
extern Node *adjust_inherited_attrs(Node *node,

src/test/regress/expected/inherit.out

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -539,3 +539,34 @@ CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);
539539
NOTICE: CREATE TABLE / PRIMARY KEY will create implicit index 'z_pkey' for table 'z'
540540
INSERT INTO z VALUES (NULL, 'text'); -- should fail
541541
ERROR: ExecInsert: Fail to add null value in not null attribute aa
542+
-- Check UPDATE with inherited target and an inherited source table
543+
create temp table foo(f1 int, f2 int);
544+
create temp table foo2(f3 int) inherits (foo);
545+
create temp table bar(f1 int, f2 int);
546+
create temp table bar2(f3 int) inherits (bar);
547+
insert into foo values(1,1);
548+
insert into foo values(3,3);
549+
insert into foo2 values(2,2,2);
550+
insert into foo2 values(3,3,3);
551+
insert into bar values(1,1);
552+
insert into bar values(2,2);
553+
insert into bar values(3,3);
554+
insert into bar values(4,4);
555+
insert into bar2 values(1,1,1);
556+
insert into bar2 values(2,2,2);
557+
insert into bar2 values(3,3,3);
558+
insert into bar2 values(4,4,4);
559+
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
560+
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid;
561+
relname | f1 | f2
562+
---------+----+-----
563+
bar | 4 | 4
564+
bar | 1 | 101
565+
bar | 2 | 102
566+
bar | 3 | 103
567+
bar2 | 4 | 4
568+
bar2 | 1 | 101
569+
bar2 | 2 | 102
570+
bar2 | 3 | 103
571+
(8 rows)
572+

src/test/regress/sql/inherit.sql

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -96,3 +96,26 @@ SELECT relname, d.* FROM ONLY d, pg_class where d.tableoid = pg_class.oid;
9696
-- Confirm PRIMARY KEY adds NOT NULL constraint to child table
9797
CREATE TEMP TABLE z (b TEXT, PRIMARY KEY(aa, b)) inherits (a);
9898
INSERT INTO z VALUES (NULL, 'text'); -- should fail
99+
100+
-- Check UPDATE with inherited target and an inherited source table
101+
create temp table foo(f1 int, f2 int);
102+
create temp table foo2(f3 int) inherits (foo);
103+
create temp table bar(f1 int, f2 int);
104+
create temp table bar2(f3 int) inherits (bar);
105+
106+
insert into foo values(1,1);
107+
insert into foo values(3,3);
108+
insert into foo2 values(2,2,2);
109+
insert into foo2 values(3,3,3);
110+
insert into bar values(1,1);
111+
insert into bar values(2,2);
112+
insert into bar values(3,3);
113+
insert into bar values(4,4);
114+
insert into bar2 values(1,1,1);
115+
insert into bar2 values(2,2,2);
116+
insert into bar2 values(3,3,3);
117+
insert into bar2 values(4,4,4);
118+
119+
update bar set f2 = f2 + 100 where f1 in (select f1 from foo);
120+
121+
SELECT relname, bar.* FROM bar, pg_class where bar.tableoid = pg_class.oid;

0 commit comments

Comments
 (0)