Skip to content
ERPForAll DocsDevelopment

    Running the transient cleanup (vacuum)

    Wizard (transient) models keep their rows in ordinary tables. The cleanup removes old rows through the normal unlink chain (ADR 0092). Developers declare the thresholds in the addon guide.

    State: inactive until an operator runs or schedules it. The server, workers and requests never start a cleanup. A deployment removes no transient row until someone runs the command below. Nothing in this release schedules it. Activating a schedule is a separate operator decision.

    erp db vacuum-transient --config <server.toml> (--model <name> | --all)
    [--max-batch-rows N] [--max-batches N] [--max-rows N]
    • The configuration file is the server’s own server.toml. The command reads the database, the owner connection and the addons path from it and composes the registry and the ORM the way the server does for that database: the installed modules (data-only addons and installed packages), the WebAssembly method layers, the unlink hooks and the base models’ implementations. A transient model of any installed addon is therefore found by name and by --all.
    • If an installed module is unavailable (for example, its native code is not linked into this binary) or waits for post-commit recovery, the command stops with an error before it vacuums anything: those models are not in the registry, and a run that skipped them would look like success. Repair the modules and run again.
    • Use the erp binary of the release that serves the database, so its linked code matches the installed modules.
    • --model cleans one transient model; --all cleans every transient model of the registry, one after the other, and stops at the first error. Exactly one of the two is required.
    • The caps lower the engine’s hard caps for this call: rows per batch (1 to 1000), batches (1 to 100) and rows (1 to 100000). A value outside the bounds is refused at argument parsing.
    • It prints one line per model: the outcome in a stable lowercase form (completed, pending, budget_exhausted, not_due, busy, lost_lease, policy_changed, disabled), the rows and batches this call committed, the cycle totals and the epoch.
    • Exit codes follow the error kind of the engine and the command:
      • 0: every model ended with an outcome (including not_due and busy: another attempt owns the work, or it is not due yet).
      • 1: a refusal by the caller’s input: error kinds Missing (unknown model), Validation (a model that is not transient), UserError (an unlink override refused a row), Access and AccessDenied.
      • 2: a usage error of the command line, an unreadable configuration (Execution), a database fault or any other error kind (Database, Concurrency, Lock, Internal, Programming), including an unknown commit outcome.
      • On an error the message carries the lines of the models already done, because their batches stay committed; the original error (with its SQLSTATE) is kept as the source.
    • It is not an RPC method and not a WebAssembly import. Only someone who can run the erp binary with the server’s configuration (and so the owner credentials) can invoke it.

    pending means the call stopped at its own caps, or the rows left are locked by other transactions: run it again and the next attempt continues the same cycle.

    The command connects with the owner connection of server.toml. That role owns the state table erp_transient_vacuum_state (created by the schema sync) and can delete the transient rows.

    For a dedicated maintenance role, run erp db grant-vacuum-role --config SERVER_TOML --role ROLE. It grants exactly SELECT, INSERT, UPDATE, DELETE on the state table, after revoking everything else, as the owner. The equivalent SQL, run as the owner, is:

    REVOKE ALL ON erp_transient_vacuum_state FROM maintenance_role;
    GRANT SELECT, INSERT, UPDATE, DELETE ON erp_transient_vacuum_state TO maintenance_role;

    The role’s privileges on the transient tables come from your normal data grants. The command refuses the table owner and the runtime role. erp db vacuum-transient itself always connects with the owner connection of the file you pass; a dedicated maintenance role is for the operator’s own tooling that calls the engine (Orm::vacuum_transient) with that role’s connection.

    The request (runtime) role holds nothing on the state table:

    • the table’s DDL revokes PUBLIC, also in a database whose default privileges would grant new tables to PUBLIC;
    • erp db publish-grants revokes the table from the runtime role after the schema-wide grant;
    • after publishing, the command checks with has_table_privilege (which counts PUBLIC and every role the runtime role is a member of) that the runtime role has no SELECT, INSERT, UPDATE or DELETE on it, and fails loudly with the table name if it has.

    The release helper (scripts/human-release.py) runs the target binary’s publish-grants and keeps no table list of its own.

    Use the host’s scheduler (cron, a systemd timer, a Kubernetes CronJob) to run the command per tenant database. The database decides who works: running it from several hosts at once is safe, the others report Busy. A cycle starts at most every 300 seconds, so running it more often only produces NotDue. A daily run suffices for the default one-hour age threshold. Do not start a detached copy per model without a bound: use --all or a short loop.

    • A failing unlink blocks the cycle. Rows are taken in ORDER BY id. If unlink fails on the first ids (a hook that refuses them, or a constraint), each later call fails the same way. The open cycle keeps its cutoff and no new cycle can start. The error is loud (exit 1, with the model and the original message). Remedy: fix or remove the offending rows or the hook, then run the command again; it continues the same cycle.
    • LostLease: another attempt took over after this one stalled; nothing is lost, rerun.
    • PolicyChanged: the model’s thresholds changed while a cycle was open; the old cycle was aborted and a new one starts when due.
    • Unknown commit outcome: if the database does not answer a COMMIT, the error says so and the batch is not retried. The next run reads the durable state first.

    Stop running the command (remove the schedule) first; the state table is left in place. Deleted transient rows cannot be restored by restoring an older executable, only from a backup. Drain old workers before changing the lease schema or a model’s policy.

    Restoring the previous release must run that release’s own grant publication before it starts. The newer binary revokes the runtime role on erp_transient_vacuum_state; the older binary’s startup check expects the runtime role to hold rights on every table it does not know, and refuses to start (“Runtime grants are not published”) until its own publish-grants has run. scripts/human-release.py does this for both the automatic restore after a failed activation and --rollback: it runs the restored release’s bin/erp db publish-grants with the same configuration and starts the service only if that succeeds. If you restore by hand, run erp db publish-grants --config SERVER_TOML with the restored release’s binary first. The old binary’s publication grants the runtime role the state table again (it does not know the table); the next activation of the newer release revokes it again, and that publication verifies it.

    A failed activation is covered the same way: if the candidate’s publish-grants fails after it committed its revokes (a failing check, a lost connection, the 120 second timeout), the helper runs the active release’s own publish-grants before it reports the failure, and logs old_grants_republished (or old_grants_republish_failed). A release older than the publish-grants command is restored by a plain switch (restore_grants_publication_unavailable). The command also refuses to run while module operations are pending, and grant-vacuum-role refuses a role the runtime role is a member of.