]> granicus.if.org Git - postgresql/commitdiff
Remove dead code for temporary relations in partition planning
authorMichael Paquier <michael@paquier.xyz>
Wed, 4 Jul 2018 01:37:40 +0000 (10:37 +0900)
committerMichael Paquier <michael@paquier.xyz>
Wed, 4 Jul 2018 01:37:40 +0000 (10:37 +0900)
Since recent commit 1c7c317c, 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

src/backend/optimizer/prep/prepunion.c
src/test/regress/expected/select.out
src/test/regress/sql/select.sql

index 2d470240d5d3464036da3a0da423c3a64ea4be60..7d75e1eda935c572a1d2d7e06a7a70d5f10c42b7 100644 (file)
@@ -1681,7 +1681,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
        int                     i;
        RangeTblEntry *childrte;
        Index           childRTindex;
-       bool            has_child = false;
        PartitionDesc partdesc = RelationGetPartitionDesc(parentrel);
 
        check_stack_depth();
@@ -1707,6 +1706,16 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
                                                                        top_parentrc, parentrel,
                                                                        appinfos, &childrte, &childRTindex);
 
+       /*
+        * If the partitioned table has no partitions, treat this as the
+        * non-inheritance case.
+        */
+       if (partdesc->nparts == 0)
+       {
+               parentrte->inh = false;
+               return;
+       }
+
        for (i = 0; i < partdesc->nparts; i++)
        {
                Oid                     childOID = partdesc->oids[i];
@@ -1715,15 +1724,13 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
                /* Open rel; we already have required locks */
                childrel = heap_open(childOID, NoLock);
 
-               /* As in expand_inherited_rtentry, skip non-local temp tables */
+               /*
+                * Temporary partitions belonging to other sessions should have been
+                * disallowed at definition, but for paranoia's sake, let's double
+                * check.
+                */
                if (RELATION_IS_OTHER_TEMP(childrel))
-               {
-                       heap_close(childrel, lockmode);
-                       continue;
-               }
-
-               /* We have a real partition. */
-               has_child = true;
+                       elog(ERROR, "temporary relation from another session found as partition");
 
                expand_single_inheritance_child(root, parentrte, parentRTindex,
                                                                                parentrel, top_parentrc, childrel,
@@ -1738,14 +1745,6 @@ expand_partitioned_rtentry(PlannerInfo *root, RangeTblEntry *parentrte,
                /* Close child relation, but keep locks */
                heap_close(childrel, NoLock);
        }
-
-       /*
-        * If the partitioned table has no partitions or all the partitions are
-        * temporary tables from other backends, treat this as non-inheritance
-        * case.
-        */
-       if (!has_child)
-               parentrte->inh = false;
 }
 
 /*
index 1fab5136d29f6c92605407ffc8a39d16a1a739df..7ad1f0bc773593e9c45554c35e0d94e6ae106166 100644 (file)
@@ -951,3 +951,16 @@ select * from (values (2),(null),(1)) v(k) where k = k;
  1
 (2 rows)
 
+-- Test partitioned tables with no partitions, which should be handled the
+-- same as the non-inheritance case when expanding its RTE.
+create table list_parted_tbl (a int,b int) partition by list (a);
+create table list_parted_tbl1 partition of list_parted_tbl
+  for values in (1) partition by list(b);
+explain (costs off) select * from list_parted_tbl;
+        QUERY PLAN        
+--------------------------
+ Result
+   One-Time Filter: false
+(2 rows)
+
+drop table list_parted_tbl;
index c80429e7d0333151ccfcb6e5912a90b8c3b214eb..fdab06d5bc1fdce4accf07e5ddc68cbbb931f2de 100644 (file)
@@ -254,3 +254,11 @@ drop function sillysrf(int);
 -- (see bug #5084)
 select * from (values (2),(null),(1)) v(k) where k = k order by k;
 select * from (values (2),(null),(1)) v(k) where k = k;
+
+-- Test partitioned tables with no partitions, which should be handled the
+-- same as the non-inheritance case when expanding its RTE.
+create table list_parted_tbl (a int,b int) partition by list (a);
+create table list_parted_tbl1 partition of list_parted_tbl
+  for values in (1) partition by list(b);
+explain (costs off) select * from list_parted_tbl;
+drop table list_parted_tbl;