From: Mark Dilger <mark.dilger@enterprisedb.com>
To: Tom Lane <tgl@sss.pgh.pa.us>
Cc: Markhof, Ingolf <ingolf.markhof@de.verizon.com>
Cc: pgsql-general@postgresql.org
Subject: Re: [E] Regexp_replace bug / does not terminate on long strings
Date: Fri, 20 Aug 2021 12:32:26 -0700
Message-ID: <D84B8669-286F-4F90-89D6-6566D93C2C08@enterprisedb.com> (raw)
In-Reply-To: <1917678.1629478364@sss.pgh.pa.us>
References: <CALZg0g4FA1Xc5UMLrGBKM--erUGEAhe8GGLE-YcN7=O6rw2=0A@mail.gmail.com>
<1809528.1629412955@sss.pgh.pa.us>
<CALZg0g4T5u+9DUomsuN913MK_TS1GZM=8pMuJR+j=-0yiQdJrQ@mail.gmail.com>
<1917678.1629478364@sss.pgh.pa.us>
> On Aug 20, 2021, at 9:52 AM, Tom Lane <tgl@sss.pgh.pa.us> wrote:
>
> "a*" is easy. "(a*)\1" is less easy --- if you let the a* consume the
> whole string, you will not get a match, even though one is possible.
> In general, backrefs create a mess in what would otherwise be a pretty
> straightforward concept :-(.
The following queries take radically different time to run:
\timing
select regexp_replace(
repeat('someone,one,one,one,one,one,one,', 60),
'(?<=^|,)([^,]+)(?:,\1)+(?=$|,)',
'\1', -- replacement
'g' -- apply globally (all matches)
);
Time: 16476.529 ms (00:16.477)
select regexp_replace(
repeat('someone,one,one,one,one,one,one,', 60),
'(?<=^|,)([^,]+)(?:,\1){5}(?=$|,)',
'\1', -- replacement
'g' -- apply globally (all matches)
);
Time: 1.452 ms
The only difference in the patterns is the + vs. the {5}. It looks to me like the first pattern should greedily match five ",one" matches and be forced to stop since ",someone" doesn't match, and the second pattern should grab the five ",one" matches it was told to grab and not try to grab the ",someone", but other than that, they should be performing the same work. I don't see why the performance should be so different.
—
Mark Dilger
EnterpriseDB: http://www.enterprisedb.com
The Enterprise PostgreSQL Company
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-general@postgresql.org
Cc: mark.dilger@enterprisedb.com, tgl@sss.pgh.pa.us, ingolf.markhof@de.verizon.com
Subject: Re: [E] Regexp_replace bug / does not terminate on long strings
In-Reply-To: <D84B8669-286F-4F90-89D6-6566D93C2C08@enterprisedb.com>
References: <CALZg0g4FA1Xc5UMLrGBKM--erUGEAhe8GGLE-YcN7=O6rw2=0A@mail.gmail.com> <1809528.1629412955@sss.pgh.pa.us> <CALZg0g4T5u+9DUomsuN913MK_TS1GZM=8pMuJR+j=-0yiQdJrQ@mail.gmail.com> <1917678.1629478364@sss.pgh.pa.us> <D84B8669-286F-4F90-89D6-6566D93C2C08@enterprisedb.com>
* 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