Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n4LKH-00018I-8L for pgsql-bugs@arkaria.postgresql.org; Mon, 03 Jan 2022 11:16:29 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1n4LKF-0002Gl-QI for pgsql-bugs@arkaria.postgresql.org; Mon, 03 Jan 2022 11:16:27 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n4LKF-0002Ga-IF for pgsql-bugs@lists.postgresql.org; Mon, 03 Jan 2022 11:16:27 +0000 Received: from relay2-d.mail.gandi.net ([217.70.183.194]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n4LKC-0001du-Le for pgsql-bugs@lists.postgresql.org; Mon, 03 Jan 2022 11:16:27 +0000 Received: (Authenticated sender: vik@postgresfriends.org) by relay2-d.mail.gandi.net (Postfix) with ESMTPSA id 07C4D40004; Mon, 3 Jan 2022 11:16:21 +0000 (UTC) Subject: Re: BUG #17352: Column CHECK regex error To: salamsp@gmail.com, pgsql-bugs@lists.postgresql.org References: <17352-7564a4d9c118056d@postgresql.org> From: Vik Fearing Message-ID: <7b75d6b6-f44e-6380-19cf-35736666e24b@postgresfriends.org> Date: Mon, 3 Jan 2022 12:16:21 +0100 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.14.0 MIME-Version: 1.0 In-Reply-To: <17352-7564a4d9c118056d@postgresql.org> Content-Type: text/plain; charset=utf-8 Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 1/3/22 11:38 AM, PG Bug reporting form wrote: > The following bug has been logged on the website: > > Bug reference: 17352 > Logged by: Serge Salamatin > Email address: salamsp@gmail.com > PostgreSQL version: 13.5 > Operating system: Ubuntu 13.5-2.pgdg20.04+1 > Description: > > select '01' ~ '^[\d-]{2,8}$' -> true, > > but in next > > CREATE TABLE IF NOT EXISTS public.mnu > ( > mnunm character(10) COLLATE pg_catalog."default" NOT NULL, > code character(8) COLLATE pg_catalog."default" NOT NULL > CONSTRAINT "Only digits" CHECK (code ~ '^[\d-]{2,8}$'), The bug here is that you are using the character(n) type, which you shouldn't. That type has archaic (but standard) behavior that pads the value with spaces. Your regex does not account for that. Don't use that type. Use text instead. https://wiki.postgresql.org/wiki/Don%27t_Do_This#Don.27t_use_char.28n.29 -- Vik Fearing