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

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Chengpeng Yan <chengpeng_yan(at)outlook(dot)com>
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 16:12:48
Message-ID: 1010898.1795536768@sss.pgh.pa.us
Views: Whole Thread | Raw Message | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Chengpeng Yan <chengpeng_yan(at)outlook(dot)com> writes:
>> 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.

> 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.

Right.

> 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).

I don't quite agree with that definition: a length restriction is
not "no constraint". But that's a side point.

Yes, we could distinguish cases where the pattern is like "abc%"
or "^abc", and maybe that's common enough to be worth the trouble,
but I'm not really convinced. I think when we put in the
check-all-the-histogram-entries logic we eliminated pretty much
all reason to work hard on pattern analysis. The fixed-prefix
logic is worth keeping because it allows a preliminary index-based
filter, but I think we should stop with that rather than trying
to do fragmentary pattern analysis.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message David Geier 2026-11-24 16:30:14 Re: Reducing relcache memory usage 2: shrink sizeof(RelationData)
Previous Message Melanie Plageman 2026-11-24 16:10:52 Re: Stop masking visibility information during WAL consistency checking