Skip to content

Partitioned tables

The module (Go package partitions, Java framework-partitions with the package io.flowcatalyst.framework.partitions) implements the partitions specification: range-partitioned tables whose partitions are created ahead of time and dropped whole (or archived to an object store first), with a critical health check that goes down when the next partition is missing.

Partition a table that is large, append-mostly and time-ordered, and that is queried by id or by a recent time window: readings, telemetry, logs, history, audit. The reason is retention: DELETE at volume costs as much WAL and vacuum as the data deleted, and dropping a partition is O(1).

Do not partition:

  • master data and small tables (the cost is complexity, the gain nothing);
  • tables mostly queried without the key (every query then scans every partition);
  • with an interval so small that there would be thousands of partitions (a table with daily partitions kept a year is 365; hourly is not supported for this reason).
  • ByID("rdg") (Java Key.byId("rdg")) partitions on the table’s id, <prefix>_<TSID>. The primary key stays id, the database still enforces its uniqueness, and WHERE id = $1 prunes to one partition. Use it for tables whose rows are made with the framework’s ids (Go id.New, Ids.Prefixed; Java Ids.generate("rdg")). Every id in the table must carry that prefix. In Java Key.byIdColumn("event_id", "evt") is the same on another column.
  • ByColumn("recorded_at") (Java Key.byColumn) partitions on a timestamptz column. PostgreSQL then requires every unique constraint, the primary key included, to contain that column: the primary key becomes (id, recorded_at) and id alone is no longer unique in the database.
  • ByDateColumn("service_day") (Java Key.byDateColumn) is the same on a date column: a service day, a business date. The bounds are date literals (every interval starts at UTC midnight, so they are exact), and you store the date itself rather than midnight UTC in a timestamp. A date column needs ByDateColumn and a timestamptz one ByColumn; start-up refuses the other pairing and a timestamp without time zone.

Day, Week (ISO, Monday to Monday), Month and Year (the calendar year, all in UTC); in Java Interval.DAY, WEEK, MONTH and YEAR. Pick the interval so that a table has tens, not thousands, of partitions: daily for a high-volume table kept weeks, monthly for most. Year is for long-retention, low-volume tables, such as one row per vehicle trip kept for seven years: Retention: partitions.DropAfter(7 * 366 * 24 * time.Hour) (Java Retention.dropAfter(Duration.ofDays(7 * 366))) is then eight partitions. The retention is at least one interval, so for Year at least 366 days; a partition goes only when its whole year is past the cutoff, so the oldest data on disk can be nearly a year older than the retention.

The framework never creates your table: the table is yours, in your migration, with the exact shape below. Start-up checks it and fails naming the DDL it expected.

A ByID table (note COLLATE "C" on the column: the bounds compare as text, only byte order is TSID order, and PostgreSQL prunes an id lookup only when the column itself is in the key’s collation):

CREATE TABLE readings (
id text COLLATE "C" PRIMARY KEY,
recorded_at timestamptz NOT NULL,
device_id text NOT NULL,
value double precision NOT NULL
) PARTITION BY RANGE (id);
CREATE INDEX readings_device_idx ON readings (device_id);

A ByColumn table:

CREATE TABLE telemetry (
id text NOT NULL,
recorded_at timestamptz NOT NULL,
payload jsonb NOT NULL,
PRIMARY KEY (id, recorded_at)
) PARTITION BY RANGE (recorded_at);

A partition is named <table>_p<YYYYMMDD> of its interval’s start (readings_p20261101), in the parent’s schema, for every interval.

Do not create a DEFAULT partition (start-up refuses a table that has one), and do not create partitions in the migration: the module creates them, including the first, before the application serves. In Java your table’s migration is part of your module’s migration set: DatabaseModule applies every module’s set when the database is built, and PartitionsModule validates the tables after that, so the order of the module list does not matter.

Tables are code, not configuration: there is no partitions section.

A table is a Table value; the module takes them in its builder, as LocksModule takes its stores:

App.builder()
.module(new DatabaseModule())
.module(
ObjectStoresModule.builder()
.store("archive", ObjectStoreFactories.memory()) // S3ObjectStores.s3(...) in production
.build())
.module(
LocksModule.builder()
.store("main", DatabaseLockStores.database()) // with LocksDatabaseModule
.build())
.module(
PartitionsModule.builder()
.table(
Table.named("readings")
.by(Key.byId("rdg"))
.interval(Interval.MONTH)
.ahead(3) // the current month and three more are always created
.retention(Retention.dropAfter(Duration.ofDays(90))))
.table(
Table.named("telemetry")
.by(Key.byColumn("recorded_at"))
.interval(Interval.WEEK)
.ahead(4)
.retention(Retention.archiveThenDropAfter(Duration.ofDays(180), "archive")))
.locks() // one replica runs the loop
.build())
.start();

The module contributes the migration set (module id partitions) that creates fw_partition_archives and fw_partition_passes on the default database instance. A table in another instance says so with .database("analytics"); migrations run on the default instance only, so for that instance create fw_partition_archives yourself if it archives, and fw_partition_passes (the last pass of its tables is not recorded without it; start-up warns once per instance). Make fw_partition_passes as in the module’s V0002__create_partition_passes_table.sql and V0003__add_partition_pass_trigger.sql: a table made in the shape of V0002 alone still records and reads, but without what ran the last pass and who, and start-up warns once that the columns pass_trigger and actor are missing.

