From 6095cc49dce11e88840ed0c94071b124a1114461 Mon Sep 17 00:00:00 2001
From: Fujii Masao <fujii@postgresql.org>
Date: Wed, 8 Jul 2026 23:52:39 +0900
Subject: [PATCH v1] 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.
---
 src/backend/postmaster/datachecksum_state.c | 22 +++++++++++++--------
 1 file changed, 14 insertions(+), 8 deletions(-)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index 68557c16cb9..2b3e1df6cf1 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -1329,9 +1329,12 @@ ProcessAllDatabases(void)
 		 * When one database has completed, it will have done shared catalogs
 		 * so we don't have to process them again.
 		 */
-		LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
-		DataChecksumState->process_shared_catalogs = false;
-		LWLockRelease(DataChecksumsWorkerLock);
+		if (result == DATACHECKSUMSWORKER_SUCCESSFUL)
+		{
+			LWLockAcquire(DataChecksumsWorkerLock, LW_EXCLUSIVE);
+			DataChecksumState->process_shared_catalogs = false;
+			LWLockRelease(DataChecksumsWorkerLock);
+		}
 	}
 
 	FreeDatabaseList(DatabaseList);
@@ -1469,11 +1472,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)
@@ -1499,6 +1502,9 @@ BuildRelationList(bool temp_relations, bool include_shared)
 		{
 			if (!temp_relations)
 				continue;
+
+			if (!RELKIND_HAS_STORAGE(pgc->relkind))
+				continue;
 		}
 		else
 		{
-- 
2.55.0

