Skip to content

Scheduled mutations

A scheduled mutation is a write that Base runs later: “send this message in 10 seconds unless the user presses Undo”, “publish this post at 9:00”, “delete this account in 7 days”. You schedule insert, update or delete steps, the user (or you) can cancel them until the moment they run, and when they run they are checked by your row-level security policies as the user who scheduled them. Nothing runs with more rights than that user had.

It is opt-in per project and works in Community and Cloud. A pending row lives in the project database, so it survives restarts.

As the project owner, in the SQL editor:

select base.enable_scheduled_mutations();

or with the Management API: PATCH /v1/projects/{ref}/scheduled with { "enabled": true }. Turning it off (base.disable_scheduled_mutations()) is refused while rows are pending, unless you pass true to force it.

The payload is the same list of steps as the transaction API, limited to insert, upsert, update and delete (no select, no rpc). Give either delay or run_at:

import { createClient } from '@potalab/base'
const base = createClient({ url, key })
// the callback records the steps exactly like base.transaction(...)
const { data, error } = await base.schedule(
(tx) => tx.from('messages').insert({ room_id: 7, body: 'hello' }),
{
delay: 10_000, // milliseconds or '10 seconds'; or runAt: new Date('2026-11-01T09:00:00Z')
idempotencyKey: 'msg-draft-42', // optional
},
)
// data = { id: '…', run_at: '…', state: 'pending' }

schedule() checks its options before sending anything and answers { data: null, error } with code schedule_invalid for a bad one (both delay and runAt, a select or rpc step, no steps). The same call without the SDK is POST /rest/v1/rpc/schedule_mutation with the user’s access token and { payload, delay | run_at, idempotency_key }.

Rule Detail
Who Signed-in users (authenticated). anon and the service key cannot schedule
When delay (an interval) or run_at (a time), never both. A run_at in the past runs at the next poll. At most 30 days ahead
Size 1 to 50 steps, up to 64 KB, tables in an exposed schema (the same allowlist as the transaction API)
Limit 1000 pending rows per user (429 scheduled_quota)
Idempotency The same idempotency_key with the same payload returns the first row (a safe retry). With another payload: 409 idempotency_conflict. Keys are per user
await base.scheduled.cancel(data.id) // or POST /rest/v1/rpc/cancel_scheduled { id }
Answer Meaning
{ id, state: 'cancelled' } It will not run. Cancelling twice is fine
404 not_found No such row, or it belongs to someone else
409 already_ran It already ran. Cancel does not reverse it: delete or update the data yourself
409 already_running It is running right now. Try again in a moment to see the final state

List the caller’s own rows with base.scheduled.list() (GET /rest/v1/rpc/list_scheduled): it is a normal query builder, so .eq('state', 'pending').order('run_at').limit(20) work.

const { data: waiting } = await base.scheduled.list().eq('state', 'pending').order('run_at')

At the due time a Core API worker runs the steps in one transaction that also marks the row done:

  • as the authenticated role with the claims saved when you scheduled (sub, role, aal, tenant_id, tenant_role, app_metadata; nothing else, so no email), on a connection with no privileges of its own. auth.uid(), policies, column defaults, triggers, the audit log and Realtime behave as for a normal request. The audit row’s request_id is sched:<id>.
  • a policy that reads another claim (for example email) sees it missing. Keep what a policy needs in app_metadata or in a table.
  • the saved claims are signed with a key of the project. A row whose claims or payload were edited does not run (bad_claims_sig).
  • permissions are checked when it runs, not when you schedule it. If a policy now refuses, or the user was deleted or banned, the row ends failed (identity_revoked for a missing or banned user) and nothing is written.

Each row runs at most once: “done” is written in the same commit as your data. A crash while it runs leaves nothing behind and the row is picked up again.

State Meaning
pending Waiting for run_at (or for a retry)
running Being executed
done Applied. result.counts holds the rows affected by each step
cancelled Cancelled by the owner of the row, or by a project member in the dashboard
failed A permanent error (policy refused, constraint, bad input, missing table). Not retried
dead A temporary error (deadlock, serialization failure, lock or statement timeout, connection) kept happening: 5 attempts, with a wait of 15 s, 45 s, 2 min, 6 min between them
expired It was not run within 24 hours after run_at (the worker was down), so an old “undo send” never fires by surprise

The polling interval is 2 seconds: the 95th percentile of the delay between run_at and the start is under 5 seconds. Do not rely on sub-second precision.

In the dashboard, Database / Scheduled (project developers and up) turns the feature on or off, shows the backlog (waiting, oldest overdue, dead, failed, when the worker last polled) and lists the rows by state. Cancel stops a waiting row on behalf of its user; Replay puts a dead, failed or expired row back in the queue (attempts and expiry restart, the identity is checked again). Both are written to the audit log. The page shows what a row does (insert messages), never the values.

Core API exports base_scheduled_runs_total{outcome} (done, pending for a retry, failed, dead, stale) and base_scheduled_poll_errors_total. Alert on a growing dead count or on poll errors; the page and GET /v1/projects/{ref}/scheduled give the per-project backlog (counts, oldest_due_seconds).

GET /v1/projects/{ref}/scheduled/mutations lists the rows of the project (what they do, not the values), POST …/{id}/cancel cancels one and POST …/{id}/replay runs a dead, failed or expired row again (project developer role and up, written to the audit log). Finished rows are removed after 7 days by base.scheduled_purge() (a scheduled job; set base.scheduled_retention_days to change it, 0 keeps them).