| From: | ZhangChi <798604270(at)qq(dot)com> |
|---|---|
| To: | 798604270 <798604270(at)qq(dot)com>, pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org> |
| Cc: | 798604270 <798604270(at)qq(dot)com> |
| Subject: | Re: BUG #19977: constraint_exclusion Silently Returns 0 Rows on Subpartitioned Table |
| Date: | 2026-12-04 08:58:46 |
| Message-ID: | tencent_344207967A77E9AF6DE629C9164D8EE30C09@qq.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-bugs |
So sorry, I just found that this is a duplicate of #19943. I will carefully check the reported bug for the next report.
Original
From: PG Bug reporting form <noreply(at)postgresql(dot)org>
Date: 2026-12-04 16:56
To: pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org>
Cc: 798604270 <798604270(at)qq(dot)com>
Subject: BUG #19977: constraint_exclusion Silently Returns 0 Rows on Subpartitioned Table
The following bug has been logged on the website:
Bug reference: 19977
Logged by: Chi Zhang
Email address: 798604270(at)qq(dot)com
PostgreSQL version: 19.0
Operating system: ubuntu 22.04
Description:
Hi,
I found that on PostgreSQL 20devel
(b4cc5e924b7a85f76637227002eb1a4be443d956), under the default
constraint_exclusion = partition setting, querying a 2-level subpartitioned
table silently returns 0 rows instead of 1 row when a parent CHECK (or NOT
NULL) constraint references a VIRTUAL generated column whose generation
expression is overridden by a child partition. (The same wrong result also
reproduces on any single-level partitioned or inherited table with
constraint_exclusion = on.)
This is the test case:
```
CREATE TEMP TABLE gp(
k1 int,
k2 int,
a int,
g int GENERATED ALWAYS AS (a - 10) VIRTUAL
) PARTITION BY RANGE (k1);
CREATE TEMP TABLE p(
k1 int,
k2 int,
a int,
g int GENERATED ALWAYS AS (a - 10) VIRTUAL
) PARTITION BY RANGE (k2);
CREATE TEMP TABLE c(
k1 int,
k2 int,
a int,
g int GENERATED ALWAYS AS (a * 10) VIRTUAL
);
ALTER TABLE gp ATTACH PARTITION p FOR VALUES FROM (0) TO (10);
ALTER TABLE p ATTACH PARTITION c FOR VALUES FROM (0) TO (10);
ALTER TABLE gp ADD CONSTRAINT ck CHECK (g > 0);
-- Valid row: in c, g = a * 10 = 50 > 0, so CHECK (g > 0) passes.
-- When queried via gp, gp's expression evaluates g = a - 10 = -5 < 0.
INSERT INTO c(k1, k2, a) VALUES (1, 1, 5);
-- Reference (constraint_exclusion = off): returns 1 row (1, 1, 5, -5)
SET constraint_exclusion = off;
SELECT k1, k2, a, g FROM gp WHERE g < 0;
-- Default (constraint_exclusion = partition): BUG -> silently returns 0
rows
RESET constraint_exclusion;
SELECT k1, k2, a, g FROM gp WHERE g < 0;
```
In get_relation_constraints()
(src/backend/optimizer/util/plancat.c:1639-1642), CHECK and NOT NULL
constraints on an intermediate partitioned table (p) are expanded via
expand_generated_columns_in_expr() using p's own virtual column expression
(a - 10 > 0). Because child partitions (c) are allowed to override a VIRTUAL
generated column's expression (a * 10 > 0), p's expanded constraint does not
hold for rows in c. Combining the false constraint a - 10 > 0 with the query
predicate a - 10 < 0 causes constraint exclusion to prove a spurious
contradiction (One-Time Filter: false) and prune p (and its child c).
For comparison, clauses.c:5172 explicitly skips expanding virtual generated
columns on partitioned/inherited relations for this reason, whereas
get_relation_constraints() does not.
Best,
Chi Zhang
| From | Date | Subject | |
|---|---|---|---|
| Next Message | Michael Paquier | 2026-12-04 10:15:08 | Re: BUG #19953: pg_stat_io exposes impossible rows for io worker backends |
| Previous Message | Gyorgy Bolyki | 2026-12-04 08:52:10 | Re: Logical replication: replicated TRUNCATE crash-loops a subscriber with a foreign partition lacking TRUNCATE support |