Sub topic 1
Why partition a PostgreSQL table?
Prerequisites
This chapter assumes that you can define PostgreSQL tables, use primary keys and indexes, read basic EXPLAIN output, and run SQL through a database client. You should also understand that a table stores rows and that an index provides an alternative access path to those rows.
The problem partitioning addresses
A large table can become difficult to query and maintain as it grows. For example, an orders table may contain many years of data, while most queries use only the current month. An index can help locate matching rows, but PostgreSQL may still need to maintain a very large index and scan substantial table or index structures.
Declarative partitioning divides one logical table into smaller physical tables called partitions. PostgreSQL exposes them through a single parent table, so applications can continue to query orders. PostgreSQL routes each inserted row to the appropriate partition according to a partition key.
CREATE TABLE orders (
order_id bigint NOT NULL,
customer_id bigint NOT NULL,
order_date date NOT NULL,
amount numeric(12, 2) NOT NULL
) PARTITION BY RANGE (order_date);
CREATE TABLE orders_2026_01
PARTITION OF orders
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TABLE orders_2026_02
PARTITION OF orders
FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');The parent table defines the logical schema and partitioning rule. Each child table stores a subset of the rows. In this example, the lower date bound is inclusive and the upper bound is exclusive.
Main benefits
Partitioning is most useful when the partition key matches common access or maintenance patterns.
| Benefit | How it helps |
|---|---|
| Partition pruning | PostgreSQL can skip partitions that cannot contain rows matching a query condition. |
| Faster retention work | An old time-based partition can be detached or removed without deleting rows individually. |
| Smaller local structures | Indexes and table data are divided into smaller physical objects. |
| Operational isolation | Maintenance can target one partition instead of the entire logical table. |
For example, a query with a narrow order_date range may need to inspect only the January partition. This behavior is called partition pruning. It reduces the amount of data considered by the planner and executor, but it depends on a usable predicate and accurate partition bounds.
Partitioning also makes retention policies easier to implement. Instead of running a large DELETE for expired data, you can detach an entire partition and archive or drop it after checking dependencies and backup requirements.
Costs and trade-offs
Partitioning adds design and operational complexity. You must create partitions before rows need them, or provide a deliberate strategy for unmatched rows. Poorly chosen bounds can cause insert failures or route data unexpectedly.
Partitioned tables also do not automatically make every query faster. A query that lacks a condition on the partition key may scan every partition. Too many partitions can increase planning time and complicate backups, monitoring, indexing, and schema changes. Partitioning should support a clear workload or lifecycle need, not replace ordinary indexing.
Exercise
A table stores ten years of event data, but reports normally read only the last seven days. What potential benefit does time-based partitioning provide, and what limitation remains?
Show answer
PostgreSQL may prune older partitions when the report filters by the event timestamp, reducing the data it scans. Queries without a usable timestamp predicate may still scan every partition, and the partitions must be created and maintained.