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

Commit56a8ab2

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 parent0c2dda1 commit56a8ab2

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
@@ -5911,10 +5911,18 @@ plan_create_index_workers(Oid tableOid, Oid indexOid)
59115911
* Currently, parallel workers can't access the leader's temporary tables.
59125912
* Furthermore, any index predicate or index expressions must be parallel
59135913
* safe.
5914+
*
5915+
* Fetch the list of expressions and predicates directly from the
5916+
* catalogs. Retrieving this information from the relcache would cause
5917+
* the expressions and predicates to be flattened, losing properties that
5918+
* can be important to check if parallel workers can be used. For
5919+
* example, immutable parallel-unsafe functions, that cannot be used in
5920+
* parallel workers, would be changed to Const nodes, that are safe in
5921+
* parallel workers.
59145922
*/
59155923
if (heap->rd_rel->relpersistence==RELPERSISTENCE_TEMP||
5916-
!is_parallel_safe(root, (Node*)RelationGetIndexExpressions(index))||
5917-
!is_parallel_safe(root, (Node*)RelationGetIndexPredicate(index)))
5924+
!is_parallel_safe(root, (Node*)get_index_expressions(indexOid))||
5925+
!is_parallel_safe(root, (Node*)get_index_predicate(indexOid)))
59185926
{
59195927
parallel_workers=0;
59205928
gotodone;

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

Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3510,6 +3510,74 @@ get_index_column_opclass(Oid index_oid, int attno)
35103510
returnopclass;
35113511
}
35123512

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

‎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
@@ -341,3 +341,22 @@ CREATE INDEX btree_part_idx ON btree_part(id);
341341
ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
342342
ERROR: "btree_part_idx" is not a table, materialized view, or foreign table
343343
DROP TABLE btree_part;
344+
-- Test with index expression and predicate that include a parallel unsafe
345+
-- function.
346+
CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
347+
AS $$
348+
BEGIN
349+
RETURN 0;
350+
EXCEPTION WHEN OTHERS THEN
351+
RETURN 1;
352+
END$$ LANGUAGE plpgsql;
353+
CREATE TABLE btree_para_bld(i int);
354+
ALTER TABLE btree_para_bld SET (parallel_workers = 4);
355+
SET max_parallel_maintenance_workers TO 4;
356+
-- With parallel-unsafe expression
357+
CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
358+
-- With parallel-unsafe predicate
359+
CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
360+
RESET max_parallel_maintenance_workers;
361+
DROP TABLE btree_para_bld;
362+
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
@@ -183,3 +183,25 @@ CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
183183
CREATEINDEXbtree_part_idxON btree_part(id);
184184
ALTERINDEX btree_part_idx ALTER COLUMN idSET (n_distinct=100);
185185
DROPTABLE btree_part;
186+
187+
-- Test with index expression and predicate that include a parallel unsafe
188+
-- function.
189+
CREATEFUNCTIONpara_unsafe_f() RETURNSint IMMUTABLE PARALLEL UNSAFE
190+
AS $$
191+
BEGIN
192+
RETURN0;
193+
EXCEPTION WHEN OTHERS THEN
194+
RETURN1;
195+
END$$ LANGUAGE plpgsql;
196+
197+
CREATETABLEbtree_para_bld(iint);
198+
ALTERTABLE btree_para_bldSET (parallel_workers=4);
199+
SET max_parallel_maintenance_workers TO4;
200+
-- With parallel-unsafe expression
201+
CREATEINDEXON btree_para_bld((i+ para_unsafe_f()));
202+
-- With parallel-unsafe predicate
203+
CREATEINDEXON btree_para_bld(i)WHERE i> para_unsafe_f();
204+
205+
RESET max_parallel_maintenance_workers;
206+
DROPTABLE btree_para_bld;
207+
DROPFUNCTION para_unsafe_f;

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp