Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6825911
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T22:05:12+00:00 2026-05-26T22:05:12+00:00

I want to take an array of n dimensions and return set containing rows

  • 0

I want to take an array of n dimensions and return set containing rows of arrays of n-1 dimensions. For example, take the array ARRAY[[1,2,3], [4,5,6], [7,8,9]] and return a set {1,2,3}, {4,5,6}, {7,8,9}. Using unnest returns the set 1,2,3,4,5,6,7,8,9.

I tried grabbing the unnest function from PostgreSQL 8.4, which seems like it would do what I’m looking for:

CREATE OR REPLACE FUNCTION tstng.unnest2(anyarray)
    RETURNS SETOF anyelement
    LANGUAGE plpgsql
    IMMUTABLE
    AS $$
    BEGIN
            RETURN QUERY SELECT $1[i]
                FROM generate_series(array_lower($1,1), array_upper($1,1)) i;
        END;
    $$;

However, SELECT tstng.unnest2(ARRAY[[1,2,3], [4,5,6], [7,8,9]]); returns the set , , (i.e.: 3 null rows).

I’ve also found that SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[0]; returns null, which I believe to be the root of my problem.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-26T22:05:13+00:00Added an answer on May 26, 2026 at 10:05 pm

    Function

    To break out 1-dimensional arrays from n-dimensional arrays – representing leaves of the nested dimensions. (With n >= 1.)

    PL/pgSQL

    With a FOR loop looping through the array:

    CREATE OR REPLACE FUNCTION unnest_nd_1d(a ANYARRAY, OUT a_1d ANYARRAY)
      RETURNS SETOF ANYARRAY
      LANGUAGE plpgsql IMMUTABLE PARALLEL SAFE STRICT AS
    $func$
    BEGIN
       FOREACH a_1d SLICE 1 IN ARRAY a LOOP
          RETURN NEXT;
       END LOOP;
    END
    $func$;
    

    SLICE 1 instructs to take the 1-dimensonal arrays. (SLICE 2 would take 2-dimensional arrays.)

    PARALLEL SAFE only for Postgres 9.6 or later.

    Later tests revealed this PL/pgSQL function to be fastest.
    Related:

    • How to unnest a 2d array into a 1d array quickly in PostgreSQL?

    Pure SQL

    Only works for 2D arrays:

    CREATE OR REPLACE FUNCTION unnest_2d_1d(anyarray)
      RETURNS SETOF anyarray
      LANGUAGE sql IMMUTABLE PARALLEL SAFE STRICT AS
    $func$
    SELECT array_agg($1[d1][d2])
    FROM   generate_subscripts($1,1) d1
        ,  generate_subscripts($1,2) d2
    GROUP  BY d1
    ORDER  BY d1
    $func$;
    

    This is an improved and simplified version of the function Lukas posted.

    db<>fiddle here
    Old sqlfiddle

    Explanation

    SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[0]
    

    returns the same as:

    SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[17]
    

    … which is NULL. The manual:

    By default, the lower bound index value of an array’s dimensions is
    set to one.

    0 has no special meaning as array subscript. There’s just nothing there for Postgres arrays with default indexes.
    Also, with two-dimensional arrays, you need two indexes to get a base element. Like:

    SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[1][2]
    

    Result:

    2
    

    The first part of your message is a bit unclear.

    SELECT array_dims(ARRAY[[1,2,3], [4,5,6], [7,8,9]]);
    

    Result:

    [1:3][1:3]
    

    That’s two dimensions with 3 elements (1 to 3) each (9 base elements).
    If you want n-1 dimensions then this is a correct result:

    SELECT ARRAY (SELECT unnest('{{1,2,3}, {4,5,6}, {7,8,9}}'::int[]))
    

    Result:

    {1,2,3,4,5,6,7,8,9}
    

    That’s one dimension. unnest() produces one base element per row (regardless of array dimensions). Your example is just another 2-dimensional array with a missing set of curly brackets … ?

    {1,2,3}, {4,5,6}, {7,8,9}
    

    If you want a slice of the array:

    SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[1:2]
    

    Result:

    {{1,2,3},{4,5,6}}
    

    Or:

    SELECT (ARRAY[[1,2,3], [4,5,6], [7,8,9]])[2:2][1:2]
    

    Result:

    {{4,5}}
    

    To flatten the result (get a 1D array):

    • How to select 1d array from 2d array?

    Read the manual here.

    For very old versions

    For Postgres versions < 8.4, array_agg() is not installed by default. Create it first:

    CREATE AGGREGATE array_agg(anyelement) (
     SFUNC = array_append,
     STYPE = anyarray,
     INITCOND = '{}'
    );
    

    Also, generate_subscripts() is not born, yet. Use instead:

    ...
    FROM   generate_series(array_lower($1,1), array_upper($1,1)) d1
        ,  generate_series(array_lower($1,2), array_upper($1,2)) d2
    ...
    

    Call:

    SELECT unnest_2d_1d(ARRAY[[1,2], [3,4], [5,6]]);
    

    Result

    {1,2}
    {3,4}
    {5,6}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to take an array and use that array's values to populate an
I want to take advantage of the new features in Windows 7 using C#
I want to take a snapshot with my webcam using java and save it
I have X , a three-dimensional array in R. I want to take a
I want to take some indices from the elements of a numpy.array. This is
I want to take a table as represented by a multidimensional string array (column
I have been experimenting with using UUIDs as database keys. I want to take
I have a LINQ query which returns IEnumerable<List<int>> but i want to return only
I have a multi-dimensional array similar to the example below that I want to
Let's say I've array with n elements. I want to take first ten elements

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.