DEV Community

tongxi
tongxi

Posted on

openGauss 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. Its default value is 5000.
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.
Procedure
Log in as the OS user omm to the primary node of the database.

Run the following command to connect to the database:

""
gsql -d postgres -p 8000
postgres is the name of the database to be connected, and 8000 is the port number of the database primary node.

If information similar to the following is displayed, the connection succeeds:

""
gsql ((openGauss 1.0 build 290d125f) compiled at 2020-05-08 02:59:43 commit 2143 last mr 131)
Non-SSL connection (SSL connection is recommended when requiring high-security)
Type "help" for help.

postgres=#
View the upper limit of the number of global connections.

""
postgres=# SHOW max_connections;

max_connections

800
(1 row)
800 is the maximum number of session connections.

View the number of connections that have been used.

For details, see Table 1.

NOTICE:
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 commands to view the upper limit of the number of omm's session connections. -1 indicates that no upper limit is set for the number of omm's session connections.

""postgres=# 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 commands to view the number of session connections that have been used by omm. 1 indicates the number of session connections that have been used by omm.

""postgres=# CREATE OR REPLACE VIEW DV_SESSIONS AS
SELECT
sa.sessionid AS SID,
0::integer AS SERIAL#,
sa.usesysid AS USER#,
ad.rolname AS USERNAME
FROM pg_stat_get_activity(NULL) AS sa
LEFT JOIN pg_authid ad ON(sa.usesysid = ad.oid)
WHERE sa.application_name <> 'JobScheduler';
postgres=# 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.

""postgres=# 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.

""postgres=# 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:

""postgres=# CREATE OR REPLACE VIEW DV_SESSIONS AS
SELECT
sa.sessionid AS SID,
0::integer AS SERIAL#,
sa.usesysid AS USER#,
ad.rolname AS USERNAME
FROM pg_stat_get_activity(NULL) AS sa
LEFT JOIN pg_authid ad ON(sa.usesysid = ad.oid)
WHERE sa.application_name <> 'JobScheduler';
postgres=# SELECT COUNT(*) FROM DV_SESSIONS;
count
--------
10
(1 row)

Top comments (0)