]> granicus.if.org Git - postgresql/blobdiff - src/backend/commands/dbcommands.c
Phase 2 of pgindent updates.
[postgresql] / src / backend / commands / dbcommands.c
index 5a63b1abcbe0b563e515b1e700c8a4b7b7521b9f..96ab6f23cd1bde997df7886bd2a0a1473cd324e6 100644 (file)
@@ -322,7 +322,7 @@ createdb(ParseState *pstate, const CreatedbStmt *stmt)
         * won't change underneath us.
         */
        if (!dbtemplate)
-               dbtemplate = "template1";               /* Default template database name */
+               dbtemplate = "template1";       /* Default template database name */
 
        if (!get_db_info(dbtemplate, ShareLock,
                                         &src_dboid, &src_owner, &src_encoding,
@@ -845,19 +845,22 @@ dropdb(const char *dbname, bool missing_ok)
                                 errmsg("cannot drop the currently open database")));
 
        /*
-        * Check whether there are, possibly unconnected, logical slots that refer
-        * to the to-be-dropped database. The database lock we are holding
-        * prevents the creation of new slots using the database.
+        * Check whether there are active logical slots that refer to the
+        * to-be-dropped database. The database lock we are holding prevents the
+        * creation of new slots using the database or existing slots becoming
+        * active.
         */
-       if (ReplicationSlotsCountDBSlots(db_id, &nslots, &nslots_active))
+       (void) ReplicationSlotsCountDBSlots(db_id, &nslots, &nslots_active);
+       if (nslots_active)
+       {
                ereport(ERROR,
                                (errcode(ERRCODE_OBJECT_IN_USE),
-                         errmsg("database \"%s\" is used by a logical replication slot",
-                                        dbname),
-                                errdetail_plural("There is %d slot, %d of them active.",
-                                                                 "There are %d slots, %d of them active.",
-                                                                 nslots,
-                                                                 nslots, nslots_active)));
+                                errmsg("database \"%s\" is used by an active logical replication slot",
+                                               dbname),
+                                errdetail_plural("There is %d active slot",
+                                                                 "There are %d active slots",
+                                                                 nslots_active, nslots_active)));
+       }
 
        /*
         * Check for other backends in the target database.  (Because we hold the
@@ -914,6 +917,11 @@ dropdb(const char *dbname, bool missing_ok)
         */
        dropDatabaseDependencies(db_id);
 
+       /*
+        * Drop db-specific replication slots.
+        */
+       ReplicationSlotsDropDBSlots(db_id);
+
        /*
         * Drop pages for this database that are in the shared buffer cache. This
         * is important to ensure that no remaining backend tries to write out a
@@ -1276,7 +1284,7 @@ movedb(const char *dbname, const char *tblspcname)
                sysscan = systable_beginscan(pgdbrel, DatabaseNameIndexId, true,
                                                                         NULL, 1, &scankey);
                oldtuple = systable_getnext(sysscan);
-               if (!HeapTupleIsValid(oldtuple))                /* shouldn't happen... */
+               if (!HeapTupleIsValid(oldtuple))        /* shouldn't happen... */
                        ereport(ERROR,
                                        (errcode(ERRCODE_UNDEFINED_DATABASE),
                                         errmsg("database \"%s\" does not exist", dbname)));
@@ -2124,11 +2132,18 @@ dbase_redo(XLogReaderState *record)
                         * InitPostgres() cannot fully re-execute concurrently. This
                         * avoids backends re-connecting automatically to same database,
                         * which can happen in some cases.
+                        *
+                        * This will lock out walsenders trying to connect to db-specific
+                        * slots for logical decoding too, so it's safe for us to drop
+                        * slots.
                         */
                        LockSharedObjectForSession(DatabaseRelationId, xlrec->db_id, 0, AccessExclusiveLock);
                        ResolveRecoveryConflictWithDatabase(xlrec->db_id);
                }
 
+               /* Drop any database-specific replication slots */
+               ReplicationSlotsDropDBSlots(xlrec->db_id);
+
                /* Drop pages for this database that are in the shared buffer cache */
                DropDatabaseBuffers(xlrec->db_id);