The postgres docs on these 2 array functions are pretty weak.
I've tried both functions a few different ways and they seem to return the same results.
SELECT array_length(array[[1, 2], [3, 4], [5, 6]], 1);
SELECT array_upper(array[[1, 2], [3, 4], [5, 6]], 1);
SELECT array_length(array[[1, 2], [3, 4], [5, 6]], 2);
SELECT array_upper(array[[1, 2], [3, 4], [5, 6]], 2);
Unnest Multi-Dimension Array in PostgreSQL Example: Unnest method using 2-D array as an integer. The below code snippet will expand the integer array into 6 rows. SELECT unnest('[2:4][2:3]={{21,22},{23,24},{25,26}}'::integer[]); Output: In the result set, each array element becomes a row.
PostgreSQL ARRAY_LENGTH() function This function is used to return the length of the requested array dimension.
cardinality() is a system function returning the total number of individual elements in an array. cardinality() was added in PostgreSQL 8.4.
Yes, there is a difference. PostgreSQL array subscripts start at one by default but they don't have to:
By default PostgreSQL uses a one-based numbering convention for arrays, that is, an array of
n
elements starts witharray[1]
and ends witharray[n]
.
[...]
Subscripted assignment allows creation of arrays that do not use one-based subscripts. For example one might assign tomyarray[-2:7]
to create an array with subscript values from-2 to 7
.
[...]
By default, the lower bound index value of an array's dimensions is set to one. To represent arrays with other lower bounds, the array subscript ranges can be specified explicitly before writing the array contents.
In general, you need to use array_lower
and array_upper
instead of assuming that the array will start at 1
and end at array_length(a, n)
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With