开发者

PostgreSQL partition constraints behaving strangely

I have a table partitioned by two columns, expired and type_id. expired is a boolean and type_id partitions by range. Table examples:

mos_active_1
...
mos_active_15
mos_expired_1
...
mos_expired_15

My constraints are set up like this:

ADD CONSTRAINT mos_active_1_check CHECK (expired = false AND type_id < 100 )
...
ADD CONSTRAINT mos_expired_1_check CHECK (expired = true AND type_id < 100 )

If I now run a SELECT * from mos WHERE expired = true AND type_id = 34 everything works like expected, just mos_expired_1 is hit.

Result  (cost=0.00..19.77 rows=2 width=627)
  ->  Append  (cost=0.00..19.77 rows=2 width=627)
        ->  Seq Scan on mos  (cost=0.00..11.50 rows=1 width=627)
              Filter: (expired AND (type_id = 34))
        ->  Index Scan using index_mos_expired_1_on_type_id_and_region_id on mos_expired_1 mos  (cost=0.00..8.27 rows=1 width=627)
              Index Cond: (type_id = 34)
              Filter: expired

Strangly enought SELECT * from mos WHERE expired = false AND type_id = 34 does not work. EXPLAIN reveals开发者_如何学Go that both mos_expired_1 and mos_active_1 are queried.

Result  (cost=0.00..2464.71 rows=5863 width=150)
  ->  Append  (cost=0.00..2464.71 rows=5863 width=150)
        ->  Seq Scan on mos  (cost=0.00..11.50 rows=1 width=627)
              Filter: ((NOT expired) AND (type_id = 34))
        ->  Index Scan using index_mos_expired_1_on_type_id_and_region_id on mos_expired_1 mos  (cost=0.00..8.27 rows=1 width=627)
              Index Cond: (type_id = 34)
              Filter: (NOT expired)
        ->  Bitmap Heap Scan on mos_active_1 mos  (cost=113.68..2444.95 rows=5861 width=150)
              Recheck Cond: (type_id = 34)
              Filter: (NOT expired)
              ->  Bitmap Index Scan on index_mos_active_1_on_type_id  (cost=0.00..112.22 rows=5861 width=0)
                    Index Cond: (type_id = 34)

complete SQL (besides the actual mos table creation)

I would really like to know if I am missing something or it's a query planner issue.

UPDATE: I was able to reproduce the same problem with a much simpler example, just two tables having one constraint based on expired:

CREATE TABLE mos (type_id   INTEGER UNIQUE, expired boolean);
CREATE TABLE mos_expired_1 ( CHECK ( expired = true  ) ) INHERITS (mos);
CREATE TABLE mos_active_1 ( CHECK ( expired = false ) ) INHERITS (mos);
INSERT INTO mos_expired_1 (type_id,expired) VALUES(1, true);
INSERT INTO mos_active_1 (type_id,expired) VALUES(2, false);
EXPLAIN SELECT * from mos where expired = false;
EXPLAIN SELECT * from mos where expired = true;


It looks like in PostgreSQL constraint exclusion on booleans does not work. When you use integer or enum then it works well.

Integer:

CREATE TABLE mos (type_id INTEGER UNIQUE, expired int);
CREATE TABLE mos_expired_1 ( CHECK ( expired = 0 ) ) INHERITS (mos);
CREATE TABLE mos_active_1 ( CHECK ( expired = 1 ) ) INHERITS (mos); 
INSERT INTO mos_expired_1 (type_id,expired) VALUES(1, 0);
INSERT INTO mos_active_1 (type_id,expired) VALUES(2, 1); 
analyze;

EXPLAIN SELECT * from mos where expired = 0;
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Result  (cost=0.00..37.76 rows=12 width=8)
   ->  Append  (cost=0.00..37.76 rows=12 width=8)
         ->  Seq Scan on mos  (cost=0.00..36.75 rows=11 width=8)
               Filter: (expired = 0)
         ->  Seq Scan on mos_expired_1 mos  (cost=0.00..1.01 rows=1 width=8)
               Filter: (expired = 0)
(6 rows)

EXPLAIN SELECT * from mos where expired = 1;
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Result  (cost=0.00..37.76 rows=12 width=8)
   ->  Append  (cost=0.00..37.76 rows=12 width=8)
         ->  Seq Scan on mos  (cost=0.00..36.75 rows=11 width=8)
               Filter: (expired = 1)
         ->  Seq Scan on mos_active_1 mos  (cost=0.00..1.01 rows=1 width=8)
               Filter: (expired = 1)
(6 rows)

Enum:

CREATE TYPE mybool AS ENUM ('true', 'false');                                     
CREATE TABLE mos (type_id INTEGER UNIQUE, expired mybool);
CREATE TABLE mos_expired_1 ( CHECK ( expired = 'true' ) ) INHERITS (mos);
CREATE TABLE mos_active_1 ( CHECK ( expired = 'false' ) ) INHERITS (mos);
INSERT INTO mos_expired_1 (type_id,expired) VALUES(1, 'true');
INSERT INTO mos_active_1 (type_id,expired) VALUES(2, 'false');
analyze;

EXPLAIN SELECT * from mos where expired = 'true';
                                 QUERY PLAN                                  
-----------------------------------------------------------------------------
 Result  (cost=0.00..37.76 rows=12 width=8)
   ->  Append  (cost=0.00..37.76 rows=12 width=8)
         ->  Seq Scan on mos  (cost=0.00..36.75 rows=11 width=8)
               Filter: (expired = 'true'::mybool)
         ->  Seq Scan on mos_expired_1 mos  (cost=0.00..1.01 rows=1 width=8)
               Filter: (expired = 'true'::mybool)
(6 rows)

EXPLAIN SELECT * from mos where expired = 'false';
                                 QUERY PLAN                                 
----------------------------------------------------------------------------
 Result  (cost=0.00..37.76 rows=12 width=8)
   ->  Append  (cost=0.00..37.76 rows=12 width=8)
         ->  Seq Scan on mos  (cost=0.00..36.75 rows=11 width=8)
               Filter: (expired = 'false'::mybool)
         ->  Seq Scan on mos_active_1 mos  (cost=0.00..1.01 rows=1 width=8)
               Filter: (expired = 'false'::mybool)
(6 rows)

I don't know if it is a bug. I think it is not documented anywhere.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