【发布时间】:2021-08-26 12:43:18
【问题描述】:
我想通过 Postgres 中的继承来实现分区。 我通过参考 Postgres article 实现了以下步骤:-
- 创建了一个名为“test_table”的主表
CREATE TABLE kirana_customer.test_table
(
col1 bigint NOT NULL DEFAULT nextval('kirana_customer."testTable_col1_seq"'::regclass),
col2 bigint NOT NULL,
col3 integer,
CONSTRAINT "testTable_pkey" PRIMARY KEY (col1)
)
- 创建了子表/继承表
CREATE TABLE kirana_customer.test_table_1
(
-- Inherited from table kirana_customer.test_table: col1 bigint NOT NULL DEFAULT nextval('kirana_customer."testTable_col1_seq"'::regclass),
-- Inherited from table kirana_customer.test_table: col2 bigint NOT NULL,
-- Inherited from table kirana_customer.test_table: col3 integer,
CONSTRAINT check_col3 CHECK (col3 = 1)
)
INHERITS (kirana_customer.test_table)
- 将“BEFORE INSERT”触发器附加到主表,以便将基于列“col3”的数据插入到正确的分区表中
DECLARE
v_col3 bigint;
BEGIN
v_col3 := NEW.col3;
EXECUTE 'INSERT INTO kirana_customer.test_table_'||v_col3||' VALUES ($1.*)' USING NEW;
RETURN NULL;
END;
完成所有这些步骤后,我可以将我的条目插入正确的分区,但是在分析 select 语句时,我发现 Postgres 正在扫描所有分区
explain select * from kirana_customer.test_table where col3 = 1
这给出了以下输出
"Append (cost=0.00..34.42 rows=9 width=20)"
" -> Seq Scan on test_table (cost=0.00..3.12 rows=1 width=20)"
" Filter: (col3 = 1)"
" -> Seq Scan on test_table_1 (cost=0.00..31.25 rows=8 width=20)"
" Filter: (col3 = 1)"
那么,我错过了什么吗?或者这就是 Postgres 分区的工作方式?
【问题讨论】:
-
"我想通过继承实现分区" - 为什么?使用 Postres 12,您应该使用声明性分区。更高效,更好地集成到查询优化器中
-
如果我的分区键不是主键或复合主键,则声明性分区不允许我创建分区。我也无法使用外键。所以,我选择使用继承基础分区
-
您也不能将外键与基于继承的分区一起使用。而且你也不能在所有继承的表中都有一个正确的主键。
-
但我仍然可以使用不是 PK 或复合 PK 的分区键。我知道声明性分区是新的并且很容易使用。但是,不应该基于继承的分区工作吗?
-
基于继承的分区一直是穷人的分区实现。没有很好地集成到查询优化器中,而且它从来没有达到应有的效果。诚然,您可能能够使用不属于 PK 的分区键 - 但 PK 本身并不是真正的(全局)PK。
标签: postgresql database-partitioning postgres-12