| 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-01 09:27:11 |
| Message-ID: | tencent_0C4C7885B61E5AA0898D6F24CB156AFE4607@qq.com |
| Views: | Whole Thread | Raw Message | Download mbox | Resend email |
| Thread: | |
| Lists: | pgsql-hackers |
Hi Shihao,
> GROUP BY a COLLATE ci uses the statistics. This query still estimates
> 100 rows for 200 actual, same as master.
Thanks for the report. I reproduced this with v7. The attached v8 fixes
your WHERE example, which now estimates 200 rows.
The difference arises during operator resolution. There is no built-in
=(varchar, varchar), so the WHERE clause selects =(text, text) and
implicitly converts the left operand to text. The statistics expression
has no such operator context and retains varchar. After simplification,
the expressions differ only in the top-level relabel's result type:
query: RelabelType(text, ci) -> Var(a, varchar, "C")
stats: RelabelType(varchar, ci) -> Var(a, varchar, "C")
The ordinary equal() comparison fails. In v7, stripping the query's
relabel then exposes a simple Var, so fallback uses the column statistics
collected under "C". Those statistics keep 'A' and 'a' separate, giving
100 rows for either value, whereas ci treats them as equal and the query
returns 200. GROUP BY does not require this conversion to text, so it
already matches the varchar expression statistics.
After the ordinary equal() check, v8 allows an extended-statistics match
when both expressions have a top-level RelabelType, their collation OIDs
are equal, their result types are identical or the text/varchar pair, and
the expressions beneath the relabels compare equal().
The text/varchar exception is deliberately narrow. Relabeling existing
values between these types preserves their representation, equality, and
ordering under the same collation. Binary compatibility alone would not
justify this for other pairs, such as text/bpchar or int4/oid. This does
not recursively replace types inside the expression.
The same reasoning covers varchar(n): the relabels may also differ in
typmod, but a RelabelType does not enforce a length limit. Any actual
truncation or length-checking function remains in the underlying
expressions and must compare equal as well.
I added three cases to collate.icu.utf8.sql and its expected output:
* Your varchar WHERE case: 200 estimated rows instead of 100.
* The same case with varchar(10): also 200 estimated rows.
* The reverse direction, with statistics on a text column and
GROUP BY (a::varchar) COLLATE case_insensitive: 32 estimated groups
instead of the column statistics' 52.
The lookup loops also reuse the stripped-expression comparison when
recording fallback candidates. The text/varchar adjustment is confined
to extended expression statistics; the index matching and fallback
policies otherwise remain as in v7.
As in v7, 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 split out 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.
Planning-time overhead has not been benchmarked for this revision.
regards,
--
ZizhuanLiu (X-MAN)
44973863(at)qq(dot)com
| Attachment | Content-Type | Size |
|---|---|---|
| v8-0001-Prefer-exact-expression-statistics-before-stripping-top-level-relabels.patch | application/octet-stream | 64.6 KB |
| From | Date | Subject | |
|---|---|---|---|
| Previous Message | Yuefei Shi | 2026-12-01 09:21:47 | Re: [PATCH] Handle SQL NULL results from casts in JSON generation |