我已经基于 row_created_tmst 为 PostgreSQL 表按范围创建了分区。由于在 PostgreSQL 中分区键需要作为主键的一部分,因此 row_created_tmst 包含在表的主键中。
通过此更改,我面临子表中外键的问题。
父表:source_record
source_record_id bigint NOT NULL,
row_created_tmst timestamp with time zone NOT NULL,
list of other attributes..
CONSTRAINT source_record_pk PRIMARY KEY (source_record_id, row_created_tmst)
Child Table: role_player (one source_record have 1 or more role player)
role_player_id bigint NOT NULL,
row_created_tmst timestamp with timezone NOT NULL,
source_record_id bigint NOT NULL, -- Foreign Key
sr_row_created_tmst bigint NOT NULL, -- Foreign Key
list of other attributes..
CONSTRAINT role_player_pk PRIMARY KEY (role_player_id, row_created_tmst)
CONSTRAINT role_player_ak FOREIGN KEY (source_record_id, sr_row_created_tmst)
由于基于 row_created_tmst 列进行分区,source_record 的所有子表都有 2 row_created_tmst 列(sr_row_created_tmst(FK),row_created_tmst)
有没有办法避免在子表中同时拥有父表和子表 row_created_tmst 列。
不,没有办法避免这种情况。此外,如果您定义引用分区表的外键,您将无法再分离分区。
你能做的最好的事情(在我看来)不是在分区表上破坏主键,而是
source_record_id
在每个分区上定义一个主键。牺牲参照完整性并且根本不定义外键约束。如果您想从分区中获益,则必须忍受降低的数据完整性保证。