在 Postgres 数组中排序有多可靠?
How reliable is ordering in a Postgres array?
我有 store
table:
CREATE TABLE store(
id SERIAL,
product_ids INT[],
product_prices NUMERIC[]
)
例如:
id | product_ids | product_prices
----------------------------------
1 | {12, 4} | {100, 202.5}
即ID为12
的商品价格为100
,商品ID为4
的价格为202.5
。 product_ids
数组中的 ID 是唯一的。
如果产品 id=4
有新价格,例如 199
,我会这样更新它:
UPDATE store SET
product_prices [idx(product_ids , 4)] = 199
WHERE id = ?
这些数组中元素的顺序非常重要。我从不对这些数组进行排序,也许只是添加一个新的 product_id 和使用 array_append()
.
的适当价格
所以问题是:Postgres 数组中的顺序有多可靠?有没有可能有一天 Postgres 会以某种方式对数组进行排序或交换元素位置本身?
P.S。我需要用数组做这个,我不想使用其他一些 table 列 store_id
、product_id
、product_price
等
不,Postgres 不会自行更改数组的顺序。如果数据库那样做,我们都会遇到很多麻烦。
我有 store
table:
CREATE TABLE store(
id SERIAL,
product_ids INT[],
product_prices NUMERIC[]
)
例如:
id | product_ids | product_prices
----------------------------------
1 | {12, 4} | {100, 202.5}
即ID为12
的商品价格为100
,商品ID为4
的价格为202.5
。 product_ids
数组中的 ID 是唯一的。
如果产品 id=4
有新价格,例如 199
,我会这样更新它:
UPDATE store SET
product_prices [idx(product_ids , 4)] = 199
WHERE id = ?
这些数组中元素的顺序非常重要。我从不对这些数组进行排序,也许只是添加一个新的 product_id 和使用 array_append()
.
所以问题是:Postgres 数组中的顺序有多可靠?有没有可能有一天 Postgres 会以某种方式对数组进行排序或交换元素位置本身?
P.S。我需要用数组做这个,我不想使用其他一些 table 列 store_id
、product_id
、product_price
等
不,Postgres 不会自行更改数组的顺序。如果数据库那样做,我们都会遇到很多麻烦。