pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
2+ messages / 2 participants
[nested] [flat]

* BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
@ 2026-10-22 14:03  PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 1 reply; 2+ messages in thread

From: PG Bug reporting form @ 2026-10-22 14:03 UTC (permalink / raw)
  To: pgsql-bugs@lists.postgresql.org; +Cc: alpkeles99@gmail.com

The following bug has been logged on the website:

Bug reference:      19789
Logged by:          Alperen Keles
Email address:      alpkeles99@gmail.com
PostgreSQL version: 18.6
Operating system:   macOS 26.7 arm64
Description:        

Hello,

If a column whose type is a domain over boolean is part of an MCV
extended statistics object, a query that uses that column as a
boolean condition together with another clause on the object's
columns fails in the planner with an internal error:

  CREATE DOMAIN db AS bool;
  CREATE TABLE t (a int8, b db);
  INSERT INTO t VALUES (1, true);
  CREATE STATISTICS t_s (mcv) ON a, b FROM t;
  ANALYZE t;

  SELECT * FROM t WHERE a = 1 AND b;
  ERROR:  expression not found in statistics object

The same error occurs with NOT b, b = true, b = false, b <> false,
b::bool, a = 1 OR b, for EXPLAIN and DELETE, with two columns of the
domain (WHERE b1 AND b2), with a domain over that domain, and with
statistics created without a kind list (so including mcv). It does
not occur with a plain boolean column, with a domain over int4 or
text compared to a constant, with b IS TRUE or b IS NULL, with only
ndistinct or dependencies statistics, or before ANALYZE.

Reproduced on 18.6 (REL_18_STABLE a85b5523e67) and master
158b6d5109f, both with --enable-cassert, on macOS arm64. 13.23 fails
on the same statements with "ERROR:  unknown clause type: 125"
(T_RelabelType). The message changed with e33ae53ddec (the 14.5 fix
for MCV statistics on boolean expressions), but the failure remains.

As far as I can tell, the domain column used as a boolean is a
RelabelType over the Var. statext_is_compatible_clause_internal()
strips the RelabelType and accepts the clause as a plain column
reference, but mcv_get_match_bitmap() gets the unstripped clause.
That clause is not a Var, so it goes to the bare boolean expression
branch, and mcv_match_expression() does not find it among the
statistics object's expressions.

Found with a query fuzzer.

Regards,
Alperen Keles








^ permalink  raw  reply  [nested|flat] 2+ messages in thread

* Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
@ 2026-10-23 13:59  Matheus Alcantara <matheusssilv97@gmail.com>
  parent: PG Bug reporting form <noreply@postgresql.org>
  0 siblings, 0 replies; 2+ messages in thread

From: Matheus Alcantara @ 2026-10-23 13:59 UTC (permalink / raw)
  To: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>

On 22/10/26 11:03, PG Bug reporting form wrote:
> If a column whose type is a domain over boolean is part of an MCV
> extended statistics object, a query that uses that column as a
> boolean condition together with another clause on the object's
> columns fails in the planner with an internal error:
> 
>    CREATE DOMAIN db AS bool;
>    CREATE TABLE t (a int8, b db);
>    INSERT INTO t VALUES (1, true);
>    CREATE STATISTICS t_s (mcv) ON a, b FROM t;
>    ANALYZE t;
> 
>    SELECT * FROM t WHERE a = 1 AND b;
>    ERROR:  expression not found in statistics object
> 
> The same error occurs with NOT b, b = true, b = false, b <> false,
> b::bool, a = 1 OR b, for EXPLAIN and DELETE, with two columns of the
> domain (WHERE b1 AND b2), with a domain over that domain, and with
> statistics created without a kind list (so including mcv). It does
> not occur with a plain boolean column, with a domain over int4 or
> text compared to a constant, with b IS TRUE or b IS NULL, with only
> ndistinct or dependencies statistics, or before ANALYZE.
> 
> Reproduced on 18.6 (REL_18_STABLE a85b5523e67) and master
> 158b6d5109f, both with --enable-cassert, on macOS arm64. 13.23 fails
> on the same statements with "ERROR:  unknown clause type: 125"
> (T_RelabelType). The message changed with e33ae53ddec (the 14.5 fix
> for MCV statistics on boolean expressions), but the failure remains.
> 
> As far as I can tell, the domain column used as a boolean is a
> RelabelType over the Var. statext_is_compatible_clause_internal()
> strips the RelabelType and accepts the clause as a plain column
> reference, but mcv_get_match_bitmap() gets the unstripped clause.
> That clause is not a Var, so it goes to the bare boolean expression
> branch, and mcv_match_expression() does not find it among the
> statistics object's expressions.
> 
> Found with a query fuzzer.
> 

