12

I read over the docs for PostgreSQL v 9.3 arrays (http://www.postgresql.org/docs/9.3/static/arrays.html), but I don't see the question of ordering covered. Can someone confirm that Postgres preserves the insertion order/original order of an array when it's inserted into an array column? This seems to be the case but I would like absolute confirmation.

Thank you.

1
  • a) I don't see explicit documentation. b) It wouldn't make sense to change the order. The example table "sal_emp" has an array of quarterly pay; if "pay_by_quarter[1]" sometimes returned the value for the first quarter and sometimes for the fourth quarter, heads would have exploded by now. AFAIK, array data types in every language preserves order. c) For explicit confirmation, you'll probably have to check source code or the pghacker mailing list. Commented Feb 3, 2015 at 22:58

2 Answers 2

18

The documentation is entirely clear that arrays are useful in scenarios where order is important, inasmuch as it explicitly documents querying against specific positions within an array. If those positions were not reliable, these queries would have no meaning. (Using the word "array" is also clear on this point, being as it is a term of the art: An array is an ordered datatype by its nature; an unordered collection allowed to contain duplicates would be a bag, not an array, just as an unordered collection in which duplicates were not allowed would be a set).

See the examples given in section 8.1.4.3, of "pay by quarter", with index position within the array indicating which quarter is being queried against.

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

Comments

-2

Cannot find in documentation, but I'm pretty sure. Yes, order is preserved. And [2,4,5] is different from [5,2,4].
In case I'm wrong, indexes cannot work.

1 Comment

"Cannot find in documentation, but I'm pretty sure." Well, that's basically the OP's question, isn't it?

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.