Explain the concept and applications of table partitioning.

Instruction: Describe what table partitioning is and provide examples of how and when it should be used.

Context: This question tests the candidate's knowledge of advanced database design techniques, specifically table partitioning, and their ability to apply these concepts to improve query performance and management.

Updated

Official answer

Partitioning divides a logical table into physical pieces by a key. In PostgreSQL, monthly range partitioning can support date-scoped queries and retention:

CREATE TABLE events (
  event_date date NOT NULL,
  event_id bigint NOT NULL,
  payload text,
  PRIMARY KEY (event_date, event_id)
) PARTITION BY RANGE (event_date);
CREATE TABLE events_2026_01 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');

The lower bound is inclusive and the upper bound exclusive. Inserting a date without a matching partition fails unless a suitable default partition exists. Provision future partitions and monitor that process.

Predicates on the partition key can let the planner/executor skip irrelevant partitions. Queries spanning all dates may gain little, and excessive partitions add planning and maintenance cost. Index design is still necessary. The primary key includes the partition key; this example does not make event_id globally unique by itself.

Partitioning is not replication or automatic distribution across servers. Retention operations can make data inaccessible or remove it, so coordinate them with recovery and dependency requirements. Validate pruning and representative performance rather than promising a speedup.

Related Questions