BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
Hackorum builds and tests every patch posted to the lists, not only commitfest submissions. This is Hackorum's own CI rather than the PostgreSQL project's, and it is still under testing - please report anything that looks wrong.
You can run a PostgreSQL built from this patch straight from Docker, with no checkout and no build:
docker run --rm -p 5432:5432 ghcr.io/hackorum-dev/postgres-patch:t254346psql -h localhost -U postgresBuilt from patchset v2 (message #2), October 24, 2026 at 02:11 PM.
Every patchset is also pushed to a branch of our PostgreSQL fork, so you can check out the same tree CI built. Without a PostgreSQL checkout:
git clone --branch t254346_2 https://github.com/hackorum-dev/postgres.gitIn a checkout you already have, add the fork once:
git remote add hackorum https://github.com/hackorum-dev/postgres.gitthen, for this patchset and every later one:
git fetch hackorum t254346_2 && git checkout t254346_2Patchset v2 (message #2) is on t254346_2
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
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 objectThe 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