array_random
jian he <jian.universality@gmail.com>
From: jian he <jian.universality@gmail.com>
To: PostgreSQL-development <pgsql-hackers@postgresql.org>
Date: 2025-06-30T15:04:25Z
Lists: pgsql-hackers
Attachments
- v1-0001-array_random.patch (text/x-patch) patch v1-0001
hi.
context: [1].
the attached patch already posted in [1].
I don't want to hijack another thread. so I post it in a separate thread.
The attached patch introduces a new function: array_random.
array_random description:
Returns an array filled with random values in the range min <= x <= max,
having dimensions of the lengths specified by dims. The optional lbounds
argument supplies lower-bound values for each dimension (which default
to all 1).
array_random function signature:
array_random(min int4, max int4, dims int[] [, lbounds int[]]) -> int[]
array_random(min int8, max int8, dims int[] [, lbounds int[]]) -> int8[]
array_random(min numeric, max numeric, dims int[] [, lbounds int[]])
-> numeric[]
demo:
SELECT array_random(1, 6, array[2,5], array[2,4]);
array_random
--------------------------------------
[2:3][4:8]={{6,2,2,5,4},{4,5,6,4,6}}
reasons for adding array_random is:
1. This is better than array_fill. This can fill random and constant
values (random, min and max the same).
2. Building a multi-dimensional PL/pgSQL function equivalent to
array_random is not efficient and is also not easier.
[1] https://www.postgresql.org/message-id/CACJufxGRCP19Rm66%3DTSBwmEuVr92FwL_e6YFjmCpJrgu6Km9hQ%40mail.gmail.com