At start-up the module checks every declaration (all problems at once, each naming table <name>, in a StartupException), validates every table against its database, runs one ensure pass before the application’s started hooks, adds a critical health check partitions:<table> per table (named as declared), and registers the background loop partitions (every hour by default; .every(Duration) changes it; the first pass is one minute after start). .locks() gates the loop with the service lock fw-partitions on the default lock store, so one replica runs each pass; .archiveSegmentBytes(n) sets the size an archive object is cut at. Metrics: fw_partitions_ahead{table}, fw_partition_failures_total{table,step} (ensure, retention, archive, record), fw_partitions_dropped_total, fw_partitions_archived_total and fw_partition_rows_archived_total.

Retention is required: a table without one does not start.

Retention Does
DropAfter(d) (Java Retention.dropAfter) drops a partition when its whole range ended at least d ago
ArchiveThenDropAfter(d, "store") (archiveThenDropAfter) writes the partition to the named object store as gzip’d NDJSON, verifies it, records it in fw_partition_archives, then drops it
KeepForever() (keepForever) removes nothing: the decision is written down

d is at least one interval. A partition is removed only when its whole range is past the cutoff, so with monthly partitions and DropAfter(90 days) the oldest data on disk is between 90 days and 90 days plus a month old. The current and future partitions are never removed whatever the clock says.

An archive is NDJSON (row_to_json, one object per line) in objects of about 16 MiB compressed (an object store takes whole values; partitions.ArchiveSegmentBytes(n) changes it, keep it below the store’s MaxBytes), at partitions/<schema>.<table>/<partition>.ndjson.gz and .part2.ndjson.gz, and so on (Java Partitions.archiveKey is the first). The partition is detached first and archived from the detached table, so a write cannot slip in between the archive and the drop (an insert into that range fails, an update through the parent finds nothing). A failure at any later step (the put, the read-back check, the manifest) drops nothing, leaves the table detached (it is not re-attached), is logged and counted (fw_partition_failures_total{step="archive"}), and is retried by the next pass; a pass that died after the detach is finished the same way (recovery: detached tables with this table’s partition names that are due and carry the mark the detach sets as their table comment; a table of such a name that the module did not detach is never touched). A partition whose archive keeps failing is never dropped: watch the counter. A detached table of a partition’s name in the current or future range makes the ensure pass fail loudly instead of leaving inserts without a partition.

An id lookup needs nothing. A time window is written with the bounds of the window as ids, so the planner prunes to the overlapping partitions:

Key.IdRange window = Key.idRange("rdg", from, to);
// SELECT id, value FROM readings WHERE id >= ? AND id < ? AND device_id = ? with window.lo(), window.hi()

Do not filter with a function of the id (substr(id, ...)): it defeats pruning. The window selects rows whose id carries a millisecond in [from, to); if the row’s recorded_at is not the moment the id was made, filter on recorded_at as well.

partitions:<table> is critical and is down when the partition for the next interval does not exist: inserts will fail from the boundary. It reads the catalog on every check. The loop normally heals it; a down check means the passes are failing (read background:partitions, the logs at ERROR and fw_partition_failures_total), or the loop is gated by a lock nobody holds, or the database user cannot create tables. fw_partitions_ahead{table} is how many partitions beyond the current one exist.

Rows older than the window the loop keeps need their partitions first:

EnsureResult res = partitions.ensure("readings", from, to); // the partitions covering [from, to]; at most 400
// res.created(): how many it created; res.alreadyDue(): how many of the range retention removes at the next pass

Retention removes a backfilled partition at the next pass if it is older than the retention, so import within it, or declare a longer one; res.AlreadyDue says how many partitions of the range are already past it, so warn before relying on them. The refusals are typed (partitions.ErrRangeInvalid, ErrRangeTooLarge, ErrTableNotDeclared, ErrDetachedLeftover; errors.Is). In Java the refusal is a PartitionRefusedException whose reason() is RANGE_INVALID, RANGE_TOO_LARGE, TABLE_NOT_DECLARED or DETACHED_LEFTOVER.

In Go ps.Status(ctx) and ps.TableStatus(ctx, "readings") read the state of the tables from the catalog (a fixed number of queries per table, never a row count): the declaration, Healthy (the health check’s own decision), the LastPass (recorded in the database, so any replica sees what the loop’s replica did; its Trigger is LOOP or MANUAL and its Actor is the principal id of a manual pass, both empty when not recorded; the row is overwritten by the next pass, so it shows who ran the latest pass and is not a history or an audit trail), every partition with its Position, size, estimated rows, RemovalDue (when retention removes it) and Archived, the detached tables a dead pass left and the strays. Key is the declared partitions.Key: read its Kind() (ID or COLUMN), Column(), Prefix() and ColumnType() (TIMESTAMPTZ or DATE) instead of parsing text. Rows is the sum of the partitions’ estimates and is nil when the table has partitions and none was analysed yet, which is not the same as an empty table; PartitionsWithoutEstimate says how many partitions have no estimate. ps.Archives(ctx, "telemetry", after, limit) lists the archived partitions, newest first, each with the key of its first object in the store.

