Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPg5e-0004DL-Im for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 04:27:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hPg5c-0001n2-Ch for pgsql-bugs@arkaria.postgresql.org; Sun, 12 May 2019 04:27:56 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPg5c-0001mu-19 for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 04:27:56 +0000 Received: from lungold.riddles.org.uk ([82.68.208.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hPg5Y-0003pd-U3 for pgsql-bugs@lists.postgresql.org; Sun, 12 May 2019 04:27:54 +0000 Received: from [192.168.127.1] (port=41855 helo=caithnard.riddles.org.uk) by lungold.riddles.org.uk with esmtps (TLSv1.2:ECDHE-RSA-AES256-GCM-SHA384:256) (Exim 4.88 (FreeBSD)) (envelope-from ) id 1hPg5V-000LhC-55; Sun, 12 May 2019 04:27:49 +0000 Received: from [127.0.0.1] (port=43450 helo=caithnard.riddles.org.uk) by caithnard.riddles.org.uk with esmtp (Exim 4.89 (FreeBSD)) (envelope-from ) id 1hPg5U-0008OX-GE; Sun, 12 May 2019 04:27:48 +0000 From: Andrew Gierth To: Tom Lane Cc: "Daniel Verite" , "Robert Schreiber" , pgsql-bugs@lists.postgresql.org Subject: Re: PostgreSQL 9.3.5 substring(text from pattern for escape) bug In-Reply-To: <27602.1557633043@sss.pgh.pa.us> (Tom Lane's message of "Sat, 11 May 2019 23:50:43 -0400") Message-ID: <87imuggwi4.fsf@news-spur.riddles.org.uk> References: <785708a0-a64c-42ee-9173-f596c1fc92ec@manitou-mail.org> <32565.1557590361@sss.pgh.pa.us> <87mujsgz14.fsf@news-spur.riddles.org.uk> <27602.1557633043@sss.pgh.pa.us> User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/25.1 (berkeley-unix) Date: Sun, 12 May 2019 05:27:48 +0100 MIME-Version: 1.0 Content-Type: text/plain List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk >>>>> "Tom" == Tom Lane writes: > Andrew Gierth writes: >> I looked up the spec on this point. As far as I can see, we're not >> following it, but neither does the spec do what the OP wanted; in >> fact the result should have included the _leading_ Q as well as the >> trailing one. Tom> Huh, interesting. So we should be translating the initial Tom> substring to a non-greedy pattern. I believe Spencer's engine can Tom> handle that by sticking (?:...){1,1}? around it. Tom> Come to think of it, we probably need to be putting (?:...) around Tom> the trailing substring as well. I suspect what we're doing today Tom> produces non-spec results if "|" appears in the trailing part. Digging into it more: SUBSTRING(x FROM 'expr' FOR 'escape') is from sql92/sql99 and is gone by sql2008, replaced by SUBSTRING(x SIMILAR 'expr' ESCAPE 'escape'). sql99 defines the matching rule using different language, but with the same actual effect (requiring shortest matches for the leading and trailing strings). Your suggested fix doesn't seem to work. If the leading/trailing substrings do not have | or parens in then it seems to work to wrap them in (?:(?:)??...), thanks to the rule that the first quantified atom in a subexpression sets the whole subexpression's greediness, but handling | or parens correctly seems harder. Are there any other dbs that implement this feature that we can compare against? -- Andrew (irc:RhodiumToad)