I have store table:
CREATE TABLE store(
id SERIAL,
product_ids INT[],
product_prices NUMERIC[]
)
For example:
id | product_ids | product_prices
----------------------------------
1 | {12, 4} | {100, 202.5}
Which means that the price for the product with ID 12 is 100, and the price for product ID 4 is 202.5. IDs in the product_ids array are unique.
If product id=4 has a new price, for example 199, I update it this way:
UPDATE store SET
product_prices [idx(product_ids , 4)] = 199
WHERE id = ?
The order of elements in these arrays is very important. I never sort these arrays, maybe just add a new product_id and the appropriate price using array_append().
So the question is: How reliable is the ordering in a Postgres array? Is there a chance that somehow Postgres will sort the array or exchang element positions itself someday?
P.S. I need to make this with arrays, I don't want to use some other table with columns store_id, product_id, product_price etc.
stores, is huge, approximately 500 000, but products for each store, would be not so big (max 5000), so if store this data with normal database design, e.i. in different table, the rows count will be very huge, in this case,product_idsandproduct_pricesarrays will be contain maximum 5000 element, no more.Or prevent the same product to appear twice with different prices- before adding product id toproduct_idsarray, I check, if there exists this ID in array, i do price update as I show in question , if not, then I add new product id and its price at the end of appropriate arraysAnd it will get quite complicated if you ever need to run (real-world) reports on it- because of reports will be from only one concrete store (and not from several stores together), executing data and making report will be easy, because of little amount of products for this concrete store