PostgreSQL array_upper() Function

The PostgreSQL array_upper() function returns the maximum subscript of the specified array dimension.

array_upper() Syntax

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

array_upper(array, dimension) -> integer

Parameters

array

Required. array.

dimension

Required. The dimension of the array, which is an integer.

Return value

The PostgreSQL array_upper() function returns an integer that is the maximum index of the specified dimension in the specified array.

If you specify a dimension that does not exist in the array, the function will return NULL.

array_upper() Examples

one-dimensional array

This example shows how to use a PostgreSQL array_upper() function to return the maximum index of a one-dimensional array.

SELECT array_upper(ARRAY[0, 1, 2], 1);
 array_upper
-------------
           3

This means that the maximum index of [0, 1, 2] is 3.

SELECT array_upper('[3:7]={1,1,1,1,1}'::integer[], 1);
 array_upper
-------------
           7

This means that the maximum index of [3:7]={1,1,1,1,1} is 7.

Multidimensional Arrays

For a multidimensional array, you can get the maximum index of an array dimension. for example:

SELECT array_upper('[2:4][2:3]={{1,1},{1,1},{1,1}}'::integer[], 2);
 array_upper
-------------
           3

This means that the maximum index of the second demension of [2:4][2:3]={{1,1},{1,1},{1,1}} is 3.