Partitions and retention
Partition an append-heavy table by month, keep every partition behind row level security, add the next partitions ahead of time and drop expired ones, knowing the limits of partitioned tables on Aurabase.
A partitioned table is a parent that holds no rows itself and routes each row to a child table, its partition, by the value of a key such as the creation date. Queries that filter on the key only read the partitions they need, and dropping a whole month of data is one drop table instead of a long delete. You create and maintain partitions in SQL with aura db raw; Aurabase does not create or drop them for you.
Prerequisites#
-
A linked project:
aura link <project-id>. -
The example schema, installed with the service role key of the project:
export AURABASE_SERVICE_ROLE_KEY=aura_sk_… aura db seed --example docsIt creates
profiles,orgs,org_members,posts,todos, the viewopen_todosand the functiontodo_count(p_user uuid). See Example schema.
The examples create the partitioned table sch_part_events and its partitions sch_part_events_…, then remove them.
When to partition#
Partition when a table grows by time (events, logs, measurements), most queries read a recent period, and old data expires. Below a few million rows, an index on the date column (Indexes) and a batched delete are simpler. Partitioning adds rules: the partition key must be part of every primary key and unique constraint, and each partition is a table you create, protect and drop.
What Aurabase automates and what it does not#
| Status | |
|---|---|
| Creating future partitions of your tables | Not automated. Create them ahead of time (see Add the next partitions). pg_partman and pg_cron are not among the extensions of a project. |
| Dropping expired partitions of your tables | Not automated. Drop or detach them yourself (see Drop expired data). |
| Row level security on new partitions | Not applied by aura db raw (measured): enable it yourself (see Protect every partition). |
| Retention of the platform's own tables in your project | Automated, see below. These tables belong to Storage, Realtime, notifications and AI; they are not yours to change. |
Aurabase runs two kinds of background jobs, and neither reads or drops your tables:
-
Retention inside your project's database. Rows of the platform's own append-only tables are deleted when they are older than a window. The values below are the defaults of the deployment.
Table (internal schemas of the project) Kept for How often Notification attempts 30 days Once a day, in batches of 5,000 rows Notification events 7 days Once a day, in batches of 5,000 rows AI request logs 90 days Once a day, in batches of 5,000 rows Storage audit log 90 days Once a day, in batches of 5,000 rows Realtime database changes ( postgres_changes), kept only to replay them after a reconnection1 hour (10 minutes at least) Every 10 minutes Realtime broadcast and presence messages, on dedicated databases 72 hours (1 hour at least) Every 10 minutes -
Maintenance of the platform's console database. Once a day, jobs create the next monthly partitions of the account audit log (three months ahead), drop its partitions older than 24 months, and delete usage events older than 90 days and platform audit events older than 90 days, by default. This database is the platform's, outside your project's database.
Create a partitioned table#
Range partitions by month, plus a default partition that catches rows outside every range:
create table sch_part_events (
id bigint generated always as identity,
user_id uuid not null,
kind text not null,
created_at timestamptz not null default now(),
primary key (id, created_at)
) partition by range (created_at);
create table sch_part_events_2026_11 partition of sch_part_events
for values from ('2026-11-01') to ('2026-12-01');
create table sch_part_events_2026_12 partition of sch_part_events
for values from ('2026-12-01') to ('2027-01-01');
create table sch_part_events_default partition of sch_part_events default;
create index sch_part_events_user on sch_part_events (user_id, created_at); ℹ No results (0 rows).The primary key includes created_at because PostgreSQL requires the partition key in every unique constraint of a partitioned table. The index created on the parent is created on every partition, present and future. Other strategies exist: partition by list (region) for a fixed set of values, partition by hash (user_id) to spread rows evenly.
Write and read through the parent#
Always write to and read from the parent; PostgreSQL routes each row:
aura db raw "insert into sch_part_events (user_id, kind, created_at) values
('00000000-0000-4000-8000-0000000000a1', 'login', '2026-11-21'),
('00000000-0000-4000-8000-0000000000a1', 'login', '2026-12-02'),
('00000000-0000-4000-8000-0000000000b2', 'login', '2027-03-15')
returning tableoid::regclass::text as partition, created_at::date as day" day │ partition
───────────┼────────────────────────
2026-11-21 │ sch_part_events_2026_11
2026-12-02 │ sch_part_events_2026_12
2027-03-15 │ sch_part_events_default
ℹ 3 row(s).The March row had no monthly partition and went to the default one. A query that filters on created_at skips the partitions that cannot match:
aura db raw "explain (costs off) select count(*) from sch_part_events where created_at >= '2026-12-01' and created_at < '2027-01-01'" QUERY PLAN
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
Aggregate
-> Seq Scan on sch_part_events_2026_12 sch_part_events
Filter: ((created_at >= '2026-12-01 00:00:00+00'::timestamp with time zone) AND (created_at < '2027-01-01 00:00:00+00'::timestamp with time zone))
ℹ 3 row(s).The SDK and the REST API reach the parent like any table (from('sch_part_events')). They do not expose the partitions themselves: from('sch_part_events_2026_11') answers 404 TABLE_NOT_FOUND.
Protect every partition#
aura db raw enables and forces row level security on new tables after a schema change, but not on partitions. The parent is protected; the partitions are not, and they carry the default rights of the project's relations, so the anon role could read or write a partition by its name in SQL (for example inside a function or a view you expose). aura db table list shows it:
aura db table list --json | node -e 'for (const t of JSON.parse(require("fs").readFileSync(0, "utf8")))
if (t.name.startsWith("sch_part_")) console.log(t.name, "rls:", t.rls_enabled)'sch_part_events_2026_11 rls: false
sch_part_events_2026_12 rls: false
sch_part_events_default rls: falseThe list shows the partitions but not the parent sch_part_events. Enable row level security on each partition when you create it. With no policy on the partition, direct access is refused to every role except service_role, while reads and writes through the parent keep following the parent's policies:
for p in sch_part_events_2026_11 sch_part_events_2026_12 sch_part_events_default; do
aura db table rls "$p" on
done
aura db raw --as anon "select count(*) as visible from sch_part_events_2026_11" ✔ RLS enabled on 'sch_part_events_2026_11'.
✔ RLS enabled on 'sch_part_events_2026_12'.
✔ RLS enabled on 'sch_part_events_default'.
ℹ Executed as anon.
visible
───────
0
ℹ 1 row(s).In SQL, the same is alter table <partition> enable row level security; alter table <partition> force row level security; in the request that creates the partition. Add the policies your callers need on the parent: Row-level security.
Add the next partitions#
A row whose date falls in no range goes to the default partition, which grows without limit and blocks the creation of a partition whose range overlaps rows it already holds. Create the partitions of the coming months in advance, for example once a month from a scheduled job of yours that calls aura db raw or POST /v1/db/{project_id}/raw:
create table if not exists sch_part_events_2027_01 partition of sch_part_events
for values from ('2027-01-01') to ('2027-02-01');
alter table sch_part_events_2027_01 enable row level security;
alter table sch_part_events_2027_01 force row level security; ℹ No results (0 rows).Drop expired data#
Dropping a partition removes its rows at once, without scanning them and without leaving dead rows to vacuum:
aura db raw "drop table sch_part_events_2026_11"
aura db raw "select count(*) as events from sch_part_events" ℹ No results (0 rows).
events
──────
2
ℹ 1 row(s).To keep the data outside the live table (archive, export), detach the partition instead: alter table sch_part_events detach partition sch_part_events_2026_12 turns it into a standalone table. The concurrently form, which avoids blocking the parent, is refused by aura db raw because it cannot run inside a transaction:
aura db raw "alter table sch_part_events detach partition sch_part_events_2026_12 concurrently" || echo "exit code: $?"Error: [400] BAD_REQUEST: Invalid request: ALTER TABLE ... DETACH CONCURRENTLY cannot run inside a transaction block
exit code: 1On a table that is not partitioned, expire rows with a delete … where created_at < … limited in size (a few thousand rows per request), since each request must finish within the statement timeout of the endpoint (10 seconds by default).
Limits of partitioned tables#
| Feature | Behavior with a partitioned table |
|---|---|
aura db table list, GET /v1/db/{project_id}/schema | List the partitions, not the parent. |
GET /v1/db/{project_id}/schema/dump, aura db pull | Omit the parent and its partitions: the exported schema does not recreate them. Keep their create table statements in your migrations. |
| Preview branches | A project with a partitioned table cannot be branched: the clone refuses partitioned and inherited tables. |
| SDK and REST API | Reach the parent only. |
| Realtime | Not tested with partitioned tables. |
Result#
aura db raw "select tableoid::regclass::text as partition, count(*) as events from sch_part_events group by 1 order by 1" events │ partition
───────┼────────────────────────
1 │ sch_part_events_2026_12
1 │ sch_part_events_default
ℹ 2 row(s).Common errors#
| Message | Cause | Fix |
|---|---|---|
unique constraint on partitioned table must include all partitioning columns | A primary key or unique constraint without the partition key. | Add the key column (primary key (id, created_at)). |
no partition of relation "…" found for row | No partition covers the value and there is no default partition. | Create the partition, or a default one. |
updated partition constraint for default partition "…" would be violated by some row | A new partition overlaps rows already in the default partition. | Move those rows out of the default partition first, then create the partition. |
ALTER TABLE ... DETACH CONCURRENTLY cannot run inside a transaction block | concurrently through aura db raw. | Detach without concurrently, or use the direct connection. |
404 TABLE_NOT_FOUND on a partition from the SDK | The REST API does not expose partitions. | Query the parent. |
Clean up#
Dropping the parent drops every partition attached to it:
drop table sch_part_events; ℹ No results (0 rows).