During Hot Standby, fix drop database when sessions idle.

Previously we only cancelled sessions that were in-transaction.

Simple fix is to just cancel all sessions without waiting. Doing
it this way avoids complicating common code paths, which would
not be worth the trouble to cover this rare case.

Problem report and fix by Andres Freund, edited somewhat by me
This commit is contained in:
Simon Riggs 2010-01-10 15:44:28 +00:00
parent 87091cb1f1
commit 3bfcccc295
3 changed files with 48 additions and 16 deletions

View File

@ -13,7 +13,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.230 2010/01/02 16:57:37 momjian Exp $ * $PostgreSQL: pgsql/src/backend/commands/dbcommands.c,v 1.231 2010/01/10 15:44:28 sriggs Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1945,22 +1945,27 @@ dbase_redo(XLogRecPtr lsn, XLogRecord *record)
if (InHotStandby) if (InHotStandby)
{ {
VirtualTransactionId *database_users;
/* /*
* Find all users connected to this database and ask them * We don't do ResolveRecoveryConflictWithVirutalXIDs() here since
* politely to immediately kill their sessions before processing * that only waits for transactions and completely idle sessions
* the drop database record, after the usual grace period. * would block us. This is rare enough that we do this as simply
* We don't wait for commit because drop database is * as possible: no wait, just force them off immediately.
* non-transactional. *
* No locking is required here because we already acquired
* AccessExclusiveLock. Anybody trying to connect while we do this
* will block during InitPostgres() and then disconnect when they
* see the database has been removed.
*/ */
database_users = GetConflictingVirtualXIDs(InvalidTransactionId, while (CountDBBackends(xlrec->db_id) > 0)
xlrec->db_id, {
false); CancelDBBackends(xlrec->db_id);
ResolveRecoveryConflictWithVirtualXIDs(database_users, /*
"drop database", * Wait awhile for them to die so that we avoid flooding an
CONFLICT_MODE_FATAL); * unresponsive backend when system is heavily loaded.
*/
pg_usleep(10000);
}
} }
/* Drop pages for this database that are in the shared buffer cache */ /* Drop pages for this database that are in the shared buffer cache */

View File

@ -37,7 +37,7 @@
* *
* *
* IDENTIFICATION * IDENTIFICATION
* $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.54 2010/01/02 16:57:51 momjian Exp $ * $PostgreSQL: pgsql/src/backend/storage/ipc/procarray.c,v 1.55 2010/01/10 15:44:28 sriggs Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -1826,6 +1826,32 @@ CountDBBackends(Oid databaseid)
return count; return count;
} }
/*
* CancelDBBackends --- cancel backends that are using specified database
*/
void
CancelDBBackends(Oid databaseid)
{
ProcArrayStruct *arrayP = procArray;
int index;
/* tell all backends to die */
LWLockAcquire(ProcArrayLock, LW_EXCLUSIVE);
for (index = 0; index < arrayP->numProcs; index++)
{
volatile PGPROC *proc = arrayP->procs[index];
if (proc->databaseId == databaseid)
{
proc->recoveryConflictMode = CONFLICT_MODE_FATAL;
kill(proc->pid, SIGINT);
}
}
LWLockRelease(ProcArrayLock);
}
/* /*
* CountUserBackends --- count backends that are used by specified user * CountUserBackends --- count backends that are used by specified user
*/ */

View File

@ -7,7 +7,7 @@
* Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group * Portions Copyright (c) 1996-2010, PostgreSQL Global Development Group
* Portions Copyright (c) 1994, Regents of the University of California * Portions Copyright (c) 1994, Regents of the University of California
* *
* $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.28 2010/01/02 16:58:08 momjian Exp $ * $PostgreSQL: pgsql/src/include/storage/procarray.h,v 1.29 2010/01/10 15:44:28 sriggs Exp $
* *
*------------------------------------------------------------------------- *-------------------------------------------------------------------------
*/ */
@ -63,6 +63,7 @@ extern pid_t CancelVirtualTransaction(VirtualTransactionId vxid,
extern int CountActiveBackends(void); extern int CountActiveBackends(void);
extern int CountDBBackends(Oid databaseid); extern int CountDBBackends(Oid databaseid);
extern void CancelDBBackends(Oid databaseid);
extern int CountUserBackends(Oid roleid); extern int CountUserBackends(Oid roleid);
extern bool CountOtherDBBackends(Oid databaseId, extern bool CountOtherDBBackends(Oid databaseId,
int *nbackends, int *nprepared); int *nbackends, int *nprepared);