From: Tom Lane <tgl@sss.pgh.pa.us>
To: pgsql-bugs@postgreSQL.org
Subject: pg_regprefix() doesn't handle lookahead constraints correctly
Date: Mon, 19 Oct 2015 09:44:55 -0700
Message-ID: <1218.1445273095@sss.pgh.pa.us> (raw)
List-Unsubscribe: <mailto:majordomo@postgresql.org?body=unsub%20pgsql-bugs>
Whilst poking around the topic of lookbehind constraints, I realized
that pg_regprefix(), which is code we added to extract any fixed prefix
that a regex might have, is already broken for lookahead constraints.
Observe:
regression=# create table pp (f1 text unique);
CREATE TABLE
regression=# explain select * from pp where f1 ~ '^abcde(f|(?=\d))';
QUERY PLAN
------------------------------------------------------------------------
Bitmap Heap Scan on pp (cost=4.22..14.37 rows=7 width=32)
Filter: (f1 ~ '^abcde(f|(?=\d))'::text)
-> Bitmap Index Scan on pp_f1_key (cost=0.00..4.22 rows=7 width=0)
Index Cond: ((f1 >= 'abcdef'::text) AND (f1 < 'abcdeg'::text))
(4 rows)
It thinks that "f" is part of the fixed prefix of the regex, whereas
in point of fact the regex will match strings that have a digit there.
The reason is that pg_regprefix thinks it can ignore LACON arcs
altogether, whereas really what it had better do is treat them as
a reason to give up searching.
In principle we could traverse across the LACON arc (which consumes no
input) and look to see if the following state has a single outarc color.
But that would greatly complicate the code and I doubt it corresponds to
any case that's of real-world interest. That is, it'd allow us to
recognize that, say, '^abcde(?=\w)f' has a fixed prefix of 'abcdef' not
just 'abcde', but who writes regexes like that? Most likely, a lookahead
constraint is at the end of the regex anyway. (Similarly, it won't be
very interesting to optimize for lookbehind constraints here, because
nobody would write such a thing in a fixed-prefix regex.)
This is such a corner case that it's not too surprising we've had no
field reports about it. But still, a bug is a bug.
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
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
Subject: Re: pg_regprefix() doesn't handle lookahead constraints correctly
In-Reply-To: <1218.1445273095@sss.pgh.pa.us>
References: <1218.1445273095@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