pg.ddx.io  pgsql-general@postgresql.org mailing list archive  
help / color / mirror / Atom feed
How to generate random string for all rows in postgres
3+ messages / 3 participants
[nested] [flat]

* How to generate random string for all rows in postgres
@ 2021-01-04 13:14 Hassan Akefirad <akefirad@gmail.com>
  2021-01-04 13:46 ` Re: How to generate random string for all rows in postgres hubert depesz lubaczewski <depesz@depesz.com>
  0 siblings, 1 reply; 3+ messages in thread

From: Hassan Akefirad @ 2021-01-04 13:14 UTC (permalink / raw)
  To: pgsql-general@lists.postgresql.org

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
<https://stackoverflow.com/a/65562867/2194119;):

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.

^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to generate random string for all rows in postgres
  2021-01-04 13:14 How to generate random string for all rows in postgres Hassan Akefirad <akefirad@gmail.com>
@ 2021-01-04 13:46 ` hubert depesz lubaczewski <depesz@depesz.com>
  2021-01-04 14:19   ` Re: How to generate random string for all rows in postgres Rad Akefirad <akefirad@gmail.com>
  0 siblings, 1 reply; 3+ messages in thread

From: hubert depesz lubaczewski @ 2021-01-04 13:46 UTC (permalink / raw)
  To: Hassan Akefirad <akefirad@gmail.com>; +Cc: pgsql-general@lists.postgresql.org

On Mon, Jan 04, 2021 at 02:14:11PM +0100, Hassan Akefirad wrote:
> 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 ([1]here):

Hi,
first of all - there is no need to use array_to_string(array( ... ))

just bar = (select string_agg).

it will not work, for the reasons you said, but it's better not to
overcomplicate stuff.

For your case, I think I'd simply make a function for generating random
strings:

CREATE OR REPLACE FUNCTION random_string( int ) RETURNS TEXT as $$
    SELECT string_agg(substring('0123456789bcdfghjkmnpqrstvwxyz', round(random() * 30)::integer, 1), '') FROM generate_series(1, $1);
$$ language sql;

And then use it like this:

update foo set bar = random_string(9)

I know it's not perfect, but:
1. it works
2. your query becomes easier to read/understand
3. as a side benefit you will get function for other use cases :)

Best regards,

depesz






^ permalink  raw  reply  [nested|flat] 3+ messages in thread

* Re: How to generate random string for all rows in postgres
  2021-01-04 13:14 How to generate random string for all rows in postgres Hassan Akefirad <akefirad@gmail.com>
  2021-01-04 13:46 ` Re: How to generate random string for all rows in postgres hubert depesz lubaczewski <depesz@depesz.com>
@ 2021-01-04 14:19   ` Rad Akefirad <akefirad@gmail.com>
  0 siblings, 0 replies; 3+ messages in thread

From: Rad Akefirad @ 2021-01-04 14:19 UTC (permalink / raw)
  To: depesz@depesz.com; +Cc: pgsql-general@lists.postgresql.org

Got it. Many thanks.

On Mon, Jan 4, 2021 at 2:46 PM hubert depesz lubaczewski <depesz@depesz.com>
wrote:

> On Mon, Jan 04, 2021 at 02:14:11PM +0100, Hassan Akefirad wrote:
> > 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 ([1]here):
>
> Hi,
> first of all - there is no need to use array_to_string(array( ... ))
>
> just bar = (select string_agg).
>
> it will not work, for the reasons you said, but it's better not to
> overcomplicate stuff.
>
> For your case, I think I'd simply make a function for generating random
> strings:
>
> CREATE OR REPLACE FUNCTION random_string( int ) RETURNS TEXT as $$
>     SELECT string_agg(substring('0123456789bcdfghjkmnpqrstvwxyz',
> round(random() * 30)::integer, 1), '') FROM generate_series(1, $1);
> $$ language sql;
>
> And then use it like this:
>
> update foo set bar = random_string(9)
>
> I know it's not perfect, but:
> 1. it works
> 2. your query becomes easier to read/understand
> 3. as a side benefit you will get function for other use cases :)
>
> Best regards,
>
> depesz
>
>

^ permalink  raw  reply  [nested|flat] 3+ messages in thread


end of thread, other threads:[~2021-01-04 14:19 UTC | newest]

Thread overview: 3+ messages (download: mbox mbox.gz follow: Atom feed)
-- links below jump to the message on this page --
2021-01-04 13:14 How to generate random string for all rows in postgres Hassan Akefirad <akefirad@gmail.com>
2021-01-04 13:46 ` hubert depesz lubaczewski <depesz@depesz.com>
2021-01-04 14:19   ` Rad Akefirad <akefirad@gmail.com>

This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox