Normalize RelabelTypes after stripping PlaceHolderVars

From: ZizhuanLiu X-MAN <44973863(at)qq(dot)com>
To: pgsql-hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>
Cc: guofenglinux <guofenglinux(at)gmail(dot)com>
Subject: Normalize RelabelTypes after stripping PlaceHolderVars
Date: 2026-11-26 16:03:28
Message-ID: tencent_F3747F5B5B4250CF3542C1C7B5A9D0BE4F0A@qq.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

Attached is a patch to normalize RelabelTypes exposed by PlaceHolderVar
removal during statistics lookup in examine_variable().

Commit 7e9f852a79fe [1] added deep PHV stripping and a loop to remove
top-level RelabelTypes. Removing PHVs can also expose adjacent relabels
below the expression root. For example, the attached test produces:

lower(RT_C(PHV(RT_POSIX(E))))
-> lower(RT_C(RT_POSIX(E))))
-> lower(E)

Here E is coalesce(left(a, 1), '') with collation "C". The subquery
applies collation "POSIX", while the outer query applies "C" inside
lower()'s argument. The second step strips the PHV; the last step
normalizes the relabels. Both relabels can be removed because E already
has the outer relabel's type, typmod, and collation. Without this
normalization, the top-level stripping loop cannot reach them, so
equal() fails to match the expression to index or extended-statistics
expressions already simplified by eval_const_expressions().

The patch normalizes adjacent and redundant relabels during the existing
recursive PHV-removal walk, after processing each node's children. It
discards inner relabels and retains the outer one unless its type,
typmod, and collation match the final argument. The redundancy check
also applies when there is only one relabel.

The existing mutator copies expression nodes, so the normalization
changes only the expression used for statistics lookup.

The tests in join.sql first show an estimate of 1000 groups for the
actual 10 before creating the expression index. After creating the
index and running ANALYZE, the estimate is still 1000 without the fix
and becomes 10 with it. The same comparison is covered for extended
expression statistics. A further case checks that a required outer
collation is retained. The tests use built-in collations and do not
require ICU.

I also checked a version that normalizes only at the expression root;
it still estimates 1000 groups in all three cases with expression
statistics. This confirms that the tests exercise the deeper relabels.

The updated join regression test passes in an assertion-enabled debug
build. The stats_ext regression test also passed with the same code.

Comments welcome.

[1] https://git.postgresql.org/gitweb/?p=postgresql.git;a=commitdiff;h=7e9f852a79fe19d4d0f18aabc32a620797fb676e

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

Attachment Content-Type Size
v1-0001-Normalize-RelabelTypes-after-stripping-PlaceHolderVars.patch application/octet-stream 11.7 KB

Browse pgsql-hackers by date

  From Date Subject
Next Message Ayoub Kazar 2026-11-26 16:04:37 Re: Simplify the uniqueness proof for self-join elimination
Previous Message vignesh C 2026-11-26 15:35:11 Re: Proposal: Conflict log history table for Logical Replication