Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Partitioning

Status: the declaration and explicit PARTITION OF bounds are persisted. A HASH ... SUBPARTITIONS n declaration creates its children synchronously; future interval partitions are maintained by the internal alarm worker. Routing, pruning, and policies are being expanded incrementally; RETENTION already performs a logical DETACH without deleting data.

Partitioning divides one logical table into physical relations. Queries and writes target the parent; the server routes rows, prunes partitions, and creates future partitions when an automatic policy is configured.

Automatic declaration

CREATE TABLE events (
    id         bigint,
    client_id  bigint NOT NULL,
    created_at timestamp NOT NULL,
    payload    jsonb,
    PRIMARY KEY (created_at, id)
)
PARTITION BY RANGE (created_at)
EVERY INTERVAL '1 month'
RETENTION '12 months';

`HASH ... SUBPARTITIONS n` materializes deterministic children such as `events_h0` through
`events_h<n-1>` during the `CREATE TABLE` DDL. For `RANGE`, the server does not invent bounds:
explicit children use `PARTITION OF`, while `EVERY` interval windows are created by the worker
when they become due. `EVERY` and `RETENTION` remain policy metadata. On the server, a persisted
occurrence in `sys.system_alarms` schedules the next window; the worker wakes at the alarm deadline
and runs the core `partition` handler. The short reconciliation interval only detects a
newly-created policy with an earlier deadline; it is not a daily maintenance scan.

The HASH definition is structural: its key, strategy, and bucket cardinality cannot be changed by
an ordinary `ALTER`. Changing it requires a planned tree migration or rebuild.

An explicit partition can inherit its parent columns:

```sql
CREATE TABLE events_2026_11
PARTITION OF events
FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');

In this slice, the parent link and bound are persisted; the child’s physical file is created by DDL, while the parent’s logical storage is not yet fully separated.

The first write path now routes INSERT ... VALUES, including statements whose rows belong to different partitions. Parent DELETE and UPDATE that does not change the partition key also scan the children. INSERT ... SELECT can use the same path when a DEFAULT RANGE partition exists; HASH uses its fixed buckets without requiring DEFAULT. Updates that move the key uses the move operator and reapplies the index, check, and foreign-key validation of an ordinary write. Specific coverage for those combinations is still being expanded in tests.

EVERY creates interval partitions. The short RETENTION form means DETACH: when the alarm runs, the partition is removed from the parent, but its relation and data remain available. The worker currently supports retention in months or days and never turns this operation into DROP. The alarm state, attempt count, and active lease are visible through sys.system_alarms.

The Web Console shows these alarms in the detail view of a partitioned table or collection, including kind, state (waiting, running, success, or failed), schedule, attempt, and error.

Explicit partitions

CREATE TABLE events (
    id         bigint,
    created_at timestamp NOT NULL,
    payload    jsonb
)
PARTITION BY RANGE (created_at);

CREATE TABLE events_2026_11
PARTITION OF events
FOR VALUES FROM ('2026-11-01') TO ('2026-12-01');

A child can also declare the next level of the tree. This is useful for an explicit RANGE → HASH layout:

CREATE TABLE events_2026_11 PARTITION OF events
FOR VALUES FROM ('2026-11-01') TO ('2026-12-01')
PARTITION BY HASH (client_id) SUBPARTITIONS 8;

An existing table can also be attached:

ALTER TABLE events
ATTACH PARTITION events_2026_10
FOR VALUES FROM ('2026-10-01') TO ('2026-11-01');

ATTACH validates bounds, schema, constraints, indexes, and references before publishing the partition. For collections, validation also decodes every existing document, resolves the doc.* key path, and rejects missing, incompatible, or out-of-bound values before publishing. DETACH removes the relation from the tree and turns it into a regular table. Publication is transactional; physical index and file cleanup may continue in the background.

Subpartitions

CREATE TABLE events (
    id         bigint,
    client_id  bigint NOT NULL,
    created_at timestamp NOT NULL,
    payload    jsonb
)
PARTITION BY RANGE (created_at)
PARTITION BY HASH (client_id)
SUBPARTITIONS 8
EVERY INTERVAL '1 month';

Pruning cascades through the tree: first by date, then by client_id. When an explicit RANGE child is created, its HASH children are materialized by the same DDL. The worker creates only new RANGE windows and, inside them, the fixed HASH buckets.

An equality predicate on the HASH key can prune the scan to one bucket; EXPLAIN reports the reduction, for example PartitionAppend 1 of 8 (pruned 7). Without a compatible predicate, all buckets are visited.

Default policies

A policy can be associated with a database or schema. When a table matches, the server applies the policy automatically; the CREATE TABLE statement does not need to repeat the clause.

CREATE PARTITION POLICY monthly_events
MATCH TABLES WITH COLUMN created_at timestamp
PARTITION BY RANGE (created_at)
EVERY INTERVAL '1 month'
RETENTION '12 months';

When retention must export before detaching, target an already configured `SERVER`:

```sql
CREATE PARTITION POLICY archived_events
MATCH TABLES WITH COLUMN created_at timestamp
PARTITION BY RANGE (created_at)
EVERY INTERVAL '1 month'
RETENTION '12 months'
ON RETENTION EXPORT TO SERVER archive_s3 THEN DETACH;

