慢SQL诊断

背景信息

在SQL语句执行性能不符合预期时,可以查看SQL语句执行信息,便于事后分析SQL语句执行时的行为,从而诊断SQL语句执行出现的相关问题。

前提条件

  • 数据库实例运行正常。
  • 查询SQL语句信息,需要正确设置GUC参数track_stmt_stat_level。
  • 只能用系统管理员和监控管理员权限进行操作。
  1. 执行命令查看数据库实例中SQL语句执行信息
  2. select * from dbe_perf.get_global_full_sql_by_timestamp(start_timestamp, end_timestamp);
  3. 例如:
  4. select * from DBE_PERF.get_global_full_sql_by_timestamp('2020-12-01 09:25:22', '2020-12-31 23:54:41');
  5. -[ RECORD 1 ]--------+---------------------------------------------------------------------------------------------------------------
  6. ---------------------------------------------------------------
  7. node_name | dn_6001_6002_6003
  8. db_name | postgres
  9. schema_name | "$user",public
  10. origin_node | 1938253334
  11. user_name | user_dj
  12. application_name | gsql
  13. client_addr |
  14. client_port | -1
  15. unique_query_id | 3671179229
  16. debug_query_id | 72339069014839210
  17. query | select name, setting from pg_settings where name in (?)
  18. start_time | 2020-12-19 16:19:51.216818+08
  19. finish_time | 2020-12-19 16:19:51.224513+08
  20. slow_sql_threshold | 1800000000
  21. transaction_id | 0
  22. thread_id | 139884662093568
  23. session_id | 139884662093568
  24. n_soft_parse | 0
  25. n_hard_parse | 1
  26. query_plan | Datanode Name: dn_6001_6002_6003
  27. | Function Scan on pg_show_all_settings a (cost=0.00..12.50 rows=5 width=64)
  28. | Filter: (name = '***'::text)
  29. ...
  30. 执行命令查看数据库实例中慢SQL语句执行信息
  31. select * from dbe_perf.get_global_slow_sql_by_timestamp(start_timestamp, end_timestamp);
  32. 例如:
  33. select * from DBE_PERF.get_global_slow_sql_by_timestamp('2020-12-01 09:25:22', '2020-12-31 23:54:41');
  34. -[ RECORD 1 ]--------+---------------------------------------------------------------------------------------------------
  35. node_name | dn_6001_6002_6003
  36. db_name | postgres
  37. schema_name | "$user",public
  38. origin_node | 1938253334
  39. user_name | user_dj
  40. application_name | gsql
  41. client_addr |
  42. client_port | -1
  43. unique_query_id | 2165004317
  44. debug_query_id | 72339069014839319
  45. query | select * from DBE_PERF.get_global_slow_sql_by_timestamp(?, ?);
  46. start_time | 2020-12-19 16:23:20.738491+08
  47. finish_time | 2020-12-19 16:23:20.773714+08
  48. slow_sql_threshold | 10000
  49. transaction_id | 0
  50. thread_id | 139884662093568
  51. session_id | 139884662093568
  52. n_soft_parse | 10
  53. n_hard_parse | 8
  54. query_plan | Datanode Name: dn_6001_6002_6003
  55. | Result (cost=1.01..1.02 rows=1 width=0)
  56. | InitPlan 1 (returns $0)
  57. | -> Seq Scan on pgxc_node (cost=0.00..1.01 rows=1 width=64)
  58. | Filter: (nodeis_active AND ((node_type = '***'::"char") OR (node_type = '***'::"char")))
  59. ...
  60. 查看当前主节点SQL语句执行信息
  61. select * from statement_history;
  62. 例如:
  63. select * from statement_history;
  64. -[ RECORD 1 ]--------+---------------------------------------------------------------------------------------------------------------
  65. ---------------------------------------------------------------
  66. db_name | postgres
  67. schema_name | "$user",public
  68. origin_node | 1938253334
  69. user_name | user_dj
  70. application_name | gsql
  71. client_addr |
  72. client_port | -1
  73. unique_query_id | 3671179229
  74. debug_query_id | 72339069014839210
  75. query | select name, setting from pg_settings where name in (?)
  76. start_time | 2020-12-19 16:19:51.216818+08
  77. finish_time | 2020-12-19 16:19:51.224513+08
  78. slow_sql_threshold | 1800000000
  79. transaction_id | 0
  80. thread_id | 139884662093568
  81. session_id | 139884662093568
  82. n_soft_parse | 0
  83. n_hard_parse | 1
  84. query_plan | Datanode Name: dn_6001_6002_6003
  85. | Function Scan on pg_show_all_settings a (cost=0.00..12.50 rows=5 width=64)
  86. | Filter: (name = '***'::text)
  87. ...
  88. 查看当前备节点SQL语句执行信息
  89. select * from dbe_perf.standby_statement_history(is_only_slow, start_timestamp, end_timestamp);
  90. 例如:
  91. select * from dbe_perf.standby_statement_history(true, '2022-08-01 09:25:22', '2022-08-31 23:54:41');
  92. db_name | postgres
  93. schema_name | "$user",public
  94. origin_node | 0
  95. user_name | user_dj
  96. application_name | gsql
  97. client_addr |
  98. client_port | -1
  99. unique_query_id | 1660376009
  100. debug_query_id | 281474976710740
  101. query | select name, setting from pg_settings where name in (?)
  102. start_time | 2022-08-19 16:19:51.216818+08
  103. finish_time | 2022-08-19 16:19:51.224513+08
  104. slow_sql_threshold | 1800000000
  105. transaction_id | 0
  106. thread_id | 140058747205376
  107. session_id | 140058747205376
  108. n_soft_parse | 0
  109. n_hard_parse | 1
  110. query_plan | Datanode Name: sgnode
  111. | Function Scan on pg_show_all_settings a (cost=0.00..12.50 rows=5 width=64)
  112. | Filter: (name = '***'::text)
  113. ...