This topic describes how to diagnose and manage SQL queries in a Hologres instance.

Overview

Hologres is compatible with PostgreSQL and allows you to query the execution information about SQL queries in a Hologres instance by querying the pg_stat_activity view. This helps you analyze and diagnose SQL execution. This feature involves the following operations:

Query the pg_stat_activity view

pg_stat_activity is a helpful PostgreSQL system view. You can use pg_stat_activity to analyze and diagnose running PostgreSQL tasks and troubleshoot problems. To query the execution information about SQL queries in a Hologres instance, execute the following statement:
select * from pg_stat_activity ;
The following table describes the parameters in the query results of the pg_stat_activity view.
Parameter Description
datid The object identifier (OID) of the connected database at the Hologres backend.
datname The name of the connected database at the Hologres backend.
pid The ID of a process at the Hologres backend.
usesysid The OID of a user that has logged on to the Hologres backend.
usename The username of the current connection.

holo_admin is the username of a built-in service account in Hologres. The connection that is automatically created by using this username is a PostgreSQL connection. This connection is required. If the number of connections to your Hologres instance has not reached the upper limit, you do not need to optimize this connection. For information about how to query the number of connections, see Query the information about connections.

application_name The type of the application on the client.
client_addr The IP address of the client.

The displayed IP address may have been resolved and may not be the actual IP address of the source.

client_hostname The hostname of the client.
client_port The port number of the client.
backend_start The start time of the background process.
xact_start The start time of the current active transaction of the process.
  • If no transaction is active, the value of this parameter is empty.
  • If the current query is the first transaction of the process, the value of this parameter is the same as the value of the query_start parameter.
query_start The start time of the current active query. If the current connection is not active, the value of this parameter is the start time of the last query.
state_change The point in time when the status of the connection was last changed.
wait_event_type The type of event for which the backend is waiting. If the backend is not waiting for an event, the value of this parameter is NULL. Valid values:
  • LWLock: The backend is waiting for a lightweight lock.
  • Lock: The backend is waiting for a heavyweight lock. The wait_event parameter indicates the type of lock for which the backend is waiting.
  • BufferPin: The server process is waiting to access a data buffer while no other process is checking the data buffer.
  • Activity: The server process is idle. This may be a system process that is waiting to be run in the main processing loop.
  • Extension: The server process is in an extension module and is waiting to be run.
  • Client: The server process is waiting for a query from a user application. In addition, the server is expecting an activity that is unrelated to its internal processing to happen.
  • PC: The server process is waiting for an activity of another process on the server.
  • Timeout: The server process is waiting for a timeout.
  • IO: The server process is waiting for the completion of an I/O.
wait_event The name of the event for which the backend is waiting. If the backend is not waiting for an event, the value of this parameter is NULL.
state The status of the connection. Valid values:
  • active: The connection is active.
  • idle: The connection is idle.
  • idle in transaction: The connection is idle in a long-running transaction.
  • idle in transaction (aborted): The connection is idle in a failed transaction.
backend_xid The identifier of the top-level transaction at the Hologres backend.
backend_xmin The xmin scope of the current backend.
query The last query that was run at the backend. If the value of the state parameter is active, the value of this parameter is the query that is being run. Otherwise, the value of this parameter is the query that was last run.
backend_type The type of the current backend. Supported types include autovacuum launcher, autovacuum worker, logical replication launcher, logical replication worker, parallel worker, background writer, client backend, checkpointer, startup, walreceiver, walsender, and walwriter. In addition, backend execution components such as Panel Quality Engineering (PQE) are also supported.

Query the execution information about SQL statements

The superuser of a Hologres instance can query the execution information about SQL statements that are submitted by all the users who are connected to the Hologres instance. RAM users can query the execution information only about SQL statements that are submitted by themselves. For more information about the parameters in the following statements, see the parameters in the query results of the pg_stat_activity view.
  1. To query the execution information about SQL statements that are submitted by a user who is connected to the current Hologres instance, execute the following statement:
    SELECT datname,usename,query,pid ,state FROM pg_stat_activity ;
  2. To query the execution information about SQL statements that are submitted by a user and are being executed, execute the following statement:
    SELECT datname,usename,query,pid,state
       FROM pg_stat_activity
       WHERE state != 'idle' ;

