Range : The table is partitioned into "range" defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions
List : According to a key in table, ex : country, sales
Hash : The partition specifying a modulus and a reminder for each partition.
Table Inheritance
createtablemaster ( pk INTEGERprimary key , tag text, parent integer);createtablemaster_child() inherits (master);ALTERTABLE public.master_childADDPRIMARY KEY (pk);select*frommaster;select*from master_child;insert intomaster (pk, tag, parent) values (1,'pencil',0);insert into master_child (pk, tag, parent) values (2,'pen',0);updatemasterset tag ='monitor'where pk =2;select*from only master;select*from only master_child;-- errordroptablemaster;droptablemaster cascade ;
Range Partitioning
createtableemployees_range ( id bigserial, birth_date DATENOT NULL , country_code VARCHAR(2) NOT NULL) PARTITIONBYRANGE (birth_date);CREATETABLEemployee_range_y2000PARTITION of employees_range forvaluesfrom ('2000-01-01') to ('2001-01-01');CREATETABLEemployee_range_y2001PARTITION of employees_range forvaluesfrom ('2001-01-01') to ('2002-01-01');insert into employees_range (birth_date, country_code) values('2000-01-01','US'),('2000-01-02','US'),('2000-12-31','US'),('2000-01-01','US'),('2001-01-01','US'),('2001-01-02','US'),('2001-12-31','US'),('2001-01-01','US');select*from employees_range;select*from only employees_range; -- you will get nothingselect*from employee_range_y2000;select*from employee_range_y2001;explain analyze select*from employees_range where birth_date ='2001-01-01';
List Partitioning
createtableemployee_list ( id bigserial, birth_date DATENOT NULL , country_code VARCHAR(2) not null) PARTITIONBY LIST ( country_code );createtableemployee_list_euPARTITION of employee_listforvaluesin ('UK','DE');createtableemployee_list_usPARTITION of employee_listforvaluesin ('US','BZ');
Hash Partitioning
createtableemployee_hash ( id bigserial, birth_date DATENOT NULL, country_code varchar(2) not null) PARTITIONBYHASH (id);createtableemployee_hash_0partition of employee_hashforvalueswith (modulus 3, remainder 0);createtableemployee_hash_1partition of employee_hashforvalueswith (modulus 3, remainder 1);createtableemployee_hash_2partition of employee_hashforvalueswith (modulus 3, remainder 2);
Default Partitioning
createtableemployee_default_partpartition of employee_list default;
Multilevel Partitioning
createtableemployee_list_master ( id bigserial, birth_date DATENOT NULL , country_code VARCHAR(2) not null) PARTITIONBY LIST ( country_code );createtableemployee_list_eu_mPARTITION of employee_list_masterforvaluesin ('UK','DE')partitionbyhash (id);createtableemployee_list_us_mPARTITION of employee_list_masterforvaluesin ('US','BZ');createtableemployee_hash_0_mpartition of employee_list_eu_mforvalueswith (modulus 3, remainder 0);createtableemployee_hash_1_mpartition of employee_list_eu_mforvalueswith (modulus 3, remainder 1);createtableemployee_hash_2_mpartition of employee_list_eu_mforvalueswith (modulus 3, remainder 2);
Attach and DeAttach Partitions
createtableemployees_list_sppartition of employee_listforvaluesin ('SP');insert into employee_list (birth_date, country_code) values ('2001-01-01','SP');createtableemployees_list_inpartition of employee_listforvaluesin ('IN');createtableemployees_list_defaultpartition of employee_list defaultALTERTABLE employee_list detach partition employees_list_in;
Altering Partitions
createtablet1( a int, b int) partitionbyrange (a);createtablet1p1partition of t1 forvaluesfrom (0) to (1000);createtablet1p2partition of t1 forvaluesfrom (2000) to (3000);insert into t1 (a, b)values (1, 1);-- detach-- alter-- attachBEGINTRANSACTION ;ALTERTABLE T1 DETACH PARTITION t1p1;ALTERTABLE t1 attachpartition t1p1 forvaluesFROM (0) to (200);commitTRANSACTION;
Partition Indexes
Creating an index on the master/parent table will automatically create same indexes to every attached table partition
PostgreSQL doesnt allow a way to create a single index covering every partition of the parent table. You have to create indexes for each table