postgres.git / summary / log / commit / refs
commit 61553eacadbca0a084c7ae46214ffdee71208823
Author: Robert Haas <rhaas@postgresql.org>
Date: Thu Oct 29 16:37:07 2026 +0000
First released in: 19.0
pg_stash_advice: Don't forget unsaved changes after a failed write.
If the persistence worker hits an error while writing
pg_stash_advice.tsv, it exits and the postmaster restarts it. The new
worker doesn't think the file needs to be rewritten until something
changes or the server shuts down. Instead, it should retry the failed
write.
This also fixes a second bug. The worker should record the current
change count before, rather than after, setting stashes_ready.
Failure to do so creates a narrow window during which any changes
won't trigger the persistence worker to rewrite the file.
Zsolt's original patch included a test case, but I've left that out
here because it doesn't seem worth the cycles for a bug that seems
unlikely to be reintroduced. If others disagree, we can revisit.
Oversight in commit c10edb102ada607eb054bc9e23690109d86849ef.
Reported-by: Zsolt Parragi <zsolt.parragi@percona.com>
Author: Zsolt Parragi <zsolt.parragi@percona.com>
Reviewed-by: Dongpo Liu <poe.liu@pm.me>
Discussion: http://postgr.es/m/CAN4CZFMoWWdXhrNRk1id=N_K4o5kae3kQTtJi__UfzrEOeUCCA@mail.gmail.com
Backpatch-through: 19
contrib/pg_stash_advice/stashpersist.c | 15 +++++++++++----
1 file changed, 11 insertions(+), 4 deletions(-)
diff --git a/contrib/pg_stash_advice/stashpersist.c b/contrib/pg_stash_advice/stashpersist.c
index 2b23ba969f5..5d3dbf50882 100644
--- a/contrib/pg_stash_advice/stashpersist.c
+++ b/contrib/pg_stash_advice/stashpersist.c
@@ -139,16 +139,23 @@ pg_stash_advice_worker_main(Datum main_arg)
/*
* If pg_stash_advice.persist was set to true during
* process_shared_preload_libraries() and the data has not yet been
- * successfully loaded, load it now.
+ * successfully loaded, load it now. At that point, what's in memory
+ * matches what's on disk, so note the current change count before
+ * allowing any further changes.
+ *
+ * Otherwise, we can't assume that what's in memory has been saved: a
+ * previous worker may have died while trying to write the file. Since
+ * change_count starts at zero, comparing against zero makes us write at
+ * the first opportunity unless nothing has ever been changed.
*/
if (pg_atomic_unlocked_test_flag(&pgsa_state->stashes_ready))
{
pgsa_read_from_disk();
+ last_change_count = pg_atomic_read_u64(&pgsa_state->change_count);
pg_atomic_test_set_flag(&pgsa_state->stashes_ready);
}
-
- /* Note the current change count so we can detect future changes. */
- last_change_count = pg_atomic_read_u64(&pgsa_state->change_count);
+ else
+ last_change_count = 0;
/* Periodically write to disk until terminated. */
while (!ShutdownRequestPending)
[parent: 2a55a15d6b7c]