From bdb49627b59a24ecefaf308f2dd782b25a6dcca9 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 8 Jul 2026 23:52:39 +0900
Subject: [PATCH v2] Fix data checksum processing for temp relations and
 dropped databases

When building the list of temporary relations to wait for, the code
previously included temporary relations without storage, such as
temporary views, even though they are irrelevant to checksum
processing. As a result, enabling data checksums could wait for a
long-lived session that owned only a temporary view.

This commit fixes the issue by filtering temporary relations with storage
only, matching the existing behavior for non-temporary relations.

Also, when enabling data checksums online, the launcher assigns the
first worker to process shared catalogs and prevents later workers from
doing so. Previously, if that worker's database was dropped after it
had been selected for processing but before checksum processing began,
the worker failed without processing the shared catalogs, yet they were
still marked as processed. As a result, later workers skipped them, and
checksum enabling could complete successfully even though the shared
catalogs had never been processed.

This commit fixes the issue by marking shared catalogs as processed
only after a worker completes successfully.

Author: Fujii Masao <masao.fujii@gmail.com>
Reviewed-by: Kyotaro Horiguchi <horikyota.ntt@gmail.com>
Reviewed-by: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/CAHGQGwGDHAQw=bmpRzk+EmKzVtxZiD5YDurMUffBMwr6WXugQA@mail.gmail.com
---
 src/backend/postmaster/datachecksum_state.c | 24 ++++++++++++++-------
 1 file changed, 16 insertions(+), 8 deletions(-)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index 68557c16cb9..6b136a86b74 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1324,6 +1324,14 @@ ProcessAllDatabases(void)
 			/* Abort flag set, so exit the whole process */
 			return false;
 		}
+		else if (result == DATACHECKSUMSWORKER_DROPDB)
+		{
+			/*
+			 * Ignore databases that were dropped before their worker could
+			 * process them, and continue with the remaining databases.
+			 */
+			continue;
+		}
 
 		/*
 		 * When one database has completed, it will have done shared catalogs
@@ -1469,11 +1477,11 @@ FreeDatabaseList(List *dblist)
  *		Compile a list of relations in the database
  *
  * Returns a list of OIDs for the requested relation types. If temp_relations
- * is True then only temporary relations are returned. If temp_relations is
- * False then non-temporary relations which have data checksums are returned.
- * If include_shared is True then shared relations are included as well in a
- * non-temporary list. include_shared has no relevance when building a list of
- * temporary relations.
+ * is True then only temporary relations with storage are returned.  If
+ * temp_relations is False then non-temporary relations with storage are
+ * returned.  If include_shared is True then shared relations are included as
+ * well in a non-temporary list. include_shared has no relevance when building
+ * a list of temporary relations.
  */
 static List *
 BuildRelationList(bool temp_relations, bool include_shared)
@@ -1494,6 +1502,9 @@ BuildRelationList(bool temp_relations, bool include_shared)
 	{
 		Form_pg_class pgc = (Form_pg_class) GETSTRUCT(tup);
 
+		if (!RELKIND_HAS_STORAGE(pgc->relkind))
+			continue;
+
 		/* Only include temporary relations when explicitly asked to */
 		if (pgc->relpersistence == RELPERSISTENCE_TEMP)
 		{
@@ -1509,9 +1520,6 @@ BuildRelationList(bool temp_relations, bool include_shared)
 			if (temp_relations)
 				continue;
 
-			if (!RELKIND_HAS_STORAGE(pgc->relkind))
-				continue;
-
 			if (pgc->relisshared && !include_shared)
 				continue;
 		}
-- 
2.55.0