Query the information about time-consuming SQL statements

To query the information about time-consuming SQL statements, execute the following statement. For more information about the parameters in the following statement, see the parameters in the query results of the pg_stat_activity view.
select current_timestamp - query_start as runtime, datname, usename, query,pid
    from pg_stat_activity
    where state != 'idle'
    order by 1 desc;
The following code shows sample results. In this example, an UPDATE statement is time-consuming.
runtime     |    datname     | usename  | pid    |      current_query
-----------------+----------------+----------+------------------------------------
 00:00:24.258388 | holotest  | 123xxx   | 1267xx | UPDATE holo_order 
                                                   : set gmt = now(),
                                                   : trade_id = $1,
                                                   : trade_create_time = $2;
 00:00:1.186394  | testdb    | 156xx    | 1783xx | select * from oder;
(2 rows)

Cancel queries

If queries do not meet your expectations, you can cancel them as needed by using one of the following methods:
  • Cancel queries in a visualized way.

    You can cancel active queries with a few clicks in the HoloWeb console. For more information, see View the details of a query task.

  • Cancel queries by using SQL statements.
    For more information about the parameters in the following statements, see the parameters in the query results of the pg_stat_activity view.
    • Cancel the queries that use a specific connection.
      select pg_cancel_backend(<pid>);
    • Cancel multiple queries at a time.
      SELECT pg_cancel_backend(pid)
              ,query
              ,datname
              ,usename
              ,application_name
              ,client_addr
              ,client_port
              ,backend_start
              ,state
      FROM    pg_stat_activity
      WHERE   length(query) > 0
      AND     pid != pg_backend_pid()
      AND     backend_type = 'client backend'
      AND     application_name != 'hologres'
      AND     usename != 'holo_admin';

Change the timeout period of queries

Hologres allows you to change the timeout period of queries by executing an SQL statement.

  • Sample code
    set statement_timeout = <time>  
  • Parameters
    time: the timeout period of queries. Valid values: 0 to 2147483647. Default unit: milliseconds. If the unit is added after the specific time value, the value and unit must be enclosed in single quotation marks (' '). Otherwise, an error is returned. The current default timeout period is 10 hours. This setting is available only for sessions.
    Note The SET statement_timeout = <time> statement takes effect only when this statement is executed along with other SQL statements.
  • Examples
    • Set the timeout period to 5,000 minutes. If the unit is added after 5000 in the statement, 5000min must be enclosed in single quotation marks (' ').
      set statement_timeout = '5000min' ; 
      select * from tablename;
    • Set the timeout period to 5,000 ms.
      set statement_timeout = 5000 ; 
      select * from tablename;

Query slow query logs

In Hologres V0.10 and later, you can query slow query logs. For more information, see Query and analyze slow query logs (beta).

Resolve query deadlock issues

If you forget to submit the transaction after you execute the BEGIN statement, transaction leakage occurs and a specific database is locked. This affects the use of services. For example, if you execute the BEGIN ; SELECT * FROM t; statement to start a transaction but do not execute the COMMIT statement, a deadlock occurs.

Hologres can automatically roll back transactions after you set a timeout period for transactions. This prevents deadlocks. To be specific, you can use the idle_in_transaction_session_timeout parameter to set a timeout period for transactions. If a transaction has not been submitted or rolled back after its connection is idle for longer than the specified timeout period, Hologres automatically rolls back the transaction and closes the connection.
Note If the timeout period is too short, transactions that are in use may be rolled back by mistake. We recommend that you set an appropriate timeout period for transactions.
The following sample code provides examples. To set the timeout period, take note of the following rules: Unit: milliseconds. Default value: 0. A value of 0 indicates that transactions are not automatically cleaned up.
  • Set the timeout period of transactions for a database.
    alter database db_name set idle_in_transaction_session_timeout=300000;
  • Set the timeout period of transactions for a single connection.
    set idle_in_transaction_session_timeout=300000;