create table goliath (id serial primary key, val text) partition by hash (id);

create table goliath_0 partition of goliath for values with (modulus 100, remainder 0);
create table goliath_1 partition of goliath for values with (modulus 100, remainder 1);
create table goliath_2 partition of goliath for values with (modulus 100, remainder 2);
create table goliath_3 partition of goliath for values with (modulus 100, remainder 3);
create table goliath_4 partition of goliath for values with (modulus 100, remainder 4);
create table goliath_5 partition of goliath for values with (modulus 100, remainder 5);
create table goliath_6 partition of goliath for values with (modulus 100, remainder 6);
create table goliath_7 partition of goliath for values with (modulus 100, remainder 7);
create table goliath_8 partition of goliath for values with (modulus 100, remainder 8);
create table goliath_9 partition of goliath for values with (modulus 100, remainder 9);
create table goliath_10 partition of goliath for values with (modulus 100, remainder 10);
create table goliath_11 partition of goliath for values with (modulus 100, remainder 11);
create table goliath_12 partition of goliath for values with (modulus 100, remainder 12);
create table goliath_13 partition of goliath for values with (modulus 100, remainder 13);
create table goliath_14 partition of goliath for values with (modulus 100, remainder 14);
create table goliath_15 partition of goliath for values with (modulus 100, remainder 15);
create table goliath_16 partition of goliath for values with (modulus 100, remainder 16);
create table goliath_17 partition of goliath for values with (modulus 100, remainder 17);
create table goliath_18 partition of goliath for values with (modulus 100, remainder 18);
create table goliath_19 partition of goliath for values with (modulus 100, remainder 19);
create table goliath_20 partition of goliath for values with (modulus 100, remainder 20);
create table goliath_21 partition of goliath for values with (modulus 100, remainder 21);
create table goliath_22 partition of goliath for values with (modulus 100, remainder 22);
create table goliath_23 partition of goliath for values with (modulus 100, remainder 23);
create table goliath_24 partition of goliath for values with (modulus 100, remainder 24);
create table goliath_25 partition of goliath for values with (modulus 100, remainder 25);
create table goliath_26 partition of goliath for values with (modulus 100, remainder 26);
create table goliath_27 partition of goliath for values with (modulus 100, remainder 27);
create table goliath_28 partition of goliath for values with (modulus 100, remainder 28);
create table goliath_29 partition of goliath for values with (modulus 100, remainder 29);
create table goliath_30 partition of goliath for values with (modulus 100, remainder 30);
create table goliath_31 partition of goliath for values with (modulus 100, remainder 31);
create table goliath_32 partition of goliath for values with (modulus 100, remainder 32);
create table goliath_33 partition of goliath for values with (modulus 100, remainder 33);
create table goliath_34 partition of goliath for values with (modulus 100, remainder 34);
create table goliath_35 partition of goliath for values with (modulus 100, remainder 35);
create table goliath_36 partition of goliath for values with (modulus 100, remainder 36);
create table goliath_37 partition of goliath for values with (modulus 100, remainder 37);
create table goliath_38 partition of goliath for values with (modulus 100, remainder 38);
create table goliath_39 partition of goliath for values with (modulus 100, remainder 39);
create table goliath_40 partition of goliath for values with (modulus 100, remainder 40);
create table goliath_41 partition of goliath for values with (modulus 100, remainder 41);
create table goliath_42 partition of goliath for values with (modulus 100, remainder 42);
create table goliath_43 partition of goliath for values with (modulus 100, remainder 43);
create table goliath_44 partition of goliath for values with (modulus 100, remainder 44);
create table goliath_45 partition of goliath for values with (modulus 100, remainder 45);
create table goliath_46 partition of goliath for values with (modulus 100, remainder 46);
create table goliath_47 partition of goliath for values with (modulus 100, remainder 47);
create table goliath_48 partition of goliath for values with (modulus 100, remainder 48);
create table goliath_49 partition of goliath for values with (modulus 100, remainder 49);
create table goliath_50 partition of goliath for values with (modulus 100, remainder 50);
create table goliath_51 partition of goliath for values with (modulus 100, remainder 51);
create table goliath_52 partition of goliath for values with (modulus 100, remainder 52);
create table goliath_53 partition of goliath for values with (modulus 100, remainder 53);
create table goliath_54 partition of goliath for values with (modulus 100, remainder 54);
create table goliath_55 partition of goliath for values with (modulus 100, remainder 55);
create table goliath_56 partition of goliath for values with (modulus 100, remainder 56);
create table goliath_57 partition of goliath for values with (modulus 100, remainder 57);
create table goliath_58 partition of goliath for values with (modulus 100, remainder 58);
create table goliath_59 partition of goliath for values with (modulus 100, remainder 59);
create table goliath_60 partition of goliath for values with (modulus 100, remainder 60);
create table goliath_61 partition of goliath for values with (modulus 100, remainder 61);
create table goliath_62 partition of goliath for values with (modulus 100, remainder 62);
create table goliath_63 partition of goliath for values with (modulus 100, remainder 63);
create table goliath_64 partition of goliath for values with (modulus 100, remainder 64);
create table goliath_65 partition of goliath for values with (modulus 100, remainder 65);
create table goliath_66 partition of goliath for values with (modulus 100, remainder 66);
create table goliath_67 partition of goliath for values with (modulus 100, remainder 67);
create table goliath_68 partition of goliath for values with (modulus 100, remainder 68);
create table goliath_69 partition of goliath for values with (modulus 100, remainder 69);
create table goliath_70 partition of goliath for values with (modulus 100, remainder 70);
create table goliath_71 partition of goliath for values with (modulus 100, remainder 71);
create table goliath_72 partition of goliath for values with (modulus 100, remainder 72);
create table goliath_73 partition of goliath for values with (modulus 100, remainder 73);
create table goliath_74 partition of goliath for values with (modulus 100, remainder 74);
create table goliath_75 partition of goliath for values with (modulus 100, remainder 75);
create table goliath_76 partition of goliath for values with (modulus 100, remainder 76);
create table goliath_77 partition of goliath for values with (modulus 100, remainder 77);
create table goliath_78 partition of goliath for values with (modulus 100, remainder 78);
create table goliath_79 partition of goliath for values with (modulus 100, remainder 79);
create table goliath_80 partition of goliath for values with (modulus 100, remainder 80);
create table goliath_81 partition of goliath for values with (modulus 100, remainder 81);
create table goliath_82 partition of goliath for values with (modulus 100, remainder 82);
create table goliath_83 partition of goliath for values with (modulus 100, remainder 83);
create table goliath_84 partition of goliath for values with (modulus 100, remainder 84);
create table goliath_85 partition of goliath for values with (modulus 100, remainder 85);
create table goliath_86 partition of goliath for values with (modulus 100, remainder 86);
create table goliath_87 partition of goliath for values with (modulus 100, remainder 87);
create table goliath_88 partition of goliath for values with (modulus 100, remainder 88);
create table goliath_89 partition of goliath for values with (modulus 100, remainder 89);
create table goliath_90 partition of goliath for values with (modulus 100, remainder 90);
create table goliath_91 partition of goliath for values with (modulus 100, remainder 91);
create table goliath_92 partition of goliath for values with (modulus 100, remainder 92);
create table goliath_93 partition of goliath for values with (modulus 100, remainder 93);
create table goliath_94 partition of goliath for values with (modulus 100, remainder 94);
create table goliath_95 partition of goliath for values with (modulus 100, remainder 95);
create table goliath_96 partition of goliath for values with (modulus 100, remainder 96);
create table goliath_97 partition of goliath for values with (modulus 100, remainder 97);
create table goliath_98 partition of goliath for values with (modulus 100, remainder 98);
create table goliath_99 partition of goliath for values with (modulus 100, remainder 99);

