简体   繁体   English

Postgres Inheritance 基于分区扫描所有分区

[英]Postgres Inheritance based partition scanning all the partitions

I wanted to implement partitioning via inheritance in Postgres.我想在 Postgres 中通过 inheritance 实现分区。 I implemented the below steps by referring to the Postgres article :-我通过参考 Postgres 文章实现了以下步骤:-

  1. Created a master table called "test_table"创建了一个名为“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)
)
  1. Created the child/inherited table创建子/继承表
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)
  1. Attached a "BEFORE INSERT" trigger to the master table for inserting data based on column "col3" to the correct partition 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;

After completing all these steps i am able to insert my entries into the correct partition, but while analysing the select statements i found that Postgres is scanning all the partitions完成所有这些步骤后,我能够将我的条目插入到正确的分区中,但是在分析 select 语句时我发现 Postgres 正在扫描所有分区

explain select * from  kirana_customer.test_table where col3 = 1 

This gives the below output这给出了以下 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)"

So, did I miss something?那么,我错过了什么吗? Or is this the way Postgres partitioning works?或者这是 Postgres 分区的工作方式吗?

You can't really draw conclusions with a sample size of 1. And you only have one child table.样本量为 1,你不能真正得出结论。而且你只有一张子表。

There is no constraint that the root table cannot contain rows where col3=1 , so it needs to be scanned.没有限制根表不能包含where col3=1的行,因此需要对其进行扫描。 Scanning an empty able is not a big deal, but if you did want to avoid it you could add a constraint:扫描一个空的 able 没什么大不了的,但如果你确实想避免它,你可以添加一个约束:

alter table kirana_customer.test_table add constraint whatever
   check (col3 is null) no inherit; 

Also, your reason for not wanting to use declarative partitioning doesn't make any sense.此外,您不想使用声明性分区的原因没有任何意义。 Maybe you should ask a question with an example about whatever misconception you have about that.也许你应该用一个例子来问一个问题,说明你对此有什么误解。

You have to set constraint_exclusion to on or partition for that to work.您必须将constraint_exclusion设置为onpartition才能工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM