fix: prevent data loss on restart after unclean shutdown#7
Merged
nicoloboschi merged 1 commit intomainfrom Mar 26, 2026
Merged
Conversation
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Fixes #6 —
pg0 startwas deleting the entire data directory after an unclean shutdown (SIGKILL, OOM kill, power loss, VM reboot), causing silent data loss.Root cause: The
postgresql_embeddedlibrary defaultsSettings.temporarytotrue. When thePostgreSQLstruct is dropped (on any error path during start), itsDropimpl callsremove_dir_allon the data directory. On the happy path,std::mem::forget()prevents this, but ifpg_ctl startfails (e.g., due to a stalepostmaster.pid), the Drop fires and destroys all data.Fixes:
temporary: falseexplicitly so the library never deletes the data directory — pg0 manages data lifecycle via itsdropcommandpostmaster.pidwhen a dead instance is detected, so PostgreSQL can restart cleanly with existing dataTest plan
test_data_survives_crash: starts postgres, inserts data, SIGKILLs the process (leaving stale postmaster.pid), restarts, and verifies data is intactcargo checkpasses