Rearrange dropdb() to avoid errors after allowing other sessions to exit.
authorAmit Kapila <[email protected]>
Sat, 9 Nov 2019 11:58:27 +0000 (17:28 +0530)
committerAmit Kapila <[email protected]>
Mon, 11 Nov 2019 02:12:45 +0000 (07:42 +0530)
During Drop Database, it is better to error out before allowing other
sessions to exit and forcefully terminating autovacuum workers.  All the
other errors except for checking subscriptions are already done before.

Author: Amit Kapila
Discussion: https://postgr.es/m/CAA4eK1+qhLkCYG2oy9xug9ur_j=G2wQNRYAyd+-kZfZ1z42pLw@mail.gmail.com

src/backend/commands/dbcommands.c

index 01d66212e9815d415ef217b694ac6e9014885ee8..4ad62e6bf85478c1d5826309bb7c29c1971b470c 100644 (file)
@@ -896,19 +896,6 @@ dropdb(const char *dbname, bool missing_ok)
                                                                  nslots_active, nslots_active)));
        }
 
-       /*
-        * Check for other backends in the target database.  (Because we hold the
-        * database lock, no new ones can start after this.)
-        *
-        * As in CREATE DATABASE, check this after other error conditions.
-        */
-       if (CountOtherDBBackends(db_id, &notherbackends, &npreparedxacts))
-               ereport(ERROR,
-                               (errcode(ERRCODE_OBJECT_IN_USE),
-                                errmsg("database \"%s\" is being accessed by other users",
-                                               dbname),
-                                errdetail_busy_db(notherbackends, npreparedxacts)));
-
        /*
         * Check if there are subscriptions defined in the target database.
         *
@@ -924,6 +911,19 @@ dropdb(const char *dbname, bool missing_ok)
                                                                  "There are %d subscriptions.",
                                                                  nsubscriptions, nsubscriptions)));
 
+       /*
+        * Check for other backends in the target database.  (Because we hold the
+        * database lock, no new ones can start after this.)
+        *
+        * As in CREATE DATABASE, check this after other error conditions.
+        */
+       if (CountOtherDBBackends(db_id, &notherbackends, &npreparedxacts))
+               ereport(ERROR,
+                               (errcode(ERRCODE_OBJECT_IN_USE),
+                                errmsg("database \"%s\" is being accessed by other users",
+                                               dbname),
+                                errdetail_busy_db(notherbackends, npreparedxacts)));
+
        /*
         * Remove the database's tuple from pg_database.
         */