Using PGXC_STAT_ACTIVITY to Analyze SQL Statements That Are Being Executed to Handle DWS Performance Issues
During development, developers often encounter problems such as excessive SQL connections, long SQL query time, and SQL query blocking. You can use the PGXC_STAT_ACTIVITY and PGXC_THREAD_WAIT_STATUS views to analyze and locate SQL problems. This section describes some common locating methods using PGXC_STAT_ACTIVITY.
| Column | Type | Description |
|---|---|---|
| coorname | text | Name of the CN in the current cluster |
| datid | oid | OID of the database that the user session connects to in the backend |
| datname | name | Name of the database that the user session connects to in the backend |
| pid | bigint | ID of the backend thread |
| lwtid | integer | Lightweight thread ID of the backend thread |
| usesysid | oid | OID of the user logging in to the backend |
| usename | name | Name of the user logging in to the backend |
| application_name | text | Name of the application connected to the backend |
| client_addr | inet | IP address of the client connected to the backend. null indicates that the client is connected via a Unix socket on the server machine or that this is an internal process such as autovacuum. |
| client_hostname | text | Host name of the connected client, as reported by a reverse DNS lookup of client_addr. This column will only be non-null for IP connections, and only when log_hostname is enabled. |
| client_port | integer | TCP port number used by the client to communicate with the backend. If a Unix socket is used, it is –1. |
| backend_start | timestamp with time zone | Startup time of the backend process, that is, the time when the client connects to the server |
| xact_start | timestamp with time zone | Time when the current transaction was started, or NULL if no transaction is active. If the current query is the first of its transaction, this column is equal to the query_start column. |
| query_start | timestamp with time zone | Time when the currently active query was started, or time when the last query was started if state is not active |
| state_change | timestamp with time zone | Time for the last status change |
| waiting | boolean | The value is t if the backend is waiting for a lock or node. Otherwise, the value is f. |
| enqueue | text | Queuing status of a statement. Its value can be:
|
| state | text | Overall state of the backend. Its value can be:
NOTE: Only system administrators can view the session status of their accounts. The state information of other accounts is empty. |
| resource_pool | name | Resource pool used by the user |
| stmt_type | text | Type of a user statement |
| query_id | bigint | ID of a query |
| query | text | Text of this backend's most recent query. If state is active, this column shows the ongoing query. In all other states, it shows the last query that was executed. |
| connection_info | text | A string in JSON format recording the driver type, driver version, driver deployment path, and process owner of the connected database |
Viewing Connection Information
- Set track_activities to on.
SET track_activities = on;
The database collects the running information about active queries only if this parameter is set to on.
- You can run the following SQL statements to check the current connection user, connection address, connection application, status, whether to wait for a lock, queuing status, and thread ID.
1SELECT coorname, usename,client_addr,application_name,state,waiting,enqueue,pid FROM PGXC_STAT_ACTIVITY WHERE DATNAME='Database name';
The following command output is displayed:

1usename | client_addr | application_name | state | waiting | enqueue | pid
- End a session (only the system administrator has the permission). cn_name is the coorname value in the preceding command output, and pid is the corresponding PID.
1EXECUTE DIRECT ON(cn_name) 'SELECT pg_terminate_backend(pid)';
Viewing SQL Running Information
- Run the following command to obtain all SQL information that the current user has permission to view (if the current user has administrator or preset role permission, all user query information can be displayed):
1SELECT usename,state,query FROM PGXC_STAT_ACTIVITY WHERE DATNAME='Database name';
If the value of state is active, the query column indicates the SQL statement that is being executed. In other cases, the query column indicates the previous query statement. If the value of state is idle, the connection is idle and waits for the user to enter a command. The following command output is displayed:1 2 3 4 5 6
usename | state | query ---------+--------+--------------------------------------------------------------------------- leo | idle | select * from joe.mytable; dbadmin | active | SELECT usename,state,query FROM PGXC_STAT_ACTIVITY WHERE DATNAME='gaussdb'; joe | idle | GRANT SELECT ON TABLE mytable to leo; (3 rows)
- Run the following command to view the information about the SQL statements that are not in the idle state:
1SELECT datname,usename,query FROM PGXC_STAT_ACTIVITY WHERE state != 'idle' ;
Viewing Time-Consuming Statements
- Check the SQL statements that take a long time to execute.
1SELECT current_timestamp - query_start as runtime, datname, usename, query FROM PGXC_STAT_ACTIVITY WHERE state != 'idle' order by 1 desc;
Query statements are returned and sorted by execution time length in descending order. The first record is the query statement that takes the longest time to execute.
1 2 3 4 5
runtime | datname | usename | query -----------------+----------+---------+----------------------------------------------------------------------------------------------------------------------------------------- 00:04:47.054958 | gaussdb | leo | insert into mytable1 select generate_series(1, 10000000); 00:00:01.72789 | gaussdb | dbadmin | SELECT current_timestamp - query_start as runtime, datname, usename, query FROM PGXC_STAT_ACTIVITY WHERE state != 'idle' order by 1 desc; (2 rows)
- Alternatively, you can set current_timestamp - query_start to be greater than a threshold to identify query statements that are executed for a duration longer than this threshold.
1SELECT query from PGXC_STAT_ACTIVITY WHERE current_timestamp - query_start > interval '2 days';
Querying Blocked Statements
- Run the following command to view blocked query statements:
1SELECT coorname, pid, datname, usename, state, query FROM PGXC_STAT_ACTIVITY WHERE state <> 'idle' and waiting=true;
Run the following statement to end the blocked SQL session:1EXECUTE DIRECT ON(cn_name) 'SELECT pg_terminate_backend(pid)';
- In most cases, blocking is caused by internal locks and waiting=true is displayed. You can view the blocking in the pgxc_stat_activity view.
- The blocked statements about file write and event schedulers cannot be viewed in the pgxc_stat_activity view.
- View information about the blocked query statements, tables, and schemas:
1 2 3 4 5 6 7 8 9 10 11 12
SELECT w.query as waiting_query, w.coorname as w_cn, w.pid as w_pid, w.usename as w_user, l.query as locking_query, l.pid as l_pid, l.usename as l_user, t.schemaname || '.' || t.relname as tablename from pgxc_stat_activity w join pg_locks l1 on w.pid = l1.pid and not l1.granted join pg_locks l2 on l1.relation = l2.relation and l2.granted join pgxc_stat_activity l on l2.pid = l.pid join pg_stat_user_tables t on l1.relation = t.relid where w.waiting;
The session ID, CN name, user information, query status, as well as information about the tables and schemas that block the query statements are returned.
After finding the blocked table or schema information, end the faulty session based on the session ID.
1EXECUTE DIRECT ON(cn_name) 'SELECT pg_terminate_backend(pid)';
If t or true is returned, the session is ended.
If information similar to the following is returned, the user is attempting to terminate the session, but the session will be reconnected rather than terminated.
FATAL: terminating connection due to administrator command FATAL: terminating connection due to administrator command The connection to the server was lost. Attempting reset: Succeeded.
If the PG_TERMINATE_BACKEND function is used by the gsql client to terminate the background threads of the session, the client will be reconnected automatically rather than be terminated.
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.See the reply and handling status in My Cloud VOC.
For any further questions, feel free to contact us through the chatbot.
Chatbot