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

Commit ce1663c

Browse files
committed
Fix assertion failure when Parallel Append is run serially.
Parallel-aware plan nodes must be prepared to run without parallelism if it's not possible at execution time for whatever reason. Commit ab72716, which introduced Parallel Append, overlooked this. Rajkumar Raghuwanshi reported this problem, and I included his test case in this patch. The code changes are by me. Discussion: http://postgr.es/m/CAKcux6=WqkUudLg1GLZZ7fc5ScWC1+Y9qD=pAHeqy32WoeJQvw@mail.gmail.com
1 parent 4fa3964 commit ce1663c

File tree

3 files changed

+31
-7
lines changed

3 files changed

+31
-7
lines changed

src/backend/executor/nodeAppend.c

Lines changed: 14 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -162,7 +162,7 @@ ExecInitAppend(Append *node, EState *estate, int eflags)
162162
appendstate->as_whichplan =
163163
appendstate->ps.plan->parallel_aware ? INVALID_SUBPLAN_INDEX : 0;
164164

165-
/* If parallel-aware, this will be overridden later. */
165+
/* For parallel query, this will be overridden later. */
166166
appendstate->choose_next_subplan = choose_next_subplan_locally;
167167

168168
return appendstate;
@@ -361,14 +361,21 @@ choose_next_subplan_locally(AppendState *node)
361361
{
362362
int whichplan = node->as_whichplan;
363363

364-
/* We should never see INVALID_SUBPLAN_INDEX in this case. */
365-
Assert(whichplan >= 0 && whichplan <= node->as_nplans);
366-
367364
if (ScanDirectionIsForward(node->ps.state->es_direction))
368365
{
369-
if (whichplan >= node->as_nplans - 1)
370-
return false;
371-
node->as_whichplan++;
366+
/*
367+
* We won't normally see INVALID_SUBPLAN_INDEX in this case, but we
368+
* might if a plan intended to be run in parallel ends up being run
369+
* serially.
370+
*/
371+
if (whichplan == INVALID_SUBPLAN_INDEX)
372+
node->as_whichplan = 0;
373+
else
374+
{
375+
if (whichplan >= node->as_nplans - 1)
376+
return false;
377+
node->as_whichplan++;
378+
}
372379
}
373380
else
374381
{

src/test/regress/expected/select_parallel.out

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -121,6 +121,17 @@ select round(avg(aa)), sum(aa) from a_star a4;
121121
(1 row)
122122

123123
reset enable_parallel_append;
124+
-- Parallel Append that runs serially
125+
create or replace function foobar() returns setof text as
126+
$$ select 'foo'::varchar union all select 'bar'::varchar $$
127+
language sql stable;
128+
select foobar() order by 1;
129+
foobar
130+
--------
131+
bar
132+
foo
133+
(2 rows)
134+
124135
-- test with leader participation disabled
125136
set parallel_leader_participation = off;
126137
explain (costs off)

src/test/regress/sql/select_parallel.sql

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,12 @@ explain (costs off)
4949
select round(avg(aa)), sum(aa) from a_star a4;
5050
reset enable_parallel_append;
5151

52+
-- Parallel Append that runs serially
53+
create or replace function foobar() returns setof text as
54+
$$ select 'foo'::varchar union all select 'bar'::varchar $$
55+
language sql stable;
56+
select foobar() order by 1;
57+
5258
-- test with leader participation disabled
5359
set parallel_leader_participation = off;
5460
explain (costs off)

0 commit comments

Comments
 (0)