generate_series()

A function for generating a series of values

generate_series() is a system function for generating a series of values (either numeric or time-based).

generate_series() was added in PostgreSQL 8.0.

Usage

generate_series ( start integer, stop integer [, step integer ] ) → setof integer 
generate_series ( start bigint, stop bigint [, step bigint ] ) → setof bigint
generate_series ( start numeric, stop numeric [, step numeric ] ) → setof numeric
generate_series ( start timestamp, stop timestamp, step interval ) → setof timestamp
generate_series ( start timestamp with time zone, stop timestamp with time zone, step interval ) → setof timestamp with time zone

Change history

Examples

Basic execution of generate_series():

postgres=# SELECT generate_series(1, 5);
 generate_series 
-----------------
               1
               2
               3
               4
               5
(5 rows)

Generating a sequence in descending order:

postgres=# SELECT * FROM generate_series(5, 1, -1);
 generate_series 
-----------------
               5
               4
               3
               2
               1
(5 rows)

Generating a series of timestamps:

postgres=# SELECT *
             FROM generate_series(current_timestamp,
                                  current_timestamp + '5 minutes'::interval,
                                 '1 minute'::interval);
        generate_series        
-------------------------------
 2021-02-08 09:05:21.455263+01
 2021-02-08 09:06:21.455263+01
 2021-02-08 09:07:21.455263+01
 2021-02-08 09:08:21.455263+01
 2021-02-08 09:09:21.455263+01
 2021-02-08 09:10:21.455263+01
(6 rows)

Categories

System function

See also

generate_subscripts()