Skip to main content

Partitioned tables

Queries that filter on the partition column skip the other partitions. Dropping a partition frees its space at once, where deleting the same rows would take hours and leave the table bloated. Use it for append-mostly data kept for a fixed time, such as events or logs. It needs PostgreSQL.

Declare a partitioned model​

type Event struct {
ID int64
OccurredAt time.Time
}

func (Event) Configure(b *ant.ModelBuilder[Event]) {
b.CompositeKey(ant.F(&Events.ID), ant.F(&Events.OccurredAt))
b.PartitionByRange(ant.F(&Events.OccurredAt))
}

var Events = ant.Fields[Event]()

Migrations create the parent table. Primary key and unique constraints must include the partition column, since PostgreSQL enforces uniqueness per partition, so the key is composite.

Create and drop months​

Partitions come and go on a schedule, so they aren't migrations and drift ignores them. postgres.MonthlyPartitions looks after them. A partition is named events_2026_10 and runs from midnight UTC on the first of the month. Partitions with other names are left alone.

months, err := postgres.MonthlyPartitions[Event](db)
if err != nil {
return err
}

created, err := months.Ensure(ctx, time.Now(), 3) // this month and the next three
existing, err := months.List(ctx) // what exists
dropped, err := months.DropBefore(ctx, cutoff) // whole months that ended by cutoff

Ensure is safe to repeat and to run from several instances. Run it at startup, since a row for a month with no partition is an error, and again on a schedule. DropBefore drops only months that ended at or before the cutoff.

Relate two partitioned tables​

A foreign key to a partitioned table needs the whole key, which a relation can't express. Keep the relation for loading and declare the constraint separately.

type Person struct {
ID int64
EventID int64
OccurredAt time.Time
Event *Event
}

func (Person) Configure(b *ant.ModelBuilder[Person]) {
b.PartitionByRange(ant.F(&Persons.OccurredAt))
b.BelongsTo(&Persons.Event).ForeignKey(&Persons.EventID).References(&Events.ID).NoForeignKey().NoIndex()
b.ForeignKey(ant.F(&Persons.EventID), ant.F(&Persons.OccurredAt)).
References(ant.F(&Events.ID), ant.F(&Events.OccurredAt)).
OnDelete(ant.Cascade)
}

var Persons = ant.Fields[Person]()

Children copy the parent's time, which puts both rows in matching months. Name both columns in joins, so PostgreSQL pairs each month with its own.

query := db.People.Query().
JoinOn[Event](ant.EqCol(&Persons.EventID, &Events.ID), ant.EqCol(&Persons.OccurredAt, &Events.OccurredAt))

Drop children first: DropBefore on Person, then on Event, or PostgreSQL refuses to drop a month the foreign key still points at.

Limits​

  • .Concurrently() doesn't work on a partitioned table.
  • A row outside every partition is an error. A default partition would hide that, then block creating the month later.
  • Filter on the partition column wherever you can, or PostgreSQL searches every month.

The audit example is built on all of this, with retention on a cron schedule.