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

Commit4ec8f77

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 parent8870c54 commit4ec8f77

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
@@ -6695,10 +6695,18 @@ plan_create_index_workers(Oid tableOid, Oid indexOid)
66956695
* Currently, parallel workers can't access the leader's temporary tables.
66966696
* Furthermore, any index predicate or index expressions must be parallel
66976697
* safe.
6698+
*
6699+
* Fetch the list of expressions and predicates directly from the
6700+
* catalogs. Retrieving this information from the relcache would cause
6701+
* the expressions and predicates to be flattened, losing properties that
6702+
* can be important to check if parallel workers can be used. For
6703+
* example, immutable parallel-unsafe functions, that cannot be used in
6704+
* parallel workers, would be changed to Const nodes, that are safe in
6705+
* parallel workers.
66986706
*/
66996707
if (heap->rd_rel->relpersistence==RELPERSISTENCE_TEMP||
6700-
!is_parallel_safe(root, (Node*)RelationGetIndexExpressions(index))||
6701-
!is_parallel_safe(root, (Node*)RelationGetIndexPredicate(index)))
6708+
!is_parallel_safe(root, (Node*)get_index_expressions(indexOid))||
6709+
!is_parallel_safe(root, (Node*)get_index_predicate(indexOid)))
67026710
{
67036711
parallel_workers=0;
67046712
gotodone;

‎src/backend/utils/cache/lsyscache.c

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3504,6 +3504,74 @@ get_index_column_opclass(Oid index_oid, int attno)
35043504
returnopclass;
35053505
}
35063506

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

‎src/include/utils/lsyscache.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -195,6 +195,8 @@ extern Oidget_range_collation(Oid rangeOid);
195195
externOidget_range_multirange(OidrangeOid);
196196
externOidget_multirange_range(OidmultirangeOid);
197197
externOidget_index_column_opclass(Oidindex_oid,intattno);
198+
externList*get_index_expressions(Oidindex_oid);
199+
externList*get_index_predicate(Oidindex_oid);
198200
externboolget_index_isreplident(Oidindex_oid);
199201
externboolget_index_isvalid(Oidindex_oid);
200202
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
@@ -387,3 +387,22 @@ ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
387387
ERROR: ALTER action ALTER COLUMN ... SET cannot be performed on relation "btree_part_idx"
388388
DETAIL: This operation is not supported for partitioned indexes.
389389
DROP TABLE btree_part;
390+
-- Test with index expression and predicate that include a parallel unsafe
391+
-- function.
392+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
393+
AS $$
394+
BEGIN
395+
RETURN 0;
396+
EXCEPTION WHEN OTHERS THEN
397+
RETURN 1;
398+
END$$ LANGUAGE plpgsql;
399+
CREATE TABLE btree_para_bld(i int);
400+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
401+
SET max_parallel_maintenance_workers TO 4;
402+
-- With parallel-unsafe expression
403+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
404+
-- With parallel-unsafe predicate
405+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
406+
RESET max_parallel_maintenance_workers;
407+
DROP TABLE btree_para_bld;
408+
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
@@ -242,3 +242,25 @@ CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
242242
CREATEINDEXbtree_part_idxON btree_part(id);
243243
ALTERINDEX btree_part_idx ALTER COLUMN idSET (n_distinct=100);
244244
DROPTABLE btree_part;
245+
246+
-- Test with index expression and predicate that include a parallel unsafe
247+
-- function.
248+
CREATEFUNCTIONpara_unsafe_f() RETURNSint IMMUTABLE PARALLEL UNSAFE
249+
AS $$
250+
BEGIN
251+
RETURN0;
252+
EXCEPTION WHEN OTHERS THEN
253+
RETURN1;
254+
END$$ LANGUAGE plpgsql;
255+
256+
CREATETABLEbtree_para_bld(iint);
257+
ALTERTABLE btree_para_bldSET (parallel_workers=4);
258+
SET max_parallel_maintenance_workers TO4;
259+
-- With parallel-unsafe expression
260+
CREATEINDEXON btree_para_bld((i+ para_unsafe_f()));
261+
-- With parallel-unsafe predicate
262+
CREATEINDEXON btree_para_bld(i)WHERE i> para_unsafe_f();
263+
264+
RESET max_parallel_maintenance_workers;
265+
DROPTABLE btree_para_bld;
266+
DROPFUNCTION para_unsafe_f;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp