问题诊断辅助工具
============================

本部分包含以下内容：

- 工具概述_
- 脚本工具所在路径_
- 配置数据库连接信息_
- 工具代码及功能_


工具概述
------------------------

问题诊断辅助工具是针对KingbaseES数据库开发的一套问题诊断工具。方便用户在系统故障情况下，及时了解系统特定信息。

为了保证脚本的通用性，本工具使用bash语法编写脚本，实际运行环境必须支持bash。

本工具的使用需要获取查询语句及等待事件，要求开启track_activities参数。部分shell脚本还需要依赖于extension的开启，如：sys_stat_statement，
sys_kwr，sys_ksh。以上插件的开启方法可以参照《 :ref:`KingbaseES数据库性能调优指南` 》文档。

以下表格列出脚本及扩展组件的依赖关系。

.. table::

   +-----------------------------------+-----------------------------------+
   | **脚本名称**                      | **扩展组件依赖**                  |
   +===================================+===================================+
   | chk_event_sql.sh                  | sys_kwr及相关参数                 |
   |                                   |                                   |
   |                                   | sys_stat_statements 及相关参数    |
   +-----------------------------------+-----------------------------------+
   | chk_top_sql.sh                    | sys_stat_statements 及相关参数    |
   +-----------------------------------+-----------------------------------+
   | chk_top_waitevent.sh              | sys_stat_statements 及相关参数    |
   +-----------------------------------+-----------------------------------+
   | chk_top_wal.sh                    | sys_stat_statements 及相关参数    |
   +-----------------------------------+-----------------------------------+

如果当前数据库系统是高可用集群系统，需要将数据库集群的密码文件，复制到root用户的根目录下。如果数据库集群的系统用户是kingbase用户，则通常的复制命令如下：

.. code::

    kingbase$ su - root -c "cp $HOME/.encpwd ~ "



脚本工具所在路径
-------------------------

本工具所涉及的脚本统一放在软件安装目录SupTools/kb_diagtool。


配置数据库连接信息
-----------------------------

在使用辅助工具前，先配置连接信息。用户读取通用参数文件(../suptools.ini)，设置环境变量。根据用户的实际需求，修改通用参数文件。(详见 :ref:`通用环境变量参数文件` )



工具代码及功能
------------------------


chk_current_waitevent.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查看数据库当前会话的等待事件信息。通过统计各类事件数量、当前等待会话正在运行的SQL，可以大致确定系统状态。


显示结果样例
~~~~~~~~~~~~~~~

.. figure:: image/image11.png

.. note::

   该工具信息取自sys_stat_activity，具体字段的含义可以参考sys_stat_activity 定义。


.. _chk_top_waitevent.sh:

chk_top_waitevent.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查看过去60秒内的数据库等待事件，按等待事件数量排序，数据取自 perf.session_history 。


显示结果样例
~~~~~~~~~~~~~~~

.. figure:: image/image12.png

.. note::

   该工具的信息取自perf.session_history，如果报该表不存在，表示sys_ksh 没有正确启用。



chk_event_sql.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^


功能
~~~~~~~~~~~~~~~

查看过去60秒内与特定等待事件相关的SQL。脚本运行时，需要特定的等待事件作为参数传入。等待事件可以根据chk_top_waitevent.sh结果，针对性进行分析。该脚本显示的数据取自 perf.session_history 。


显示结果样例
~~~~~~~~~~~~~~~

.. figure:: image/image13.png

.. note::

   该脚本传入参数是一个等待事件，通常是 :ref:`chk_top_waitevent.sh` 返回的top waitevent，用于进一步查询具体哪些SQL导致了该等待事件。


chk_lock.sh
^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

检查当前数据库锁等待的会话信息，及当前会话执行的SQL。通过层级显示，可以方便定位源头操作。


显示结果样例
~~~~~~~~~~~~~~~

.. figure:: image/image14.png

输出信息解释如下：

- 425715： 表示进程PID。

- transactioid： 表示锁的类型，这里是事务锁。

- Exclusive： 表示锁的模式，这里是独占锁。

- Null： 表对象的名称。由于是事务锁，因此，这里为null。

- Delete from t1 ：表示引起锁的具体SQL。




chk_top_sql.sh
^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~


