Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1ZoDYU-0006sB-Oj for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Oct 2015 16:45:02 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1ZoDYU-0000Kb-1C for pgsql-bugs@arkaria.postgresql.org; Mon, 19 Oct 2015 16:45:02 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZoDYT-0000K9-66 for pgsql-bugs@postgreSQL.org; Mon, 19 Oct 2015 16:45:01 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1ZoDYQ-0000Vm-Ny for pgsql-bugs@postgreSQL.org; Mon, 19 Oct 2015 16:45:00 +0000 Received: from pro.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id t9JGiuE2005410 for ; Mon, 19 Oct 2015 12:44:56 -0400 From: Tom Lane 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> X-Pg-Spam-Score: -1.9 (-) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org 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