@@ -3753,6 +3753,8 @@ CountUserBackends(Oid roleid)
3753
3753
* backend startup. The caller should normally hold an exclusive lock on the
3754
3754
* target DB before calling this, which is one reason we mustn't wait
3755
3755
* indefinitely.
3756
+ *
3757
+ * If databaseId is InvalidOid, count all backends in a cluster.
3756
3758
*/
3757
3759
bool
3758
3760
CountOtherDBBackends (Oid databaseId , int * nbackends , int * nprepared )
@@ -3782,7 +3784,7 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3782
3784
PGPROC * proc = & allProcs [pgprocno ];
3783
3785
uint8 statusFlags = ProcGlobal -> statusFlags [index ];
3784
3786
3785
- if (proc -> databaseId != databaseId )
3787
+ if (databaseId != InvalidOid && proc -> databaseId != databaseId )
3786
3788
continue ;
3787
3789
if (proc == MyProc )
3788
3790
continue ;
@@ -3831,6 +3833,8 @@ CountOtherDBBackends(Oid databaseId, int *nbackends, int *nprepared)
3831
3833
*
3832
3834
* If the target database has a prepared transaction or permissions checks
3833
3835
* fail for a connection, this fails without terminating anything.
3836
+ *
3837
+ * If databaseId is InvalidOid, terminate all backends in a cluster.
3834
3838
*/
3835
3839
void
3836
3840
TerminateOtherDBBackends (Oid databaseId )
@@ -3847,7 +3851,7 @@ TerminateOtherDBBackends(Oid databaseId)
3847
3851
int pgprocno = arrayP -> pgprocnos [i ];
3848
3852
PGPROC * proc = & allProcs [pgprocno ];
3849
3853
3850
- if (proc -> databaseId != databaseId )
3854
+ if (databaseId != InvalidOid && proc -> databaseId != databaseId )
3851
3855
continue ;
3852
3856
if (proc == MyProc )
3853
3857
continue ;
@@ -3861,14 +3865,25 @@ TerminateOtherDBBackends(Oid databaseId)
3861
3865
LWLockRelease (ProcArrayLock );
3862
3866
3863
3867
if (nprepared > 0 )
3864
- ereport (ERROR ,
3868
+ {
3869
+ if (databaseId != InvalidOid )
3870
+ ereport (ERROR ,
3865
3871
(errcode (ERRCODE_OBJECT_IN_USE ),
3866
3872
errmsg ("database \"%s\" is being used by prepared transactions" ,
3867
3873
get_database_name (databaseId )),
3868
3874
errdetail_plural ("There is %d prepared transaction using the database." ,
3869
3875
"There are %d prepared transactions using the database." ,
3870
3876
nprepared ,
3871
3877
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
+ }
3872
3887
3873
3888
if (pids )
3874
3889
{
0 commit comments