Thread: BUG #4525: substring with this pattern works in 8.3.1; does not work in 8.3.4

BUG #4525: substring with this pattern works in 8.3.1; does not work in 8.3.4

From
"chris wood"
Date:
The following bug has been logged online:

Bug reference:      4525
Logged by:          chris wood
Email address:      chrisj.wood@sympatico.ca
PostgreSQL version: 8.3.4
Operating system:   Debian
Description:        substring with this pattern works in 8.3.1; does not
work in 8.3.4
Details:

I apologize in advance for not testing  on 8.3.5,  but that would be very
difficult for me.

I e-mailed this same problem from chris.wood@bookitnow.ca and it got
blocked

here is where it works in 8.3.1:
protocalte=> select version() ;
                                        version
----------------------------------------------------------------------------
------------
 PostgreSQL 8.3.1 on i486-pc-linux-gnu, compiled by GCC cc (GCC) 4.2.3
(Debian 4.2.3-2)
(1 row)

protocalte=> CREATE TABLE locn (
    locn_key integer NOT NULL,
    public_phone text NOT NULL,
    CONSTRAINT public_phone_ch CHECK (((public_phone = ''::text) OR
("substring"(public_phone, '^[0-9]{10}(,[0-9]{10})*$'::text) IS NOT NULL)))
);
CREATE TABLE
protocalte=> insert into locn values(10, '1231231234') ;
INSERT 0 1

and here is where it does not work in 8.3.4:
postgresbugs=# select version() ;
                                          version
----------------------------------------------------------------------------
----------------
 PostgreSQL 8.3.4 on i486-pc-linux-gnu, compiled by GCC gcc-4.3.real (Debian
4.3.2-1) 4.3.2
(1 row)

postgresbugs=# CREATE TABLE locn (
    locn_key integer NOT NULL,
    public_phone text NOT NULL,
    CONSTRAINT public_phone_ch CHECK (((public_phone = ''::text) OR
("substring"(public_phone, '^[0-9]{10}(,[0-9]{10})*$'::text) IS NOT NULL)))
);
CREATE TABLE
postgresbugs=# insert into locn values(10, '1231231234') ;
ERROR:  new row for relation "locn" violates check constraint
"public_phone_ch"

Re: BUG #4525: substring with this pattern works in 8.3.1; does not work in 8.3.4

From
hubert depesz lubaczewski
Date:
On Wed, Nov 12, 2008 at 06:45:27PM +0000, chris wood wrote:
> I apologize in advance for not testing  on 8.3.5,  but that would be very
> difficult for me.
> I e-mailed this same problem from chris.wood@bookitnow.ca and it got
> blocked

http://www.postgresql.org/docs/current/interactive/release-8-3-2.html
...
Fix a corner case in regular-expression substring matching
(substring(string from pattern)) (Tom)

The problem occurs when there is a match to the pattern overall but the
user has specified a parenthesized subexpression and that subexpression
hasn't got a match. An example is substring('foo' from 'foo(bar)?').
This should return NULL, since (bar) isn't matched, but it was
mistakenly returning the whole-pattern match instead (ie, foo).
...

so basically - it is that the code that worked before is considered to
be bad.

how to fix it:
1. change the regexpin substring to:
'^[0-9]{10}(?:,[0-9]{10})*$'

2. change check to:
CHECK ((public_phone ~ '^([0-9]{10}(,[0-9]{10})*)?$')));

choose whichever you prefer.

Best regards,

depesz

--
Linkedin: http://www.linkedin.com/in/depesz  /  blog: http://www.depesz.com/
jid/gtalk: depesz@depesz.com / aim:depeszhdl / skype:depesz_hdl / gg:6749007