Re: BUG #19977: constraint_exclusion Silently Returns 0 Rows on Subpartitioned Table

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&gt;
Date: 2026-12-04 16:56
To: pgsql-bugs <pgsql-bugs(at)lists(dot)postgresql(dot)org&gt;
Cc: 798604270 <798604270(at)qq(dot)com&gt;
Subject: BUG #19977: constraint_exclusion Silently Returns 0 Rows on Subpartitioned Table

The&nbsp;following&nbsp;bug&nbsp;has&nbsp;been&nbsp;logged&nbsp;on&nbsp;the&nbsp;website:

Bug&nbsp;reference:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;19977
Logged&nbsp;by:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Chi&nbsp;Zhang
Email&nbsp;address:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;798604270(at)qq(dot)com
PostgreSQL&nbsp;version:&nbsp;19.0
Operating&nbsp;system:&nbsp;&nbsp;&nbsp;ubuntu&nbsp;22.04
Description:&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;

Hi,

I&nbsp;found&nbsp;that&nbsp;on&nbsp;&nbsp;PostgreSQL&nbsp;20devel
(b4cc5e924b7a85f76637227002eb1a4be443d956),&nbsp;under&nbsp;the&nbsp;default
constraint_exclusion&nbsp;=&nbsp;partition&nbsp;setting,&nbsp;querying&nbsp;a&nbsp;2-level&nbsp;subpartitioned
table&nbsp;silently&nbsp;returns&nbsp;0&nbsp;rows&nbsp;instead&nbsp;of&nbsp;1&nbsp;row&nbsp;when&nbsp;a&nbsp;parent&nbsp;CHECK&nbsp;(or&nbsp;NOT
NULL)&nbsp;constraint&nbsp;references&nbsp;a&nbsp;VIRTUAL&nbsp;generated&nbsp;column&nbsp;whose&nbsp;generation
expression&nbsp;is&nbsp;overridden&nbsp;by&nbsp;a&nbsp;child&nbsp;partition.&nbsp;(The&nbsp;same&nbsp;wrong&nbsp;result&nbsp;also
reproduces&nbsp;on&nbsp;any&nbsp;single-level&nbsp;partitioned&nbsp;or&nbsp;inherited&nbsp;table&nbsp;with
constraint_exclusion&nbsp;=&nbsp;on.)

This&nbsp;is&nbsp;the&nbsp;test&nbsp;case:

