From 22e53e5b6e513ffd90de9a20c6c5bca7ce1bf421 Mon Sep 17 00:00:00 2001
From: Robert Haas <rhaas@postgresql.org>
Date: Fri, 28 Aug 2026 15:20:54 -0400
Subject: [PATCH v3 1/5] pg_stash_advice: Fix failure to reload empty advice
 string.

There's not really any good reason to store an empty advice string
in an advice stash, but it's entirely possible that some people might
do it by accident. If they do that without this fix, restarting the
system will result in failure while reloading pg_stash_advice.tsv.

The root cause is that I (rhaas) did not think clearly enough when
defining pgsa_next_tsv_field(). After returning each field on the
line, it set *cursor to the start of the next field, and after
returning all of them, it set *cursor to the trailing NUL byte. But
when the last field on the line is empty, this is ambiguous: if we're
pointing at the trailing NUL byte, it could be either because it's the
start of the zero-length field or because we're out of fields. To
remove the ambiguity, this commit decides that *cursor will instead
be set to NULL when there are no more fields.

Reported-by: Noah Misch <noah@leadboat.com>
---
 contrib/pg_stash_advice/stashpersist.c   | 33 +++++++++++++++---------
 contrib/pg_stash_advice/t/001_persist.pl | 18 +++++++------
 2 files changed, 31 insertions(+), 20 deletions(-)

diff --git a/contrib/pg_stash_advice/stashpersist.c b/contrib/pg_stash_advice/stashpersist.c
index ccdd13872b5..2b23ba969f5 100644
--- a/contrib/pg_stash_advice/stashpersist.c
+++ b/contrib/pg_stash_advice/stashpersist.c
@@ -368,7 +368,7 @@ pgsa_read_from_disk(void)
 								PGSA_DUMP_FILE, lineno)));
 
 			/* No further fields are expected. */
-			if (*cursor != '\0')
+			if (cursor != NULL)
 				ereport(ERROR,
 						(errcode(ERRCODE_DATA_CORRUPTED),
 						 errmsg("syntax error in file \"%s\" line %u: expected end of line",
@@ -423,7 +423,7 @@ pgsa_read_from_disk(void)
 								PGSA_DUMP_FILE, lineno)));
 
 			/* No further fields are expected. */
-			if (*cursor != '\0')
+			if (cursor != NULL)
 				ereport(ERROR,
 						(errcode(ERRCODE_DATA_CORRUPTED),
 						 errmsg("syntax error in file \"%s\" line %u: expected end of line",
@@ -603,25 +603,34 @@ pgsa_append_tsv_escaped_string(StringInfo buf, const char *str)
 /*
  * Extract the next tab-delimited field from *cursor.
  *
- * The tab delimiter is replaced with '\0' and *cursor is advanced past it.
- * If *cursor already points to '\0' (no more fields), returns NULL.
+ * Returns a pointer to the field data; when there are no remaining fields,
+ * returns NULL. For fields other than the last, the tab that terminates the
+ * field is replaced by \0, so that the caller can always interpret the return
+ * value as a C string.
+ *
+ * When this function is first called for a given line of text, *cursor should
+ * point to the beginning of the line. On return, *cursor will have been
+ * advanced past the tab that terminates the current field, or set to NULL if
+ * we just returned the last field.
  */
 static char *
 pgsa_next_tsv_field(char **cursor)
 {
 	char	   *start = *cursor;
-	char	   *p = start;
+	char	   *p;
 
-	if (*p == '\0')
+	if (start == NULL)
 		return NULL;
 
-	while (*p != '\0' && *p != '\t')
-		p++;
-
-	if (*p == '\t')
-		*p++ = '\0';
+	p = strchr(start, '\t');
+	if (p != NULL)
+	{
+		*p = '\0';
+		*cursor = p + 1;
+	}
+	else
+		*cursor = NULL;
 
-	*cursor = p;
 	return start;
 }
 
diff --git a/contrib/pg_stash_advice/t/001_persist.pl b/contrib/pg_stash_advice/t/001_persist.pl
index 83e98889f93..89347fb35bf 100644
--- a/contrib/pg_stash_advice/t/001_persist.pl
+++ b/contrib/pg_stash_advice/t/001_persist.pl
@@ -20,12 +20,13 @@ $node->start;
 
 $node->safe_psql("postgres", "CREATE EXTENSION pg_stash_advice;\n");
 
-# Create two stashes: one with 2 entries, one with 1 entry.
+# 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, '');
 	SELECT pg_create_advice_stash('stash_b');
 	SELECT pg_set_stashed_advice('stash_b', 2001, 'SeqScan(t)');
 });
@@ -34,23 +35,24 @@ $node->safe_psql(
 my $result = $node->safe_psql("postgres",
 	"SELECT stash_name, num_entries FROM pg_get_advice_stashes() ORDER BY stash_name"
 );
-is($result, "stash_a|2\nstash_b|1", 'stashes present before restart');
+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 3 entries");
+$node->wait_for_log("loaded 2 advice stashes and 4 entries");
 
 $result = $node->safe_psql("postgres",
 	"SELECT stash_name, num_entries FROM pg_get_advice_stashes() ORDER BY stash_name"
 );
-is($result, "stash_a|2\nstash_b|1", 'stashes survived restart');
+is($result, "stash_a|3\nstash_b|1", 'stashes survived restart');
 
-# Verify entry contents, including the one with special characters.
+# Verify entry contents, including the one with special characters and the
+# one with an empty advice string.
 $result = $node->safe_psql("postgres",
 	"SELECT stash_name, query_id, advice_string FROM pg_get_advice_stash_contents(NULL) ORDER BY stash_name, query_id"
 );
 is( $result,
-	"stash_a|1001|IndexScan(t)\nstash_a|1002|line1\nline2\ttab\\backslash\nstash_b|2001|SeqScan(t)",
+	"stash_a|1001|IndexScan(t)\nstash_a|1002|line1\nline2\ttab\\backslash\nstash_a|1003|\nstash_b|2001|SeqScan(t)",
 	'entry contents survived restart with special characters intact');
 
 # Add a third stash with 0 entries.
@@ -61,13 +63,13 @@ $node->safe_psql(
 
 # Restart again and verify all three stashes are present.
 $node->restart;
-$node->wait_for_log("loaded 3 advice stashes and 3 entries");
+$node->wait_for_log("loaded 3 advice stashes and 4 entries");
 
 $result = $node->safe_psql("postgres",
 	"SELECT stash_name, num_entries FROM pg_get_advice_stashes() ORDER BY stash_name"
 );
 is( $result,
-	"stash_a|2\nstash_b|1\nstash_c|0",
+	"stash_a|3\nstash_b|1\nstash_c|0",
 	'all three stashes survived second restart');
 
 # Drop all stashes and verify the dump file is removed after restart.
-- 
2.51.0

