drop database if exists mergeappend;
create database mergeappend;


create table tab1_part1  (check (col1 between    1 and 1000)) inherits (tab1);
create table tab1_part2  (check (col1 between 1001 and 2000)) inherits (tab1);
create table tab1_part3  (check (col1 between 2001 and 3000)) inherits (tab1);
create table tab1_part4  (check (col1 between 3001 and 4000)) inherits (tab1);
create table tab1_part5  (check (col1 between 4001 and 5000)) inherits (tab1);
create table tab1_part6  (check (col1 between 5001 and 6000)) inherits (tab1);
create table tab1_part7  (check (col1 between 6001 and 7000)) inherits (tab1);
create table tab1_part8  (check (col1 between 7001 and 8000)) inherits (tab1);
create table tab1_part9  (check (col1 between 8001 and 9000)) inherits (tab1);
create table tab1_part10 (check (col1 between 9001 and 9999)) inherits (tab1);

insert into tab1_part1  select generate_series(   1, 1000); 
insert into tab1_part2  select generate_series(1001, 2000); 
insert into tab1_part3  select generate_series(2001, 3000); 
insert into tab1_part4  select generate_series(3001, 4000); 
insert into tab1_part5  select generate_series(4001, 5000); 
insert into tab1_part6  select generate_series(5001, 6000); 
insert into tab1_part7  select generate_series(6001, 7000); 
insert into tab1_part8  select generate_series(7001, 8000); 
insert into tab1_part9  select generate_series(8001, 9000); 
insert into tab1_part10 select generate_series(9001, 9999); 

create index idx1_tab1_part1  on tab1_part1(col1);
create index idx1_tab1_part2  on tab1_part2(col1);
create index idx1_tab1_part3  on tab1_part3(col1);
create index idx1_tab1_part4  on tab1_part4(col1);
create index idx1_tab1_part5  on tab1_part5(col1);
create index idx1_tab1_part6  on tab1_part6(col1);
create index idx1_tab1_part7  on tab1_part7(col1);
create index idx1_tab1_part8  on tab1_part8(col1);
create index idx1_tab1_part9  on tab1_part9(col1);
create index idx1_tab1_part10 on tab1_part10(col1);

analyze;
set enable_sort to off;

explain analyze 
select a.* from tab1 a, tab1 b
 where a.col1 = b.col1
--   and ((a.col1 >=  997 and a.col1 <= 1000) or 
--        (a.col1 >= 4999 and a.col1 <= 5000) or
--        (a.col1 >= 5995 and a.col1 <= 6000))
 order by a.col1