pg.ddx.io  pgsql-bugs@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Tom Lane <tgl@sss.pgh.pa.us>
To: Pedro Gimeno <pgsql-004@personal.formauri.es>
Cc: Sandro Santilli <strk@keybit.net>
Cc: pgsql-bugs@postgresql.org
Subject: Re: uninterruptable regexp_replace in 9.2 and 9.3
Date: Sat, 01 Mar 2014 23:38:38 -0500
Message-ID: <17158.1393735118@sss.pgh.pa.us> (raw)
In-Reply-To: <5310925B.8010809@personal.formauri.es>
References: <20140228112858.GE5011@localhost>
	<5310925B.8010809@personal.formauri.es>
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>

Pedro Gimeno <pgsql-004@personal.formauri.es> writes:
> I'm adding this note in case it helps anyone get a bigger picture as to
> what other implementations do about this problem.

I spent some time looking at pcre to try to understand what it was doing
backtracking-wise, and eventually realized that it's not actually trying
very hard.  Consider this problem:

# select regexp_matches('123456789z', '(([0-9]+|9z)+)');
 regexp_matches  
-----------------
 {123456789z,9z}
(1 row)

To obtain the longest possible match, it's necessary to decide that the
first iteration of the + operator matches '12345678', leaving '9z' to be
matched by the second iteration.  Postgres and Tcl get this right.
Perl and pcre, not so much: they report the match as '123456789'.

$ perl -e "if ('123456789z' =~ m/(([0-9]+|9z)+)/) {print \"\$1\\n\";}"
123456789

In fact, Perl doesn't even get this simplified case right, though no
backtracking is required:

$ perl -e "if ('123456789z' =~ m/(([0-9]|9z)+)/) {print \"\$1\\n\";}"
123456789

It seems to just fail to notice that on the 9th iteration, a longer match
is available from the second OR-alternative than the first.  (No doubt
this is documented behavior somewhere, but it sure flies in the face of
what I'd consider to be expected regex behavior.)

The performance problem we're looking at comes directly from the
backtracking that's done to ensure that we detect a match in case the
pattern has this sort of pathological behavior.  The test case doesn't
actually need any such backtracking, because there aren't multiple ways
to match any particular substring; but I'm not sure if there's any easy
way to recognize that.

			regards, tom lane


-- 
Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org)
To make changes to your subscription:
http://www.postgresql.org/mailpref/pgsql-bugs



view thread (5+ messages)  latest in thread

Message-ID: <17158.1393735118@sss.pgh.pa.us>
Permalink:  ../17158.1393735118@sss.pgh.pa.us/
Also on:    postgresql.org/message-id/17158.1393735118@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-bugs@postgresql.org
  Cc: tgl@sss.pgh.pa.us, pgsql-004@personal.formauri.es, strk@keybit.net
  Subject: Re: uninterruptable regexp_replace in 9.2 and 9.3
  In-Reply-To: <17158.1393735118@sss.pgh.pa.us>
  References: <20140228112858.GE5011@localhost> <5310925B.8010809@personal.formauri.es> <17158.1393735118@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