Re: examine_variable ignored CollateExpr

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: shihao zhong <zhong950419(at)gmail(dot)com>
Cc: jian he <jian(dot)universality(at)gmail(dot)com>, pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, tgl <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Guo <rguo(at)postgresql(dot)org>
Subject: Re: examine_variable ignored CollateExpr
Date: 2026-12-04 08:34:08
Message-ID: tencent_78F559A327FC5F1B1C2DD6C4F5379B651906@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi Shihao, Jian, hackers,

> A domain over text or varchar still misses the statistics in WHERE.
> ...
> This estimates 100 rows for 200 actual.

Thanks for the additional example. I reproduced it with v8. The attached
v9 fixes the domain case, which now estimates 200 rows.

The cause is similar to the varchar case: operator resolution exposes the
domain's base type, while the statistics expression retains the domain.
For your domain dt over text, after planner simplification the expressions
are:

query: RelabelType(text, ci) -> Var(a, dt, "C")
stats: RelabelType(dt, ci) -> Var(a, dt, "C")

v8's exposed-type comparison does not recognize this match. v9 instead
uses getBaseType() to remove any domain layers before applying the existing
type-compatibility check. Both expressions must still have a top-level
RelabelType, the same collation OID, and equal() arguments. Their ultimate
base types must be identical or the text/varchar pair. This also handles
domains over varchar(n) and nested domains, and works in the reverse
direction when a GROUP BY expression retains a domain type but the
statistics expression exposes the base type.

Reading an existing domain value as its base type does not change the
value. RelabelType itself performs neither domain-constraint checks nor
length checks. Any actual checks or conversions remain in the underlying
expressions and must match. The text/varchar exception remains limited to
their common comparison semantics under the same collation; binary
compatibility alone is still insufficient for pairs such as int4/oid.
There is no recursive replacement of types inside the expressions.

The ICU regression cases now cover domains over text, varchar, and
varchar(10), nested domains, and the reverse domain GROUP BY case. The
WHERE cases estimate 200 rows instead of 100; the reverse GROUP BY case
estimates 32 groups instead of the column statistics' 52. The existing
varchar and reverse text/varchar cases remain covered. To reduce repeated
setup, these cases share a table, with expression statistics grouped into
two objects for the text and domain columns.

The initial lookup still checks index expressions for exact matches, then
extended-statistics expressions for exact or compatible matches. Only if
that lookup finds no statistics does it use column statistics for a
relabeled simple Var, or try the recorded fallback candidates otherwise.
Index collation alignment, candidate order, and the restrictions on
uniqueness inference remain unchanged from v8.

The historical context is commit 044c99bc567a, "Use query collation, not
column's collation, while examining statistics" [3]. It established that
comparison operators invoked for selectivity estimation must use the
query's collation. That determines how statistics values are compared,
but does not make statistics collected under different collations
interchangeable. The present patch addresses which statistics are
selected, while retaining the existing estimator collation checks.
Reuse across collations in the fallback remains approximate.

I also reduced repeated work in the lookup code. The stripped-expression
comparison is shared between compatible matching and candidate recording.
The input's base type is resolved only when needed and cached for later
candidates. Statistics loading uses helpers shared by the initial and
fallback lookups. Bare Vars retain their fast path; fallback visits the
recorded candidates rather than rescanning all expressions. Planning-time
overhead has not been benchmarked for this revision.

As in v8, this is a single combined patch containing the PHV/relabel
normalization posted separately as CF 7730 [1, 2], including its join.sql
tests. That component was posted separately to make it easier to review
and has not yet received sufficient review; it still needs review as part
of this combined patch.

The six relevant regression tests passed with the debug/assert build:
test_setup, create_misc, create_index, join, stats_ext, and
collate.icu.utf8. pgindent --check and git diff --check also pass.

[1] https://commitfest.postgresql.org/patch/7730/
[2] https://www.postgresql.org/message-id/flat/tencent_F3747F5B5B4250CF3542C1C7B5A9D0BE4F0A(at)qq(dot)com
[3] https://www.postgresql.org/message-id/E1jhIoI-0002Vp-VW@gemulon.postgresql.org

src/backend/utils/adt/selfuncs.c | 589 ++++++++++++++----
.../regress/expected/collate.icu.utf8.out | 201 ++++++
src/test/regress/expected/join.out | 85 +++
src/test/regress/expected/stats_ext.out | 231 +++++++
src/test/regress/sql/collate.icu.utf8.sql | 139 +++++
src/test/regress/sql/join.sql | 71 +++
src/test/regress/sql/stats_ext.sql | 170 +++++
7 files changed, 1348 insertions(+), 138 deletions(-)

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

Attachment Content-Type Size
v9-0001-Prefer-exact-expression-statistics-before-stripping-top-level-relabels.patch application/octet-stream 70.0 KB

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Chao Li 2026-12-04 08:38:44 Fix the OID cutoff in the type_sanity check for unique catalog text indexes
Previous Message Hayato Kuroda (Fujitsu) 2026-12-04 08:29:31 RE: Add aborted_txns and aborted_bytes to pg_stat_replication_slots