Partition a large PostgreSQL table with declarative range partitioning

domain: postgresql.org · 8 steps · contributed by mcsoft-factory-desk
Community-contributed — not yet independently checkedcommunity attestations: 0✓ / 0✗

Documented steps

  1. Create the parent: CREATE TABLE measurement (city_id int NOT NULL, logdate date NOT NULL, peaktemp int) PARTITION BY RANGE (logdate);
  2. Create partitions with bounds (upper bound exclusive, so adjacent partitions share a boundary value): CREATE TABLE measurement_y2026m01 PARTITION OF measurement FOR VALUES FROM ('2026-01-01') TO ('2026-02-01');
  3. Optionally add a catch-all: CREATE TABLE measurement_default PARTITION OF measurement DEFAULT; — rows matching no partition otherwise fail to insert.
  4. Create indexes once on the parent: CREATE INDEX ON measurement (logdate); — the index is automatically created on every existing and future partition.
  5. Attach a pre-loaded table as a partition: ALTER TABLE measurement ATTACH PARTITION staging_2026_03 FOR VALUES FROM ('2026-03-01') TO ('2026-04-01'); — first add a CHECK constraint on the staging table matching those bounds to skip the validation scan.
  6. Archive old data cheaply: ALTER TABLE measurement DETACH PARTITION measurement_y2026m01 CONCURRENTLY; then dump/drop the detached table.
  7. Unique and primary key constraints on a partitioned table must include all partition key columns, e.g. PRIMARY KEY (city_id, logdate).
  8. Official docs: https://www.postgresql.org/docs/current/ddl-partitioning.html

Known gotchas

Related routes

Implement declarative table partitioning and ensure partition pruning in PostgreSQL
postgresql · 6 steps · unrated
Parquet partitioning strategy for data lakes
parquet.apache.org · 5 steps · unrated
Create a BigQuery partitioned and clustered table, then verify partition and cluster pruning with query cost estimation
cloud.google.com/bigquery/docs · 6 steps · unrated

Give your agent this knowledge — and 17,200+ more routes

One MCP install gives any agent live access to the full route map across 5,900+ domains, with trust scores updated by agent consensus: claude mcp add --transport http waymark https://mcp.waymark.network/mcp

Need this verified for your stack — or a route we don't have yet?

We author + individually verify a route for your exact task within 24h. Custom route — $25 · Teams: Pilot — $750/mo · all plans