From: Matheus Alcantara <matheusssilv97@gmail.com>
To: alpkeles99@gmail.com, pgsql-bugs@lists.postgresql.org, PG Bug reporting form <noreply@postgresql.org>
Subject: Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
Date: Fri, 23 Oct 2026 10:59:23 -0300
Message-ID: <239132c7-b67a-40b9-89c2-250a65e37c0a@gmail.com> (raw)
In-Reply-To: <19789-b6592eeba8c6f6e1@postgresql.org>
References: <19789-b6592eeba8c6f6e1@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.comAttachments: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.cindex 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.outindex 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.sqlindex 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)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-bugs@postgresql.org
Cc: matheusssilv97@gmail.com, noreply@postgresql.org
Subject: Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
In-Reply-To: <239132c7-b67a-40b9-89c2-250a65e37c0a@gmail.com>
References: <19789-b6592eeba8c6f6e1@postgresql.org> <239132c7-b67a-40b9-89c2-250a65e37c0a@gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
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