Skip to content

Recover PGlite data dirs with corrupt WAL#994

Open
yestheboxer wants to merge 1 commit into
electric-sql:mainfrom
yestheboxer:codex/recover-corrupt-wal
Open

Recover PGlite data dirs with corrupt WAL#994
yestheboxer wants to merge 1 commit into
electric-sql:mainfrom
yestheboxer:codex/recover-corrupt-wal

Conversation

@yestheboxer
Copy link
Copy Markdown

This adds a recovery path for NodeFS data directories that fail during startup because Postgres reports corrupt WAL or checkpoint state. Instead of surfacing only an Emscripten abort, PGlite now captures the startup logs, resets WAL in the existing data directory, and retries startup once.

The reset code is intentionally narrow. It only runs for PGlite's PostgreSQL 17 layout, validates pg_control and WAL sizes, removes stale postmaster.pid and old WAL files, writes a replacement shutdown checkpoint record, and preserves the data files. Users can opt out with dataDirRepair: 'none'. The instance exposes repairedDataDir when recovery happened.

The regression test creates a database, inserts a row, truncates WAL, reopens the same data directory, and verifies the original row is still readable after recovery. It also checks that the opt out path still fails with a startup error.

Validation run:

pnpm --filter @electric-sql/pglite build
pnpm --filter @electric-sql/pglite typecheck
pnpm vitest tests/startup-wal-repair.test.ts
/tmp/bun-versions/stable-1.3.14/bun-darwin-aarch64/bun test --timeout 15000 tests/startup-wal-repair.test.ts
/tmp/bun-versions/canary/bun-darwin-aarch64/bun test --timeout 15000 tests/startup-wal-repair.test.ts
pnpm exec prettier --check .changeset/repair-pglite-corrupt-wal.md packages/pglite/src/fs/base.ts packages/pglite/src/fs/nodefs.ts packages/pglite/src/fs/pgResetWal.ts packages/pglite/src/interface.ts packages/pglite/src/pglite.ts packages/pglite/tests/startup-wal-repair.test.ts
git diff --check

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