Skip to content

fix(backup): resolve version-compatible pg_dump/pg_restore and fall back on PG major version mismatch - #1973

Merged
dofastted merged 1 commit into
devfrom
fix/pg-backup-version-mismatch
Nov 27, 2026
Merged

dofastted merged 1 commit into
devfrom
fix/pg-backup-version-mismatch

Conversation

@dofastted

@dofastted dofastted commented Nov 27, 2026 •

Copy link
Copy Markdown
Owner

Summary

Fixes PostgreSQL scheduled/manual backup and restore failing on hosts whose distro postgresql-client major version is older than the running PostgreSQL server (e.g. Ubuntu 22.04 pg_dump 14.24 against vm2api-postgres postgres:18-alpine 18.6: aborting because of server version mismatch).

  • Version-aware PG tool resolution (src/lib/admin/backup-core.mjs):
    • Added parsePgMajorVersion(), queryPgServerMajorVersion(), buildDockerPgUri(), and resolvePgTool(tool, { serverMajor, connectionString }) shared by both pg_dump and pg_restore (SSOT).
    • Selection order: explicit env override (KIN_PG_DUMP_BIN / KIN_PG_RESTORE_BIN) → host PATH binary (only if major >= serverMajor) → Linux PGDG versioned binaries (/usr/lib/postgresql/<N>/bin/<tool>, highest compatible major first) → local managed Docker container (docker exec <VM2API_PG_CONTAINER|vm2api-postgres> <tool>) when the DSN targets loopback (127.0.0.1/localhost/::1).
    • Docker execution streams dump stdout / restore stdin directly via file descriptors (zero multi-GB RAM buffering) and passes -e PGPASSWORD via child env so credentials never appear in argv (ps).
  • Safe logical fallback on version mismatch (runPgBackupJob):
    • When no compatible pg_dump is available (e.g. remote PG 18 with only host pg_dump 14), or if pg_dump exits with server version mismatch, runPgBackupJob cleanly falls back to the built-in streaming dumpPgLogicalNdjsonGz() (pg_logical_ndjson_gz), which restores via pure JS without needing pg_restore.
  • Restore preflight alignment (src/lib/admin/backup-service.mjs):
    • Default _pgRestoreAvailable() now queries the live PG server major version and passes { serverMajor, connectionString } to pgRestoreAvailable(), rejecting incompatible custom dumps with pg_restore_missing before creating pre_restore and allowing container-backed pg_restore when available.

Test Plan

  • node --test test/unit/backup-service.test.mjs test/unit/settings-ops-audit.test.mjs — pass (including new unit tests for parsePgMajorVersion, buildDockerPgUri, PGDG / Docker / remote resolution, argv secret safety, and pg_restore_missing)
  • npm run test:ci — all 12 core files pass
  • npx biome format on the 3 modified files — pass

Note

Medium Risk
Touches backup/restore of production database state and spawns docker/host PG tools with credentials; logic is complex but includes safe NDJSON fallback and tested preflight paths.

Overview
Fixes PostgreSQL backup/restore when the host pg_dump/pg_restore major is older than the server (e.g. client 14 vs server 18).

Version-aware tool resolution in backup-core.mjs: new resolvePgTool() picks a compatible runner via env overrides (KIN_PG_*_BIN), PATH, PGDG paths under /usr/lib/postgresql/<N>/bin, or docker exec into VM2API_PG_CONTAINER for loopback DSNs. execPgDump / execPgRestore run either a host binary or container tools, streaming I/O via file descriptors and passing PGPASSWORD in env (not argv).

Backup path: runPgBackupJob queries server major, uses the resolved pg_dump, and on failure or "server version mismatch" stderr falls back to the existing pure-JS dumpPgLogicalNdjsonGz instead of hard-failing.

Restore path: restorePgSnapshotJob and BackupService preflight now require a version-compatible pg_restore (async check with live server major) and emit clearer pg_restore_missing errors when none exists.

Unit tests cover PGDG/Docker selection, credential handling, and mismatch fallback behavior.

Reviewed by Cursor Bugbot for commit 9fb8220. Configure here.

…er) and fall back to logical dump on version mismatch
@cursor

cursor Bot commented Nov 27, 2026

Copy link
Copy Markdown
Contributor

Bugbot couldn't run - usage limit reached

Bugbot is counted against Cursor usage for this user or team, and this run hit a usage or spend limit.

A user or team admin can review and increase usage limits in the Cursor dashboard.

(requestId: e8605f17-bdc3-4859-a85c-d8ddd522c95a)

@dofastted
dofastted merged commit de60272 into dev Nov 27, 2026
6 checks passed
@dofastted
dofastted deleted the fix/pg-backup-version-mismatch branch November 27, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant