Skip to content

Commit f844bee

Browse files
committed
Modify CountOtherDBBackends/TerminateOtherDBBackends to process whole cluster by providing invalid databaseId
1 parent 38e2652 commit f844bee

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
@@ -3703,6 +3703,8 @@ CountUserBackends(Oid roleid)
37033703
* backend startup. The caller should normally hold an exclusive lock on the
37043704
* target DB before calling this, which is one reason we mustn't wait
37053705
* indefinitely.
3706+
*
3707+
* If databaseId is InvalidOid, count all backends in a cluster.
37063708
*/
37073709
bool
37083710
CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
@@ -3732,7 +3734,7 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
37323734
PGPROC *proc = &allProcs[pgprocno];
37333735
uint8 statusFlags = ProcGlobal->statusFlags[index];
37343736

3735-
if (proc->databaseId != databaseId)
3737+
if (databaseId != InvalidOid && proc->databaseId != databaseId)
37363738
continue;
37373739
if (proc == MyProc)
37383740
continue;
@@ -3781,6 +3783,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
37813783
*
37823784
* If the target database has a prepared transaction or permissions checks
37833785
* fail for a connection, this fails without terminating anything.
3786+
*
3787+
* If databaseId is InvalidOid, terminate all backends in a cluster.
37843788
*/
37853789
void
37863790
TerminateOtherDBBackends(Oid databaseId)
@@ -3797,7 +3801,7 @@ TerminateOtherDBBackends(Oid databaseId)
37973801
int pgprocno = arrayP->pgprocnos[i];
37983802
PGPROC *proc = &allProcs[pgprocno];
37993803

3800-
if (proc->databaseId != databaseId)
3804+
if (databaseId != InvalidOid && proc->databaseId != databaseId)
38013805
continue;
38023806
if (proc == MyProc)
38033807
continue;
@@ -3811,14 +3815,25 @@ TerminateOtherDBBackends(Oid databaseId)
38113815
LWLockRelease(ProcArrayLock);
38123816

38133817
if (nprepared > 0)
3814-
ereport(ERROR,
3818+
{
3819+
if (databaseId != InvalidOid)
3820+
ereport(ERROR,
38153821
(errcode(ERRCODE_OBJECT_IN_USE),
38163822
errmsg("database \"%s\" is being used by prepared transactions",
38173823
get_database_name(databaseId)),
38183824
errdetail_plural("There is %d prepared transaction using the database.",
38193825
"There are %d prepared transactions using the database.",
38203826
nprepared,
38213827
nprepared)));
3828+
else
3829+
ereport(ERROR,
3830+
(errcode(ERRCODE_OBJECT_IN_USE),
3831+
errmsg("cluster is being used by prepared transactions"),
3832+
errdetail_plural("There is %d prepared transaction using the cluster.",
3833+
"There are %d prepared transactions using the cluster.",
3834+
nprepared,
3835+
nprepared)));
3836+
}
38223837

38233838
if (pids)
38243839
{

0 commit comments

Comments
 (0)