@@ -3703,6 +3703,8 @@ CountUserBackends(Oid roleid)
3703
3703
* backend startup. The caller should normally hold an exclusive lock on the
3704
3704
* target DB before calling this, which is one reason we mustn't wait
3705
3705
* indefinitely.
3706
+ *
3707
+ * If databaseId is InvalidOid, count all backends in a cluster.
3706
3708
*/
3707
3709
bool
3708
3710
CountOtherDBBackends (Oid databaseId , int * nbackends , int * nprepared )
@@ -3732,7 +3734,7 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3732
3734
PGPROC * proc = & allProcs [pgprocno ];
3733
3735
uint8 statusFlags = ProcGlobal -> statusFlags [index ];
3734
3736
3735
- if (proc -> databaseId != databaseId )
3737
+ if (databaseId != InvalidOid && proc -> databaseId != databaseId )
3736
3738
continue ;
3737
3739
if (proc == MyProc )
3738
3740
continue ;
@@ -3781,6 +3783,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3781
3783
*
3782
3784
* If the target database has a prepared transaction or permissions checks
3783
3785
* fail for a connection, this fails without terminating anything.
3786
+ *
3787
+ * If databaseId is InvalidOid, terminate all backends in a cluster.
3784
3788
*/
3785
3789
void
3786
3790
TerminateOtherDBBackends (Oid databaseId )
@@ -3797,7 +3801,7 @@ TerminateOtherDBBackends(Oid databaseId)
3797
3801
int pgprocno = arrayP -> pgprocnos [i ];
3798
3802
PGPROC * proc = & allProcs [pgprocno ];
3799
3803
3800
- if (proc -> databaseId != databaseId )
3804
+ if (databaseId != InvalidOid && proc -> databaseId != databaseId )
3801
3805
continue ;
3802
3806
if (proc == MyProc )
3803
3807
continue ;
@@ -3811,14 +3815,25 @@ TerminateOtherDBBackends(Oid databaseId)
3811
3815
LWLockRelease (ProcArrayLock );
3812
3816
3813
3817
if (nprepared > 0 )
3814
- ereport (ERROR ,
3818
+ {
3819
+ if (databaseId != InvalidOid )
3820
+ ereport (ERROR ,
3815
3821
(errcode (ERRCODE_OBJECT_IN_USE ),
3816
3822
errmsg ("database \"%s\" is being used by prepared transactions" ,
3817
3823
get_database_name (databaseId )),
3818
3824
errdetail_plural ("There is %d prepared transaction using the database." ,
3819
3825
"There are %d prepared transactions using the database." ,
3820
3826
nprepared ,
3821
3827
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
+ }
3822
3837
3823
3838
if (pids )
3824
3839
{
0 commit comments