查询过去10秒，最耗时、逻辑IO最多、物理IO最多（扣除buffer hit）、临时块写入最多的SQL，每类选取5条（默认5条），分别列出，数据取自sys_stat_statement 。可以通过 chk_top_sql.sh $n ,返回 n 条记录。

显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]# ./chk_top_sql.sh
                  now
      -------------------------------
      2025-01-16 14:19:54.088649+08
      (1 行记录)

      TOP 5 totaltime SQL
      =============================================================================================================================
      totaltime       queryid                   query
      --------------- ------------------------- -----------------------------------------------------------------------------------
      9               1893855320279741198       select count(*) from pg_available_extensions    WHERE name = $1 and installed_version is not null
      3               -915023414547726426       create table public.tmp_sql_stats_11223344 as select * from sys_stat_statements
      0               -4414999297299836195      select b.datname                                  "数据库",                                     +
      0               -7787808219802061843      select current_database()::char(15) "数据库",                                                   +
      0               2538051077016912011       select a.rolname      "用户",                                                                    +

      TOP 5 logicalread SQL
      =============================================================================================================================
      logicalread     queryid                   query
      --------------- ------------------------- -----------------------------------------------------------------------------------
      1056            -2658965675197519045      SELECT COUNT($1) AS count FROM pg_catalog.pg_class WHERE oid = $2 and relname in ($3, $4)
      733             -915023414547726426       create table public.tmp_sql_stats_11223344 as select * from sys_stat_statements
      476             2581027241184715520       select * from pg_proc where proname like $1
      428             3315119940765777476       select pg_catalog.concat($1,$2,total_time,$3,queryid,$4,query) from (                      +
      368             4011180324870540048       select pg_catalog.concat($1,$2,logical_read,$3,queryid,$4,query) from (                    +

      TOP 5 phyicalread SQL
      =============================================================================================================================
      phyicalread     queryid                   query
      --------------- ------------------------- -----------------------------------------------------------------------------------
      0               7959668902597077560       select pg_catalog.concat($1,$2,backend_type,$3,wait_cnt,$4,sss.query) from +
      0               -4414999297299836195      select b.datname                                  "数据库",               +
      0               2538051077016912011       select a.rolname      "用户",                                              +
      0               -7787808219802061843      select current_database()::char(15) "数据库",                             +
      0               -1085290314581534244      select b.datname                                  "数据库",               +

      TOP 5 tmpfilewriten SQL
      =============================================================================================================================
      tmpfilewriten   queryid                   query
      --------------- ------------------------- -----------------------------------------------------------------------------------
      0               7959668902597077560       select pg_catalog.concat($1,$2,backend_type,$3,wait_cnt,$4,sss.query) from +
      0               -4414999297299836195      select b.datname                                  "数据库",               +
      0               2538051077016912011       select a.rolname      "用户",                                              +
      0               -7787808219802061843      select current_database()::char(15) "数据库",                             +
      0               -1085290314581534244      select b.datname                                  "数据库",               +


chk_top_tableIO.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查询过去10秒，IO 值最大的表。分别统计表的IO，表的索引IO，toast 对象的 IO ，数据取自sys_statio_all_tables 。这里的IO包括缓冲区读取和磁盘物理读取。

表格说明：

- Schename：模式名

- Relname：统计对象名（表、索引、toast）

- xxxIO：对象的IO块数量



显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]# ./chk_top_tableIO.sh
                  now
      -------------------------------
      2025-01-16 14:23:14.502476+08
      (1 行记录)

      TOP 5 tableonlyIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           tableonlyIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                     _rel                                                              310
      pg_catalog                     _att                                                              259
      pg_catalog                     _opclass                                                          151
      pg_catalog                     _stat                                                             95
      pg_catalog                     _op                                                               88

      TOP 5 indexonlyIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           indexonlyIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                     _att                                                              435
      pg_catalog                     _rel                                                              212
      pg_catalog                     _typ                                                              178
      pg_catalog                     _stat                                                             156
      pg_catalog                     _cast                                                             142

      TOP 5 toastonlyIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           toastonlyIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                     _stat                                                             7
      pg_catalog                     _rewrite                                                          6
      pg_catalog                     _tsdict                                                           0
      perf                           kwr_snap_inst_event                                               0
      information_schema             sql_packages                                                      0

      TOP 5 tableallIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           tableallIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                     _att                                                              694
      pg_catalog                     _rel                                                              522
      pg_catalog                     _stat                                                             258
      pg_catalog                     _typ                                                              255
      pg_catalog                     _proc                                                             182


chk_top_tablephysicalIO.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查询过去10秒，物理IO 值最大的表（数量可传参数）。分别统计表的IO，表的索引IO，toast 对象的 IO，数据取自sys_statio_all_tables，与chk_top_table IO不同，这里的IO只包括磁盘物理读取。

表格说明：

- Schename：模式名

- Relname：统计对象名（表、索引、toast、表的合计）

- xxxIO：对象的IO块数量


显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]# ./chk_top_tablephysicalIO.sh
                  now
      -------------------------------
      2025-01-16 14:25:51.554666+08
      (1 行记录)

      TOP 5 tableonlyphysicalIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           tableonlyphysicalIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      public                         tmp_io_stats_11223344                                             7
      pg_catalog                     _disablerole                                                      0
      pg_catalog                     _objschemats                                                      0
      pg_toast                       pg_toast_3381                                                     0
      pg_toast                       pg_toast_12255                                                    0

      TOP 5 indexonlyphysicalIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           indexonlyphysicalIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_toast                       pg_toast_12255                                                    0
      pg_catalog                     _objschemats                                                      0
      pg_toast                       pg_toast_3381                                                     0
      perf                           kwr_snap_sql_all                                                  0
      pg_toast                       pg_toast_12166                                                    0

      TOP 5 toastonlyphysicalIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           toastonlyphysicalIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      perf                           kwr_snap_inst_io                                                  0
      pg_catalog                     _tsdict                                                           0
      information_schema             sql_packages                                                      0
      perf                           kwr_snap_inst_event                                               0
      perf                           kwr_snap_host_io                                                  0

      TOP 5 tableallphysicalIO SQL
      ========================================================================================================================
      schemaname                     relname                                                           tableallphysicalIO
      ------------------------------ ----------------------------------------------------------------- -----------------------
      public                         tmp_io_stats_11223344                                             7
      pg_catalog                     _disablerole                                                      0
      pg_catalog                     _objschemats                                                      0
      pg_toast                       pg_toast_3381                                                     0
      pg_toast                       pg_toast_12255                                                    0


