Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commitc0e0174

Browse files
committed
Fix parallel-safety check of expressions and predicate for index builds
As coded, the planner logic that calculates the number of parallelworkers to use for a parallel index build uses expressions andpredicates from the relcache, which are flattened for the planner byeval_const_expressions().As reported in the bug, an immutable parallel-unsafe function flattenedin the relcache would become a Const, which would be considered asparallel-safe, even if the predicate or the expressions including thefunction are not safe in parallel workers. Depending on the expressionsor predicate used, this could cause the parallel build to fail.Tests are included that check parallel index builds with parallel-unsafepredicate and expressions. Two routines are added to lsyscache.h to beable to retrieve expressions and predicate of an index from its pg_indexdata.Reported-by: Alexander LakhinAuthor: Tender WangReviewed-by: Jian He, Michael PaquierDiscussion:https://postgr.es/m/CAHewXN=UaAaNn9ruHDH3Os8kxLVmtWqbssnf=dZN_s9=evHUFA@mail.gmail.comBackpatch-through: 12
1 parent9424640 commitc0e0174

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
gotodone;

‎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
returnopclass;
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(Oidindex_oid)
3265+
{
3266+
List*result;
3267+
HeapTupletuple;
3268+
DatumexprDatum;
3269+
boolisnull;
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+
returnNIL;
3282+
}
3283+
3284+
exprString=TextDatumGetCString(exprDatum);
3285+
result= (List*)stringToNode(exprString);
3286+
pfree(exprString);
3287+
ReleaseSysCache(tuple);
3288+
3289+
returnresult;
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(Oidindex_oid)
3299+
{
3300+
List*result;
3301+
HeapTupletuple;
3302+
DatumpredDatum;
3303+
boolisnull;
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+
returnNIL;
3316+
}
3317+
3318+
predString=TextDatumGetCString(predDatum);
3319+
result= (List*)stringToNode(predString);
3320+
pfree(predString);
3321+
ReleaseSysCache(tuple);
3322+
3323+
returnresult;
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
externOidget_range_subtype(OidrangeOid);
183183
externOidget_range_collation(OidrangeOid);
184184
externOidget_index_column_opclass(Oidindex_oid,intattno);
185+
externList*get_index_expressions(Oidindex_oid);
186+
externList*get_index_predicate(Oidindex_oid);
185187
externboolget_index_isreplident(Oidindex_oid);
186188
externboolget_index_isvalid(Oidindex_oid);
187189
externboolget_index_isclustered(Oidindex_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_tableSELECT i,1,2,3FROM generate_series(1,1000) i;
163+
164+
-- Test with index expression and predicate that include a parallel unsafe
165+
-- function.
166+
CREATEFUNCTIONpara_unsafe_f() RETURNSint IMMUTABLE PARALLEL UNSAFE
167+
AS $$
168+
BEGIN
169+
RETURN0;
170+
EXCEPTION WHEN OTHERS THEN
171+
RETURN1;
172+
END$$ LANGUAGE plpgsql;
173+
174+
CREATETABLEbtree_para_bld(iint);
175+
ALTERTABLE btree_para_bldSET (parallel_workers=4);
176+
SET max_parallel_maintenance_workers TO4;
177+
-- With parallel-unsafe expression
178+
CREATEINDEXON btree_para_bld((i+ para_unsafe_f()));
179+
-- With parallel-unsafe predicate
180+
CREATEINDEXON btree_para_bld(i)WHERE i> para_unsafe_f();
181+
182+
RESET max_parallel_maintenance_workers;
183+
DROPTABLE btree_para_bld;
184+
DROPFUNCTION para_unsafe_f;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp