Join the Stack Overflow Community
Stack Overflow is a community of 6.6 million programmers, just like you, helping each other.
Join them; it only takes a minute:
Sign up

I'd like to do the following:

CREATE TABLE example ( name text, arr text[][]);
INSERT INTO example VALUES ( 'aa', '{}');
UPDATE example SET arr = arr || ARRAY['header'] WHERE name = 'aa';

Right here, I have arr = [['header']]. I can easily add another array making it arr = [['header'], ['another']] by calling the last line from above again with 'another' inside the array constructor. However, I'm looking to add elements to those inner arrays now. Something like...

UPDATE example SET version[1] = version[1] || ARRAY['more'] WHERE name = 'aa'

However, postgres throws the error, wrong number of subscripts. I understand that postgres multidimensional arrays must have the same dimensions inside, so I would have to be able to add an element to all the inner arrays at once (probably NULL) and then change the one I'm adding to. Is there any way to do this without some kind of loop?

share|improve this question
    
Unlike C, Python, etc, a PostgreSQL multidimensional array is not an array of arrays. It's more of a matrix. PostgreSQL doesn't support taking partial-dimensionality slices. They're also pretty clumsy. It might be worth explaining the underlying problem you're trying to solve - there's probably a better way to do it that doesn't rely on arrays. – Craig Ringer Jun 19 '14 at 1:15
    
I'm implementing a collection of "Projects". Each project has a name, type, and list of any number of version series. And, each version series can have any number of version numbers. I was going to store a table of rows with a name, type, and a multidimensional array, where the head of each inner array was the series and the tail was the list of version numbers. The only other idea I had was to have another table called version_series to store rows with a formatted string (projectName_versionSeries) and array of version numbers. I'm really bummed that the multidimensional arrays won't work. – ckemper Jun 19 '14 at 3:28
    
Unless each inner array is the same length that's going to work. As I said, it's really more of a matrix, not an array of arrays. What's wrong with just modelling it relationally, or at least with series + array versions in a row? Multi-dim arrays are horrible to work with, and it's not clear they solve any problem that a simple relational model won't here. (You could also look at json or hstore) – Craig Ringer Jun 19 '14 at 5:33
    
Yeah I think I'll go with the relational model after all. Thanks for the tips! – ckemper Jun 19 '14 at 13:49

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Browse other questions tagged or ask your own question.