Skip to content

Commit 884860b

Browse files
committed
Don't enter parallel mode when holding interrupts.
Doing so caused the leader to hang in wait_event=ParallelFinish, which required an immediate shutdown to resolve. Back-patch to v12 (all supported versions). Francesco Degrassi Discussion: https://postgr.es/m/CAC-SaSzHUKT=vZJ8MPxYdC_URPfax+yoA1hKTcF4ROz_Q6z0_Q@mail.gmail.com
1 parent cbcd4bb commit 884860b

File tree

3 files changed

+61
-0
lines changed

3 files changed

+61
-0
lines changed

src/backend/optimizer/plan/planner.c

+6
Original file line numberDiff line numberDiff line change
@@ -333,6 +333,11 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
333333
* if we want to allow parallel inserts in general; updates and deletes
334334
* have additional problems especially around combo CIDs.)
335335
*
336+
* We don't try to use parallel mode unless interruptible. The leader
337+
* expects ProcessInterrupts() calls to reach HandleParallelMessages().
338+
* Even if we called HandleParallelMessages() another way, starting a
339+
* parallel worker is too delay-prone to be prudent when uncancellable.
340+
*
336341
* For now, we don't try to use parallel mode if we're running inside a
337342
* parallel worker. We might eventually be able to relax this
338343
* restriction, but for now it seems best not to have parallel workers
@@ -343,6 +348,7 @@ standard_planner(Query *parse, const char *query_string, int cursorOptions,
343348
parse->commandType == CMD_SELECT &&
344349
!parse->hasModifyingCTE &&
345350
max_parallel_workers_per_gather > 0 &&
351+
INTERRUPTS_CAN_BE_PROCESSED() &&
346352
!IsParallelWorker())
347353
{
348354
/* all the cheap tests pass, so scan the query tree */

src/test/regress/expected/select_parallel.out

+24
Original file line numberDiff line numberDiff line change
@@ -1252,3 +1252,27 @@ reset force_parallel_mode;
12521252
drop function set_and_report_role();
12531253
drop function set_role_and_error(int);
12541254
drop role regress_parallel_worker;
1255+
-- don't freeze in ParallelFinish while holding an LWLock
1256+
BEGIN;
1257+
CREATE FUNCTION my_cmp (int4, int4)
1258+
RETURNS int LANGUAGE sql AS
1259+
$$
1260+
SELECT
1261+
CASE WHEN $1 < $2 THEN -1
1262+
WHEN $1 > $2 THEN 1
1263+
ELSE 0
1264+
END;
1265+
$$;
1266+
CREATE TABLE parallel_hang (i int4);
1267+
INSERT INTO parallel_hang
1268+
(SELECT * FROM generate_series(1, 400) gs);
1269+
CREATE OPERATOR CLASS int4_custom_ops FOR TYPE int4 USING btree AS
1270+
OPERATOR 1 < (int4, int4), OPERATOR 2 <= (int4, int4),
1271+
OPERATOR 3 = (int4, int4), OPERATOR 4 >= (int4, int4),
1272+
OPERATOR 5 > (int4, int4), FUNCTION 1 my_cmp(int4, int4);
1273+
CREATE UNIQUE INDEX parallel_hang_idx
1274+
ON parallel_hang
1275+
USING btree (i int4_custom_ops);
1276+
SET force_parallel_mode = on;
1277+
DELETE FROM parallel_hang WHERE 380 <= i AND i <= 420;
1278+
ROLLBACK;

src/test/regress/sql/select_parallel.sql

+31
Original file line numberDiff line numberDiff line change
@@ -485,3 +485,34 @@ reset force_parallel_mode;
485485
drop function set_and_report_role();
486486
drop function set_role_and_error(int);
487487
drop role regress_parallel_worker;
488+
489+
-- don't freeze in ParallelFinish while holding an LWLock
490+
BEGIN;
491+
492+
CREATE FUNCTION my_cmp (int4, int4)
493+
RETURNS int LANGUAGE sql AS
494+
$$
495+
SELECT
496+
CASE WHEN $1 < $2 THEN -1
497+
WHEN $1 > $2 THEN 1
498+
ELSE 0
499+
END;
500+
$$;
501+
502+
CREATE TABLE parallel_hang (i int4);
503+
INSERT INTO parallel_hang
504+
(SELECT * FROM generate_series(1, 400) gs);
505+
506+
CREATE OPERATOR CLASS int4_custom_ops FOR TYPE int4 USING btree AS
507+
OPERATOR 1 < (int4, int4), OPERATOR 2 <= (int4, int4),
508+
OPERATOR 3 = (int4, int4), OPERATOR 4 >= (int4, int4),
509+
OPERATOR 5 > (int4, int4), FUNCTION 1 my_cmp(int4, int4);
510+
511+
CREATE UNIQUE INDEX parallel_hang_idx
512+
ON parallel_hang
513+
USING btree (i int4_custom_ops);
514+
515+
SET force_parallel_mode = on;
516+
DELETE FROM parallel_hang WHERE 380 <= i AND i <= 420;
517+
518+
ROLLBACK;

0 commit comments

Comments
 (0)