Skip to content

Commit eae7be6

Browse files
committed
Fix parallel-safety check of expressions and predicate for index builds
As coded, the planner logic that calculates the number of parallel workers to use for a parallel index build uses expressions and predicates from the relcache, which are flattened for the planner by eval_const_expressions(). As reported in the bug, an immutable parallel-unsafe function flattened in the relcache would become a Const, which would be considered as parallel-safe, even if the predicate or the expressions including the function are not safe in parallel workers. Depending on the expressions or predicate used, this could cause the parallel build to fail. Tests are included that check parallel index builds with parallel-unsafe predicate and expressions. Two routines are added to lsyscache.h to be able to retrieve expressions and predicate of an index from its pg_index data. Reported-by: Alexander Lakhin Author: Tender Wang Reviewed-by: Jian He, Michael Paquier Discussion: https://postgr.es/m/CAHewXN=UaAaNn9ruHDH3Os8kxLVmtWqbssnf=dZN_s9=evHUFA@mail.gmail.com Backpatch-through: 12
1 parent 3e76a80 commit eae7be6

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

src/backend/optimizer/plan/planner.c

+10-2
Original file line numberDiff line numberDiff line change
@@ -6667,10 +6667,18 @@ plan_create_index_workers(Oid tableOid, Oid indexOid)
66676667
* Currently, parallel workers can't access the leader's temporary tables.
66686668
* Furthermore, any index predicate or index expressions must be parallel
66696669
* safe.
6670+
*
6671+
* Fetch the list of expressions and predicates directly from the
6672+
* catalogs. Retrieving this information from the relcache would cause
6673+
* the expressions and predicates to be flattened, losing properties that
6674+
* can be important to check if parallel workers can be used. For
6675+
* example, immutable parallel-unsafe functions, that cannot be used in
6676+
* parallel workers, would be changed to Const nodes, that are safe in
6677+
* parallel workers.
66706678
*/
66716679
if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
6672-
!is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
6673-
!is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
6680+
!is_parallel_safe(root, (Node *) get_index_expressions(indexOid)) ||
6681+
!is_parallel_safe(root, (Node *) get_index_predicate(indexOid)))
66746682
{
66756683
parallel_workers = 0;
66766684
goto done;

src/backend/utils/cache/lsyscache.c

+68
Original file line numberDiff line numberDiff line change
@@ -3502,6 +3502,74 @@ get_index_column_opclass(Oid index_oid, int attno)
35023502
return opclass;
35033503
}
35043504

3505+
/*
3506+
* get_index_expressions
3507+
*
3508+
* Given the index OID, its a List of its expressions or NIL if none.
3509+
*/
3510+
List *
3511+
get_index_expressions(Oid index_oid)
3512+
{
3513+
List *result;
3514+
HeapTuple tuple;
3515+
Datum exprDatum;
3516+
bool isnull;
3517+
char *exprString;
3518+
3519+
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
3520+
if (!HeapTupleIsValid(tuple))
3521+
elog(ERROR, "cache lookup failed for index %u", index_oid);
3522+
3523+
exprDatum = SysCacheGetAttr(INDEXRELID, tuple,
3524+
Anum_pg_index_indexprs, &isnull);
3525+
if (isnull)
3526+
{
3527+
ReleaseSysCache(tuple);
3528+
return NIL;
3529+
}
3530+
3531+
exprString = TextDatumGetCString(exprDatum);
3532+
result = (List *) stringToNode(exprString);
3533+
pfree(exprString);
3534+
ReleaseSysCache(tuple);
3535+
3536+
return result;
3537+
}
3538+
3539+
/*
3540+
* get_index_predicate
3541+
*
3542+
* Given the index OID, return a List of its predicate or NIL if none.
3543+
*/
3544+
List *
3545+
get_index_predicate(Oid index_oid)
3546+
{
3547+
List *result;
3548+
HeapTuple tuple;
3549+
Datum predDatum;
3550+
bool isnull;
3551+
char *predString;
3552+
3553+
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
3554+
if (!HeapTupleIsValid(tuple))
3555+
elog(ERROR, "cache lookup failed for index %u", index_oid);
3556+
3557+
predDatum = SysCacheGetAttr(INDEXRELID, tuple,
3558+
Anum_pg_index_indpred, &isnull);
3559+
if (isnull)
3560+
{
3561+
ReleaseSysCache(tuple);
3562+
return NIL;
3563+
}
3564+
3565+
predString = TextDatumGetCString(predDatum);
3566+
result = (List *) stringToNode(predString);
3567+
pfree(predString);
3568+
ReleaseSysCache(tuple);
3569+
3570+
return result;
3571+
}
3572+
35053573
/*
35063574
* get_index_isreplident
35073575
*

src/include/utils/lsyscache.h

+2
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ extern Oid get_range_collation(Oid rangeOid);
195195
extern Oid get_range_multirange(Oid rangeOid);
196196
extern Oid get_multirange_range(Oid multirangeOid);
197197
extern Oid get_index_column_opclass(Oid index_oid, int attno);
198+
extern List *get_index_expressions(Oid index_oid);
199+
extern List *get_index_predicate(Oid index_oid);
198200
extern bool get_index_isreplident(Oid index_oid);
199201
extern bool get_index_isvalid(Oid index_oid);
200202
extern bool get_index_isclustered(Oid index_oid);

src/test/regress/expected/btree_index.out

+19
Original file line numberDiff line numberDiff line change
@@ -434,3 +434,22 @@ ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
434434
ERROR: ALTER action ALTER COLUMN ... SET cannot be performed on relation "btree_part_idx"
435435
DETAIL: This operation is not supported for partitioned indexes.
436436
DROP TABLE btree_part;
437+
-- Test with index expression and predicate that include a parallel unsafe
438+
-- function.
439+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
440+
AS $$
441+
BEGIN
442+
RETURN 0;
443+
EXCEPTION WHEN OTHERS THEN
444+
RETURN 1;
445+
END$$ LANGUAGE plpgsql;
446+
CREATE TABLE btree_para_bld(i int);
447+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
448+
SET max_parallel_maintenance_workers TO 4;
449+
-- With parallel-unsafe expression
450+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
451+
-- With parallel-unsafe predicate
452+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
453+
RESET max_parallel_maintenance_workers;
454+
DROP TABLE btree_para_bld;
455+
DROP FUNCTION para_unsafe_f;

src/test/regress/sql/btree_index.sql

+22
Original file line numberDiff line numberDiff line change
@@ -267,3 +267,25 @@ CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
267267
CREATE INDEX btree_part_idx ON btree_part(id);
268268
ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
269269
DROP TABLE btree_part;
270+
271+
-- Test with index expression and predicate that include a parallel unsafe
272+
-- function.
273+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
274+
AS $$
275+
BEGIN
276+
RETURN 0;
277+
EXCEPTION WHEN OTHERS THEN
278+
RETURN 1;
279+
END$$ LANGUAGE plpgsql;
280+
281+
CREATE TABLE btree_para_bld(i int);
282+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
283+
SET max_parallel_maintenance_workers TO 4;
284+
-- With parallel-unsafe expression
285+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
286+
-- With parallel-unsafe predicate
287+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
288+
289+
RESET max_parallel_maintenance_workers;
290+
DROP TABLE btree_para_bld;
291+
DROP FUNCTION para_unsafe_f;

0 commit comments

Comments
 (0)