pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Markhof, Ingolf <ingolf.markhof@de.verizon.com>
Cc: pgsql-general@postgresql.org
Subject: Re: Regexp_replace bug / does not terminate on long strings
Date: Thu, 19 Aug 2021 18:42:35 -0400
Message-ID: <1809528.1629412955@sss.pgh.pa.us> (raw)
In-Reply-To: <CALZg0g4FA1Xc5UMLrGBKM--erUGEAhe8GGLE-YcN7=O6rw2=0A@mail.gmail.com>
References: <CALZg0g4FA1Xc5UMLrGBKM--erUGEAhe8GGLE-YcN7=O6rw2=0A@mail.gmail.com>

"Markhof, Ingolf" <ingolf.markhof@de.verizon.com> writes:
> BRIEF:
> regexp_replace(source,pattern,replacement,flags) needs very (!) long to
> complete or does not complete at all (?!) for big input strings (a few k
> characters). (Oracle SQL completes the same in a few ms)

Regexps containing backrefs are inherently hard --- every engine has
strengths and weaknesses.  I doubt it'd be hard to find cases where
our engine is orders of magnitude faster than Oracle's; but you've
hit on a case where the opposite is true.

The core of the problem is that it's hard to tell how much of the
string could be matched by the (,\1)* subpattern.  In principle, *all*
of the remaining string could be, if it were N repetitions of the
initial word.  Or it could be N-1 repetitions followed by one other
word, and so on.  The difficulty is that since our engine guarantees
to find the longest feasible match, it tries these options from
longest to shortest.  Usually the actual match (if any) will be pretty
short, so that you have O(N) wasted work per word, making the runtime
at least O(N^2).

I think your best bet is to not try to eliminate multiple duplicates
at a time.  Get rid of one dup at a time, say by
     str := regexp_replace(str, '([^,]+)(,\1)?($|,)', '\1\3', 'g');
and repeat till the string doesn't get any shorter.

I did come across a performance bug [1] while poking at this, but
alas fixing it doesn't move the needle very much for this example.

			regards, tom lane

[1] https://www.postgresql.org/message-id/1808998.1629412269%40sss.pgh.pa.us





view thread (14+ messages)  latest in thread

Message-ID: <1809528.1629412955@sss.pgh.pa.us>
Permalink:  ../1809528.1629412955@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/1809528.1629412955@sss.pgh.pa.us

 · 

reply

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: tgl@sss.pgh.pa.us, ingolf.markhof@de.verizon.com
  Subject: Re: Regexp_replace bug / does not terminate on long strings
  In-Reply-To: <1809528.1629412955@sss.pgh.pa.us>
  References: <CALZg0g4FA1Xc5UMLrGBKM--erUGEAhe8GGLE-YcN7=O6rw2=0A@mail.gmail.com> <1809528.1629412955@sss.pgh.pa.us>

* 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