From 3e0fbc07bade81a423b668d975111bb02067442f Mon Sep 17 00:00:00 2001
From: Daniel Gustafsson <dgustafsson@postgresql.org>
Date: Thu, 23 Apr 2026 10:44:40 +0200
Subject: [PATCH 7/8] Improve database detection logic in datachecksumsworker

The worker need to know whether a database which failed checksum
processing still exists, or has been dropped.  This improves the
detection logic by checking for being partially dropped.

Author: Daniel Gustafsson <daniel@yesql.se>
Discussion: https://postgr.es/m/xxx
---
 src/backend/postmaster/datachecksum_state.c | 16 +++++++++++++---
 1 file changed, 13 insertions(+), 3 deletions(-)

diff --git a/src/backend/postmaster/datachecksum_state.c b/src/backend/postmaster/datachecksum_state.c
index c382b954d91..53b66cbbcf6 100644
--- a/src/backend/postmaster/datachecksum_state.c
+++ b/src/backend/postmaster/datachecksum_state.c
@@ -843,8 +843,7 @@ ProcessDatabase(DataChecksumsWorkerDatabase *db)
 
 		/*
 		 * Heuristic to see if the database was dropped, and if it was we can
-		 * treat it as not an error, else treat as fatal and error out. TODO:
-		 * this could probably be improved with a tighter check.
+		 * treat it as not an error, else treat as fatal and error out.
 		 */
 		if (DatabaseExists(db->dboid))
 			return DATACHECKSUMSWORKER_FAILED;
@@ -1305,7 +1304,9 @@ DataChecksumsShmemRequest(void *arg)
  * DatabaseExists
  *
  * Scans the system catalog to check if a database with the given Oid exists
- * and returns true if it is found, else false.
+ * and returns true if it is found and valid, else false. Note, we cannot use
+ * database_is_invalid_oid here as it will ERROR out, and we want to gracefully
+ * handle errors.
  */
 static bool
 DatabaseExists(Oid dboid)
@@ -1315,6 +1316,7 @@ DatabaseExists(Oid dboid)
 	SysScanDesc scan;
 	bool		found;
 	HeapTuple	tuple;
+	Form_pg_database pg_database_tuple;
 
 	StartTransactionCommand();
 
@@ -1328,6 +1330,14 @@ DatabaseExists(Oid dboid)
 	tuple = systable_getnext(scan);
 	found = HeapTupleIsValid(tuple);
 
+	/* If the Oid exists, ensure that it's not partially dropped */
+	if (found)
+	{
+		pg_database_tuple = (Form_pg_database) GETSTRUCT(tuple);
+		if (database_is_invalid_form(pg_database_tuple))
+			found = false;
+	}
+
 	systable_endscan(scan);
 	table_close(rel, AccessShareLock);
 
-- 
2.39.3 (Apple Git-146)

