From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-committers@lists.postgresql.org
Subject: pgsql: Avoid determining regexp subexpression matches, when possible.
Date: Mon, 09 Aug 2021 15:26:46 +0000
Message-ID: <E1mD7As-00060G-TA@gemulon.postgresql.org> (raw)
Avoid determining regexp subexpression matches, when possible.
Identifying the precise match locations for parenthesized subexpressions
is a fairly expensive task given the way our regexp engine works, both
at regexp compile time (where we must create an optimized NFA for each
parenthesized subexpression) and at runtime (where determining exact
match locations requires laborious search).
Up to now we've made little attempt to optimize this situation. This
patch identifies cases where we know at compile time that we won't
need to know subexpression match locations, and teaches the regexp
compiler to not bother creating per-subexpression regexps for
parenthesis pairs that are not referenced by backrefs elsewhere in
the regexp. (To preserve semantics, we obviously still have to
pin down the match locations of backref references.) Users could
have obtained the same results before this by being careful to
write "non capturing" parentheses wherever possible, but few people
bother with that.
Discussion: https://postgr.es/m/2219936.1628115334@sss.pgh.pa.us
Branch
------
master
Details
-------
https://git.postgresql.org/pg/commitdiff/0e6aa8747d439bb7f08f95e358f0509c50396785
Modified Files
--------------
contrib/pg_trgm/trgm_regexp.c | 6 +-
src/backend/regex/regc_lex.c | 5 +-
src/backend/regex/regcomp.c | 68 +++++++++++++++++-----
src/backend/regex/regexec.c | 36 ++++++++----
src/backend/utils/adt/jsonpath_gram.y | 8 +++
src/backend/utils/adt/regexp.c | 12 +++-
src/include/regex/regex.h | 2 +-
src/include/regex/regguts.h | 7 ++-
.../modules/test_regex/expected/test_regex.out | 42 +++++++++++--
src/test/modules/test_regex/sql/test_regex.sql | 13 ++++-
10 files changed, 154 insertions(+), 45 deletions(-)
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Reply to all the recipients from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: tgl@sss.pgh.pa.us
Subject: Re: pgsql: Avoid determining regexp subexpression matches, when possible.
In-Reply-To: <E1mD7As-00060G-TA@gemulon.postgresql.org>
References: <E1mD7As-00060G-TA@gemulon.postgresql.org>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox