Reading preferences
Concept

Partitioning and Pruning

Route date ranges and evaluate partition pruning and its trade-offs.

database, postgresql, partitioning, pruning, range, indexes

One logical table, separate ranges

Declarative partitioning routes rows to physical partitions by a key. Range bounds include the lower value and exclude the upper value. Pruning can exclude irrelevant partitions from a query; it is different from finding rows with an index inside a partition.

SQL illustration

In an authorised disposable session, this self-contained temporary example uses two monthly partitions:

CREATE TEMP TABLE events (id integer, event_day date)
  PARTITION BY RANGE (event_day);
CREATE TEMP TABLE events_jan PARTITION OF events
  FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
CREATE TEMP TABLE events_feb PARTITION OF events
  FOR VALUES FROM ('2026-02-01') TO ('2026-03-01');
INSERT INTO events VALUES (1, '2026-01-31'), (2, '2026-02-01');
SELECT id, event_day FROM events
WHERE event_day >= DATE '2026-02-01'
  AND event_day < DATE '2026-03-01'
ORDER BY id;

Prepared result: one row, (2, 2026-02-01). February 1 belongs to the February partition. With no default partition, inserting a date outside both ranges fails. Pruning is possible for this predicate, but an actual plan depends on settings and query details; inspect it rather than assuming.

See the reasoning

Follow the example one step at a time

Route a date and reason about which partitions a query can exclude. This is a prepared teaching demonstration, not live code execution.

Define non-overlapping ranges

January  [Jan 1, Feb 1)
February [Feb 1, Mar 1)

Range lower bounds are inclusive and upper bounds are exclusive.

Choose boundaries for operations and queries

Range, list, and hash partitioning fit different access and retention patterns. Too many partitions increase planning and management work. Plan for future partitions, data movement, indexes, locking during maintenance, and recovery. Dropping a partition deletes its data and is not a backup. A primary key or unique constraint on a partitioned table has PostgreSQL-specific restrictions, including requirements concerning partition keys. Test representative queries and writes; partitioning alone does not guarantee a speedup.

Reflect on the concept

Four fixed questions to reinforce the reasoning. Answers are not saved or scored.

1. Which partition receives February 1 in the example?
2. What can partition pruning do?
3. Does partitioning make indexes unnecessary?
4. What is a trade-off of many partitions?

References and further reading