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

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
Cc: alpkeles99 <alpkeles99(at)gmail(dot)com>, pgsql-bugs <pgsql-bugs(at)lists(dot)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-11-29 12:45:31
Message-ID: tencent_072CEFA84F1AABE063DA3E68412718BED109@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-bugs

Original
>From: Matheus Alcantara <matheusssilv97(at)gmail(dot)com>
>Date: 2026-10-23 21:59
>To: alpkeles99 <alpkeles99(at)gmail(dot)com>, pgsql-bugs <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
>
>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

Hi Matheus

Thanks for the patch.
I reviewed the patch and think both changes are correct and consistent with the existing compatibility check.

1) Consistency with statext_is_compatible_clause()

statext_is_compatible_clause() requires the clause to be a RestrictInfo (with a special case for a bare AND BoolExpr ),
then passes rinfo->clause to statext_is_compatible_clause_internal() . The first thing the internal routine does is look
through a top-level RelabelType before deciding what kind of clause it has.

mcv_get_match_bitmap() now performs the same two unwrapping steps in the same order: unwrap RestrictInfo ,
then unwrap RelabelType . The RelabelType unwrapping is correctly placed after , not inside, the if (IsA(clause, RestrictInfo)) block,
so it also applies to the sub-clauses passed in by the recursive AND / OR / NOT handling, which are not RestrictInfo s.
This mirrors the unconditional top-level unwrapping in statext_is_compatible_clause_internal() .

As a result, "can this statistics object be used for this clause" and the actual MCV evaluation now classify the clause identically.
Before the patch, mcv_get_match_bitmap() received RelabelType(Var) for a domain-over-bool column used as a bare condition,
failed to recognize it as a Var , treated it as a bare boolean expression, and mcv_match_expression() raised "expression not found
in statistics object".

2) getBaseType() in the Var branch
Changing
-Assert(var->vartype == BOOLOID);
+Assert(getBaseType(var->vartype) == BOOLOID);

is the right fix: for a domain over bool the Var's vartype is the domain OID, and getBaseType() unwraps the whole domain chain.
So it also accepts nested domains, e.g.

CREATE DOMAIN mcv_bool_domain_1 AS bool;
CREATE DOMAIN mcv_bool_domain AS mcv_bool_domain_1;

I verified this with the added regression cases (bare b , NOT b , b = true , b = false , and a domain-typed statistics expression
used as a bare condition); the estimates are as expected, including for the nested-domain variant.

The patch applies cleanly and the new regression cases produce the expected output.
I've added it to the next CommitFest so it doesn't get lost.

LGTM.

regards,
--
ZizhuanLiu (X-MAN) 
44973863(at)qq(dot)com

In response to

Browse pgsql-bugs by date

  From Date Subject
Previous Message Tender Wang 2026-11-29 06:16:27 Re: BUG #19804: "variable not found in subplan target list", DISTINCT agg over Gather`