[英]Postgres Inheritance based partition scanning all the partitions
我想在 Postgres 中通过 inheritance 实现分区。 我通过参考 Postgres 文章实现了以下步骤:-
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)
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
这给出了以下 output
"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 分区的工作方式吗?
样本量为 1,你不能真正得出结论。而且你只有一张子表。
没有限制根表不能包含where col3=1
的行,因此需要对其进行扫描。 扫描一个空的 able 没什么大不了的,但如果你确实想避免它,你可以添加一个约束:
alter table kirana_customer.test_table add constraint whatever
check (col3 is null) no inherit;
此外,您不想使用声明性分区的原因没有任何意义。 也许你应该用一个例子来问一个问题,说明你对此有什么误解。
您必须将constraint_exclusion
设置为on
或partition
才能工作。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.