Thread: [PATCH] Generate random dates/times in a specified range

[PATCH] Generate random dates/times in a specified range

From
Damien Clochard
Date:
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
Attachment

Re: [PATCH] Generate random dates/times in a specified range

From
Tom Lane
Date:
Damien Clochard <damien@dalibo.info> writes:
> 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

I'm a little uncomfortable with this proposal, mainly because it
overloads the random() function name to the point where I'm afraid
of "ambiguous function" failures in SQL code that used to be fine.

The traditional way of achieving these results would be something like

    select now() + random() * interval '10 days';

and I'm not convinced that the use-case is so large as to justify
adding built-in forms of that.

            regards, tom lane