generate_series()
Thegenerate_series()
function in PostgreSQL is a set-returning function that generates a series of values, based on the start and end values defined by the user. It is useful for generating test data or for creating a sequence of numbers or timestamps.
Basic syntax and example
The syntax for thegenerate_series()
function is as follows:
start
, stop
, and step
can be of type integer
, bigint
, numeric
, or timestamp
.
start
is the first value in the series.stop
is the last value in the series.step
is optional unlessstart
andstop
are of typetimestamp
. It is the increment value. If it is omitted, the default step value is 1.
generate_series()
function to generate a series of numbers:
generate_series()
function to generate a series of timestamps:
Advanced usage of generating timestamp
Except for generating a static set of values, RisingWave also supports continuously generating timestamps at specified intervals into a materialized view. To achieve this, usenow()
as the stop
parameter in the generate_series()
function. For example:
start
must be a constant expression of type timestamptz.stop
must benow()
.step
must be a constant expression of type interval.
range()
Therange()
function in PostgreSQL is a set-returning function that generates a series of values, based on the start and end values defined by the user. The end value is not included, unlike generate_series()
. It is useful for generating test data or for creating a sequence of numbers or timestamps.
The syntax for the range()
function is as follows:
start
, stop
, and step
can be of type integer
, bigint
, numeric
, or `timestamp.
start
is the first value in the series.stop
is the last value in the series.step
is optional unlessstart
andstop
are of typetimestamp
. It is the increment value. If it is omitted, the default step value is 1.
range()
function to generate a series of numbers:
range()
function to generate a series of timestamps:
_pg_expandarray()
The_pg_expandarray
function takes an array as input and expands it into a set of rows, providing values and their corresponding indices within the array. Ensure that information_schema
is in the search path to access the _pg_expandarray
function.
Example:
- x: The value within the array.
- n: The index of the value within the array.