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

Commit fc057b2

Browse files
committed
Remove dead code for temporary relations in partition planning
Since recent commit 1c7c317, temporary relations cannot be mixed with permanent relations within the same partition tree, and the same counts for temporary relations created by other sessions, which the planner simply discarded. Instead be paranoid and issue an error, as those should be blocked at definition time, at least for now. At the same time, a test case is added to stress what has been moved when expand_partitioned_rtentry gets called recursively but bumps on a partitioned relation with no partitions which should be handled the same way as the non-inheritance case. This code may be reworked in a close future, and covering this code path will limit surprises. Reported-by: David Rowley Author: David Rowley Reviewed-by: Amit Langote, Robert Haas, Michael Paquier Discussion: https://postgr.es/m/CAKJS1f_HyV1txn_4XSdH5EOhBMYaCwsXyAj6bHXk9gOu4JKsbw@mail.gmail.com
1 parent 2c059c8 commit fc057b2

File tree

3 files changed

+37
-17
lines changed

3 files changed

+37
-17
lines changed

src/backend/optimizer/prep/prepunion.c

+16-17
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
16811681
int i;
16821682
RangeTblEntry *childrte;
16831683
Index childRTindex;
1684-
bool has_child = false;
16851684
PartitionDesc partdesc = RelationGetPartitionDesc(parentrel);
16861685

16871686
check_stack_depth();
@@ -1707,6 +1706,16 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
17071706
top_parentrc, parentrel,
17081707
appinfos, &childrte, &childRTindex);
17091708

1709+
/*
1710+
* If the partitioned table has no partitions, treat this as the
1711+
* non-inheritance case.
1712+
*/
1713+
if (partdesc->nparts == 0)
1714+
{
1715+
parentrte->inh = false;
1716+
return;
1717+
}
1718+
17101719
for (i = 0; i < partdesc->nparts; i++)
17111720
{
17121721
Oid childOID = partdesc->oids[i];
@@ -1715,15 +1724,13 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
17151724
/* Open rel; we already have required locks */
17161725
childrel = heap_open(childOID, NoLock);
17171726

1718-
/* As in expand_inherited_rtentry, skip non-local temp tables */
1727+
/*
1728+
* Temporary partitions belonging to other sessions should have been
1729+
* disallowed at definition, but for paranoia's sake, let's double
1730+
* check.
1731+
*/
17191732
if (RELATION_IS_OTHER_TEMP(childrel))
1720-
{
1721-
heap_close(childrel, lockmode);
1722-
continue;
1723-
}
1724-
1725-
/* We have a real partition. */
1726-
has_child = true;
1733+
elog(ERROR, "temporary relation from another session found as partition");
17271734

17281735
expand_single_inheritance_child(root, parentrte, parentRTindex,
17291736
parentrel, top_parentrc, childrel,
@@ -1738,14 +1745,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
17381745
/* Close child relation, but keep locks */
17391746
heap_close(childrel, NoLock);
17401747
}
1741-
1742-
/*
1743-
* If the partitioned table has no partitions or all the partitions are
1744-
* temporary tables from other backends, treat this as non-inheritance
1745-
* case.
1746-
*/
1747-
if (!has_child)
1748-
parentrte->inh = false;
17491748
}
17501749

17511750
/*

src/test/regress/expected/select.out

+13
Original file line numberDiff line numberDiff line change
@@ -951,3 +951,16 @@ select * from (values (2),(null),(1)) v(k) where k = k;
951951
1
952952
(2 rows)
953953

954+
-- Test partitioned tables with no partitions, which should be handled the
955+
-- same as the non-inheritance case when expanding its RTE.
956+
create table list_parted_tbl (a int,b int) partition by list (a);
957+
create table list_parted_tbl1 partition of list_parted_tbl
958+
for values in (1) partition by list(b);
959+
explain (costs off) select * from list_parted_tbl;
960+
QUERY PLAN
961+
--------------------------
962+
Result
963+
One-Time Filter: false
964+
(2 rows)
965+
966+
drop table list_parted_tbl;

src/test/regress/sql/select.sql

+8
Original file line numberDiff line numberDiff line change
@@ -254,3 +254,11 @@ drop function sillysrf(int);
254254
-- (see bug #5084)
255255
select * from (values (2),(null),(1)) v(k) where k = k order by k;
256256
select * from (values (2),(null),(1)) v(k) where k = k;
257+
258+
-- Test partitioned tables with no partitions, which should be handled the
259+
-- same as the non-inheritance case when expanding its RTE.
260+
create table list_parted_tbl (a int,b int) partition by list (a);
261+
create table list_parted_tbl1 partition of list_parted_tbl
262+
for values in (1) partition by list(b);
263+
explain (costs off) select * from list_parted_tbl;
264+
drop table list_parted_tbl;

0 commit comments

Comments
 (0)