How to generate random string for all rows in postgres

Поиск
Список
Период
Сортировка
От Hassan Akefirad
Тема How to generate random string for all rows in postgres
Дата
Msg-id CAHRWLaEg3pmZc3gjTby9cGMO7mavyAWftuBrSBqcGRaxW7eBhQ@mail.gmail.com
обсуждение исходный текст
Ответы Re: How to generate random string for all rows in postgres  (hubert depesz lubaczewski <depesz@depesz.com>)
Список pgsql-general
I have foo table and would like to set bar column to a random string. I've got the following query:

update foo
set bar = array_to_string(
array(select string_agg(substring('0123456789bcdfghjkmnpqrstvwxyz', round(random() * 30)::integer, 1), '')
from generate_series(1, 9)), '');

But it generates the random string once and reuse it for all rows. I asked people on SO and one of the giants answered (here):

The problem is that the Postgres optimizer is just too smart and decides that it can execute the subquery only once for all rows. Well -- it is really missing something obvious -- the random() function makes the subquery volatile so this is not appropriate behavior.

Is this (specifically the point about random()) a bug or feature? Thanks.

В списке pgsql-general по дате отправления:

Предыдущее
От: Thiemo Kellner
Дата:
Сообщение: Re: Possible trigger bug? function call argument literalised
Следующее
От: hubert depesz lubaczewski
Дата:
Сообщение: Re: How to generate random string for all rows in postgres