In Java the same reads are partitions.status(), partitions.tableStatus("readings") and partitions.archives("telemetry", after, limit), and the fields are accessors: healthy(), lastPass() (with trigger() and actor()), position(), removalDue(), archived(), key() (kind(), column(), prefix(), columnType()), partitionsWithoutEstimate(). rows() is an OptionalLong, empty when the table has partitions and none was analysed yet, and trigger() and actor() are Optional. A partition dropped while the status is read is left out.

Two actions are for an operator, with the acting principal’s fw.Context for the record: Go ps.RunPass(ctx, by, "") (Java partitions.runPass(by, "")) runs the loop’s pass now (for one table, or every table with "") and returns a PassResult per table with the steps that failed; Go ps.EnsureAs(ctx, by, "readings", from, to) (Java partitions.ensureAs(by, "readings", from, to)) is Ensure for an operator. A manual pass never overlaps the loop’s: while one runs, here or on another replica, it is refused with partitions.ErrPassRunning (Java a PartitionRefusedException whose reason is PASS_RUNNING), never queued. Each action logs partitions: manual pass or partitions: manual ensure at INFO with the principal’s id (Java: its id and kind, the tier when the context has one, and the correlation id), the table, the range and the outcome; the framework has no audit store for an operator’s action, so that line is the record. Not provided, on purpose: dropping or detaching a partition by hand, changing a retention (it is code), restoring an archive.

The admin module puts all of this behind the application admin, headless: five typed operations (the tables, one table, its archives, run a pass, ensure a range), the permissions {app}:admin:partitions:view and :manage (Go partitionsadmin.View(app), Manage(app); Java PartitionsAdminModule.view(app), manage(app); so a server-rendered page gates on the same permission), and a navigation entry. In Go it is the package partitions/partitionsadmin. In Java it is the module framework-partitions-admin (PartitionsAdminModule, package io.flowcatalyst.framework.partitions.admin), a module of its own because framework-admin must not depend on framework-partitions; list it after AdminModule and PartitionsModule. A partition holds every client’s rows, so the section is operators-only: besides the permission the caller must be a platform operator (tier ANCHOR); a PARTNER, a CLIENT and a caller with no tier get 403 FORBIDDEN and the navigation does not show the section (the application admin, operators-only sections). It works in an ordinary admin area and in an operators-only one (Java AdminModule.Option.OPERATORS_ONLY).

On the wire the table’s key is an object (kind ID or COLUMN, column, prefix for an ID key, type TIMESTAMPTZ or DATE for a COLUMN key), lastPass has trigger and actor (omitted when not recorded), and rows is omitted when no partition has an estimate yet; partitionsWithoutEstimate is always there. The archives list is a page (items, nextCursor; limit 50, 1 to 200; after), newest first; at the largest page size (200) it still returns nextCursor when more follow.

App.builder()
.module(AdminModule.of(APPLICATION))
.module(new DatabaseModule())
.module(PartitionsModule.builder().table(readings).locks().build())
.module(PartitionsAdminModule.of(APPLICATION))
.start();

Declare the operators’ role, which holds these permissions, as a role that is not client-managed (never WithClientManaged()): client-managed roles are the ones a client’s own administrators may assign. With fc.AdminDefinitions(adm) and flowcatalyst.WithPermissionMapping(mapper) start-up refuses a client-managed role that holds the permissions, directly or through orders:admin:*:* in the mapping, naming the role and the permission. The suggested role admin that AdminDefinitions adds (not client-managed) holds them. In Java the same refusal applies with FlowCatalystModule.withPermissionMapping(mapper), and the suggested admin role holds the permissions.

A unique constraint on a partitioned table must contain the partition key. For a ByColumn table that rules out a unique business key on its own (a device serial, an external reference). Either include the timestamp in the key, or keep the business key in a small side table that is not partitioned and is written in the same transaction:

CREATE TABLE telemetry_refs (
external_ref text PRIMARY KEY,
id text NOT NULL
);

The use case inserts into telemetry_refs first; a duplicate fails there, in the database. The side table holds one narrow row per business key, so it can stay (or be pruned by its own, cheap job). ByID tables avoid the problem for id itself.

Planned, not built (in Go or in Java). The approach: a new partitioned parent, the old table attached as one partition with a pre-validated CHECK constraint (no rewrite), then the module creates the rest. See the large-table approach in the platform repository’s docs/large-table-schema-changes.md.

In Go, fwtest gives a real PostgreSQL. The module reads the application’s clock (fw.WithClock), so a test advances a fwtest.Clock and calls ps.Pass(ctx) (one ensure-then-retention pass, the loop’s work) to see partitions created and dropped. In Java, PostgresFixture (the framework-database test-jar) gives a real PostgreSQL; the module reads the application’s InstantSource (AppBuilder.instantSource), so a test moves a clock and calls partitions.pass().