Re: Random function

Поиск
Список
Период
Сортировка
От David G. Johnston
Тема Re: Random function
Дата
Msg-id CAKFQuwb7OBmDRGJg2HZPuS7EafJOiejaQO0UqZXvC20ZEnU6mQ@mail.gmail.com
обсуждение исходный текст
Ответ на Random function  (Luis Roberto Weck <luisroberto@siscobra.com.br>)
Список 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 по дате отправления:

Предыдущее
От: Tom Lane
Дата:
Сообщение: Re: Random function
Следующее
От: Anders Steinlein
Дата:
Сообщение: Re: Slow planning time when public schema included (12 vs. 9.4)