Diagnose and manage queries running in a Hologres instance.
Overview
Hologres is compatible with PostgreSQL. Use the hg_stat_activity (pg_stat_activity) view to monitor query runtime in an instance. This topic covers the following operations:
-
hg_stat_activity (pg_stat_activity) view: View SQL runtime information.
-
Manage active queries in HoloWeb: View and manage active queries in the HoloWeb console.
-
Troubleshoot locks: Identify whether a SQL statement holds or is blocked by a lock.
-
Terminate a query: Terminate underperforming queries.
-
Modify the timeout for an active query: Change the execution timeout to prevent deadlocks.
-
Modify the timeout for an idle query: Change the idle query timeout to prevent deadlocks.
-
Query slow query logs: Diagnose and optimize slow or failed queries.
-
FAQ: Causes and solutions for the
ERROR: canceling statement due to statement timeouterror.
Hologres does not support per-query memory or CPU limits. To control query resources, use a query queue to manage and schedule workload resources.
View active queries by using SQL
Use the following SQL statements to view active queries:
-
View active queries, their execution stages, and resource consumption.
NoteSuperusers can view runtime information for all users. Other users can only view their own.
-- 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' -- 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 running queries by CPU consumption.
-- 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 running queries by memory consumption.
-- 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 long-running queries in the current instance.
-- 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; -- 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
UPDATEstatement has been running for 24 seconds without completing.
Manage active queries in HoloWeb
View and manage active queries in the HoloWeb console.
-
Log on to the HoloWeb console. Connect to HoloWeb and run queries.
-
In the top navigation bar, click Diagnostics and Optimization.
-
In the left-side navigation pane, choose Management for Information About Active Queries > Active Query Tasks.
-
On the Active Query Tasks page, click Search to view and manage active queries for the current instance.
The results include the following fields:
Parameter
Description
Query start
When the query started.
Runtime
Execution duration.
PID
Process ID of the backend handling the query.
Query
SQL statement being executed.
State
Connection state. Common values:
-
active: Query is running.
-
idle: Connection is idle.
-
idle in transaction: Idle within a transaction.
-
idle in transaction (Aborted): Idle within a failed transaction.
-
\N: Null state, typically a system background process. Can be ignored.
User name
Username of the current connection.
Application
Application that initiated the query.
Client address
Client IP address.
To terminate a long-running query, click Cancel in its Actions column. You can also select multiple queries and click Batch Cancel.
-
-
(Optional) Click Details in the Actions column to view query details.
On the Details page, you can:
-
Copy: Copy the SQL statement.
-
Format: Format the SQL statement.
-
Troubleshoot locks
Check active queries to determine whether a SQL statement holds or is blocked by a lock. Locks and lock troubleshooting.
Terminate a query
Terminate underperforming queries with the following commands.
-
Terminate a single query:
SELECT pg_cancel_backend(<pid>); -
Terminate multiple 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 active query timeout
Modify the execution timeout for active queries.
-
Syntax
SET statement_timeout = <time>; -
Parameter description
time: the timeout value. Range: 0 to 2147483647. Default unit: milliseconds. To use a different unit, enclose the value and unit in single quotes. Default: 10 hours. This setting is session-specific.NoteFor the timeout to apply, the
SET statement_timeout = <time>statement must be executed in the same batch as the target SQL statement. -
Usage examples
-
Set the timeout to 5,000 minutes. Because a unit is specified, enclose the value in single quotes.
SET statement_timeout = '5000min' ; SELECT * FROM tablename; -
Set the timeout to 5,000 ms.
SET statement_timeout = 5000 ; SELECT * FROM tablename;
-
Modify idle query timeout
The idle_in_transaction_session_timeout parameter controls when idle transactions are terminated. Without this setting, idle transactions never time out, which can cause deadlocks.
-
Use cases
Set this timeout to prevent deadlocks from leaked transactions. For example, the following transaction is never committed because the
COMMITstatement is missing, causing a transaction leak that can lead to a database-level deadlock.BEGIN; SELECT * FROM t;To resolve this, set idle_in_transaction_session_timeout. If an open 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
-- Modify the idle transaction timeout at the session level. SET idle_in_transaction_session_timeout=<time>; -- Modify the idle transaction timeout at the database level. ALTER database db_name SET idle_in_transaction_session_timeout=<time>; -
Parameter description
time: the timeout value. Range: 0 to 2147483647. Default unit: milliseconds. To use a different unit, enclose the value and unit in single quotes. Default: 0 (no auto-termination) in Hologres V0.10 and earlier; 10 minutes in Hologres V1.1 and later. Transactions idle for longer than 10 minutes are rolled back.NoteDo not set an excessively short timeout. The system may roll back transactions that are still in use.
-
Usage examples
Set the timeout to 300,000 ms.
-- Modify the idle transaction timeout at the session level. SET idle_in_transaction_session_timeout=300000; -- Modify the idle transaction timeout at the database level. ALTER database db_name SET idle_in_transaction_session_timeout=300000;
Query slow query logs
Hologres V0.10 and later support slow query logs. View and analyze slow query logs.
FAQ
-
Symptom
Executing a SQL statement returns the following error:
ERROR: canceling statement due to statement timeout. -
Causes and solutions
-
Cause 1: A timeout is configured in the client or the Hologres instance. Common timeout settings:
-
Data Service APIs have a fixed timeout of
10sthat cannot be modified. Optimize your SQL statements to reduce execution time. -
The Hologres SQL module in HoloWeb or DataWorks has a fixed query timeout of
1h. Optimize your SQL to reduce execution time. -
A timeout is set at the instance level. Run the following SQL to check it. If the instance timeout causes the error, adjust it as needed.
SHOW statement_timeout; -
A timeout is set in the client or application. Check and adjust the client timeout settings as needed.
-
-
Cause 2: A
DROPorTRUNCATEoperation was performed on the table while a DML statement was running.The
TRUNCATEoperation is equivalent todrop+create. A running DML statement holds a row or table lock (Locks and lock troubleshooting). A concurrentDROPorTRUNCATEon the same table competes for this lock, causing the system to cancel the DML statement with thestatement timeouterror.Solution: Check the slow query log for concurrent
DROPorTRUNCATEoperations on the table and avoid them.-- Example: Query the logs for drop/truncate operations on a specific table over the past 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';
-