array_upper()

A function returning the upper bound of an requested array dimension

array_upper() is a system function returning the upper bound of the requested array dimension.

array_upper() was added in PostgreSQL 7.4.

Usage

array_upper ( anyarray, integer ) → integer

Given a particular array (e.g. ['foo','bar','baz']), array_lower() returns the position of the last (highest) element, i.e. the upper array bound:

postgres=# SELECT array_upper(ARRAY['foo', 'bar', 'baz'], 1); 
 array_upper 
-------------
           3
(1 row)

The second parameter in the above example references the array dimension - as the example array only has one dimension, this must of course be 1.

It is also possible to specify a different ending point for the array upper bound, e,g.:

postgres=# SELECT array_upper('[0:2]={1,2,3}'::integer[], 1); 
 array_upper 
-------------
           2
(1 row)

Change history

Examples

Basic execution example for array_upper():

postgres=# SELECT array_upper(ARRAY[1,2,3], 1); 
 array_upper 
-------------
           3
(1 row)

Using array_upper() with a multi-dimensional array:

postgres=# SELECT array_upper(array[ [1,2,3],[4,5,NULL] ], 2); 
 array_upper 
-------------
           3
(1 row)

NULL is returned if an invalid array dimension is provided:

postgres=# SELECT array_upper(ARRAY[1,2,3], 2) IS NULL;
 ?column? 
----------
 t
(1 row)

Categories

Arrays, System function

See also

array_lower(), array_length(), array_ndims()