```
CREATE&nbsp;TEMP&nbsp;TABLE&nbsp;gp(
&nbsp;&nbsp;k1&nbsp;int,
&nbsp;&nbsp;k2&nbsp;int,
&nbsp;&nbsp;a&nbsp;&nbsp;int,
&nbsp;&nbsp;g&nbsp;&nbsp;int&nbsp;GENERATED&nbsp;ALWAYS&nbsp;AS&nbsp;(a&nbsp;-&nbsp;10)&nbsp;VIRTUAL
)&nbsp;PARTITION&nbsp;BY&nbsp;RANGE&nbsp;(k1);

CREATE&nbsp;TEMP&nbsp;TABLE&nbsp;p(
&nbsp;&nbsp;k1&nbsp;int,
&nbsp;&nbsp;k2&nbsp;int,
&nbsp;&nbsp;a&nbsp;&nbsp;int,
&nbsp;&nbsp;g&nbsp;&nbsp;int&nbsp;GENERATED&nbsp;ALWAYS&nbsp;AS&nbsp;(a&nbsp;-&nbsp;10)&nbsp;VIRTUAL
)&nbsp;PARTITION&nbsp;BY&nbsp;RANGE&nbsp;(k2);

CREATE&nbsp;TEMP&nbsp;TABLE&nbsp;c(
&nbsp;&nbsp;k1&nbsp;int,
&nbsp;&nbsp;k2&nbsp;int,
&nbsp;&nbsp;a&nbsp;&nbsp;int,
&nbsp;&nbsp;g&nbsp;&nbsp;int&nbsp;GENERATED&nbsp;ALWAYS&nbsp;AS&nbsp;(a&nbsp;*&nbsp;10)&nbsp;VIRTUAL
);

ALTER&nbsp;TABLE&nbsp;gp&nbsp;ATTACH&nbsp;PARTITION&nbsp;p&nbsp;FOR&nbsp;VALUES&nbsp;FROM&nbsp;(0)&nbsp;TO&nbsp;(10);
ALTER&nbsp;TABLE&nbsp;p&nbsp;&nbsp;ATTACH&nbsp;PARTITION&nbsp;c&nbsp;FOR&nbsp;VALUES&nbsp;FROM&nbsp;(0)&nbsp;TO&nbsp;(10);
ALTER&nbsp;TABLE&nbsp;gp&nbsp;ADD&nbsp;CONSTRAINT&nbsp;ck&nbsp;CHECK&nbsp;(g&nbsp;&gt;&nbsp;0);

--&nbsp;Valid&nbsp;row:&nbsp;in&nbsp;c,&nbsp;g&nbsp;=&nbsp;a&nbsp;*&nbsp;10&nbsp;=&nbsp;50&nbsp;&gt;&nbsp;0,&nbsp;so&nbsp;CHECK&nbsp;(g&nbsp;&gt;&nbsp;0)&nbsp;passes.
--&nbsp;When&nbsp;queried&nbsp;via&nbsp;gp,&nbsp;gp's&nbsp;expression&nbsp;evaluates&nbsp;g&nbsp;=&nbsp;a&nbsp;-&nbsp;10&nbsp;=&nbsp;-5&nbsp;<&nbsp;0.
INSERT&nbsp;INTO&nbsp;c(k1,&nbsp;k2,&nbsp;a)&nbsp;VALUES&nbsp;(1,&nbsp;1,&nbsp;5);

--&nbsp;Reference&nbsp;(constraint_exclusion&nbsp;=&nbsp;off):&nbsp;returns&nbsp;1&nbsp;row&nbsp;(1,&nbsp;1,&nbsp;5,&nbsp;-5)
SET&nbsp;constraint_exclusion&nbsp;=&nbsp;off;
SELECT&nbsp;k1,&nbsp;k2,&nbsp;a,&nbsp;g&nbsp;FROM&nbsp;gp&nbsp;WHERE&nbsp;g&nbsp;<&nbsp;0;

--&nbsp;Default&nbsp;(constraint_exclusion&nbsp;=&nbsp;partition):&nbsp;BUG&nbsp;-&gt;&nbsp;silently&nbsp;returns&nbsp;0
rows
RESET&nbsp;constraint_exclusion;
SELECT&nbsp;k1,&nbsp;k2,&nbsp;a,&nbsp;g&nbsp;FROM&nbsp;gp&nbsp;WHERE&nbsp;g&nbsp;<&nbsp;0;
```

In&nbsp;get_relation_constraints()
(src/backend/optimizer/util/plancat.c:1639-1642),&nbsp;CHECK&nbsp;and&nbsp;NOT&nbsp;NULL
constraints&nbsp;on&nbsp;an&nbsp;intermediate&nbsp;partitioned&nbsp;table&nbsp;(p)&nbsp;are&nbsp;expanded&nbsp;via
expand_generated_columns_in_expr()&nbsp;using&nbsp;p's&nbsp;own&nbsp;virtual&nbsp;column&nbsp;expression
(a&nbsp;-&nbsp;10&nbsp;&gt;&nbsp;0).&nbsp;Because&nbsp;child&nbsp;partitions&nbsp;(c)&nbsp;are&nbsp;allowed&nbsp;to&nbsp;override&nbsp;a&nbsp;VIRTUAL
generated&nbsp;column's&nbsp;expression&nbsp;(a&nbsp;*&nbsp;10&nbsp;&gt;&nbsp;0),&nbsp;p's&nbsp;expanded&nbsp;constraint&nbsp;does&nbsp;not
hold&nbsp;for&nbsp;rows&nbsp;in&nbsp;c.&nbsp;Combining&nbsp;the&nbsp;false&nbsp;constraint&nbsp;a&nbsp;-&nbsp;10&nbsp;&gt;&nbsp;0&nbsp;with&nbsp;the&nbsp;query
predicate&nbsp;a&nbsp;-&nbsp;10&nbsp;<&nbsp;0&nbsp;causes&nbsp;constraint&nbsp;exclusion&nbsp;to&nbsp;prove&nbsp;a&nbsp;spurious
contradiction&nbsp;(One-Time&nbsp;Filter:&nbsp;false)&nbsp;and&nbsp;prune&nbsp;p&nbsp;(and&nbsp;its&nbsp;child&nbsp;c).

For&nbsp;comparison,&nbsp;clauses.c:5172&nbsp;explicitly&nbsp;skips&nbsp;expanding&nbsp;virtual&nbsp;generated
columns&nbsp;on&nbsp;partitioned/inherited&nbsp;relations&nbsp;for&nbsp;this&nbsp;reason,&nbsp;whereas
get_relation_constraints()&nbsp;does&nbsp;not.

Best,
Chi&nbsp;Zhang

Browse pgsql-bugs by date

  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