chk_top_tableseqscanIO.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查询过去10秒，全表扫描的IO数量最大的表。全表扫描的IO数量是seq_scan次数与表大小的乘积，数据取自sys_stat_sys_tables。

表格说明：

- Schename：模式名

- Relname：统计对象名（表）

- Seq_scan：扫描对象次数

- xxxIO：对象的IO块数量



显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]# ./chk_top_tableseqscanIO.sh
                  now
      -------------------------------
      2025-01-16 14:31:19.002396+08
      (1 行记录)

      TOP 20 tableseqscanIO SQL
      ==============================================================================================================================
      schemaname                     relname                              seq_scan                             tableseqscanIO
      ------------------------------ ------------------------------------ ------------------------------------ ---------------------
      pg_catalog                     _typ                                 0                                    0
      pg_catalog                     _att                                 0                                    0
      pg_catalog                     _proc                                0                                    0
      pg_catalog                     _rel                                 1                                    0
      pg_catalog                     _authid                              2                                    0
      pg_catalog                     _db                                  0                                    0
      pg_catalog                     _usrmapping                          0                                    0
      pg_catalog                     _am                                  3                                    0
      pg_catalog                     _amproc                              0                                    0
      pg_catalog                     _ind                                 0                                    0
      pg_catalog                     _nsp                                 1                                    0
      pg_catalog                     _opclass                             0                                    0
      pg_catalog                     _rewrite                             0                                    0
      pg_catalog                     _trigger                             0                                    0
      pg_catalog                     _ext                                 0                                    0
      pg_catalog                     _event_trigger                       0                                    0
      pg_catalog                     _ce_mst_key                          1                                    0
      pg_catalog                     _ce_col                              1                                    0
      pg_catalog                     _subscrptrel                         1                                    0
      pg_catalog                     _tabspc                              0                                    0


chk_top_indexIO.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

与 chk_top_tableIO不同，这里只关注哪些索引引发的IO最大，分别统计逻辑IO与物理IO。

表格说明：

- Schename：模式名

- Relname：统计对象名（表）

- Relname：索引名

- indexIO：索引的IO块数量


