If you’re getting an error that reads “multidimensional arrays must have array expressions with matching dimensions” it’s probably because you’re trying to create a multi-dimensional array where the inner arrays at the same level have different dimensions.
Multidimensional arrays must be rectangular. To fix this issue, make sure all arrays at the same level have the same dimensions.
Example of Error
Here’s an example of code that produces the error:
SELECT ARRAY[ [1,2,3], [4,5,6,7] ];
Result:
ERROR: multidimensional arrays must have array expressions with matching dimensions
Here, I tried to create a multi-dimensional array that contains two sub-arrays. But I specified that the first inner array has three elements while the second has four. This caused an error. They must both contain the same number of elements.
Solution
We can fix the problem by ensuring that all inner arrays have the same dimensions.
For example:
SELECT ARRAY[ [1,2,3], [4,5,6] ];
Result:
array
-------------------
{{1,2,3},{4,5,6}}
This time both sub-arrays contain three elements and we no longer get the error.