Re: [PATCH] Fix encoding-dependent LIKE/regex selectivity estimates

From: Chengpeng Yan <chengpeng_yan(at)outlook(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: PostgreSQL Hackers <pgsql-hackers(at)lists(dot)postgresql(dot)org>, Jeff Davis <pgsql(at)j-davis(dot)com>
Subject: Re: [PATCH] Fix encoding-dependent LIKE/regex selectivity estimates
Date: 2026-11-24 13:06:04
Message-ID: 5807BA68-E8E5-42DF-B40F-1B68EC457A30@outlook.com
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Hi,

> On Nov 21, 2026, at 04:06, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>
> It is nearly the same logic, but I'm not sure you can easily use that
> in combination with the fixed-prefix logic in patternsel_common,
> which we should certainly not give up. I'd be inclined to approach
> this by just replacing "rest_selec" with a constant and removing all
> the subsidiary code that calculates that.
>
> regards, tom lane

For LIKE, ILIKE and the regex matching operators the fixed prefix is real
information that a generic estimate cannot use, and the current code also
has to produce an estimate for the negated forms, which that function has
no notion of. So, agreeing with your suggestion, I would simplify
patternsel_common() in place rather than route these operators through it.

What I would change is the single line that produces the heuristic value:

heursel = prefixsel * rest_selec;

rest_selec comes from the per-byte model, so both the complexity and the
encoding dependence come from that line alone.

Following your suggestion it becomes a constant:

heursel = prefixsel * C;

but multiplying by it has two problems. The constant stands for "no
information", so the product pretends to know how likely the remainder is
once the prefix has matched; and because the prefix estimate is below 1, a
selective prefix makes the product shrink toward the clamp floor, where it
stops distinguishing anything. A different constant for the two cases
would avoid the shrink, but then the same unknown remainder would be
charged different amounts with no basis in the pattern.

So I would rather use the prefix as the answer where it is all we need,
and otherwise only bound the constant by it:

heursel = rest_free ? prefixsel : min(C, prefixsel);

rest_free is true when the pattern constrains nothing beyond the prefix:
a LIKE remainder made only of % and _, or a regex the engine reduces to a
length test (MATCHALL). The length such a pattern implies is not charged,
since we have no statistics about length, which overestimates narrow length
tests. That is the common case ('abc%', '^abc'), and there the prefix
estimate is the answer. Everything else, with or without a prefix, is
charged the same constant, capped by the prefix estimate, since a pattern
cannot match more rows than its prefix. That is the same shape
generic_restriction_selectivity() already uses: a fixed default stands in
for the part the histogram cannot speak for, and what I propose adds the
prefix only as an upper bound.

For the constant I would use DEFAULT_MATCH_SEL (0.005), the value these
estimators already return when they know nothing.

Any thoughts?

--
Best regards,
Chengpeng Yan

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Keyerror Smart 2026-11-24 13:16:07 Re: [PATCH] Consider window functions when reordering GROUP BY items
Previous Message Andrei Lepikhov 2026-11-24 13:04:52 Re: new clang warnings about unused global variables