rent
PostgreSQL extensions

pg_partman

What it is

pg_partman manages PostgreSQL declarative partitions. It creates future partitions, applies retention rules, and maintains time- or number-based partition sets as data grows.

What it provides

  • Automated creation and maintenance of child partitions
  • Time-based and numeric partition intervals
  • Retention policies and migration helpers for existing tables
  • An optional background worker for scheduled maintenance

Use it for event logs, audit tables, large append-only histories, time-bounded business records, or any table where predictable partition creation and retirement should be automated.

Use it with Rent

extension pg_partman {
  name = "pg_partman"
}

The RSL declaration makes installation and migration intent part of the reviewed application schema. Register the typed pack at runtime to call its APIs:

let partman = extensions.register_pack::<PgPartman>()?;
let events = partman
    .client(&pool)
    .partitioned_table("public.events");

events
    .create("created_at", "1 month")
    .await?;

partman
    .client(&pool)
    .run_maintenance()
    .await?;

Create the parent table with PostgreSQL's PARTITION BY RANGE clause before registering it with pg_partman. Calling run_maintenance directly does not require the optional pg_partman background worker. For automatic maintenance, configure that worker or schedule maintenance with pg_cron as an operational choice.

Run the application

cargo run -p rent --example extension_13_pg_partman

Set DATABASE_URL to a disposable PostgreSQL server with pg_partman available and a role permitted to create databases and install it. The application creates a monthly post_events partition set, runs maintenance, inserts an event, and checks that PostgreSQL routed it into a child partition. It also checks duplicate registration and invalid-parent errors. The isolated database is removed afterward. Its standalone extension declaration is crates/rent/examples/extension_13_pg_partman/schema.rsl.

On this page