Normalize array subscripts so they start with 1

Eventually, something more elegant popped up with Postgres 9.6. The manual:

It is possible to omit the lower-bound and/or
upper-bound of a slice specifier; the missing bound is replaced by the lower or upper limit of the array’s subscripts. For example:

So it’s simple now:

SELECT my_arr[:];

With my example array literal you need enclosing parentheses to make the syntax unambiguous:

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

About the same performance as Daniel’s solution with hard-coded max array subscripts – which is still the way to go with Postgres 9.5 or earlier.

Leave a Comment