From 9927a5ede8618771d5f4c3ec42709d11f67617a7 Mon Sep 17 00:00:00 2001 From: Shihao Date: Fri, 18 Sep 2026 22:44:38 -0400 Subject: [PATCH v2] Pass the current tuple bound to workers on Gather rescan Commit 3452dc5240d made Gather and Gather Merge pass the tuple bound to the workers through the DSM segment. But the bound was only stored by ExecInitParallelPlan(). ExecParallelReinitialize() reuses the segment and left the old value in place. So after a rescan the new workers still used the bound from the first scan. If a Limit above the Gather depends on a parameter, a later scan can need more tuples than the first one. The workers then stopped too early, and the query returned too few rows. With Gather Merge over a Sort it could return the right number of wrong rows. Fix by adding ExecParallelReinitializeExtended(), which takes a tuples_needed argument and stores it in the shared state, like ExecInitParallelPlan() does. Gather and Gather Merge now call it. ExecParallelReinitialize() keeps its signature and its old behavior, so that callers outside core keep working. On master the signature is changed instead. Add tests for both Gather and Gather Merge. Reported-by: Jeff Davis Discussion: https://postgr.es/m/2701c83e33f3fe66320291fb5d1b74ca2cb90bd7.camel@j-davis.com Backpatch-through: 14 --- src/backend/executor/execParallel.c | 32 ++++++++++- src/backend/executor/nodeGather.c | 7 ++- src/backend/executor/nodeGatherMerge.c | 7 ++- src/include/executor/execParallel.h | 4 ++ src/test/regress/expected/select_parallel.out | 55 +++++++++++++++++++ src/test/regress/sql/select_parallel.sql | 20 +++++++ 6 files changed, 118 insertions(+), 7 deletions(-) diff --git a/src/backend/executor/execParallel.c b/src/backend/executor/execParallel.c index 81b87d82fab..2552d512b4c 100644 --- a/src/backend/executor/execParallel.c +++ b/src/backend/executor/execParallel.c @@ -75,7 +75,9 @@ */ typedef struct FixedParallelExecutorState { - int64 tuples_needed; /* tuple bound, see ExecSetTupleBound */ + int64 tuples_needed; /* tuple bound, see ExecSetTupleBound. Must + * be updated on each rescan, see + * ExecParallelReinitializeExtended */ dsa_pointer param_exec; int eflags; int jit_flags; @@ -965,11 +967,32 @@ ExecParallelCreateReaders(ParallelExecutorInfo *pei) /* * Re-initialize the parallel executor shared memory state before launching * a fresh batch of workers. + * + * This version keeps the tuple bound from the previous scan. It exists so + * that callers outside core keep working. Core code uses + * ExecParallelReinitializeExtended() instead. */ void ExecParallelReinitialize(PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams) +{ + FixedParallelExecutorState *fpes; + + fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false); + ExecParallelReinitializeExtended(planstate, pei, sendParams, + fpes->tuples_needed); +} + +/* + * Like ExecParallelReinitialize(), but also pass the current tuple bound to + * the new workers. + */ +void +ExecParallelReinitializeExtended(PlanState *planstate, + ParallelExecutorInfo *pei, + Bitmapset *sendParams, + int64 tuples_needed) { EState *estate = planstate->state; FixedParallelExecutorState *fpes; @@ -991,6 +1014,13 @@ ExecParallelReinitialize(PlanState *planstate, fpes = shm_toc_lookup(pei->pcxt->toc, PARALLEL_KEY_EXECUTOR_FIXED, false); + /* + * The tuple bound might have changed since the last scan, for instance if + * a Limit node above us depends on a parameter. Pass the current value + * to the new batch of workers. + */ + fpes->tuples_needed = tuples_needed; + /* Free any serialized parameters from the last round. */ if (DsaPointerIsValid(fpes->param_exec)) { diff --git a/src/backend/executor/nodeGather.c b/src/backend/executor/nodeGather.c index 114693abb32..8c598337119 100644 --- a/src/backend/executor/nodeGather.c +++ b/src/backend/executor/nodeGather.c @@ -170,9 +170,10 @@ ExecGather(PlanState *pstate) gather->num_workers, node->tuples_needed); else - ExecParallelReinitialize(outerPlanState(node), - node->pei, - gather->initParam); + ExecParallelReinitializeExtended(outerPlanState(node), + node->pei, + gather->initParam, + node->tuples_needed); /* * Register backend workers. We might not get as many as we diff --git a/src/backend/executor/nodeGatherMerge.c b/src/backend/executor/nodeGatherMerge.c index c2ac5e0792c..859e24c30ef 100644 --- a/src/backend/executor/nodeGatherMerge.c +++ b/src/backend/executor/nodeGatherMerge.c @@ -214,9 +214,10 @@ ExecGatherMerge(PlanState *pstate) gm->num_workers, node->tuples_needed); else - ExecParallelReinitialize(outerPlanState(node), - node->pei, - gm->initParam); + ExecParallelReinitializeExtended(outerPlanState(node), + node->pei, + gm->initParam, + node->tuples_needed); /* Try to launch workers. */ pcxt = node->pei->pcxt; diff --git a/src/include/executor/execParallel.h b/src/include/executor/execParallel.h index 5a2034811d5..4e23a0708ca 100644 --- a/src/include/executor/execParallel.h +++ b/src/include/executor/execParallel.h @@ -45,6 +45,10 @@ extern void ExecParallelFinish(ParallelExecutorInfo *pei); extern void ExecParallelCleanup(ParallelExecutorInfo *pei); extern void ExecParallelReinitialize(PlanState *planstate, ParallelExecutorInfo *pei, Bitmapset *sendParams); +extern void ExecParallelReinitializeExtended(PlanState *planstate, + ParallelExecutorInfo *pei, + Bitmapset *sendParams, + int64 tuples_needed); extern void ParallelQueryMain(dsm_segment *seg, shm_toc *toc); diff --git a/src/test/regress/expected/select_parallel.out b/src/test/regress/expected/select_parallel.out index 933921d1860..936730a45cd 100644 --- a/src/test/regress/expected/select_parallel.out +++ b/src/test/regress/expected/select_parallel.out @@ -526,6 +526,61 @@ select count(*) from tenk1 --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); +-- likewise, but the Limit's bound changes between rescans; the workers +-- must see the new bound, not the one from the first scan. +set parallel_leader_participation = off; +explain (costs off) +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*), sum(unique1) from + (select unique1 from tenk2 order by 1 limit v.n) x) ss; + QUERY PLAN +---------------------------------------------------------- + Nested Loop + -> Values Scan on "*VALUES*" + -> Aggregate + -> Limit + -> Gather Merge + Workers Planned: 4 + -> Sort + Sort Key: tenk2.unique1 + -> Parallel Seq Scan on tenk2 +(9 rows) + +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*), sum(unique1) from + (select unique1 from tenk2 order by 1 limit v.n) x) ss; + n | count | sum +----+-------+----- + 1 | 1 | 0 + 10 | 10 | 45 +(2 rows) + +-- same for plain Gather +explain (costs off) +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*) from + (select unique1 from tenk2 limit v.n) x) ss; + QUERY PLAN +---------------------------------------------------- + Nested Loop + -> Values Scan on "*VALUES*" + -> Aggregate + -> Limit + -> Gather + Workers Planned: 4 + -> Parallel Seq Scan on tenk2 +(7 rows) + +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*) from + (select unique1 from tenk2 limit v.n) x) ss; + n | count +----+------- + 1 | 1 + 10 | 10 +(2 rows) + +reset parallel_leader_participation; reset enable_material; reset enable_bitmapscan; reset enable_indexonlyscan; diff --git a/src/test/regress/sql/select_parallel.sql b/src/test/regress/sql/select_parallel.sql index 71a75bc86ea..9397137d99f 100644 --- a/src/test/regress/sql/select_parallel.sql +++ b/src/test/regress/sql/select_parallel.sql @@ -200,6 +200,26 @@ select count(*) from tenk1 alter table tenk1 set (parallel_workers = 4); alter table tenk2 reset (parallel_workers); +-- likewise, but the Limit's bound changes between rescans; the workers +-- must see the new bound, not the one from the first scan. +set parallel_leader_participation = off; +explain (costs off) +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*), sum(unique1) from + (select unique1 from tenk2 order by 1 limit v.n) x) ss; +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*), sum(unique1) from + (select unique1 from tenk2 order by 1 limit v.n) x) ss; +-- same for plain Gather +explain (costs off) +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*) from + (select unique1 from tenk2 limit v.n) x) ss; +select v.n, ss.* from (values (1), (10)) v(n), + lateral (select count(*) from + (select unique1 from tenk2 limit v.n) x) ss; +reset parallel_leader_participation; + reset enable_material; reset enable_bitmapscan; reset enable_indexonlyscan; -- 2.37.1 (Apple Git-137.1)