Don't shut down Gather[Merge] early under Limit.
authorAmit Kapila <[email protected]>
Mon, 18 Nov 2019 08:47:41 +0000 (14:17 +0530)
committerAmit Kapila <[email protected]>
Tue, 26 Nov 2019 03:53:45 +0000 (09:23 +0530)
Revert part of commit 19df1702f5.

Early shutdown was added by that commit so that we could collect
statistics from workers, but unfortunately, it interacted badly with
rescans.  The problem is that we ended up destroying the parallel context
which is required for rescans.  This leads to rescans of a Limit node over
a Gather node to produce unpredictable results as it tries to access
destroyed parallel context.  By reverting the early shutdown code, we
might lose statistics in some cases of Limit over Gather [Merge], but that
will require further study to fix.

Reported-by: Jerry Sievers
Diagnosed-by: Thomas Munro
Author: Amit Kapila, testcase by Vignesh C
Backpatch-through: 9.6
Discussion: https://postgr.es/m/[email protected]

src/backend/executor/nodeLimit.c
src/test/regress/expected/select_parallel.out
src/test/regress/sql/select_parallel.sql

index 6e74bb23d06acb6282772d8957b1d1694396e9b2..4eb999ee4c87d8f8d36a5bd166b272ecac99afb2 100644 (file)
@@ -129,19 +129,17 @@ ExecLimit(PlanState *pstate)
                 * we are at the end of the window, return NULL without
                 * advancing the subplan or the position variable; but change
                 * the state machine state to record having done so.
+                *
+                * Once at the end, ideally, we can shut down parallel
+                * resources but that would destroy the parallel context which
+                * would be required for rescans.  To do that, we need to find
+                * a way to pass down more information about whether rescans
+                * are possible.
                 */
                if (!node->noCount &&
                    node->position - node->offset >= node->count)
                {
                    node->lstate = LIMIT_WINDOWEND;
-
-                   /*
-                    * If we know we won't need to back up, we can release
-                    * resources at this point.
-                    */
-                   if (!(node->ps.state->es_top_eflags & EXEC_FLAG_BACKWARD))
-                       (void) ExecShutdownNode(outerPlan);
-
                    return NULL;
                }
 
index 70c6fe43eb1a9539a4d59098ece88453b59255e9..d81de9d68aa96977f9f0407e1ef108b3973df0d5 100644 (file)
@@ -253,9 +253,48 @@ select * from
   9000 | 3
 (3 rows)
 
-reset enable_material;
+-- test rescans for a Limit node with a parallel node beneath it.
 reset enable_seqscan;
+set enable_indexonlyscan to off;
+set enable_indexscan to off;
+alter table tenk1 set (parallel_workers = 0);
+alter table tenk2 set (parallel_workers = 1);
+explain (costs off)
+select count(*) from tenk1
+  left join (select tenk2.unique1 from tenk2 order by 1 limit 1000) ss
+  on tenk1.unique1 < ss.unique1 + 1
+  where tenk1.unique1 < 2;
+                         QUERY PLAN                         
+------------------------------------------------------------
+ Aggregate
+   ->  Nested Loop Left Join
+         Join Filter: (tenk1.unique1 < (tenk2.unique1 + 1))
+         ->  Seq Scan on tenk1
+               Filter: (unique1 < 2)
+         ->  Limit
+               ->  Gather Merge
+                     Workers Planned: 1
+                     ->  Sort
+                           Sort Key: tenk2.unique1
+                           ->  Parallel Seq Scan on tenk2
+(11 rows)
+
+select count(*) from tenk1
+  left join (select tenk2.unique1 from tenk2 order by 1 limit 1000) ss
+  on tenk1.unique1 < ss.unique1 + 1
+  where tenk1.unique1 < 2;
+ count 
+-------
+  1999
+(1 row)
+
+--reset the value of workers for each table as it was before this test.
+alter table tenk1 set (parallel_workers = 4);
+alter table tenk2 reset (parallel_workers);
+reset enable_material;
 reset enable_bitmapscan;
+reset enable_indexonlyscan;
+reset enable_indexscan;
 -- test parallel bitmap heap scan.
 set enable_seqscan to off;
 set enable_indexscan to off;
index 8225719ff35e0bd5fa2477aae091c99300f4bd74..a08465bcf17c1cbd1ba856dd45944738f4a54ce7 100644 (file)
@@ -90,9 +90,29 @@ select * from
   (select count(*) from tenk1 where thousand > 99) ss
   right join (values (1),(2),(3)) v(x) on true;
 
-reset enable_material;
+-- test rescans for a Limit node with a parallel node beneath it.
 reset enable_seqscan;
+set enable_indexonlyscan to off;
+set enable_indexscan to off;
+alter table tenk1 set (parallel_workers = 0);
+alter table tenk2 set (parallel_workers = 1);
+explain (costs off)
+select count(*) from tenk1
+  left join (select tenk2.unique1 from tenk2 order by 1 limit 1000) ss
+  on tenk1.unique1 < ss.unique1 + 1
+  where tenk1.unique1 < 2;
+select count(*) from tenk1
+  left join (select tenk2.unique1 from tenk2 order by 1 limit 1000) ss
+  on tenk1.unique1 < ss.unique1 + 1
+  where tenk1.unique1 < 2;
+--reset the value of workers for each table as it was before this test.
+alter table tenk1 set (parallel_workers = 4);
+alter table tenk2 reset (parallel_workers);
+
+reset enable_material;
 reset enable_bitmapscan;
+reset enable_indexonlyscan;
+reset enable_indexscan;
 
 -- test parallel bitmap heap scan.
 set enable_seqscan to off;