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 1hNyfR-0006Vj-0P for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2019 11:53:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1hNyfP-0002MC-Na for pgsql-general@arkaria.postgresql.org; Tue, 07 May 2019 11:53:51 +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 1hNyfP-0002Hj-BF for pgsql-general@lists.postgresql.org; Tue, 07 May 2019 11:53:51 +0000 Received: from q7r7.com ([8.9.8.112] helo=50pop.com) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1hNyfM-00056U-Ar for pgsql-general@lists.postgresql.org; Tue, 07 May 2019 11:53:50 +0000 Received: from localhost (50pop.com [local]) by 50pop.com (OpenSMTPD) with ESMTPA id e29652a1 for ; Tue, 7 May 2019 07:53:45 -0400 (EDT) Date: Tue, 7 May 2019 07:53:45 -0400 From: Myles Miller To: pgsql-general@lists.postgresql.org Subject: random generated string matching index in inexplicable ways Message-ID: <20190507115345.GA56585@50pop.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline User-Agent: Mutt/1.11.4 (2019-03-13) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk PROBLEM: Strings or characters generated by any random function (including pg_crypto gen_random_bytes) are matching a string/char index in surprising ways. Reduced down to its simplest example: -- use random 0 or 1, plus 65, to get 'A' or 'B' # SELECT chr(round(random())::int + 65); chr ----- B (1 row) # SELECT chr(round(random())::int + 65); chr ----- A (1 row) -- simple table for matching: CREATE TABLE x( y char(1) primary key ); INSERT INTO x(y) VALUES ('A'); INSERT INTO x(y) VALUES ('B'); -- if I query 'A' or 'B' it works as expected # SELECT y FROM x WHERE y = 'A'; y --- A (1 row) # SELECT y FROM x WHERE y = 'B'; y --- B (1 row) -- if we use random-generated 'A' or 'B', things get inexplicable # SELECT y FROM x WHERE y = chr(round(random())::int + 65); y --- A B (2 rows) # SELECT y FROM x WHERE y = chr(round(random())::int + 65); y --- (0 rows) # SELECT y FROM x WHERE y = chr(round(random())::int + 65); y --- B (1 row) I've been wrestling with this for hours. Any suggestions? Thank you.