admin管理员组文章数量:1305124
I am partitioning a table in Postgresql15. I have a partition table which has country
as the partition key. Each child table has id
as the primary key. My application talks with the master partition table only.
I have some SQL includes CONFLICT ON
against the primary key id
. This doesn't work for partition table since id
is not primary key in the partition table so I will get error like: there is no unique or exclusion constraint matching the ON CONFLICT specification
.
In order to solve this issue, I will need to create the primary key index on the partition table. But the partition table needs to have the same primary key index as each child table, I will have to drop the existing id
primary key from child table, then create a new primary key including both id
and country
on both child and partition table.
My question is does the order of id
and country
on the primary index matter? What is the right order I should use?
I am partitioning a table in Postgresql15. I have a partition table which has country
as the partition key. Each child table has id
as the primary key. My application talks with the master partition table only.
I have some SQL includes CONFLICT ON
against the primary key id
. This doesn't work for partition table since id
is not primary key in the partition table so I will get error like: there is no unique or exclusion constraint matching the ON CONFLICT specification
.
In order to solve this issue, I will need to create the primary key index on the partition table. But the partition table needs to have the same primary key index as each child table, I will have to drop the existing id
primary key from child table, then create a new primary key including both id
and country
on both child and partition table.
My question is does the order of id
and country
on the primary index matter? What is the right order I should use?
1 Answer
Reset to default 0The order of the column in a primary key definition matters, since the order of columns in an index matters. An index on (a, b)
can be used to speed up statements with WHERE a = ...
, but is useless for WHERE b = ...
(there are exceptions, but let's keep it simple). Here are some guidelines:
if you predominantly use one of the columns in
WHERE
clauses, but not the other, put the one column firstif you need to support
ORDER BY a, b
, define the index in that same orderif both columns are used in the
WHERE
clause (and the conditions are combined withAND
), put the column first that is compared with=
— if both comparisons are with=
, the order doesn't matter
If several of these cases apply and suggest different orderings, you may have to define a second index.
版权声明:本文标题:What is the order of keys defined in the primary key list for partition table in postgresql? - Stack Overflow 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1741788980a2397549.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
when matched
condition than is theinsert ... on conflict
. I do not know your specific requirement but merge may allow resolution of your problem within your existing structure. – Belayer Commented Feb 4 at 5:31typeorm
which useson conflict
internally. so I can't change the library easily. that's why I stick withon conflict
. Do you know if the order of index matter? – Joey Yi Zhao Commented Feb 4 at 5:32