The worker reads physical leaves, sends batches to BackupExporter, and detaches only after finish() succeeds. The request carries parent/child identity and a stable idempotency key (partition/<parent>/<child>); providers must treat a retry with that key as the same completed export. The native GCS provider uses resumable chunked uploads and publishes NDJSON with contentType = application/x-ndjson; credentials are referenced through SECRET and are not stored in the SERVER definition.

A policy can also be limited to one schema:

CREATE PARTITION POLICY analytics_events IN SCHEMA analytics
MATCH TABLES WITH COLUMN created_at timestamp
PARTITION BY RANGE (created_at)
EVERY INTERVAL '1 month';

ALTER DATABASE app SET DEFAULT PARTITION POLICY monthly_events;

ALTER DATABASE app RESET DEFAULT PARTITION POLICY;

DROP PARTITION POLICY monthly_events;


`SET DEFAULT` marks a database-scoped policy as the administrative default and `RESET` removes
that mark. Automatic matching is still determined by the policy declaration; the server does
not choose a column heuristically.

A policy can be dropped only after it is no longer the default. Dropping a policy does not change
tables or partitions already created from it; it only prevents future automatic applications.

The server never chooses a column heuristically. The policy declares its match. If no policy
matches, the table remains unpartitioned; if multiple policies match, the command fails as
ambiguous. The protocol sends a NOTICE describing the applied policy.

## Collections

Policies targeting collections declare an explicit document path:

```sql
CREATE PARTITION POLICY collection_events
MATCH COLLECTIONS WITH COLUMN doc.created_at timestamp
PARTITION BY RANGE (doc.created_at)
EVERY INTERVAL '1 month';

The parser and catalog preserve the declared path and type. A collection can use HASH or RANGE, and a two-level tree can combine an outer RANGE with an inner HASH:

CREATE COLLECTION events
PARTITION BY RANGE (doc.created_at)
EVERY INTERVAL '1 month'
PARTITION BY HASH (doc.client_id)
SUBPARTITIONS 8;

A collection can also declare an explicit RANGE child and define its HASH level at the same time:

CREATE COLLECTION events_2026 PARTITION OF events
FOR VALUES FROM ('2026-01-01') TO ('2026-02-01')
PARTITION BY HASH (doc.client_id) SUBPARTITIONS 8;

The server routes INSERT to the physical leaf, reads the parent as one logical relation, and prunes by equality on a document key. FIND returns the same document result; when pruning is used, the protocol sends an INFO (SQLSTATE 00000) message such as:

partition pruning: selected 1 of 4 partitions (pruned 3)

EXPLAIN FIND shows the same decision before execution:

CollectionPartitionAppend 1 of 4 (pruned 3)

_id remains globally unique. Collection syntax currently supports one subpartition declaration; the physical tree and worker already traverse additional levels when they exist in the catalog.

Indexes

Users do not need to include the partition key in indexes:

CREATE INDEX events_client_idx ON events (client_id);
CREATE UNIQUE INDEX events_external_id_key ON events (external_id);

Indexes created on the parent are logical definitions maintained by physical local indexes on each existing leaf. New leaves receive that definition through the worker. Unique indexes use shared coordination across physical members, so they do not need the partition key and prevent duplicates across children. Indexes created directly on children remain local. The first coordinator uses the local members; a durable directory may replace the peer lookup without changing the SQL contract. Foreign keys can already reference this logical identity; the same infrastructure will be used for collection _id.

Triggers and inheritance

Triggers created on the parent are logical and apply to the whole tree, including direct writes to a partition. Triggers created on a child are physical and apply only to that relation.

For an UPDATE that moves a row:

  • logical triggers see one UPDATE;
  • physical triggers see DELETE on the old partition and INSERT on the new one.

Statement triggers on the parent fire once per command, even when multiple partitions are touched. The order is:

logical BEFORE
→ routing
→ physical BEFORE
→ constraints and indexes
→ write
→ physical AFTER
→ logical AFTER

PARTITION OF and INHERITS are different concepts. Partitioning provides routing, bounds, and pruning; inheritance allows extra columns, multiple parents, and overlap. Ordinary inheritance does not propagate triggers, constraints, or indexes automatically. used, the protocol sends an INFO (SQLSTATE 00000) message such as: