PostgreSQL Partitioned Tables (How to Use Partitions to Improve Performance)
PostgreSQL is a database system that supports partitioning. Partitioning is a technique of splitting large tables into several smaller ones, which can significantly improve query performance.
Why do we need partitioning?
When the amount of data in a table becomes very large, queries may scan the entire table, leading to Performance Drop. By using partitioning, you can reduce the number of disk I/O operations required to find data, making the system more performant and scalable.
Improving performance with partitioning
1. Partition types
PostgreSQL supports the following partitioning types:
- Range Partition: Partitions based on value ranges. The range for each partition is from
starttoend.
*
CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(255),
data TYPE FLOAT4
);
ALTER TABLE mytable
SET ( partitioning = range ('2020-01-01'::date, '2021-12-31'::date));
- List Partition: Partitions based on a set of values. The value sets for each partition are defined by the
LISTfunction.
*
CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(255),
data TYPE FLOAT4
);
ALTER TABLE mytable
SET ( partitioning = list ('2020-01-01', '2020-12-31'));
- Time Partition: Partitions based on dates. Each partition corresponds to a year.
*
CREATE TABLE mytable (
id INT PRIMARY KEY,
name VARCHAR(255),
data TYPE FLOAT4
);
ALTER TABLE mytable
SET ( partitioning = time ('2020-01-01', '2021-12-31'));
2. Improving performance with partitioning
Using partitioning can significantly improve query performance, especially in the following cases:
- The total number of rows in small tables is greater than 10000
- The total number of rows in large tables is greater than 100000
The benefit of partitioning lies in organizing data into feasible sizes, which helps improve disk I/O efficiency and reduces performance issues caused by overly large tables.
3. Partition considerations
Here are the things to note when using partitioning:
- Ensure table structure is correct before creating partitions
- If no partition range or value set is specified, it defaults to the entire table
- When inserting data into the original table, you need to insert it into the appropriate partition
- Using partitioning can improve performance, but consider the cost of adding and removing partitions
4. Partition management
PostgreSQL provides a system view named pg_stat_user_tables that allows you to inspect current table structures, including row counts per table, space usage information, etc.
If management is required, use the ALTER TABLE statement to add or remove partitions:
-- Delete partition
ALTER TABLE mytable DROP PARTITION partition_name;
-- Add partition
ALTER TABLE mytable ADD PARTITION partition_name FOR VALUES FROM 'start' TO 'end';
5. Pros and cons of partitioning
Pros
- Significantly improve query performance
- Reduce disk I/O
- Improve scalability
Cons
- Increase table structure maintenance cost
- Consider the cost when adding or removing partitions
Before using partitioning, consider its pros and cons to determine if it is suitable for your system's needs.