Hi,

Thanks for reporting this issue, I've reproduced on master and also on
14.

I think that making mcv_get_match_bitmap strip RelabelType as
statext_is_compatible_clause_internal is the right direction and the
attached patch do this.

Note that mcv_get_match_bitmap has Assert(var->vartype == BOOLOID) and
for a domain-typed Var, vartype is the domain's OID, so cassert builds
would crash with an assertion failure. I've changed to compare
getBaseType(var->vartype) instead.

--
Matheus Alcantara
EDB: https://www.enterprisedb.com

Attachments:

v1-0001-Fix-MCV-extended-statistics-with-boolean-clauses-.patch (text/plain, 6.0K) download From 7ef184ea40b56592b12a2ae5357d30568db33cf1 Mon Sep 17 00:00:00 2001 From: Matheus Alcantara <mths.dev@pm.me> Date: Fri, 23 Oct 2026 10:51:18 -0300 Subject: [PATCH v1] Fix MCV extended statistics with boolean clauses on domains over bool statext_is_compatible_clause_internal() looks through a top-level RelabelType before deciding what kind of clause it has, so a boolean column whose type is a domain over bool, used as a bare condition (WHERE a = 1 AND b), is accepted as a plain Var reference. mcv_get_match_bitmap() however received the clause with the RelabelType still on top. The clause was not recognized as a Var, so it was treated as a bare boolean expression, and the lookup in mcv_match_expression() failed with "expression not found in statistics object". The same happened for NOT b, b = true, b = false, and for a statistics expression whose type is a domain over bool used as a bare condition. Before e33ae53ddec, the error was "unknown clause type". Fix by looking through the RelabelType in mcv_get_match_bitmap() the same way the compatibility check does, and adjust the assertion in the Var branch to accept a domain over bool. Reported-by: Alperen Keles <alpkeles99@gmail.com> Discussion: https://www.postgresql.org/message-id/19789-b6592eeba8c6f6e1@postgresql.org Backpatch-through: 14 --- src/backend/statistics/mcv.c | 9 ++++- src/test/regress/expected/stats_ext.out | 48 +++++++++++++++++++++++++ src/test/regress/sql/stats_ext.sql | 34 ++++++++++++++++++ 3 files changed, 90 insertions(+), 1 deletion(-) diff --git a/src/backend/statistics/mcv.c b/src/backend/statistics/mcv.c index 10a91c97aeb..bbe7de7c72c 100644 --- a/src/backend/statistics/mcv.c +++ b/src/backend/statistics/mcv.c @@ -1621,6 +1621,13 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, if (IsA(clause, RestrictInfo)) clause = (Node *) ((RestrictInfo *) clause)->clause; + /* + * Look inside any binary-compatible relabeling, as + * statext_is_compatible_clause_internal does. + */ + if (IsA(clause, RelabelType)) + clause = (Node *) ((RelabelType *) clause)->arg; + /* * Handle the various types of clauses - OpClause, NullTest and * AND/OR/NOT @@ -1913,7 +1920,7 @@ mcv_get_match_bitmap(PlannerInfo *root, List *clauses, /* match the attribute to a dimension of the statistic */ int idx = bms_member_index(keys, var->varattno); - Assert(var->vartype == BOOLOID); + Assert(getBaseType(var->vartype) == BOOLOID); /* * Walk through the MCV items and evaluate the current clause. We diff --git a/src/test/regress/expected/stats_ext.out b/src/test/regress/expected/stats_ext.out index 4512670d525..d2756cda485 100644 --- a/src/test/regress/expected/stats_ext.out +++ b/src/test/regress/expected/stats_ext.out @@ -2804,6 +2804,54 @@ SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool WHERE NOT a AND 1 | 0 (1 row) +-- mcv with domains over bool +CREATE DOMAIN mcv_bool_domain AS bool; +CREATE TABLE mcv_lists_bool_domain ( + a mcv_bool_domain, + b mcv_bool_domain, + c INT +) +WITH (autovacuum_enabled = off); +INSERT INTO mcv_lists_bool_domain (a, b, c) + SELECT + (mod(i,2) = 0), (mod(i,4) = 0), mod(i,8) + FROM generate_series(1,10000) s(i); +CREATE STATISTICS mcv_lists_bool_domain_stats (mcv) + ON a, b, c, ((c > 4)::mcv_bool_domain) + FROM mcv_lists_bool_domain; +ANALYZE mcv_lists_bool_domain; +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a AND b AND c = 0'); + estimated | actual +-----------+-------- + 1250 | 1250 +(1 row) + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE NOT a AND b'); + estimated | actual +-----------+-------- + 1 | 0 +(1 row) + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a = true AND b = false'); + estimated | actual +-----------+-------- + 2500 | 2500 +(1 row) + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE (a OR b) AND c = 1'); + estimated | actual +-----------+-------- + 1 | 0 +(1 row) + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a AND (c > 4)::mcv_bool_domain'); + estimated | actual +-----------+-------- + 1250 | 1250 +(1 row) + +DROP TABLE mcv_lists_bool_domain; +DROP DOMAIN mcv_bool_domain; -- mcv covering just a small fraction of data CREATE TABLE mcv_lists_partial ( a INT, diff --git a/src/test/regress/sql/stats_ext.sql b/src/test/regress/sql/stats_ext.sql index a9176e74f3c..8676dbcd284 100644 --- a/src/test/regress/sql/stats_ext.sql +++ b/src/test/regress/sql/stats_ext.sql @@ -1395,6 +1395,40 @@ SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool WHERE NOT a AND SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool WHERE NOT a AND b AND NOT c'); +-- mcv with domains over bool +CREATE DOMAIN mcv_bool_domain AS bool; + +CREATE TABLE mcv_lists_bool_domain ( + a mcv_bool_domain, + b mcv_bool_domain, + c INT +) +WITH (autovacuum_enabled = off); + +INSERT INTO mcv_lists_bool_domain (a, b, c) + SELECT + (mod(i,2) = 0), (mod(i,4) = 0), mod(i,8) + FROM generate_series(1,10000) s(i); + +CREATE STATISTICS mcv_lists_bool_domain_stats (mcv) + ON a, b, c, ((c > 4)::mcv_bool_domain) + FROM mcv_lists_bool_domain; + +ANALYZE mcv_lists_bool_domain; + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a AND b AND c = 0'); + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE NOT a AND b'); + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a = true AND b = false'); + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE (a OR b) AND c = 1'); + +SELECT * FROM check_estimated_rows('SELECT * FROM mcv_lists_bool_domain WHERE a AND (c > 4)::mcv_bool_domain'); + +DROP TABLE mcv_lists_bool_domain; +DROP DOMAIN mcv_bool_domain; + -- mcv covering just a small fraction of data CREATE TABLE mcv_lists_partial ( a INT, -- 2.50.1 (Apple Git-155)
^ permalink raw reply [nested|flat] 2+ messages in thread


end of thread, other threads:[~2026-10-23 13:59 UTC | newest]

Thread overview: 2+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2026-10-22 14:03 BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object PG Bug reporting form <noreply@postgresql.org>
2026-10-23 13:59 ` Matheus Alcantara <matheusssilv97@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox