fix(restore): let a large restore outlast the request statement timeout

A restore of an account with 1.9 million readings, most of them deleted
by the nightly consolidation and still awaiting the purge, was cancelled
in its clearing step: `DELETE FROM measurements` ran past the 60 s
statement timeout every pooled connection carries, and the whole restore
rolled back with a message that named no cause (#1031).

Most of that statement was not the delete. Two columns point into
`measurements` with ON DELETE SET NULL and had no index, so Postgres
scanned `personal_records` and `ecg_recordings` once per deleted reading:
38 of 40 s on a seeded account with 300 personal records. Migration 0362
indexes both, which also speeds up the tombstone purge and account
deletion.

The restore transaction now sets its own statement and idle-in-transaction
limits to the transaction budget with SET LOCAL, and the budget counts
the rows the account holds as well as the rows the file carries. A
rolled-back restore names what the database refused it over (timeout,
lock, constraint, storage, connection, other) on the job row, the
console, the audit row and the wide event.
This commit is contained in:
Marc-André Bombeck committed 2026-09-27 11:24:41 +02:00
1 parent 69ddcc28e7
commit 93b6c086b4
19 files changed
+763 -18

No files matched your search

+30 -2
View File
@@ -27,6 +27,7 @@ import { WrittenOutcomeLine } from "@/components/outcome/written-outcome-line";
import { Button } from "@/components/ui/button";
import { Progress } from "@/components/ui/progress";
import { useFormatters, useTranslations } from "@/lib/i18n/context";
import type { RestoreFailureCause } from "@/lib/export/restore-failure-cause";
import type {
BackupRestoreFailureCode,
BackupRestoreJobView,
@@ -72,6 +73,7 @@ function failureText(
code: BackupRestoreFailureCode,
t: ReturnType<typeof useTranslations>["t"],
keyIds: readonly string[] = [],
cause?: RestoreFailureCause,
): string {
switch (code) {
case "backup_not_found":
@@ -101,7 +103,7 @@ function failureText(
case "time_budget":
return t("admin.section.backups.restoreFailureTimeBudget");
case "transaction_failed":
return t("admin.section.backups.restoreFailureTransaction");
return transactionFailureText(cause, t);
case "interrupted":
return t("admin.section.backups.restoreFailureInterrupted");
case "not_started":
@@ -119,6 +121,32 @@ function failureText(
}
}
/** A rolled-back transaction, by what the database refused it over. */
function transactionFailureText(
cause: RestoreFailureCause | undefined,
t: ReturnType<typeof useTranslations>["t"],
): string {
switch (cause) {
case "timeout":
return t("admin.section.backups.restoreFailureCauseTimeout");
case "lock":
return t("admin.section.backups.restoreFailureCauseLock");
case "constraint":
return t("admin.section.backups.restoreFailureCauseConstraint");
case "storage":
return t("admin.section.backups.restoreFailureCauseStorage");
case "connection":
return t("admin.section.backups.restoreFailureCauseConnection");
case "other":
case undefined:
return t("admin.section.backups.restoreFailureTransaction");
default: {
const unhandled: never = cause;
return unhandled;
}
}
}
/** What a running job is doing, and the share of it done when that is known. */
function runningLine(
job: BackupRestoreJobView,
@@ -297,7 +325,7 @@ export function RestoreJobStatus({
{t("admin.section.backups.restoreJobFailedTitle", { username })}
</p>
<p className="text-xs">
{failureText(code, t, job.failure?.keyIds)}
{failureText(code, t, job.failure?.keyIds, job.failure?.cause)}
</p>
{changedNothing ? (
<p className="text-muted-foreground mt-1 text-xs">