Skip to content

fix: prevent data loss on restart after unclean shutdown#7

Merged
nicoloboschi merged 1 commit intomainfrom
fix/preserve-data-on-stale-pid
Mar 26, 2026
Merged

fix: prevent data loss on restart after unclean shutdown#7
nicoloboschi merged 1 commit intomainfrom
fix/preserve-data-on-stale-pid

Conversation

@nicoloboschi
Copy link
Collaborator

Summary

Fixes #6pg0 start was deleting the entire data directory after an unclean shutdown (SIGKILL, OOM kill, power loss, VM reboot), causing silent data loss.

Root cause: The postgresql_embedded library defaults Settings.temporary to true. When the PostgreSQL struct is dropped (on any error path during start), its Drop impl calls remove_dir_all on the data directory. On the happy path, std::mem::forget() prevents this, but if pg_ctl start fails (e.g., due to a stale postmaster.pid), the Drop fires and destroys all data.

Fixes:

  • Set temporary: false explicitly so the library never deletes the data directory — pg0 manages data lifecycle via its drop command
  • Proactively remove stale postmaster.pid when a dead instance is detected, so PostgreSQL can restart cleanly with existing data

Test plan

  • Added regression test test_data_survives_crash: starts postgres, inserts data, SIGKILLs the process (leaving stale postmaster.pid), restarts, and verifies data is intact
  • cargo check passes
  • Test passes locally

After an unclean shutdown (SIGKILL, OOM, power loss), a stale
postmaster.pid is left behind. On restart, the postgresql_embedded
library's Drop impl would delete the entire data directory because
`temporary` defaults to `true`.

Two fixes:
- Set `temporary: false` so the library never deletes the data dir
- Proactively remove stale postmaster.pid before restarting

Adds a regression test that SIGKILLs postgres and verifies data
survives the restart.

Closes #6
@nicoloboschi nicoloboschi merged commit 21e0f08 into main Mar 26, 2026
10 of 11 checks passed
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.

pg0 start deletes data directory on stale postmaster.pid (data loss after unclean shutdown)

1 participant