Set-returning functions
generate_series()
The generate_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.
The syntax for the generate_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.
Here is an example of how you can use the generate_series()
function to generate a series of numbers:
The result looks like this:
And here is an example with a step increment of 2:
The result looks like this:
Here is an example of how you can use the generate_series()
function to generate a series of timestamps:
The result looks like this:
Except for generating a static set of values, RisingWave also supports continuously generating timestamps at specified intervals into a materialized view. To achieve this, use now()
as the stop
parameter in the generate_series()
function. For example:
And you should follow the following syntax:
start
must be a constant expression of type timestamptz.stop
must benow()
.step
must be a constant expression of type interval.
The timestamps generated by the above example will look like this:
range()
The range()
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.
Here is an example of how you can use the range()
function to generate a series of numbers:
The result looks like this:
And here is an example with a step increment of 0.5:
The result looks like this:
Here is an example of how you can use the range()
function to generate a series of timestamps:
The result looks like this:
_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:
Columns in the returned set of rows:
- x: The value within the array.
- n: The index of the value within the array.
Was this page helpful?