1
0
mirror of https://github.com/postgres/postgres.git synced 2025-12-19 17:02:53 +03:00

Fix const-simplification for constraints and stats

Constraint expressions and statistics expressions loaded from the
system catalogs need to be run through const-simplification, because
the planner will be comparing them to similarly-processed qual
clauses.  Without this step, the planner may fail to detect valid
matches.

Currently, NullTest clauses in these expressions may not be reduced
correctly during const-simplification.  This happens because their Var
nodes do not yet have the correct varno when eval_const_expressions is
applied.  Since eval_const_expressions relies on varno to reduce
NullTest quals, incorrect varno can cause problems.

Additionally, for statistics expressions, eval_const_expressions is
called with root set to NULL, which also inhibits NullTest reduction.

This patch fixes the issue by ensuring that Vars are updated to have
the correct varno before const-simplification, and that a valid root
is passed to eval_const_expressions when needed.

Author: Richard Guo <guofenglinux@gmail.com>
Discussion: https://postgr.es/m/19007-4cc6e252ed8aa54a@postgresql.org
This commit is contained in:
Richard Guo
2025-08-31 08:59:48 +09:00
parent 0c6d572c11
commit 317c117d6d
3 changed files with 75 additions and 18 deletions

View File

@@ -409,3 +409,30 @@ SELECT * FROM pred_tab t1
DROP TABLE pred_tab;
DROP TABLE pred_tab_notnull;
-- Validate that NullTest quals in constraint expressions are reduced correctly
CREATE TABLE pred_tab1 (a int NOT NULL, b int,
CONSTRAINT check_tab1 CHECK (a IS NULL OR b > 2));
CREATE TABLE pred_tab2 (a int, b int,
CONSTRAINT check_a CHECK (a IS NOT NULL));
SET constraint_exclusion TO ON;
-- Ensure that we get a dummy plan
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab1, pred_tab2 WHERE pred_tab2.a IS NULL;
QUERY PLAN
--------------------------
Result
One-Time Filter: false
(2 rows)
-- Ensure that we get a dummy plan
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab2, pred_tab1 WHERE pred_tab1.a IS NULL OR pred_tab1.b < 2;
QUERY PLAN
--------------------------
Result
One-Time Filter: false
(2 rows)
RESET constraint_exclusion;
DROP TABLE pred_tab1;
DROP TABLE pred_tab2;

View File

@@ -201,3 +201,23 @@ SELECT * FROM pred_tab t1
DROP TABLE pred_tab;
DROP TABLE pred_tab_notnull;
-- Validate that NullTest quals in constraint expressions are reduced correctly
CREATE TABLE pred_tab1 (a int NOT NULL, b int,
CONSTRAINT check_tab1 CHECK (a IS NULL OR b > 2));
CREATE TABLE pred_tab2 (a int, b int,
CONSTRAINT check_a CHECK (a IS NOT NULL));
SET constraint_exclusion TO ON;
-- Ensure that we get a dummy plan
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab1, pred_tab2 WHERE pred_tab2.a IS NULL;
-- Ensure that we get a dummy plan
EXPLAIN (COSTS OFF)
SELECT * FROM pred_tab2, pred_tab1 WHERE pred_tab1.a IS NULL OR pred_tab1.b < 2;
RESET constraint_exclusion;
DROP TABLE pred_tab1;
DROP TABLE pred_tab2;