Re: Random function

From: "David G(dot) Johnston" <david(dot)g(dot)johnston(at)gmail(dot)com>
To: Luis Roberto Weck <luisroberto(at)siscobra(dot)com(dot)br>
Cc: pgsql-performance(at)lists(dot)postgresql(dot)org
Subject: Re: Random function
Date: 2020-03-24 18:33:26
Message-ID: CAKFQuwb7OBmDRGJg2HZPuS7EafJOiejaQO0UqZXvC20ZEnU6mQ@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-performance

How is this a performance related question?

On Tue, Mar 24, 2020 at 11:10 AM Luis Roberto Weck <
luisroberto(at)siscobra(dot)com(dot)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.

In response to

Browse pgsql-performance by date

  From Date Subject
Next Message Anders Steinlein 2020-03-24 22:55:29 Re: Slow planning time when public schema included (12 vs. 9.4)
Previous Message Tom Lane 2020-03-24 18:33:11 Re: Random function