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 <noreply@anthropic.com>
This commit is contained in:
2026-12-03 21:18:00 -07:00
co-authored by Claude Opus 5.5
parent 5d90c246a1
commit 1e86866e8a
3 changed files with 37 additions and 10 deletions
+15 -5
View File
@@ -570,13 +570,23 @@ Later:
- Large databases (2026-12-03): before dumping, coreapps reads the - Large databases (2026-12-03): before dumping, coreapps reads the
database's size (`pg_database_size`; MariaDB's `information_schema` database's size (`pg_database_size`; MariaDB's `information_schema`
table sizes) and refuses the update, changing nothing, if the apps' 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 disk can't hold the copy and still keep 1 GB free; above 1 GB (200 MB
says the copy takes a while, and above 30 seconds how long it took. 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 Tested on dev.fedcom.net (sizes, the refusal with a raised reserve, the
message with a lowered limit) message with a lowered limit)
- [ ] Very large databases: a dump on every update may still be too - Done 2026-12-03: PostgreSQL dumps use zstd at level 1 (`pg_dump
slow; a btrfs snapshot of the database server's data, or skipping -Fc -Z zstd:1`; pg_dump's default compression if it fails, for a
the copy with the user's agreement, could replace it 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 - Done 2026-12-03: dropping a need from an installed app. `sso` and
`email` go freely; `postgres`/`mysql` (the app's data) need `email` go freely; `postgres`/`mysql` (the app's data) need
`--drop-database` (coreapps install/plan; `coreapp deploy` asks for the `--drop-database` (coreapps install/plan; `coreapp deploy` asks for the
+7
View File
@@ -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. container isn't restarted and no `.coreapps-rollback-*` folder is left.
With `BIG_DATABASE = 0`: "Keeping a copy of its database (...) in case With `BIG_DATABASE = 0`: "Keeping a copy of its database (...) in case
the update fails; this takes a while...", then "Updated". 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` 11. Superpanel: after deploy, `/federated/apps/superpanel/data/applist.json`
(the dashboard) and `catalog.json` have the app; `service` has (the dashboard) and `catalog.json` have the app; `service` has
`URL="https://hello.$DOMAIN/"` and applist the same `url`. Change `URL="https://hello.$DOMAIN/"` and applist the same `url`. Change
+15 -5
View File
@@ -1103,8 +1103,10 @@ ROLLBACK_FILES = [".env", "docker-compose.yml"]
# Each runs in the database's container (the same version as the server); # 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. # root's MariaDB password is read there, so it's not in a log.
DUMP = { 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", "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": ("db.sql", ["docker", "exec", "pdnsmysql", "sh", "-c",
'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb-dump -uroot ' 'MYSQL_PWD="$MYSQL_ROOT_PASSWORD" exec mariadb-dump -uroot '
"--single-transaction --routines --triggers --events --databases {db}"]), "--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 # Left free on the apps' disk after the database's copy, for the Core
ROLLBACK_RESERVE = 1024 ** 3 ROLLBACK_RESERVE = 1024 ** 3
# Above this, say that keeping the copy takes a while # Above this, say that keeping the copy takes a while
BIG_DATABASE = 200 * 1024 ** 2 BIG_DATABASE = 1024 ** 3
def size_text(n): 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 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): def keep_for_rollback(m, p, backup, log):
"""Copies of the app's service folder, settings, database and (where the """Copies of the app's service folder, settings, database and (where the
filesystem makes it cheap) /data, in backup. Refuses the update if 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 " say(f"Keeping a copy of its database ({size_text(size)}) in case the update "
"fails; this takes a while...") "fails; this takes a while...")
started = time.monotonic() started = time.monotonic()
with open(os.path.join(backup, file), "wb") as out, open(log, "a") as err: result = dump_database(cmd, m["dbname"], os.path.join(backup, file), log)
result = subprocess.run([c.replace("{db}", m["dbname"]) for c in cmd], if result.returncode != 0 and "-Z" in cmd:
stdout=out, stderr=err) # 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: if result.returncode != 0:
raise Refused(f"Couldn't back up {name}'s database before updating, " raise Refused(f"Couldn't back up {name}'s database before updating, "
f"so nothing was changed (log on the Core: {log}).") f"so nothing was changed (log on the Core: {log}).")