[PATCH] Add planner support function to two-argument regexp_like()
Thread notes
1 note#performance#moderate#enhancement
hackorum-bot#performance#moderate#enhancement
@global #performance #moderate #enhancement
Hi hackers,
The ~ operator gets index conditions from a fixed pattern prefix, via
textregexeq_support(). regexp_like(), which is the same test, has no
support function and so misses out.
With no flags the two are identical, and like_regex_support() already
handles a FuncExpr, so the patch is just a prosupport entry plus a test.
1M rows, text column, btree index, C locale:
before: Seq Scan 255.094 ms
after: Index Only Scan, Index Cond on prefix 0.010 ms
I left the three-argument form alone: with 'n', '^' matches after a
newline, so a prefix range would miss rows. Handling flags needs its
own support function.
make check passes. Needs a catversion bump.
Regards,
Shihao
The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not tested
I have checked that make installcheck-world does not report any issue: the new test case works as expected.
There is no change in SQL so I did not test SQL compliance. I don't think that documentation should be changed as this is an optimization change only.
Average execution time of make installcheck-world with patch is 748s.
Average execution time of make installcheck-world without patch is 749,6 s.
This patch review looks easy because there is little code change . However it's for more difficult to understand the context if you don't know what a planned support function is (that was my case).
I think this patch should be reviewed by a more experienced contributor with query planner skills.
On Thu, Aug 13, 2026 at 10:24 AM Pierre Forstmann
<pierre.forstmann@gmail.com> wrote:
The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not testedI have checked that make installcheck-world does not report any issue: the new test case works as expected.
There is no change in SQL so I did not test SQL compliance. I don't think that documentation should be changed as this is an optimization change only.
Average execution time of make installcheck-world with patch is 748s.
Average execution time of make installcheck-world without patch is 749,6 s.
This patch review looks easy because there is little code change . However it's for more difficult to understand the context if you don't know what a planned support function is (that was my case).
I think this patch should be reviewed by a more experienced contributor with query planner skills.
Hi Pierre,
Thanks for reviewing my patch! Let me briefly explain how this works.
Postgres uses "planner support functions" to help the database
optimize queries. The ~ operator already has one, but the two-argument
regexp_like() function did not, even though they do the exact same
thing.
Here is why this support function allows for index scans: Standard
B-tree indexes cannot search regular expressions directly. However, if
a regex starts with a fixed prefix (like ^item999), the support
function extracts that prefix and translates it into a simple range
condition (like >= 'item999' and < 'item99:'). This allows Postgres to
quickly search the index instead of scanning the entire table.
My patch simply links regexp_like() to this existing support function.
Now, regexp_like() can also extract these fixed prefixes to trigger
fast index scans. The test cases I added just prove that the index is
correctly being used when we do this.
I hope this makes the context clearer!
Thanks,
Shihao
Import Notes
Reply to msg id not found: 178663100991.1096.14061735930335908485.pgcf@coridan.postgresql.org
The following review has been posted through the commitfest application:
make installcheck-world: tested, passed
Implements feature: tested, passed
Spec compliant: not tested
Documentation: not tested
As already said patch looks good to me.
I set status Ready For Committer.
The new status of this patch is: Ready for Committer
On Wed, Jul 29, 2026 at 12:11 PM shihao zhong <zhong950419@gmail.com> wrote:
Hi hackers,
The ~ operator gets index conditions from a fixed pattern prefix, via
textregexeq_support(). regexp_like(), which is the same test, has no
support function and so misses out.With no flags the two are identical,
regexp_like_no_flags is the same as textregexeq,
therefore let the textregexeq support function also used in
regexp_like_no_flags is OK, i think.
we already have coverage for textregexeq_support,
https://coverage.postgresql.org/src/backend/utils/adt/like_support.c.gcov.html.
I tend to think V1 regression tests are unnecessary, but that's a minor point.
Hi Jian,
Thanks for reviewing my patch.
we already have coverage for textregexeq_support,
I tend to think V1 regression tests are unnecessary, but that's a minor
point.
Agreed, v2 keeps one EXPLAIN that shows the Index Cond and drops the rest
of the tests.
Thanks,
Shihao
On Fri, Oct 9, 2026 at 12:51 PM shihao zhong <zhong950419@gmail.com> wrote:
Rebase this patch with master
This patch introduces a row-count estimation regression when expression
index statistics are available for regexp_like(). For example:
-------------------------------------------------
CREATE TABLE t AS
SELECT CASE WHEN i <= 8000 THEN 'abc' ELSE 'xyz' END AS s,
'^a'::text AS p
FROM generate_series(1, 10000) AS g(i);
CREATE INDEX ON t (regexp_like(s, p));
ANALYZE t;
EXPLAIN (ANALYZE, TIMING OFF)
SELECT * FROM t WHERE regexp_like(s, p);
-------------------------------------------------
With the patch, the estimate drops from 8,000 rows to 50, while
8,000 rows actually match. The support function returns the default
selectivity of 0.005 instead of allowing boolvarsel() to use the
expression index statistics.
Other functions, such as starts_with(), already have the same issue.
But, this patch introduces the problem for regexp_like(), so I think
we should address it together?
Regards,
--
Fujii Masao
Hi Fujii-san,
Thanks, good catch.
0001 in v4 makes starts_with() and the LIKE and regex functions use
statistics on the call. 0002 is the regexp_like() patch on top. Your
example now estimates 8000 rows. Operators are unchanged. Both are
master only.
Thanks,
Shihao