Postgres 9.5 sends an additional variant of the aggregate function array_agg() , which can aggregate arrays into an array from an array of one higher dimension. Cm:
- How to sort a two-dimensional int array in PostgreSQL?
Aggregation function for any type of array
With the polymorphic type anyarray it works for all types of arrays, not just integer :
CREATE AGGREGATE array_agg_mult (anyarray) ( SFUNC = array_cat ,STYPE = anyarray ,INITCOND = '{}' );
As pointed out by @Lukas, the custom arrayappend() function is not needed. The built-in array_cat() function does the job. However, this does not explain why your example fails while the answer in @Lukas works. The corresponding difference is that @Lukas nested the array in another array with array[da] .
You are straying from the incorrect assumption that you can declare an int[][] . But you cannot: int[][] is the same type as int[] for a system like PostgreSQL. The chapter on array types in the manual explains:
The current implementation does not apply the declared number of sizes either. Arrays of a certain type of element - all are considered of the same type, regardless of size or quantity. Overall dimensions. So, declaring an array size or number of dimensions in a CREATE TABLE is just documentation; This does not affect runtime behavior.
An n dimensional array of integers is effectively an array of n-1 dimensional integer arrays in PostgreSQL. You cannot say this by type that defines only the base element . You must ask array_dims() to get the specifics.
To demonstrate:
SELECT array_agg_mult(arr) AS arr1 --> 1-dimensional array ,array_agg_mult(ARRAY[arr]) AS arr2 --> 2-dimensional array ,array_agg_mult(ARRAY[ARRAY[arr]]) AS arr3 --> 3-dimensional array -- etc. FROM ( VALUES ('{1,2,3}'::int[]) -- 1-dimensional array ,('{4,5,6}') ,('{7,8,9}')) ) x(arr);
Or:
SELECT array_agg_mult(arr) AS arr1 --> 2-dimensional array FROM ( VALUES ('{{1,2,3}}'::int[]) -- 2-dimensional array ,('{{4,5,6}}') ,('{{7,8,9}}') ) x(arr);
All resulting columns are of the same type: int[] .