BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
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>
Дата:
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
Re: BUG #19789: "expression not found in statistics object" for a boolean domain column in an MCV statistics object
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