diff options
| author | Michael Paquier | 2024-03-06 08:23:56 +0000 |
|---|---|---|
| committer | Michael Paquier | 2024-03-06 08:23:56 +0000 |
| commit | eae7be600be715b2f393b018fc4b98c5b89296da (patch) | |
| tree | aa09d2d410e98bd4be1b0235e9649fa0ef5af62a /src/test | |
| parent | 3e76a806cbb0f8a6cf3ba7a3b371643fa1e0fe96 (diff) | |
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
Diffstat (limited to 'src/test')
| -rw-r--r-- | src/test/regress/expected/btree_index.out | 19 | ||||
| -rw-r--r-- | src/test/regress/sql/btree_index.sql | 22 |
2 files changed, 41 insertions, 0 deletions
diff --git a/src/test/regress/expected/btree_index.out b/src/test/regress/expected/btree_index.out index 8311a03c3df..ca4ef0d72c6 100644 --- a/src/test/regress/expected/btree_index.out +++ b/src/test/regress/expected/btree_index.out @@ -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; diff --git a/src/test/regress/sql/btree_index.sql b/src/test/regress/sql/btree_index.sql index ef843542347..a916e660c34 100644 --- a/src/test/regress/sql/btree_index.sql +++ b/src/test/regress/sql/btree_index.sql @@ -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; |
