0001-Fix-drop-database-with-pg_database-toast-attribute.patch

application/x-patch

Filename: 0001-Fix-drop-database-with-pg_database-toast-attribute.patch
Type: application/x-patch
Part: 0
Message: Drop database command will raise "wrong tuple length" if pg_database tuple contains toast attribute.
From 52bfa40c67815e5929f09d6b5c48310c6a4db49b Mon Sep 17 00:00:00 2001
From: Ayush Tiwari <aytiwari@microsoft.com>
Date: Tue, 13 Aug 2024 02:54:57 +0530
Subject: [PATCH] Fix-drop-database-with-pg_database-toast-attribute

---
 src/backend/commands/dbcommands.c | 14 +++++++++++++-
 1 file changed, 13 insertions(+), 1 deletion(-)

diff --git a/src/backend/commands/dbcommands.c b/src/backend/commands/dbcommands.c
index 7026352bc9..b5673217b8 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -1649,6 +1649,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
 	bool		db_istemplate;
 	Relation	pgdbrel;
 	HeapTuple	tup;
+	ScanKeyData scankey;
+	SysScanDesc scan;
 	Form_pg_database datform;
 	int			notherbackends;
 	int			npreparedxacts;
@@ -1786,7 +1788,15 @@ dropdb(const char *dbname, bool missing_ok, bool force)
 	 */
 	pgstat_drop_database(db_id);
 
-	tup = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
+	ScanKeyInit(&scankey,
+				Anum_pg_database_datname,
+				BTEqualStrategyNumber, F_NAMEEQ,
+				CStringGetDatum(dbname));
+
+	scan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true,
+							  NULL, 1, &scankey);
+	tup = systable_getnext(scan);
+
 	if (!HeapTupleIsValid(tup))
 		elog(ERROR, "cache lookup failed for database %u", db_id);
 	datform = (Form_pg_database) GETSTRUCT(tup);
@@ -1812,6 +1822,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
 	 */
 	CatalogTupleDelete(pgdbrel, &tup->t_self);
 
+	systable_endscan(scan);
+
 	/*
 	 * Drop db-specific replication slots.
 	 */
-- 
2.34.1