Help Center> GaussDB> Distributed_2.x> Database Security Management> Checking the Number of Database Connections
Updated on 2023-10-23 GMT+08:00

Checking the Number of Database Connections

Background

If the number of connections reaches its upper limit, new connections cannot be created. Therefore, if a user fails to connect a database, the administrator must check whether the number of connections has reached the upper limit. The following are details about database connections:

  • The maximum number of global connections is specified by the max_connections parameter.
  • The number of a user's connections is specified by CONNECTION LIMIT connlimit in the CREATE ROLE statement and can be changed using CONNECTION LIMIT connlimit in the ALTER ROLE statement.
  • The number of a database's connections is specified by the CONNECTION LIMIT connlimit parameter in the CREATE DATABASE statement.
  • Some connections need to be reserved for the gs_clean tool to remove residual transactions as the residual transactions may affect system operation. In a cluster with n CNs, at least n connections need to be reserved for the gs_clean tool on the CNs.

Procedure

  1. Connect to a database. For details, see Connecting to a Database.
  2. View the upper limit of the number of global connections.

    1
    2
    3
    4
    5
    openGauss=# SHOW max_connections;
     max_connections
    -----------------
     800
    (1 row)
    

    800 is the maximum number of session connections.

  3. View the number of session connections that have been used.

    For details, see Table 1.

    Except for database and usernames that are enclosed in double quotation marks (") during creation, uppercase letters are not allowed in the database and usernames in the commands in the following table.

    Table 1 Viewing the number of session connections

    Description

    Command

    View the maximum number of sessions connected to a specific user.

    Run the following command to view the upper limit of the number of user omm's connections. -1 indicates that no upper limit is set for user omm.

    1
    2
    3
    4
    5
    openGauss=# SELECT ROLNAME,ROLCONNLIMIT FROM PG_ROLES WHERE ROLNAME='omm';
     rolname  | rolconnlimit
    ----------+--------------
     omm |           -1
    (1 row)
    

    View the number of session connections that have been used by a user.

    Run the following command to view the number of session connections that have been used by user omm: 1 indicates that one connection has been used by user omm.

    1
    2
    3
    4
    5
    6
    openGauss=# SELECT COUNT(*) FROM dv_sessions WHERE USERNAME='omm';
    
     count
    -------
         1
    (1 row)
    

    View the maximum number of sessions connected to a specific database.

    Run the following commands to view the upper limit of the number of postgres's session connections: -1 indicates that no upper limit is set for the number of postgres's session connections.

    1
    2
    3
    4
    5
    6
    openGauss=# SELECT DATNAME,DATCONNLIMIT FROM PG_DATABASE WHERE DATNAME='postgres';
    
     datname  | datconnlimit
    ----------+--------------
     postgres |           -1
    (1 row)
    

    View the number of session connections that have been used by a specific database.

    Run the following commands to view the number of session connections that have been used by postgres: 1 indicates the number of session connections that have been used by postgres.

    1
    2
    3
    4
    5
    openGauss=# SELECT COUNT(*) FROM PG_STAT_ACTIVITY WHERE DATNAME='postgres';
     count 
    -------
         1
    (1 row)
    

    View the number of session connections that have been used by all users.

    Run the following commands to view the number of session connections that have been used by all users:

    1
    2
    3
    4
    5
    openGauss=# SELECT COUNT(*) FROM dv_sessions;
     count
    -------
         10
    (1 row)