The best practices for using partition table in PostgreSQL
What is partition table?
Partition table is a table that is divided into smaller parts. Each part is called a partition.
Why use partition table?
- Performance: Partition table can improve the performance of the table. Because each partition can be stored in a separate file, the query can be executed faster.
- Maintenance: Partition table can make the maintenance of the table easier. Because each partition can be maintained separately, the maintenance of the table can be done faster.
How to create a partition table?
- Create a partition table:
1 | CREATE TABLE measurement ( |
- Create a partition table for each partition:
1 | CREATE TABLE measurement_y2006m02 PARTITION OF measurement |
Partition table types
There are 3 basic types of partition table in PostgreSQL:
- List Partition: The table is partitioned by explicitly listing which key value(s) appear in each partition.
- Range Partition: The table is partitioned into “ranges” defined by a key column or set of columns, with no overlap between the ranges of values assigned to different partitions.
- Hash Partition: The table is partitioned by specifying a modulus and a remainder for each partition.
Combine these 3 basic types of partition table, we can create more complex partition table. This is called Composite Partitioning.
There are 4 types of composite partitioning:
- List-Range Partition: The partition is divided into several ranges.
The step is: create table A partition by list, then create table A1 partition of A partition by range, then create table A1-1 partition of A1 - Range-List Partition: The partition is divided into several ranges.
The step is: create table A partition by range, then create table B of table A but partition by list. - Range-Hash Partition: The partition is divided into several ranges.
- Hash-Range Partition: The partition is divided into several ranges.
Data dictionary about partition table
- pg_partitioned_table: This table stores the information about the partitioned table.
- pg_partition_rule: This table stores the information about the partition rule.
- pg_partition_column: This table stores the information about the partition column.
- pg_partition_index: This table stores the information about the partition index.
- pg_partition_constraint: This table stores the information about the partition constraint.
- pg_partition_constraint_column: This table stores the information about the partition constraint column.
- pg_partition_constraint_index: This table stores the information about the partition constraint index.