Skip to content

PostgreSQL declarative partitioning for large tables

Design RANGE, LIST, and HASH partitioning on PostgreSQL 14+ with constraint exclusion, partition pruning, and maintenance workflows that hold at scale.

Tables that grow past a few hundred million rows become awkward — vacuum runs long, index bloat balloons, archival is messy, and a query that scans the whole heap is a slow query you cannot tune. Declarative partitioning, available since PostgreSQL 10 and steadily improved through 16, splits a logical table into physical children Postgres can prune at planning time. This article covers RANGE, LIST, and HASH layouts, the gotchas you only see in production, and the operational habits that keep partition sets healthy.

How to verify

Confirm your major version and inspect the current partitioning shape:

psql -d app -c "SELECT version();"
psql -d app -c "\d+ events"
psql -d app -c "SELECT relname, pg_size_pretty(pg_total_relation_size(oid))
                FROM pg_class WHERE relkind = 'p';"
psql -d app -c "EXPLAIN (ANALYZE, BUFFERS)
                SELECT count(*) FROM events WHERE created_at >= '2026-05-01';"

If the EXPLAIN output shows Partitions removed: N and only the months you care about under the Append node, pruning is working.

What’s happening

A partitioned parent is a routing skeleton — it owns no rows of its own. Each child inherits the parent’s columns plus a partition bound (FOR VALUES FROM ... TO ..., FOR VALUES IN (...), or WITH (modulus N, remainder R)). Inserts go through the parent and Postgres routes the row to the child whose bound matches. Queries against the parent are planned across all children, but the planner removes ones whose bounds cannot satisfy the WHERE clause — that is partition pruning, and it happens at plan time (and at execution time when bind parameters arrive).

Indexes on a partitioned parent are templates: when you CREATE INDEX ON parent (col), Postgres creates the same index on every existing child and on any child attached later. Primary keys and unique constraints must include the partition key — if you partition by created_at, a unique index on id alone is not possible at the parent level; you need UNIQUE (id, created_at). Foreign keys pointing into a partitioned table are supported since 12; foreign keys from a partitioned table to another table work fine.

Pruning is not magic. It depends on the WHERE clause being a sargable expression on the partition key — WHERE created_at >= now() - interval '7 days' prunes, WHERE date_trunc('day', created_at) = '2026-06-01' does not, because the planner can’t reason through date_trunc.

The procedure

  1. Pick the partition key by access pattern, not by data shape. RANGE on a time column when queries filter by recent windows. LIST when rows belong to a small fixed set of tenants or regions. HASH only when you need even distribution across a fixed number of shards and never query by the key directly.

  2. Create the parent and the bounds:

    CREATE TABLE events (
      id         bigserial,
      tenant_id  uuid       NOT NULL,
      created_at timestamptz NOT NULL,
      payload    jsonb      NOT NULL,
      PRIMARY KEY (id, created_at)
    ) PARTITION BY RANGE (created_at);
    
    CREATE TABLE events_2026_06 PARTITION OF events
      FOR VALUES FROM ('2026-06-01') TO ('2026-07-01');
    CREATE TABLE events_2026_07 PARTITION OF events
      FOR VALUES FROM ('2026-07-01') TO ('2026-08-01');
  3. Add a default partition, but treat it as a tripwire. A DEFAULT partition catches rows that fit no bound — useful, but it blocks ATTACH PARTITION for any range overlapping rows already in the default. Monitor it and alert when it has rows.

  4. Index the children, not just the parent:

    CREATE INDEX ON events (tenant_id, created_at DESC);
    CREATE INDEX ON events USING BRIN (created_at) WITH (pages_per_range = 64);

    The BRIN index on created_at is cheap and prunes child blocks for analytical scans; see BRIN indexes for large tables.

  5. Roll new partitions ahead of time. A cron job (or pg_partman) that creates next month’s partition on the 15th of this month avoids INSERT failures at the boundary.

  6. Detach, do not drop, when archiving:

    ALTER TABLE events DETACH PARTITION events_2025_01 CONCURRENTLY;
    -- copy the standalone table to cold storage, then DROP TABLE

    CONCURRENTLY (PG 14+) avoids the AccessExclusiveLock that blocks SELECTs during a long detach.

Common pitfalls

  • Queries that filter on now() or current_date directly will prune at execution time only in PG 12+; older planners fall back to scanning every partition.
  • INSERT ... ON CONFLICT on partitioned tables only works against partition-local unique indexes — if the unique constraint must span all partitions, you can’t use it.
  • Indexes added to children manually are not automatically attached to a parent index; use ALTER INDEX parent_idx ATTACH PARTITION child_idx or always create through the parent.
  • Vacuum and analyze run per child — autovacuum scheduling matters more, not less, after partitioning.
  • pg_dump dumps the parent and all children; logical replication (PG 13+) replicates partitions individually unless publish_via_partition_root = true is set.

In the engagements we run, declarative partitioning is the default for any append-heavy table past 50 GB or 200 million rows. We standardise on RANGE-by-month for events and metrics, LIST-by-tenant for hard-isolation workloads, and pair every partitioned set with pg_partman, BRIN indexes, and a detach-to-cold-storage routine. That combination keeps autovacuum predictable and lets the warehouse pipeline read whole months in one extract. For a closer look at how we operate Postgres in production, see managed operations.