Re: some random() clarification needed

From: David Rowley <dgrowleyml(at)gmail(dot)com>
To: Marc Millas <marc(dot)millas(at)mokadb(dot)com>
Cc: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>, "pgsql-general(at)lists(dot)postgresql(dot)org" <pgsql-general(at)lists(dot)postgresql(dot)org>
Subject: Re: some random() clarification needed
Date: 2020-07-14 23:53:21
Message-ID: CAApHDvqHQN557Yr8+2m3Crd0YvJ9Zg_hXLGbhfc2G+Zvso2WxA@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-general

On Wed, 15 Jul 2020 at 04:01, Marc Millas <marc(dot)millas(at)mokadb(dot)com> wrote:
> your answer helps me understand my first problem.
> so, I rewrote a simple loop so as to avoid the "volatile" behaviour.

Not sure what you're trying to do with the plpgsql, but you can just
escape the multiple evaluations by putting the volatile function in a
sub-select with no FROM clause.

SELECT ... FROM ... WHERE id = (SELECT ceiling(random()*2582));

Or the more traditional way to get a random row is:

SELECT ... FROM ... WHERE id BETWEEN 0 AND 2585 ORDER BY random() LIMIT 1;

David

In response to

Responses

Browse pgsql-general by date

  From Date Subject
Next Message TALLURI Nareshkumar 2020-07-15 01:53:26 RE: psql: FATAL: database "postgres" does not exist or ERROR: 23505: duplicate key value violates unique constraint "pg_namespace_nspname_index"
Previous Message Tom Lane 2020-07-14 23:44:54 Re: Convert hot_standby 9.4 postgresql into standalone server