Module A-14·19 min read

The operational difference between designing a partition and maintaining 500 of them at scale.

JJS
Written by Jatin Jain Saraf · Senior Software Engineer

Module 14 — Advanced Partitioning: Pruning, Maintenance, and pg_partman

What this module covers: Module 7 introduced partitioning strategies. This module goes deeper into the mechanics that make partitioning actually work at scale: how the query planner prunes partitions (and what silently breaks it), declarative partitioning vs inheritance-based partitioning and when each applies, constraint exclusion, and the operational reality of maintaining hundreds of partitions over months and years using pg_partman.


Declarative Partitioning vs Inheritance-Based Partitioning

Postgres has two partitioning systems with very different operational profiles.

Inheritance-Based Partitioning (Pre-PG10)

Before PostgreSQL 10, partitioning was implemented via table inheritance and CHECK constraints:

sql

Problems with inheritance partitioning:

  • INSERT routing requires a trigger on every insert (trigger overhead on every write)
  • The trigger must be manually updated every time a new partition is added
  • Unique constraints cannot span partitions
  • Foreign keys to/from partitioned tables are not enforced
  • No automatic partition pruning in the query planner (requires constraint_exclusion = on)

Declarative Partitioning (PG10+)

Declarative partitioning is a first-class feature. The database handles routing, pruning, and constraint enforcement natively:

sql

Advantages of declarative over inheritance:

  • No trigger required for INSERT routing (handled by the executor natively)
  • Primary key and unique constraints work (must include partition key)
  • Foreign keys to partitioned tables are supported (PG12+)
  • Native partition pruning — no constraint_exclusion needed
  • ATTACH PARTITION / DETACH PARTITION for online maintenance

When to still use inheritance: very old databases still on PG9.x (rare), or when you need a child table to have additional columns beyond the parent (inheritance allows this, declarative does not).


Partition Pruning: How It Works and What Breaks It

Partition pruning is the planner optimization that skips scanning irrelevant partitions. It is the primary reason to partition: a query on timestamp > '2026-05-01' should only scan transactions_2026, not all historical partitions.

How Pruning Works

The planner examines the partition key predicate and eliminates partitions whose bounds cannot contain matching rows.

sql

What Silently Breaks Pruning

1. Type mismatch between predicate and partition key:

sql

2. Function wrapping the partition key:

sql

3. OR conditions across partition key:

sql

4. Partition key in a subquery:

sql

5. Runtime parameter (Postgres < 14):

sql

Sign in to keep reading

The rest of this module is free — sign in with Google to unlock it and track your progress.

Sign in & Register

Discussion

0

Join the discussion

Loading comments...

© 2026 Jatin Jain Saraf (JJS). All rights reserved.