create table david (id serial primary key, val text) partition by hash (id);

create table david_0 partition of david for values with (modulus 100, remainder 0);
create table david_1 partition of david for values with (modulus 100, remainder 1);
create table david_2 partition of david for values with (modulus 100, remainder 2);
create table david_3 partition of david for values with (modulus 100, remainder 3);
create table david_4 partition of david for values with (modulus 100, remainder 4);
create table david_5 partition of david for values with (modulus 100, remainder 5);
create table david_6 partition of david for values with (modulus 100, remainder 6);
create table david_7 partition of david for values with (modulus 100, remainder 7);
create table david_8 partition of david for values with (modulus 100, remainder 8);
create table david_9 partition of david for values with (modulus 100, remainder 9);
create table david_10 partition of david for values with (modulus 100, remainder 10);
create table david_11 partition of david for values with (modulus 100, remainder 11);
create table david_12 partition of david for values with (modulus 100, remainder 12);
create table david_13 partition of david for values with (modulus 100, remainder 13);
create table david_14 partition of david for values with (modulus 100, remainder 14);
create table david_15 partition of david for values with (modulus 100, remainder 15);
create table david_16 partition of david for values with (modulus 100, remainder 16);
create table david_17 partition of david for values with (modulus 100, remainder 17);
create table david_18 partition of david for values with (modulus 100, remainder 18);
create table david_19 partition of david for values with (modulus 100, remainder 19);
create table david_20 partition of david for values with (modulus 100, remainder 20);
create table david_21 partition of david for values with (modulus 100, remainder 21);
create table david_22 partition of david for values with (modulus 100, remainder 22);
create table david_23 partition of david for values with (modulus 100, remainder 23);
create table david_24 partition of david for values with (modulus 100, remainder 24);
create table david_25 partition of david for values with (modulus 100, remainder 25);
create table david_26 partition of david for values with (modulus 100, remainder 26);
create table david_27 partition of david for values with (modulus 100, remainder 27);
create table david_28 partition of david for values with (modulus 100, remainder 28);
create table david_29 partition of david for values with (modulus 100, remainder 29);
create table david_30 partition of david for values with (modulus 100, remainder 30);
create table david_31 partition of david for values with (modulus 100, remainder 31);
create table david_32 partition of david for values with (modulus 100, remainder 32);
create table david_33 partition of david for values with (modulus 100, remainder 33);
create table david_34 partition of david for values with (modulus 100, remainder 34);
create table david_35 partition of david for values with (modulus 100, remainder 35);
create table david_36 partition of david for values with (modulus 100, remainder 36);
create table david_37 partition of david for values with (modulus 100, remainder 37);
create table david_38 partition of david for values with (modulus 100, remainder 38);
create table david_39 partition of david for values with (modulus 100, remainder 39);
create table david_40 partition of david for values with (modulus 100, remainder 40);
create table david_41 partition of david for values with (modulus 100, remainder 41);
create table david_42 partition of david for values with (modulus 100, remainder 42);
create table david_43 partition of david for values with (modulus 100, remainder 43);
create table david_44 partition of david for values with (modulus 100, remainder 44);
create table david_45 partition of david for values with (modulus 100, remainder 45);
create table david_46 partition of david for values with (modulus 100, remainder 46);
create table david_47 partition of david for values with (modulus 100, remainder 47);
create table david_48 partition of david for values with (modulus 100, remainder 48);
create table david_49 partition of david for values with (modulus 100, remainder 49);
create table david_50 partition of david for values with (modulus 100, remainder 50);
create table david_51 partition of david for values with (modulus 100, remainder 51);
create table david_52 partition of david for values with (modulus 100, remainder 52);
create table david_53 partition of david for values with (modulus 100, remainder 53);
create table david_54 partition of david for values with (modulus 100, remainder 54);
create table david_55 partition of david for values with (modulus 100, remainder 55);
create table david_56 partition of david for values with (modulus 100, remainder 56);
create table david_57 partition of david for values with (modulus 100, remainder 57);
create table david_58 partition of david for values with (modulus 100, remainder 58);
create table david_59 partition of david for values with (modulus 100, remainder 59);
create table david_60 partition of david for values with (modulus 100, remainder 60);
create table david_61 partition of david for values with (modulus 100, remainder 61);
create table david_62 partition of david for values with (modulus 100, remainder 62);
create table david_63 partition of david for values with (modulus 100, remainder 63);
create table david_64 partition of david for values with (modulus 100, remainder 64);
create table david_65 partition of david for values with (modulus 100, remainder 65);
create table david_66 partition of david for values with (modulus 100, remainder 66);
create table david_67 partition of david for values with (modulus 100, remainder 67);
create table david_68 partition of david for values with (modulus 100, remainder 68);
create table david_69 partition of david for values with (modulus 100, remainder 69);
create table david_70 partition of david for values with (modulus 100, remainder 70);
create table david_71 partition of david for values with (modulus 100, remainder 71);
create table david_72 partition of david for values with (modulus 100, remainder 72);
create table david_73 partition of david for values with (modulus 100, remainder 73);
create table david_74 partition of david for values with (modulus 100, remainder 74);
create table david_75 partition of david for values with (modulus 100, remainder 75);
create table david_76 partition of david for values with (modulus 100, remainder 76);
create table david_77 partition of david for values with (modulus 100, remainder 77);
create table david_78 partition of david for values with (modulus 100, remainder 78);
create table david_79 partition of david for values with (modulus 100, remainder 79);
create table david_80 partition of david for values with (modulus 100, remainder 80);
create table david_81 partition of david for values with (modulus 100, remainder 81);
create table david_82 partition of david for values with (modulus 100, remainder 82);
create table david_83 partition of david for values with (modulus 100, remainder 83);
create table david_84 partition of david for values with (modulus 100, remainder 84);
create table david_85 partition of david for values with (modulus 100, remainder 85);
create table david_86 partition of david for values with (modulus 100, remainder 86);
create table david_87 partition of david for values with (modulus 100, remainder 87);
create table david_88 partition of david for values with (modulus 100, remainder 88);
create table david_89 partition of david for values with (modulus 100, remainder 89);
create table david_90 partition of david for values with (modulus 100, remainder 90);
create table david_91 partition of david for values with (modulus 100, remainder 91);
create table david_92 partition of david for values with (modulus 100, remainder 92);
create table david_93 partition of david for values with (modulus 100, remainder 93);
create table david_94 partition of david for values with (modulus 100, remainder 94);
create table david_95 partition of david for values with (modulus 100, remainder 95);
create table david_96 partition of david for values with (modulus 100, remainder 96);
create table david_97 partition of david for values with (modulus 100, remainder 97);
create table david_98 partition of david for values with (modulus 100, remainder 98);
create table david_99 partition of david for values with (modulus 100, remainder 99);


insert into goliath select i, md5(i::text) from generate_series(1,10000000) s(i);
analyze goliath;

insert into david select id, md5(id::text) from (select (10000000 * random())::int id from generate_series(1,10000) s(i) group by id);
analyze david;

-- original merge
explain merge into goliath using (select * from david order by val) as david on david.id = goliath.id when matched then update set val = david.val when not matched then insert (id, val) values (david.id, david.val);

-- simplified
explain merge into goliath using david on david.id = goliath.id when matched then update set val = david.val when not matched then insert (id, val) values (david.id, david.val);
