Skip to content

pg_partman for rolling partitions on PostgreSQL

Use pg_partman to auto-create future partitions and retain old ones on PostgreSQL — config, maintenance jobs, retention policies, and migration from manual partitioning.

Declarative partitioning gives you the shape; pg_partman gives you the operational glue. It creates future partitions ahead of time, detaches and drops old ones according to a retention policy, and orchestrates the whole partition lifecycle via a single periodic maintenance call. This article covers the install on a fresh hypertable, the migration path from hand-rolled partitioning, and the operational habits that keep the maintenance job healthy.

How to verify

Check that pg_partman is installed and inspect the parent’s configuration:

psql -d app -c "SELECT extversion FROM pg_extension WHERE extname='pg_partman';"
psql -d app -c "SELECT * FROM partman.part_config WHERE parent_table='public.events';"
psql -d app -c "SELECT count(*) FROM pg_inherits
                WHERE inhparent = 'events'::regclass;"
psql -d app -c "SELECT job_name, status, last_run_started_at, last_run_ended_at,
                       last_run_error_message
                FROM partman.show_partition_info('public.events');"

The partman.part_config row holds the partition strategy, retention, and the next partition to create.

What’s happening

pg_partman is a Postgres extension that maintains partition sets. You give it a parent table partitioned by RANGE, a partition interval (1 month, 1 day, 1 hour), and a target — how many “premake” partitions to keep ahead of now() and how much retention. The run_maintenance() procedure (or run_maintenance_proc() as a stored procedure call) checks every configured parent, creates any missing future partitions up to the premake horizon, optionally detaches and drops partitions past the retention boundary, and updates the catalog.

It works with declarative RANGE partitioning natively. For very old code using inheritance-based partitioning, pg_partman supports that too — but the migration path to declarative is the right answer for any new work.

The maintenance call needs to run on a schedule — pg_cron or systemd timers. Without it, the partition set stops growing, INSERTs at the boundary fail with “no partition found”.

The procedure

  1. Install and create the schema:

    sudo apt install postgresql-16-partman
    CREATE SCHEMA partman;
    CREATE EXTENSION pg_partman SCHEMA partman;

    The extension lives in its own schema by convention — keeps the catalog tidy.

  2. Set up the parent and let pg_partman create the children:

    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);
    
    SELECT partman.create_parent(
      p_parent_table => 'public.events',
      p_control      => 'created_at',
      p_type         => 'range',
      p_interval     => '1 month',
      p_premake      => 4
    );

    p_premake = 4 means pg_partman keeps 4 future months ready ahead of now(). On a monthly cadence that’s 4 months of headroom — enough that a missed maintenance run for a few weeks doesn’t break INSERTs.

  3. Configure retention:

    UPDATE partman.part_config
    SET retention            = '90 days',
        retention_keep_table = true,
        retention_keep_index = false,
        infinite_time_partitions = true
    WHERE parent_table = 'public.events';

    retention_keep_table = true detaches the old partition but doesn’t drop the table — useful when you want to archive before deletion. retention_keep_index = false drops the indexes on detached partitions to save space.

  4. Schedule maintenance. Via pg_cron:

    CREATE EXTENSION IF NOT EXISTS pg_cron;
    SELECT cron.schedule('partman-maintenance', '15 * * * *',
      $$CALL partman.run_maintenance_proc()$$);

    Or via systemd timer if pg_cron isn’t installed:

    ; /etc/systemd/system/partman.service
    [Service]
    Type=oneshot
    User=postgres
    ExecStart=/usr/bin/psql -d app -c "CALL partman.run_maintenance_proc()"

    We run it hourly on most engagements — daily is enough for monthly partitions but hourly gives slack for daily/hourly partitions and surfaces failures faster.

  5. Migrate an existing partitioned table: pg_partman has partition_data_proc() to move rows from a non-partitioned table into a new partitioned one in batches. For tables already partitioned by hand, just adopt them — create_parent with the existing parent and pg_partman picks up the children.

  6. Monitor the job: alert if partman.show_partition_info() shows an error or if last_run_ended_at is more than 2 hours old.

Common pitfalls

  • The maintenance job is silent on success — set up alerts on last_run_error_message IS NOT NULL and on stale last_run_ended_at.
  • A DEFAULT partition (catch-all for out-of-range rows) blocks ATTACH PARTITION for the range it overlaps. pg_partman warns; check it shows zero rows in the default before bulk-loading near a boundary.
  • infinite_time_partitions = true (recommended) means pg_partman keeps generating future partitions even if now() doesn’t reach them yet — without it, retention can collide with premake in odd ways.
  • Detach with CONCURRENTLY is the option you want; older pg_partman versions don’t pass it through. Verify on PG 14+ that detach is non-blocking.
  • pg_partman doesn’t manage indexes added after the parent was created — create_parent clones the indexes that existed at the time. Add a new index to the parent later? pg_partman won’t propagate it to future children unless you re-run apply_constraints or recreate the partition set.

In the engagements we run, pg_partman is the default for any RANGE-partitioned table we operate. We schedule maintenance hourly, alert on failures and staleness, and standardise on monthly partitions for events/metrics and daily for short-retention logs. Paired with declarative partitioning and BRIN indexes, the result is a partition set that maintains itself with no operator touch for years. For the operating model, see managed operations.