[PATCH] Generate random dates/times in a specified range
Damien Clochard <damien@dalibo.info>
From: Damien Clochard <damien@dalibo.info>
To: pgsql-hackers@lists.postgresql.org
Date: 2025-07-09T21:14:46Z
Lists: pgsql-hackers
Commits
Same data as JSON:
GET /api/v1/messages/:b64id/commits
the thread's linked commits as JSON, with link sources.
API reference →
-
doc: Improve description of new random(min, max) functions.
- 9c24111c4dad 19 (unreleased) landed
-
Add date and timestamp variants of random(min, max).
- faf071b55383 19 (unreleased) landed
Attachments
- v1-0001-Generate-random-dates-times-in-a-specified-range.patch (text/x-diff) patch v1-0001
Hello,
As I am involved in the PostgreSQL Anonymizer extension, I found that
the random(min,max) functions introduced recently are very useful to
generate synthetic data or define a masking policy.
I decided to submit a similar set of functions for random dates and
times.
So this adds 5 new variants of the random() function:
random(min date, max date) returns date
random(min time, max time) returns time
random(min time, max time, zone text) returns timetz
random(min timestamp, max timestamp) returns timestamp
random(min timestamptz, max timestamptz) returns timestamptz
Each one returns a random date/time value t in the range min <= t <=
max.
For the timetz function, a third parameter is required to define the
timezone.
However if the value is an empty string, the session timezone is used.
These functions all rely on the pg_prng_int64_range function developped
in
PG 17 for the random(bigint,bigint) function.
Regards,
--
Damien Clochard