PostgreSQL generate_subscripts() Function

The PostgreSQL generate_subscripts() function returns a set of array subscripts according to the specified array and dimensions.

generate_subscripts() Syntax

Here is the syntax of the PostgreSQL generate_subscripts() function:

generate_subscripts(arr array, dim integer[, reverse boolean]) -> setof integer

Parameters

arr

Required. It can be an array of any type.

dim

Required. The dimension of the array.

reverse

Optional. If returns the subscripts array in reverse order. The default value is false.

Return value

The PostgreSQL generate_subscripts() function returns a set of subscripts of the specified array dimension.

generate_subscripts() Examples

Here are some examples of the generate_subscripts() function.

To return all subscripts of the {3, 4, 5, 7}, use the following statement:

SELECT generate_subscripts('{3, 4, 5, 7}'::int[], 1);
 generate_subscripts
---------------------
                   1
                   2
                   3
                   4

To return all subscripts of the array {3, 4, 5, 7} in reverse order, use the following statement:

SELECT generate_subscripts('{3, 4, 5, 7}'::int[], 1, true);
 generate_subscripts
---------------------
                   4
                   3
                   2
                   1

To return all subscripts of the first dimension of the two-dimensional array {{3, 4, 5}, {7, 8, 9}}, use the following statement:

SELECT generate_subscripts('{{3, 4, 5}, {7, 8, 9}}'::int[][], 1);
 generate_subscripts
---------------------
                   1
                   2

To return all subscripts of the second-dimensional array in a two-dimensional array {{3, 4, 5}, {7, 8, 9}}, use the following statement:

SELECT generate_subscripts('{{3, 4, 5}, {7, 8, 9}}'::int[][], 2);
 generate_subscripts
---------------------
                   1
                   2
                   3