This is an automated email from the ASF dual-hosted git repository.

reshke pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit e66d65ce4c900961985e8ba767fbaa8363ccfce7
Author: Tomas Vondra <[email protected]>
AuthorDate: Mon Aug 19 00:04:41 2024 +0200

    Fix DROP DATABASE for databases with many ACLs
    
    Commit c66a7d75e652 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=bicd3mh8c2jdhlofpxsw3m2dkdfw...@mail.gmail.com
---
 src/backend/commands/dbcommands.c | 17 ++++++++++++++++-
 1 file changed, 16 insertions(+), 1 deletion(-)

diff --git a/src/backend/commands/dbcommands.c 
b/src/backend/commands/dbcommands.c
index 2afd694a967..6ad0a791a1c 100644
--- a/src/backend/commands/dbcommands.c
+++ b/src/backend/commands/dbcommands.c
@@ -1688,6 +1688,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
        bool            db_istemplate = true;
        Relation        pgdbrel;
        HeapTuple       tup;
+       ScanKeyData scankey;
+       SysScanDesc scan;
        Form_pg_database datform;
        int                     notherbackends;
        int                     npreparedxacts;
@@ -1847,7 +1849,18 @@ dropdb(const char *dbname, bool missing_ok, bool force)
         */
        pgstat_drop_database(db_id);
 
-       tup = SearchSysCacheCopy1(DATABASEOID, ObjectIdGetDatum(db_id));
+       /*
+        * Update the database's pg_database tuple
+        */
+       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);
@@ -1897,6 +1910,8 @@ dropdb(const char *dbname, bool missing_ok, bool force)
                pfree(buffer.data);
        }
 
+       systable_endscan(scan);
+
        /*
         * Drop db-specific replication slots.
         */


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to