pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
From: Myles Miller <pg@q7r7.com>
To: pgsql-general@lists.postgresql.org
Subject: random generated string matching index in inexplicable ways
Date: Tue, 7 May 2019 07:53:45 -0400
Message-ID: <20190507115345.GA56585@50pop.com> (raw)

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.






view thread (5+ messages)  latest in thread

Message-ID: <20190507115345.GA56585@50pop.com>
Permalink:  ../20190507115345.GA56585@50pop.com/
Also on:    postgresql.org/message-id/20190507115345.GA56585@50pop.com

 · 

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients from your mail client; the link fills in
  the headers below: reply via email
  Some clients, webmail especially, drop In-Reply-To and References from
  a link, and the reply then starts a new thread: use the mbox below.

  To: pgsql-general@postgresql.org
  Cc: pg@q7r7.com
  Subject: Re: random generated string matching index in inexplicable ways
  In-Reply-To: <20190507115345.GA56585@50pop.com>
  References: <20190507115345.GA56585@50pop.com>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox