This topic describes how to diagnose and manage queries in an instance.
Overview
Hologres is compatible with PostgreSQL. You can view the runtime information of queries in an instance using the HG_STAT_ACTIVITY (PG_STAT_ACTIVITY) view. This helps you analyze and diagnose running SQL statements. The operations include the following:
-
HG_STAT_ACTIVITY (PG_STAT_ACTIVITY) view: View SQL runtime information to better manage SQL statements.
-
Manage active queries in the HoloWeb console: View and manage active queries in the HoloWeb console.
-
Troubleshoot locks: Use active queries to check whether the current SQL statement holds a lock or is blocked by one.
-
Stop a query: Use a command to stop a query that does not meet expectations.
-
Modify the timeout period for active queries: Modify the timeout period for active queries to prevent deadlocks.
-
Modify the timeout period for idle queries: Modify the timeout period for idle queries to prevent deadlocks.
-
Query slow query logs: Query slow query logs to diagnose, analyze, and optimize slow or failed queries.
-
FAQ: Learn the causes of and solutions to the
ERROR: canceling statement due to statement timeouterror.
View active queries using SQL
If you prefer to query active queries using SQL, run the following SQL statements:
-
View the current active queries and their execution stages and resource consumption:
NoteSuperusers can view the SQL runtime information of all users. Non-superusers can only view their own SQL runtime information.
-- Syntax for Hologres V2.0 and later SELECT query,state,query_id,transaction_id,running_info, extend_info FROM hg_stat_activity WHERE state = 'active' AND backend_type = 'client backend' AND application_name != 'hologres' -- Syntax for Hologres V1.3 and earlier SELECT query,state,pid FROM pg_stat_activity WHERE state = 'active' AND backend_type = 'client backend' AND application_name != 'hologres'Sample result:
------------------------------------------------------------------------------- query | insert into test_hg_stat_activity select i, (i % 7) :: text, (i % 1007) from generate_series(1, 10000000)i; state | active query_id | 100713xxxx transaction_id | 100713xxxx running_info | {"current_stage" : {"stage_duration_ms" :5994, "stage_name" :"EXECUTE" }, "engine_type" :"{HQE,PQE}", "fe_id" :1, "warehouse_id" :0 } extend_info | {"affected_rows" :9510912, "scanned_rows" :9527296 } -
Sort the currently running queries by CPU consumption:
-- Syntax for Hologres V2.0 and later SELECT query,((extend_info::json)->'total_cpu_max_time_ms')::text::bigint AS cpu_cost,state,query_id,transaction_id FROM hg_stat_activity WHERE state = 'active' ORDER BY 2 DESC;Sample result:
--------------------------------------------------------------------------------- query | select xxxxx cpu_cost | 523461 state | active query_id | 10053xxxx transaction_id | 10053xxxx --------------------------------------------------------------------------------- query | insert xxxx cpu_cost | 4817 state | active query_id | 1008305xxx transaction_id | 1008305xxx -
Sort the currently running queries by memory consumption:
-- Syntax for Hologres V2.0 and later SELECT query,((extend_info::json)->'total_mem_max_bytes')::text::bigint AS mem_max_cost,state,query_id,transaction_id FROM hg_stat_activity WHERE state = 'active' ORDER BY 2 DESC;Sample result:
--------------------------------------------------------------------------------- query | update xxxx; mem_max_cost | 5727634542 state | active query_id | 10053302784827629 transaction_id | 10053302784827629 --------------------------------------------------------------------------------- query | select xxxx; mem_max_cost | 19535640 state | active query_id | 10083259096119559 transaction_id | 10083259096119559 -
View the long-running queries in the current instance:
-- Syntax for Hologres V2.0 and later SELECT current_timestamp - query_start AS runtime, datname::text, usename, query, query_id FROM hg_stat_activity WHERE state != 'idle' AND backend_type = 'client backend' AND application_name != 'hologres' ORDER BY 1 DESC; -- Syntax for Hologres V1.3 and earlier SELECT current_timestamp - query_start AS runtime, datname::text, usename, query, pid FROM pg_stat_activity WHERE state != 'idle' AND backend_type = 'client backend' AND application_name != 'hologres' ORDER BY 1 DESC;Sample result:
runtime | datname | usename | query_id | current_query -----------------+----------------+----------+------------------------------------ 00:00:24.258388 | holotest | 123xxx | 1267xx | UPDATE xxx; 00:00:1.186394 | testdb | 156xx | 1783xx | select xxxx;The update statement has been running for 24 seconds and has not finished.
Manage active queries in the HoloWeb console
You can view and manage active queries in the HoloWeb console.
-
Log on to the HoloWeb console. For more information, see Connect to HoloWeb and run queries.
-
In the top navigation bar, click Diagnostics and Optimization.
-
In the navigation pane on the left, choose Management for Information About Active Queries > Active Query Tasks.
-
On the Active Query Tasks page, click Search to view and manage the active queries of the current instance.
The query result list contains the following information:
Parameter
Description
Query Start
The time when the query started.
Runtime
The running time of the query.
PID
The ID of the query service process.
Query
The SQL statement that is run.
State
The status of the current connection. Common statuses are:
-
active: The connection is active.
-
idle: Idle.
-
idle in transaction: The connection is idle within a long-running transaction.
-
idle in transaction (Aborted): The connection is idle within a failed transaction.
-
\N: The status is empty. This indicates a process that is not a user connection. It is usually a background maintenance process of the system and can be ignored.
User Name
The username of the current connection.
Application
The type of the query application.
Client Address
The IP address of the client that initiated the query.
If a query runs for a long time, click Cancel in the Actions column of the query to stop it. You can also select multiple queries and click Batch Cancel.
-
-
(Optional) Click Details in the Actions column of the target query to view its details.
On the Details page, you can perform the following operations:
-
Copy: Copy the SQL statement that is run.
-
Format: Format the SQL statement that is run.
-
Troubleshoot locks
You can use active queries to check whether the current SQL statement holds a lock or is blocked by one. For more information, see Locks and lock troubleshooting.
Stop a query
To stop a query that does not meet expectations, run the following commands.
-
Stop a single query:
SELECT pg_cancel_backend(<pid>); -
Stop queries in a batch:
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'
Modify the timeout period for active queries
Hologres lets you modify the timeout period for active queries in the following ways.
-
Syntax example
SET statement_timeout = <time>; -
Parameter description
time: The timeout period. The value ranges from 0 to 2147483647. The default unit is milliseconds (ms). If you specify a unit for the time value, you must enclose the value in single quotation marks. Otherwise, an error is reported. The default timeout period is 8 hours. This setting is effective at the session level.
NoteThe `SET statement_timeout = <time>` statement must be run together with the SQL statement for which you want to modify the timeout period.
-
Examples
-
Set the timeout period to 5000 minutes. The value `5000min` includes a unit and must be enclosed in single quotation marks.
SET statement_timeout = '5000min' ; SELECT * FROM tablename; -
Set the timeout period to 5000 ms.
SET statement_timeout = 5000 ; SELECT * FROM tablename;
-
Modify the timeout period for idle queries
The idle_in_transaction_session_timeout parameter specifies the timeout behavior for a transaction after it enters the idle state. If you do not set this parameter, the transaction is not released upon timeout by default. This can cause transactions to remain open, which leads to deadlocks in queries. Hologres lets you modify the timeout period for idle queries in the following ways.
-
Scenarios
Set a timeout period when a query execution causes a deadlock. For example, in the following code, a transaction is started but not committed because the
commitstatement is not run. This causes a transaction leak, which can lead to a database-level deadlock and affect the normal use of the service.BEGIN; SELECT * FROM t;When this deadlock scenario occurs, you can resolve it by setting the idle_in_transaction_session_timeout parameter. If an idle connection with a transaction is not committed or rolled back within the time specified by idle_in_transaction_session_timeout, the system automatically rolls back the transaction and closes the connection.
-
Syntax examples
-- Modify the idle transaction timeout period at the session level SET idle_in_transaction_session_timeout=<time>; -- Modify the idle transaction timeout period at the database level ALTER database db_name SET idle_in_transaction_session_timeout=<time>; -
Parameter description
time: The timeout period. The value ranges from 0 to 2147483647. The default unit is milliseconds (ms). If you specify a unit for the time value, you must enclose the value in single quotation marks. Otherwise, an error is reported. In Hologres V0.10 and earlier, the default value is 0, which means idle transactions are not automatically cleared. In Hologres V1.1, the default value is 10 minutes. After 10 minutes, the transaction is rolled back.
NoteDo not set the timeout period to a small value. A short timeout period can cause transactions that are in use to be rolled back by mistake.
-
Examples
Set the timeout period to 300000 ms.
-- Modify the idle transaction timeout period at the session level SET idle_in_transaction_session_timeout=300000; -- Modify the idle transaction timeout period at the database level ALTER database db_name SET idle_in_transaction_session_timeout=300000;
Query slow query logs
Starting from Hologres V0.10, you can query slow query logs. For more information, see View and analyze slow query logs.
FAQ
-
Symptom
The following error is reported after you run an SQL statement:
ERROR: canceling statement due to statement timeout. -
Causes and solutions
-
Cause 1: A timeout period is set for the client or the Hologres instance. Common timeout settings are:
-
APIs are generated using DataService Studio. The timeout period for DataService Studio is
10 sand cannot be modified. Optimize the SQL statement to reduce its running time. -
Queries are run in the HoloWeb console or the Hologres SQL module of DataWorks. The timeout period is
1 hand cannot be modified. Optimize the SQL statement to reduce its running time. -
A timeout period is set for the Hologres instance. You can run the following SQL statement to view the timeout period set for the instance. If the error is caused by the instance timeout period, reset it to a reasonable value as needed.
SHOW statement_timeout; -
A timeout period is set for the client or application. Check the client settings. If the error is caused by the client timeout period, reset it to a reasonable value as needed.
-
-
Cause 2: A `DROP` or `TRUNCATE` operation is performed on a table while a Data Manipulation Language (DML) SQL statement is being run on the same table, which causes a timeout.
The `TRUNCATE` operation works by running
drop+create, which first deletes the table and then re-creates it. When a DML statement is run, it acquires row locks or a table lock. For more information about locks, see Locks and lock troubleshooting. If you run a `DROP` or `TRUNCATE` operation on the same table at the same time, the `DROP` or `TRUNCATE` operation competes for the lock held by the DML statement. The system then cancels the DML statement, and thestatement timeouterror is reported.Solution: Check the slow query logs to see if a
droportruncateoperation was performed on the table at the same time. Avoid such operations. The following example shows how to query the logs.-- Example: Query the drop/truncate records for a specific table in the last day SELECT * FROM hologres.hg_query_log WHERE command_tag IN ('DROP TABLE','TRUNCATE TABLE') AND query LIKE '%xxx%' AND query_start >= now() - interval '1 day';
-