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:00:24 +0000 (08:30 +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
Back-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 baa669abe84208e87bdac1b00d01e17509cd7781..5e4d02ce4a6d58f53371882cef069b2155390f9f 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 0eca76cb41e78300ef41069d92f660606b10aab0..191ffe7e238075f4c58ffce4cc843fd8d0808887 100644 (file)
@@ -455,9 +455,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 03c056b8b771f64c7c02d6157412f6708636c485..11e7735533241491748699ab89191fa296e54bbe 100644 (file)
@@ -168,9 +168,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;