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 1hkIAy-0005Y8-U9 for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Jul 2019 01:10:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hkIAx-0007DW-PO for pgsql-bugs@arkaria.postgresql.org; Mon, 08 Jul 2019 01:10:39 +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_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkIAx-0007DP-Hp for pgsql-bugs@lists.postgresql.org; Mon, 08 Jul 2019 01:10:39 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hkIAs-00026T-JO for pgsql-bugs@lists.postgresql.org; Mon, 08 Jul 2019 01:10:39 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.14.4/8.14.4) with ESMTP id x681AR2D020123; Sun, 7 Jul 2019 21:10:27 -0400 From: Tom Lane To: Manuel Rigger cc: PostgreSQL mailing lists Subject: Re: Issue with CHAR column and "column LIKE column" condition In-reply-to: References: Comments: In-reply-to Manuel Rigger message dated "Mon, 08 Jul 2019 00:42:24 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <20121.1562548227.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Sun, 07 Jul 2019 21:10:27 -0400 Message-ID: <20122.1562548227@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Manuel Rigger writes: > I was surprised by the behavior of LIKE and the CHAR type. Consider > the following statements: > CREATE TABLE t0(c0 CHAR(2)) ; > INSERT INTO t0(c0) VALUES('a'); > SELECT * FROM t0 WHERE c0 LIKE c0; -- expected: fetches the row, > actual: does not fetch the row Yeah. That's because LIKE is not, in fact, symmetric. The available LIKE operators are regression=3D# \do ~~ List of operators Schema | Name | Left arg type | Right arg type | Result type | = Description = ------------+------+---------------+----------------+-------------+-------= ------------------ pg_catalog | ~~ | bytea | bytea | boolean | matche= s LIKE expression pg_catalog | ~~ | character | text | boolean | matche= s LIKE expression pg_catalog | ~~ | name | text | boolean | matche= s LIKE expression pg_catalog | ~~ | text | text | boolean | matche= s LIKE expression (4 rows) of which the first and third aren't relevant here, and we end up choosing "character ~~ text" ... so the pattern side receives a coercion to text, which strips its trailing blanks, and then you have 'a '::char(2) ~~ 'a'::text which doesn't match. The semantics of char(N) are a mess generally. It's possible that this particular case would act less surprisingly if we got rid of the char ~~ text operator (forcing blank-stripping on both sides), or adding char ~~ char (preventing any blank-stripping), but probably somebody out there would complain if we did either, because it'd break some other case. Generally speaking, PG developers aren't excited about messing around with the semantics of char(N) --- we think it's a legacy datatype that you're best off not using. Trying to make it act less surprisingly would be a lot of work with, most likely, negative return. Anyone who *is* using it has probably tweaked their app until they got acceptable results, and would complain that we broke it. regards, tom lane