Manage queries

更新时间:
复制 MD 格式

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:

Note

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:

  1. View active queries, their execution stages, and resource consumption.

    Note

    Superusers 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 }
  2. 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
  3. 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
  4. 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 UPDATE statement has been running for 24 seconds without completing.

Manage active queries in HoloWeb

View and manage active queries in the HoloWeb console.

  1. Log on to the HoloWeb console. Connect to HoloWeb and run queries.

  2. In the top navigation bar, click Diagnostics and Optimization.

  3. In the left-side navigation pane, choose Management for Information About Active Queries > Active Query Tasks.

  4. 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.

  5. (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.

    Note

    For 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 COMMIT statement 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.

    Note

    Do 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 10s that 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 DROP or TRUNCATE operation was performed on the table while a DML statement was running.

      The TRUNCATE operation is equivalent to drop+create. A running DML statement holds a row or table lock (Locks and lock troubleshooting). A concurrent DROP or TRUNCATE on the same table competes for this lock, causing the system to cancel the DML statement with the statement timeout error.

      Solution: Check the slow query log for concurrent DROP or TRUNCATE operations 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';