Fix parallel-safety check of expressions and predicate for index builds
authorMichael Paquier <[email protected]>
Wed, 6 Mar 2024 08:23:56 +0000 (17:23 +0900)
committerMichael Paquier <[email protected]>
Wed, 6 Mar 2024 08:23:56 +0000 (17:23 +0900)
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/optimizer/plan/planner.c
src/backend/utils/cache/lsyscache.c
src/include/utils/lsyscache.h
src/test/regress/expected/btree_index.out
src/test/regress/sql/btree_index.sql

index 443ab08d75ab57bfd8849d43112bf926fabfbb2b..d1552fb5eab68519acede72a0f25746b271e30a9 100644 (file)
@@ -6667,10 +6667,18 @@ plan_create_index_workers(Oid tableOid, Oid indexOid)
     * Currently, parallel workers can't access the leader's temporary tables.
     * Furthermore, any index predicate or index expressions must be parallel
     * safe.
+    *
+    * Fetch the list of expressions and predicates directly from the
+    * catalogs.  Retrieving this information from the relcache would cause
+    * the expressions and predicates to be flattened, losing properties that
+    * can be important to check if parallel workers can be used.  For
+    * example, immutable parallel-unsafe functions, that cannot be used in
+    * parallel workers, would be changed to Const nodes, that are safe in
+    * parallel workers.
     */
    if (heap->rd_rel->relpersistence == RELPERSISTENCE_TEMP ||
-       !is_parallel_safe(root, (Node *) RelationGetIndexExpressions(index)) ||
-       !is_parallel_safe(root, (Node *) RelationGetIndexPredicate(index)))
+       !is_parallel_safe(root, (Node *) get_index_expressions(indexOid)) ||
+       !is_parallel_safe(root, (Node *) get_index_predicate(indexOid)))
    {
        parallel_workers = 0;
        goto done;
index 6418d1c6ebade9db22ed92e99f3d2b5b8ad96257..3fbae06de9b970f64db60fba7c8d64d69fb7afa2 100644 (file)
@@ -3502,6 +3502,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
  *
index e4a200b00ec9c15030cdadee53c25a74dacb79bf..62a0d576f5758447c27fcb37794de1790c344e64 100644 (file)
@@ -195,6 +195,8 @@ extern Oid  get_range_collation(Oid rangeOid);
 extern Oid get_range_multirange(Oid rangeOid);
 extern Oid get_multirange_range(Oid multirangeOid);
 extern Oid get_index_column_opclass(Oid index_oid, int attno);
+extern List *get_index_expressions(Oid index_oid);
+extern List *get_index_predicate(Oid index_oid);
 extern bool get_index_isreplident(Oid index_oid);
 extern bool get_index_isvalid(Oid index_oid);
 extern bool get_index_isclustered(Oid index_oid);
index 8311a03c3df984d51dfc47d3d14adf8c09aacf42..ca4ef0d72c6c26cf0c67f79bb2a7e8c3b01c7eb8 100644 (file)
@@ -434,3 +434,22 @@ ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
 ERROR:  ALTER action ALTER COLUMN ... SET cannot be performed on relation "btree_part_idx"
 DETAIL:  This operation is not supported for partitioned indexes.
 DROP TABLE btree_part;
+-- Test with index expression and predicate that include a parallel unsafe
+-- function.
+CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
+AS $$
+BEGIN
+    RETURN 0;
+EXCEPTION WHEN OTHERS THEN
+    RETURN 1;
+END$$ LANGUAGE plpgsql;
+CREATE TABLE btree_para_bld(i int);
+ALTER TABLE btree_para_bld SET (parallel_workers = 4);
+SET max_parallel_maintenance_workers TO 4;
+-- With parallel-unsafe expression
+CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
+-- With parallel-unsafe predicate
+CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
+RESET max_parallel_maintenance_workers;
+DROP TABLE btree_para_bld;
+DROP FUNCTION para_unsafe_f;
index ef8435423472d6ff4100a0d73ad1e21d75fbd113..a916e660c34048bbddfa9df822e6328665df9391 100644 (file)
@@ -267,3 +267,25 @@ CREATE TABLE btree_part (id int4) PARTITION BY RANGE (id);
 CREATE INDEX btree_part_idx ON btree_part(id);
 ALTER INDEX btree_part_idx ALTER COLUMN id SET (n_distinct=100);
 DROP TABLE btree_part;
+
+-- Test with index expression and predicate that include a parallel unsafe
+-- function.
+CREATE FUNCTION para_unsafe_f() RETURNS int IMMUTABLE PARALLEL UNSAFE
+AS $$
+BEGIN
+    RETURN 0;
+EXCEPTION WHEN OTHERS THEN
+    RETURN 1;
+END$$ LANGUAGE plpgsql;
+
+CREATE TABLE btree_para_bld(i int);
+ALTER TABLE btree_para_bld SET (parallel_workers = 4);
+SET max_parallel_maintenance_workers TO 4;
+-- With parallel-unsafe expression
+CREATE INDEX ON btree_para_bld((i + para_unsafe_f()));
+-- With parallel-unsafe predicate
+CREATE INDEX ON btree_para_bld(i) WHERE i > para_unsafe_f();
+
+RESET max_parallel_maintenance_workers;
+DROP TABLE btree_para_bld;
+DROP FUNCTION para_unsafe_f;