Skip to content

Commit c0e0174

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 9424640 commit c0e0174

File tree

5 files changed

+121
-2
lines changed

5 files changed

+121
-2
lines changed

src/backend/optimizer/plan/planner.c

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6306,10 +6306,18 @@ plan_create_index_workers(Oid tableOid, Oid indexOid)
63066306
* Currently, parallel workers can't access the leader's temporary tables.
63076307
* Furthermore, any index predicate or index expressions must be parallel
63086308
* safe.
6309+
*
6310+
* Fetch the list of expressions and predicates directly from the
6311+
* catalogs. Retrieving this information from the relcache would cause
6312+
* the expressions and predicates to be flattened, losing properties that
6313+
* can be important to check if parallel workers can be used. For
6314+
* example, immutable parallel-unsafe functions, that cannot be used in
6315+
* parallel workers, would be changed to Const nodes, that are safe in
6316+
* parallel workers.
63096317
*/
63106318
if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
6311-
!is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
6312-
!is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
6319+
!is_parallel_safe(root, (Node *) get_index_expressions(indexOid)) ||
6320+
!is_parallel_safe(root, (Node *) get_index_predicate(indexOid)))
63136321
{
63146322
parallel_workers = 0;
63156323
goto done;

src/backend/utils/cache/lsyscache.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3255,6 +3255,74 @@ get_index_column_opclass(Oid index_oid, int attno)
32553255
return opclass;
32563256
}
32573257

3258+
/*
3259+
* get_index_expressions
3260+
*
3261+
* Given the index OID, its a List of its expressions or NIL if none.
3262+
*/
3263+
List *
3264+
get_index_expressions(Oid index_oid)
3265+
{
3266+
List *result;
3267+
HeapTuple tuple;
3268+
Datum exprDatum;
3269+
bool isnull;
3270+
char *exprString;
3271+
3272+
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
3273+
if (!HeapTupleIsValid(tuple))
3274+
elog(ERROR, "cache lookup failed for index %u", index_oid);
3275+
3276+
exprDatum = SysCacheGetAttr(INDEXRELID, tuple,
3277+
Anum_pg_index_indexprs, &isnull);
3278+
if (isnull)
3279+
{
3280+
ReleaseSysCache(tuple);
3281+
return NIL;
3282+
}
3283+
3284+
exprString = TextDatumGetCString(exprDatum);
3285+
result = (List *) stringToNode(exprString);
3286+
pfree(exprString);
3287+
ReleaseSysCache(tuple);
3288+
3289+
return result;
3290+
}
3291+
3292+
/*
3293+
* get_index_predicate
3294+
*
3295+
* Given the index OID, return a List of its predicate or NIL if none.
3296+
*/
3297+
List *
3298+
get_index_predicate(Oid index_oid)
3299+
{
3300+
List *result;
3301+
HeapTuple tuple;
3302+
Datum predDatum;
3303+
bool isnull;
3304+
char *predString;
3305+
3306+
tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid));
3307+
if (!HeapTupleIsValid(tuple))
3308+
elog(ERROR, "cache lookup failed for index %u", index_oid);
3309+
3310+
predDatum = SysCacheGetAttr(INDEXRELID, tuple,
3311+
Anum_pg_index_indpred, &isnull);
3312+
if (isnull)
3313+
{
3314+
ReleaseSysCache(tuple);
3315+
return NIL;
3316+
}
3317+
3318+
predString = TextDatumGetCString(predDatum);
3319+
result = (List *) stringToNode(predString);
3320+
pfree(predString);
3321+
ReleaseSysCache(tuple);
3322+
3323+
return result;
3324+
}
3325+
32583326
/*
32593327
* get_index_isreplident
32603328
*

src/include/utils/lsyscache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,8 @@ extern char *get_namespace_name_or_temp(Oid nspid);
182182
extern Oid get_range_subtype(Oid rangeOid);
183183
extern Oid get_range_collation(Oid rangeOid);
184184
extern Oid get_index_column_opclass(Oid index_oid, int attno);
185+
extern List *get_index_expressions(Oid index_oid);
186+
extern List *get_index_predicate(Oid index_oid);
185187
extern bool get_index_isreplident(Oid index_oid);
186188
extern bool get_index_isvalid(Oid index_oid);
187189
extern bool get_index_isclustered(Oid index_oid);

src/test/regress/expected/btree_index.out

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -330,3 +330,22 @@ VACUUM delete_test_table;
330330
-- The vacuum above should've turned the leaf page into a fast root. We just
331331
-- need to insert some rows to cause the fast root page to split.
332332
INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,1000) i;
333+
-- Test with index expression and predicate that include a parallel unsafe
334+
-- function.
335+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
336+
AS $$
337+
BEGIN
338+
RETURN 0;
339+
EXCEPTION WHEN OTHERS THEN
340+
RETURN 1;
341+
END$$ LANGUAGE plpgsql;
342+
CREATE TABLE btree_para_bld(i int);
343+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
344+
SET max_parallel_maintenance_workers TO 4;
345+
-- With parallel-unsafe expression
346+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
347+
-- With parallel-unsafe predicate
348+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
349+
RESET max_parallel_maintenance_workers;
350+
DROP TABLE btree_para_bld;
351+
DROP FUNCTION para_unsafe_f;

src/test/regress/sql/btree_index.sql

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,3 +160,25 @@ VACUUM delete_test_table;
160160
-- The vacuum above should've turned the leaf page into a fast root. We just
161161
-- need to insert some rows to cause the fast root page to split.
162162
INSERT INTO delete_test_table SELECT i, 1, 2, 3 FROM generate_series(1,1000) i;
163+
164+
-- Test with index expression and predicate that include a parallel unsafe
165+
-- function.
166+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
167+
AS $$
168+
BEGIN
169+
RETURN 0;
170+
EXCEPTION WHEN OTHERS THEN
171+
RETURN 1;
172+
END$$ LANGUAGE plpgsql;
173+
174+
CREATE TABLE btree_para_bld(i int);
175+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
176+
SET max_parallel_maintenance_workers TO 4;
177+
-- With parallel-unsafe expression
178+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
179+
-- With parallel-unsafe predicate
180+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
181+
182+
RESET max_parallel_maintenance_workers;
183+
DROP TABLE btree_para_bld;
184+
DROP FUNCTION para_unsafe_f;

0 commit comments

Comments
 (0)