This topic describes how to diagnose and manage queries in an instance.
Overview
Hologres is compatible with PostgreSQL. You can use the HG_STAT_ACTIVITY (PG_STAT_ACTIVITY) view to view the runtime information of queries in an instance for analysis and diagnosis. The specific operations are as follows:
HG_STAT_ACTIVITY (PG_STAT_ACTIVITY) view: View SQL runtime information to better manage SQL statements.
Manage active queries in HoloWeb: View and manage active queries visually using HoloWeb.
Troubleshoot locks: Check for locks or lock contention that affect a SQL statement.
Cancel queries: Use command statements to cancel unexpected queries.
Set the timeout for active queries: Set a timeout for active queries to prevent deadlocks.
Set the timeout for idle queries: Set a timeout for idle queries to prevent deadlocks.
Query slow query logs: Query the slow query log to diagnose, analyze, and optimize slow or failed queries.
FAQ: Causes of and solutions to the
ERROR: canceling statement due to statement timeouterror.
View active queries
To find active queries using SQL, use the following SQL statements:
View current active queries along with their execution stages and resource consumption:
NoteSuperusers can view the SQL runtime information of all users. Non-superusers 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:
-- 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 | 1008305xxxSort 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 | 10083259096119559View long-running queries:
-- 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 result shows the
UPDATEstatement is taking a long time, having run for 24 seconds without completing.
Manage active queries in HoloWeb
Use HoloWeb to view and manage active queries visually.
In the top navigation bar, select Diagnostics and Optimization.
In the left navigation menu, choose Active Quert Tasks.
Click Search to view and manage active queries for the current instance.
The query results list contains the following information:
Parameter
Description
Query Start
The time when the query starts.
Runtime
The duration of the query.
PID
The process ID of the query service.
Query
The executed SQL statement.
State
The current state of the connection. Common states include:
active: The connection is active.
idle: The component is not in use.
idle in transaction: The connection is idlewithin a long transaction.
idle in transaction (Aborted): The connection is idle in a failed transaction.
\N: The state is empty. This typically indicates a system background maintenance process, not a user connection. You can ignore this.
User Name
The username for the current connection.
Application
The application type of the query.
Client Address
The IP address of the client.
If a query runs for too long, you can click Cancel in the Actions column to terminate it. To cancel multiple queries at once, select them and click Batch Cancel.
(Optional) To view the details of a target query, click Details in the Actions column.
On the Details page, do the following:
Copy the SQL statement.
Format the executed SQL statement.
Troubleshoot locks
Use active queries to check if a SQL statement is a lock holder or is blocked by a lock. For more information, see Locks: Types, behavior, troubleshooting, and common errors.
Cancel a query
If a query does not perform as expected, cancel it using the following SQL commands.
Cancel a query:
SELECT pg_cancel_backend(<pid>);Cancel 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'
Set the timeout for active queries
In Hologres, you can set the timeout for active queries as follows:
Syntax
SET statement_timeout = <time>;Parameter description
time: The timeout value, which can range from 0 to 2147483647. The default unit is milliseconds (ms). To specify a unit, enclose the value in single quotation marks. The default timeout is 8 hours. This setting is effective at the session level.NoteThe
SET statement_timeout = <time>statement must be executed in the same batch as the SQL statement to which it applies.Examples
Set the timeout to 5,000 minutes. The unit is specified, so the entire value
5000minmust 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;
Set the timeout for idle queries
The idle_in_transaction_session_timeout parameter specifies the maximum allowed duration for a transaction to remain idle. By default, idle transactions do not time out, which can hold resources and lead to deadlocks.
Scenarios
Set this timeout to prevent a query from causing a deadlock. For example, in the following code, a transaction is started but not committed because the
commitstatement is missing. This can cause a transaction leak, leading to a database-level deadlock that affects service availability.BEGIN; SELECT * FROM t;When such a deadlock occurs, resolve it by setting
idle_in_transaction_session_timeout. Once the specified timeout is reached for an idle transaction, Hologres automatically rolls back the transaction and closes the connection.Syntax
-- Change the idle transaction timeout period at the session level. SET idle_in_transaction_session_timeout=<time>; -- Change 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 value, which can range from 0 to 2147483647. The default unit is milliseconds (ms). If you specify a unit, you must enclose the value and unit in single quotation marks. In Hologres V0.10 and earlier, the default value is 0, which means idle transactions do not time out. In Hologres V1.1, the default value is 10 minutes.NoteAvoid setting the timeout too short to prevent accidental rollback of useful idle transactions.
Examples
Set the timeout to 300,000 ms.
-- Change the idle transaction timeout period for the session. SET idle_in_transaction_session_timeout=300000; -- Change 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 the slow query log. For more information, see Query and analyze slow query logs.
FAQ
Question
Why am I getting an
"ERROR: canceling statement due to statement timeout"?Causes and solutions
Cause 1: The query exceeded a defined timeout. A
statement_timeoutcan be set in several places, and the query is canceled if it runs longer than this limit. Common places to check for this setting include:DataService APIs: Have a non-configurable timeout of
10s. To resolve this, optimize your SQL query to execute faster.HoloWeb or DataWorks: Queries run in the HoloWeb or DataWorks Hologres SQL node have a non-configurable timeout of
1h. You must optimize the query to finish within this limit.Hologres instance timeout: A global timeout may be set for the instance. Check the current value using the following command. If it's too short, adjust it.
SHOW statement_timeout;Client or application settings: A timeout is set in your client's connection string or application code. Review your client-side configuration to find and adjust the timeout.
Cause 2: A DML statement was canceled due to lock contention with a concurrent
DROPorTRUNCATEoperation.Hologres handles a
TRUNCATEoperation as aDROPfollowed by aCREATE. BothTRUNCATEandDROPrequire a strong lock that conflicts with concurrent DML statements (likeINSERTorUPDATE). If your DML statement is running and aDROPorTRUNCATEon the same table is initiated, Hologres may cancel the DML statement to resolve the lock contention, which triggers thestatement timeouterror.Solution: To confirm this is the cause, search the slow query log for
DROP TABLEorTRUNCATE TABLEcommands that ran at the same time as your failed query. The permanent solution is to avoid performing these DDL operations concurrently with DML operations on the same table. For example, you can use the following query to find such operations within the last day.-- Example: Query the records of drop/truncate operations performed on a 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';