Skip to content

Commit 05e5a08

Browse files
committed
Modify CountOtherDBBackends/TerminateOtherDBBackends to process whole cluster by providing invalid databaseId
1 parent fcb88ae commit 05e5a08

File tree

1 file changed

+18
-3
lines changed

1 file changed

+18
-3
lines changed

src/backend/storage/ipc/procarray.c

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3753,6 +3753,8 @@ CountUserBackends(Oid roleid)
37533753
* backend startup. The caller should normally hold an exclusive lock on the
37543754
* target DB before calling this, which is one reason we mustn't wait
37553755
* indefinitely.
3756+
*
3757+
* If databaseId is InvalidOid, count all backends in a cluster.
37563758
*/
37573759
bool
37583760
CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
@@ -3782,7 +3784,7 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
37823784
PGPROC *proc = &allProcs[pgprocno];
37833785
uint8 statusFlags = ProcGlobal->statusFlags[index];
37843786

3785-
if (proc->databaseId != databaseId)
3787+
if (databaseId != InvalidOid && proc->databaseId != databaseId)
37863788
continue;
37873789
if (proc == MyProc)
37883790
continue;
@@ -3831,6 +3833,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
38313833
*
38323834
* If the target database has a prepared transaction or permissions checks
38333835
* fail for a connection, this fails without terminating anything.
3836+
*
3837+
* If databaseId is InvalidOid, terminate all backends in a cluster.
38343838
*/
38353839
void
38363840
TerminateOtherDBBackends(Oid databaseId)
@@ -3847,7 +3851,7 @@ TerminateOtherDBBackends(Oid databaseId)
38473851
int pgprocno = arrayP->pgprocnos[i];
38483852
PGPROC *proc = &allProcs[pgprocno];
38493853

3850-
if (proc->databaseId != databaseId)
3854+
if (databaseId != InvalidOid && proc->databaseId != databaseId)
38513855
continue;
38523856
if (proc == MyProc)
38533857
continue;
@@ -3861,14 +3865,25 @@ TerminateOtherDBBackends(Oid databaseId)
38613865
LWLockRelease(ProcArrayLock);
38623866

38633867
if (nprepared > 0)
3864-
ereport(ERROR,
3868+
{
3869+
if (databaseId != InvalidOid)
3870+
ereport(ERROR,
38653871
(errcode(ERRCODE_OBJECT_IN_USE),
38663872
errmsg("database \"%s\" is being used by prepared transactions",
38673873
get_database_name(databaseId)),
38683874
errdetail_plural("There is %d prepared transaction using the database.",
38693875
"There are %d prepared transactions using the database.",
38703876
nprepared,
38713877
nprepared)));
3878+
else
3879+
ereport(ERROR,
3880+
(errcode(ERRCODE_OBJECT_IN_USE),
3881+
errmsg("cluster is being used by prepared transactions"),
3882+
errdetail_plural("There is %d prepared transaction using the cluster.",
3883+
"There are %d prepared transactions using the cluster.",
3884+
nprepared,
3885+
nprepared)));
3886+
}
38723887

38733888
if (pids)
38743889
{

0 commit comments

Comments
 (0)