Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object

From: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
To: alpkeles99(at)gmail(dot)com, pgsql-bugs(at)lists(dot)postgresql(dot)org, PG Bug reporting form <noreply(at)postgresql(dot)org>
Subject: Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
Date: 2026-10-23 13:59:23
Message-ID: 239132c7-b67a-40b9-89c2-250a65e37c0a@gmail.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

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

Attachment Content-Type Size
v1-0001-Fix-MCV-extended-statistics-with-boolean-clauses-.patch text/plain 6.0 KB

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Fujii Masao 2026-10-23 13:45:42 Re: START_REPLICATION silently truncates an overlong LSN component