4

Concatenating the nested arrays {{1,2}} and {{3,4}} is no problem at all:

SELECT array_cat(
           ARRAY[ARRAY[1,2]]
         , ARRAY[ARRAY[3,4]]
       )

   array_cat   
---------------
 {{1,2},{3,4}}

But how to concatenate {{1,2}} and {{3}} in order to get {{1,2},{3}}?

SELECT array_cat(
           ARRAY[ARRAY[1,2]]
         , ARRAY[ARRAY[3]]
       )
psql: …: ERROR:  cannot concatenate incompatible arrays
DETAIL:  Arrays with differing element dimensions are not compatible
         for concatenation.
2
  • What structure do you expect for the final array? {{1,2},{3}} ? Commented May 28, 2014 at 16:34
  • I've just updated the question. Commented May 28, 2014 at 16:35

2 Answers 2

2

This is impossible in PostgreSQL. Multi-dimensional arrays must have the same number of element dimensions, just as the error message informs. Per documentation:

Multidimensional arrays must have matching extents for each dimension. A mismatch causes an error.

You might want to pad with NULL or some other dummy value ...

Sign up to request clarification or add additional context in comments.

1 Comment

I've already feared that. Seems like I have to look for another approach for my application.
0

I ended up using the following method, instead of padding with NULL, for jagged arrays which extends from the original example:

SELECT jsonb_agg(elem) FROM (
  SELECT jsonb_array_elements(
    jsonb_agg(to_jsonb(v))
  ) AS elem
  FROM (
    VALUES 
    (ARRAY[1,2]), 
    (ARRAY[3])
  ) AS vals(v)
) q;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.