From 1e86866e8aa0c5bedcc1559ca8305c19f8df93bb Mon Sep 17 00:00:00 2001 From: David Young Date: Thu, 3 Dec 2026 21:18:00 -0700 Subject: [PATCH] Copy PostgreSQL databases with zstd before updates pg_dump -Fc -Z zstd:1 instead of its default gzip: on the dev Core a 1.3 GB database took 6 s and 158 MB instead of 27 s and 223 MB, and restores as fast. A pg_dump without zstd falls back to its default. The "takes a while" note now starts at 1 GB. Co-Authored-By: Claude Opus 5.5 --- PLAN.md | 20 +++++++++++++++----- TEST.md | 7 +++++++ core/coreapps | 20 +++++++++++++++----- 3 files changed, 37 insertions(+), 10 deletions(-) diff --git a/PLAN.md b/PLAN.md index b0149fd..139bd68 100644 --- a/PLAN.md +++ b/PLAN.md @@ -570,13 +570,23 @@ Later: - Large databases (2026-12-03): before dumping, coreapps reads the database's size (`pg_database_size`; MariaDB's `information_schema` table sizes) and refuses the update, changing nothing, if the apps' - disk can't hold the copy and still keep 1 GB free; above 200 MB it - says the copy takes a while, and above 30 seconds how long it took. + disk can't hold the copy and still keep 1 GB free; above 1 GB (200 MB + before zstd) it says the copy takes a while, and above 30 seconds how long it took. Tested on dev.fedcom.net (sizes, the refusal with a raised reserve, the message with a lowered limit) - - [ ] Very large databases: a dump on every update may still be too - slow; a btrfs snapshot of the database server's data, or skipping - the copy with the user's agreement, could replace it + - Done 2026-12-03: PostgreSQL dumps use zstd at level 1 (`pg_dump + -Fc -Z zstd:1`; pg_dump's default compression if it fails, for a + PostgreSQL without zstd). Measured on dev.fedcom.net with a 1.3 GB + database: gzip (the default) 27.2 s / 223 MB, lz4 5.2 s / 363 MB, + zstd:1 6.4 s / 158 MB, none 7.5 s / 896 MB; restoring each took + 28-34 s (mostly index builds). A whole update of an app with that + database took 26 s; a failed one rolled back with all 4.5 million + rows (2 minutes) + - [ ] Very large databases (tens of GB): even at ~5 s/GB the copy + grows with the data; a btrfs snapshot can't replace it (the server's + data is shared by all apps), so skipping the copy with the user's + agreement would be next, if a Core needs it. A faster rollback: + `pg_restore -j` (needs the dump as a file in the container) - Done 2026-12-03: dropping a need from an installed app. `sso` and `email` go freely; `postgres`/`mysql` (the app's data) need `--drop-database` (coreapps install/plan; `coreapp deploy` asks for the diff --git a/TEST.md b/TEST.md index 393c56b..79dc344 100644 --- a/TEST.md +++ b/TEST.md @@ -317,6 +317,13 @@ The Core's own scripts trace secrets, so their output goes to container isn't restarted and no `.coreapps-rollback-*` folder is left. With `BIG_DATABASE = 0`: "Keeping a copy of its database (...) in case the update fails; this takes a while...", then "Updated". +10b. Dump speed: an app whose database holds 1.3 GB (e.g. a table of 4.5 + million rows with an index): `coreapp deploy --recreate` says it's + keeping a copy (above 1 GB) and the whole update takes under a minute; + the log on the Core shows no pg_dump error. A failing update (a wrong + `port`) rolls back with every row. In the module, a `DUMP` command that + fails with `-Z` is run again without it (the copy is kept, the error in + the log); one that always fails refuses the update. 11. Superpanel: after deploy, `/federated/apps/superpanel/data/applist.json` (the dashboard) and `catalog.json` have the app; `service` has `URL="https://hello.$DOMAIN/"` and applist the same `url`. Change diff --git a/core/coreapps b/core/coreapps index b40cfa7..f363d61 100755 --- a/core/coreapps +++ b/core/coreapps @@ -1103,8 +1103,10 @@ ROLLBACK_FILES = [".env", "docker-compose.yml"] # Each runs in the database's container (the same version as the server); # root's MariaDB password is read there, so it's not in a log. DUMP = { + # zstd at level 1: on the dev Core, a 1.3 GB database took 6 s and + # 158 MB, against 27 s and 223 MB with pg_dump's default (gzip) "postgres": ("db.dump", ["docker", "exec", "postgresql", - "pg_dump", "-U", "postgres", "-Fc", "{db}"]), + "pg_dump", "-U", "postgres", "-Fc", "-Z", "zstd:1", "{db}"]), "mysql": ("db.sql", ["docker", "exec", "pdnsmysql", "sh", "-c", 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb-dump -uroot ' "--single-transaction --routines --triggers --events --databases {db}"]), @@ -1138,7 +1140,7 @@ DB_SIZE = { # Left free on the apps' disk after the database's copy, for the Core ROLLBACK_RESERVE = 1024 ** 3 # Above this, say that keeping the copy takes a while -BIG_DATABASE = 200 * 1024 ** 2 +BIG_DATABASE = 1024 ** 3 def size_text(n): @@ -1151,6 +1153,11 @@ def database_size(need, db): return int(text) if out.returncode == 0 and text.isdigit() else None +def dump_database(cmd, dbname, path, log): + with open(path, "wb") as out, open(log, "a") as err: + return subprocess.run([c.replace("{db}", dbname) for c in cmd], stdout=out, stderr=err) + + def keep_for_rollback(m, p, backup, log): """Copies of the app's service folder, settings, database and (where the filesystem makes it cheap) /data, in backup. Refuses the update if the @@ -1178,9 +1185,12 @@ def keep_for_rollback(m, p, backup, log): say(f"Keeping a copy of its database ({size_text(size)}) in case the update " "fails; this takes a while...") started = time.monotonic() - with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err: - result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd], - stdout=out, stderr=err) + result = dump_database(cmd, m["dbname"], os.path.join(backup, file), log) + if result.returncode != 0 and "-Z" in cmd: + # A PostgreSQL built without zstd: its default compression + at = cmd.index("-Z") + result = dump_database(cmd[:at] + cmd[at + 2:], m["dbname"], + os.path.join(backup, file), log) if result.returncode != 0: raise Refused(f"Couldn't back up {name}'s database before updating, " f"so nothing was changed (log on the Core: {log}).")