Search code examples
sqlarrayspostgresqlunnestset-returning-functions

Unnest arrays of different dimensions


Is there a function or query that could return arrays of different dimensions as a set? For example, I would like to return the values

ARRAY[1]
ARRAY[2,3]
ARRAY[4,5,6]

as

1
2
3
4
5
6

Solution

  • Use unnest():

    SELECT unnest(arr) AS elem
    FROM (
     VALUES
       (ARRAY[1])
      ,(ARRAY[2,3])
      ,(ARRAY[4,5,6])
      ) t (arr);
    

    Returns as requested.
    More details: