From b60d71c202cde344e0b447c2bb301addf95fa3b5 Mon Sep 17 00:00:00 2001 From: Michael Paquier Date: Wed, 6 Mar 2024 17:24:12 +0900 Subject: 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 --- src/backend/utils/cache/lsyscache.c | 68 +++++++++++++++++++++++++++++++++++++ 1 file changed, 68 insertions(+) (limited to 'src/backend/utils/cache/lsyscache.c') diff --git a/src/backend/utils/cache/lsyscache.c b/src/backend/utils/cache/lsyscache.c index 3303c31e575..fad425c5f1a 100644 --- a/src/backend/utils/cache/lsyscache.c +++ b/src/backend/utils/cache/lsyscache.c @@ -3352,6 +3352,74 @@ get_index_column_opclass(Oid index_oid, int attno) return opclass; } +/* + * get_index_expressions + * + * Given the index OID, its a List of its expressions or NIL if none. + */ +List * +get_index_expressions(Oid index_oid) +{ + List *result; + HeapTuple tuple; + Datum exprDatum; + bool isnull; + char *exprString; + + tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid)); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for index %u", index_oid); + + exprDatum = SysCacheGetAttr(INDEXRELID, tuple, + Anum_pg_index_indexprs, &isnull); + if (isnull) + { + ReleaseSysCache(tuple); + return NIL; + } + + exprString = TextDatumGetCString(exprDatum); + result = (List *) stringToNode(exprString); + pfree(exprString); + ReleaseSysCache(tuple); + + return result; +} + +/* + * get_index_predicate + * + * Given the index OID, return a List of its predicate or NIL if none. + */ +List * +get_index_predicate(Oid index_oid) +{ + List *result; + HeapTuple tuple; + Datum predDatum; + bool isnull; + char *predString; + + tuple = SearchSysCache1(INDEXRELID, ObjectIdGetDatum(index_oid)); + if (!HeapTupleIsValid(tuple)) + elog(ERROR, "cache lookup failed for index %u", index_oid); + + predDatum = SysCacheGetAttr(INDEXRELID, tuple, + Anum_pg_index_indpred, &isnull); + if (isnull) + { + ReleaseSysCache(tuple); + return NIL; + } + + predString = TextDatumGetCString(predDatum); + result = (List *) stringToNode(predString); + pfree(predString); + ReleaseSysCache(tuple); + + return result; +} + /* * get_index_isreplident * -- cgit v1.2.3