On Thu, Oct 29, 2026 at 9:59 AM Robert Haas <[email protected]> wrote:
> I'm guessing that this is fixed by Zsolt's patch, which I just committed.

The 001_persist.pl race Alexander reported still reproduces after
6f16120176 on REL_19_STABLE (61553eacad on master).

Adding pg_usleep(1000000); immediately after pgsa_read_from_disk()
in pg_stash_advice_worker_main(), before stashes_ready is set, makes
the unchanged test on REL_19_STABLE fail at
pg_create_advice_stash('stash_a'):

ERROR:  stash modifications are not allowed because
"pg_stash_advice.tsv" has not been loaded yet

The "loaded" log line used by the test after restart also comes before
stashes_ready is set. Once the first race is avoided, the delayed test
fails again when creating stash_c after the first restart.

The attached test-only patch waits for stash creation to succeed at
startup, for the expected stashes to appear after each restart, and for
stash modifications to be allowed after the final empty restart. The
focused TAP test passes with and without the delay on both branches.
The sleep is only for reproduction; it is not in the patch. AI prepared
the patch; I have not manually reviewed it.

Nik
From 187a85e3eab8106a0619e05eea81b2bab3a410b5 Mon Sep 17 00:00:00 2001
From: Nik Samokhvalov <[email protected]>
Date: Thu, 29 Oct 2026 12:09:23 -0700
Subject: [PATCH] pg_stash_advice: Wait for worker readiness in persistence
 test

---
 contrib/pg_stash_advice/t/001_persist.pl | 26 ++++++++++++++++++------
 1 file changed, 20 insertions(+), 6 deletions(-)

diff --git a/contrib/pg_stash_advice/t/001_persist.pl b/contrib/pg_stash_advice/t/001_persist.pl
index 89347fb35b..679fba243c 100644
--- a/contrib/pg_stash_advice/t/001_persist.pl
+++ b/contrib/pg_stash_advice/t/001_persist.pl
@@ -20,10 +20,14 @@ $node->start;
 
 $node->safe_psql("postgres", "CREATE EXTENSION pg_stash_advice;\n");
 
+# The persistence worker loads the dump file after the server starts.  Wait
+# until it allows changes before creating the first stash.
+$node->poll_query_until("postgres", "SELECT pg_create_advice_stash('stash_a')", '')
+	or die "timed out waiting for pg_stash_advice to load";
+
 # Create two stashes: one with 3 entries, one with just 1 entry.
 $node->safe_psql(
 	"postgres", qq{
-	SELECT pg_create_advice_stash('stash_a');
 	SELECT pg_set_stashed_advice('stash_a', 1001, 'IndexScan(t)');
 	SELECT pg_set_stashed_advice('stash_a', 1002, E'line1\\nline2\\ttab\\\\backslash');
 	SELECT pg_set_stashed_advice('stash_a', 1003, '');
@@ -39,7 +43,9 @@ is($result, "stash_a|3\nstash_b|1", 'stashes present before restart');
 
 # Restart and verify the data survived.
 $node->restart;
-$node->wait_for_log("loaded 2 advice stashes and 4 entries");
+$node->poll_query_until("postgres",
+	"SELECT count(*) = 2 FROM pg_get_advice_stashes()")
+	or die "timed out waiting for two advice stashes to load";
 
 $result = $node->safe_psql("postgres",
 	"SELECT stash_name, num_entries FROM pg_get_advice_stashes() ORDER BY stash_name"
@@ -63,7 +69,9 @@ $node->safe_psql(
 
 # Restart again and verify all three stashes are present.
 $node->restart;
-$node->wait_for_log("loaded 3 advice stashes and 4 entries");
+$node->poll_query_until("postgres",
+	"SELECT count(*) = 3 FROM pg_get_advice_stashes()")
+	or die "timed out waiting for three advice stashes to load";
 
 $result = $node->safe_psql("postgres",
 	"SELECT stash_name, num_entries FROM pg_get_advice_stashes() ORDER BY stash_name"
@@ -82,13 +90,19 @@ $node->safe_psql(
 
 $node->restart;
 
+ok( !-f $node->data_dir . '/pg_stash_advice.tsv',
+	'dump file removed after all stashes dropped');
+
+# An empty result also occurs before the worker finishes loading.  Check
+# that stash modifications are allowed before asserting the empty state.
+$node->poll_query_until("postgres", "SELECT pg_create_advice_stash('probe')", '')
+	or die "timed out waiting for pg_stash_advice to load";
+$node->safe_psql("postgres", "SELECT pg_drop_advice_stash('probe')");
+
 $result = $node->safe_psql("postgres",
 	"SELECT count(*) FROM pg_get_advice_stashes()");
 is($result, "0", 'no stashes after dropping all and restarting');
 
-ok( !-f $node->data_dir . '/pg_stash_advice.tsv',
-	'dump file removed after all stashes dropped');
-
 $node->stop;
 
 done_testing();
-- 
2.50.1 (Apple Git-155)

Reply via email to