Skip to content

Commit 33c615f

Browse files
committed
Fix DROP DATABASE for databases with many ACLs
Commit c66a7d7 modified DROP DATABASE so that if interrupted, the database is known to be in an invalid state and can only be dropped. This is done by setting a flag using an in-place update, so that it's not lost in case of rollback. For databases with many ACLs, this may however fail like this: ERROR: wrong tuple length This happens because with many ACLs, the pg_database.datacl attribute gets TOASTed. The dropdb() code reads the tuple from the syscache, which means it's detoasted. But the in-place update expects the tuple length to match the on-disk tuple. Fixed by reading the tuple from the catalog directly, not from syscache. Report and fix by Ayush Tiwari. Backpatch to 12. The DROP DATABASE fix was backpatched to 11, but 11 is EOL at this point. Reported-by: Ayush Tiwari Author: Ayush Tiwari Reviewed-by: Tomas Vondra Backpatch-through: 12 Discussion: https://postgr.es/m/CAJTYsWWNkCt+-UnMhg=BiCD3Mh8c2JdHLofPxsW3m2dkDFw8RA@mail.gmail.com
1 parent 881ca9c commit 33c615f

File tree

1 file changed

+16
-1
lines changed

1 file changed

+16
-1
lines changed

src/backend/commands/dbcommands.c

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
827827
bool db_istemplate;
828828
Relation pgdbrel;
829829
HeapTuple tup;
830+
ScanKeyData scankey;
831+
SysScanDesc scan;
830832
Form_pg_database datform;
831833
int notherbackends;
832834
int npreparedxacts;
@@ -959,7 +961,18 @@ dropdb(const char *dbname, bool missing_ok, bool force)
959961
*/
960962
dropDatabaseDependencies(db_id);
961963

962-
tup = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
964+
/*
965+
* Update the database's pg_database tuple
966+
*/
967+
ScanKeyInit(&scankey,
968+
Anum_pg_database_datname,
969+
BTEqualStrategyNumber, F_NAMEEQ,
970+
CStringGetDatum(dbname));
971+
972+
scan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true,
973+
NULL, 1, &scankey);
974+
975+
tup = systable_getnext(scan);
963976
if (!HeapTupleIsValid(tup))
964977
elog(ERROR, "cache lookup failed for database %u", db_id);
965978
datform = (Form_pg_database) GETSTRUCT(tup);
@@ -985,6 +998,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
985998
*/
986999
CatalogTupleDelete(pgdbrel, &tup->t_self);
9871000

1001+
systable_endscan(scan);
1002+
9881003
/*
9891004
* Drop db-specific replication slots.
9901005
*/

0 commit comments

Comments
 (0)