Re: Random function - Mailing list pgsql-performance

From David G. Johnston
Subject Re: Random function
Date
Msg-id CAKFQuwb7OBmDRGJg2HZPuS7EafJOiejaQO0UqZXvC20ZEnU6mQ@mail.gmail.com
Whole thread Raw
In response to Random function  (Luis Roberto Weck <luisroberto@siscobra.com.br>)
List pgsql-performance
How is this a performance related question?

On Tue, Mar 24, 2020 at 11:10 AM Luis Roberto Weck <luisroberto@siscobra.com.br> wrote:
However, I am getting the same result over mulitiple rows. This is a
sample of the SQL I am using:

select (select string_agg(random()::text,';')
           from pg_catalog.generate_series(1,3,1) )
   from generate_series(1,10,1)

And I am getting something like:

|string_agg |
+--------------------------------------------------------------+
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|
|0.243969671428203583;0.692578794434666634;0.291524752043187618|

If this is the expected output,

Yes, you've asked it to compute a value, assign it to a column, then generate 10 rows of that value.

is there a way to always generate random
numbers?

Don't use a scalar subquery in the main target list.

One possible answer:

select format('%s;%s;%s', random(), random(), random()) from generate_series(1, 10)

David J.

pgsql-performance by date:

Previous
From: Tom Lane
Date:
Subject: Re: Random function
Next
From: Anders Steinlein
Date:
Subject: Re: Slow planning time when public schema included (12 vs. 9.4)