显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]# ./chk_top_indexIO.sh
                  now
      -------------------------------
      2025-01-16 14:32:53.949728+08
      (1 行记录)

      TOP 5 indexIO SQL
      ==============================================================================================================================
      schemaname                     relname                              indexrelname                         indexIO
      ------------------------------ ------------------------------------ ------------------------------------ ---------------------
      pg_catalog                     _rel                                 _rel_oid_index                       796
      pg_catalog                     _att                                 _att_relid_attnum_index              341
      pg_catalog                     _typ                                 _typ_oid_index                       134
      pg_catalog                     _stat                                _stat_relid_att_inh_index            108
      pg_catalog                     _cast                                _cast_source_target_index            81

      TOP 5 indexphysicalIO SQL
      ==============================================================================================================================
      schemaname                     relname                              indexrelname                         indexphysicalIO
      ------------------------------ ------------------------------------ ------------------------------------ ---------------------
      pg_toast                       pg_toast_2604                        pg_toast_2604_index                  0
      pg_toast                       pg_toast_3456                        pg_toast_3456_index                  0
      pg_toast                       pg_toast_2606                        pg_toast_2606_index                  0
      pg_toast                       pg_toast_826                         pg_toast_826_index                   0
      pg_toast                       pg_toast_2600                        pg_toast_2600_index                  0


chk_top_table_dml.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

查询过去10秒（时间参数，可填），增删改次数最多的表。数据取自sys_stat_user_tables。

表格说明：

- Schename：模式名

- Relname：统计对象名（表）

- insertopeation：增删改次数


显示结果样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@localhost kb_diagtool]$ ./chk_top_table_dml.sh
              now
      -------------------------------
      2025-01-16 11:10:25.174903+08
      (1 行记录)

      NOTICE:  table "tmp_io_stats_11223344" does not exist, skipping

      TOP 5 insertopeation SQL
      ========================================================================================================================
      schemaname                     relname                                                           insertopeation
      ------------------------------ ----------------------------------------------------------------- -----------------------
      public                          tmp_io_stats_11223344                                            253
      pg_catalog                      _att                                                             28
      pg_catalog                      _dep                                                             3
      pg_catalog                      _typ                                                             2
      pg_catalog                      _rel                                                             1

      TOP 5 deleteoperation SQL
      ========================================================================================================================
      schemaname                     relname                                                           deleteoperation
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                      _tsparser                                                        0
      pg_toast                        pg_toast_2620                                                    0
      pg_catalog                      _rel                                                             0
      sys_hm                          hm_run_t                                                         0
      pg_catalog                      _trigger                                                         0

      TOP 5 updateoperation SQL
      ========================================================================================================================
      schemaname                     relname                                                           updateoperation
      ------------------------------ ----------------------------------------------------------------- -----------------------
      pg_catalog                      _tsparser                                                        0
      pg_toast                        pg_toast_2620                                                    0
      pg_catalog                      _rel                                                             0
      sys_hm                          hm_run_t                                                         0
      pg_catalog                      _trigger                                                         0

      TOP 5 tableallDML SQL
      ========================================================================================================================
      schemaname                     relname                                                           tableallDML
      ------------------------------ ----------------------------------------------------------------- -----------------------
      public                          tmp_io_stats_11223344                                            253
      pg_catalog                      _att                                                             28
      pg_catalog                      _dep                                                             3
      pg_catalog                      _typ                                                             2
      pg_catalog                      _rel                                                             1


chk_top_wal.sh
^^^^^^^^^^^^^^^^^^^^^^^^^^^^

功能
~~~~~~~~~~~~~~~

检查当前哪些 SQL引发最多的 WALWrite 或 WALSync 等待事件，通过观察当前的等待事件，可以间接确认哪些SQL引发频繁redo日志写或redo日志刷盘。

表格说明：

- calls：查询次数

- times：等待事件次数

- query：查询语句



显示功能样例
~~~~~~~~~~~~~~~

   .. code::

      [kingbase@kingbase kb_diagtool]# ./chk_top_wal.sh
                  now
      -------------------------------
      2025-01-16 14:35:19.202123+08
      (1 行记录)

      TOP 5 WALWritecount SQL
      ========================================================================================================================
      calls           times           query
      --------------- --------------- ----------------------------------------------------------------------------------------

      TOP 5 WALSynccount SQL
      ========================================================================================================================
      calls           times           query
      --------------- --------------- ----------------------------------------------------------------------------------------
