当前位置:   article > 正文

PostgreSQL日志中的SQL记录时机 —— log_statement 和 log_min_duration_statement

log_min_duration_statement

       最近跟朋友讨论到PostgreSQL日志中的SQL记录时机,研究了下log_statement 和 log_min_duration_statement两个参数,记录一下。

一、 参数简介

1. log_statement

① 作用

控制记录SQL的类型,可选值为:

  • none:关闭(默认)
  • ddl:DDL语句
  • mod:DDL和所有涉及数据修改的语句(DML、COPY FROM、PREPARE、EXECUTE等)。对于explain和explain analyze,如果后面的语句类型符合,也会被记录
  • all:所有语句

② 记录时机

       SQL语句解析成功后,执行前。因此即使设置为all,也不会记录有语法错误的语句(如果想记录,应该使用log_min_error_statement参数)。

③ 记录内容

       记录SQL语句,包含参数,但不包含执行用户、主机名等信息,这些需要审计插件才有。

2. log_min_duration_statement

① 作用

记录超过指定执行时间阈值的SQL,可选值为:

  • -1:关闭(默认)
  • 0:所有语句
  • 正数:慢SQL阈值

② 记录时机

       SQL语句执行完成后,因此能记录到执行时间。

③ 记录内容

  • 记录SQL语句、执行时间,但不包含参数。
  • 对于使用扩展查询协议的客户端,对语法分析、绑定、执行每一步所花时间会独立记录。

3. 同时符合两者的SQL会如何?

  • 语句在解析完成后、执行开始前,即被记入日志(log_statement生效)
  • 语句执行完成后,单独将duration记入日志(log_min_duration_statement生效),但不再重复记录语句
  • 因此建议使用log_line_prefix记录PID或会话ID,避免duration和语句关联不上

二、 效果测试

1. 两者均设置

log_statement='all',log_min_duration_statement='0s'

2023-11-28 17:51:45.222 CST [2484] LOG:  statement: select pg_sleep(10);
2023-11-28 17:51:55.227 CST [2484] LOG:  duration: 10005.249 ms

       语句记录为开始时间(log_statement生效),duration在执行完成时单独记录(log_min_duration_statement生效),但不再重复记录语句。

2. 仅设置log_statement

log_statement='all',log_min_duration_statement='-1'(禁用)

2023-11-28 17:53:49.540 CST [2760] LOG:  statement: select pg_sleep(10);

仅记录语句,记录时间为开始时间,没有duration

3. 仅设置log_min_duration_statement

log_statement='none'(禁用),log_min_duration_statement='0s'

2023-11-28 17:55:20.288 CST [2826] LOG:  duration: 15015.447 ms  statement: select pg_sleep(15);

记录语句和duration,记录时间为结束时间

三、 参数记录时机

         从上面文档可以知道,两个参数记录时机都在SQL执行阶段,只是一个在前一个在后。源码中SQL执行相关的函数主要是exec_simple_query,因此我们就看看这个函数。

         函数刚开头就可以看到 pg_parse_query 和 check_log_statement函数。

  1. /*
  2. * exec_simple_query
  3. *
  4. * Execute a "simple Query" protocol message.
  5. */
  6. static void
  7. exec_simple_query(const char *query_string)
  8. {
  9. CommandDest dest = whereToSendOutput;
  10. MemoryContext oldcontext;
  11. List *parsetree_list;
  12. ListCell *parsetree_item;
  13. bool save_log_statement_stats = log_statement_stats;
  14. bool was_logged = false;
  15. bool use_implicit_block;
  16. char msec_str[32];
  17. ...
  18. /*
  19. * Do basic parsing of the query or queries (this should be safe even if
  20. * we are in aborted transaction state!)
  21. */
  22. parsetree_list = pg_parse_query(query_string);
  23. /* Log immediately if dictated by log_statement */
  24. if (check_log_statement(parsetree_list))
  25. {
  26. ereport(LOG,
  27. (errmsg("statement: %s", query_string),
  28. errhidestmt(true),
  29. errdetail_execute(parsetree_list)));
  30. was_logged = true;
  31. }
  • pg_parse_query函数用于SQL解析,符合log_statement记录的语句发生在解析完成后
  • check_log_statement函数就用于检查log_statement的设置,标记是否需要记录
  • was_logged=true表示已记录SQL语句,主要是给后面的慢SQL记录函数,提示其不需重复记录SQL文本
  1. /*
  2. * check_log_statement
  3. * Determine whether command should be logged because of log_statement
  4. *
  5. * stmt_list can be either raw grammar output or a list of planned
  6. * statements
  7. */
  8. static bool
  9. check_log_statement(List *stmt_list)
  10. {
  11. ListCell *stmt_item;
  12. if (log_statement == LOGSTMT_NONE)
  13. return false;
  14. if (log_statement == LOGSTMT_ALL)
  15. return true;
  16. /* Else we have to inspect the statement(s) to see whether to log */
  17. foreach(stmt_item, stmt_list)
  18. {
  19. Node *stmt = (Node *) lfirst(stmt_item);
  20. if (GetCommandLogLevel(stmt) <= log_statement)
  21. return true;
  22. }
  23. return false;
  24. }

       

       后面一大堆是SQL执行相关的代码,非本次重点,直接拉到函数末尾,可以看到check_log_duration函数,看名字都能猜到是它了,也符合在SQL执行完才记录。

  1. ...
  2. /*
  3. * Emit duration logging if appropriate.
  4. */
  5. switch (check_log_duration(msec_str, was_logged))
  6. {
  7. case 1:
  8. ereport(LOG,
  9. (errmsg("duration: %s ms", msec_str),
  10. errhidestmt(true)));
  11. break;
  12. case 2:
  13. ereport(LOG,
  14. (errmsg("duration: %s ms statement: %s",
  15. msec_str, query_string),
  16. errhidestmt(true),
  17. errdetail_execute(parsetree_list)));
  18. break;
  19. }
  20. if (save_log_statement_stats)
  21. ShowUsage("QUERY STATISTICS");
  22. TRACE_POSTGRESQL_QUERY_DONE(query_string);
  23. debug_query_string = NULL;
  24. }

check_log_duration函数

         可以看到注释中给出了上面case 1,2的含义:

  • 1:仅记录duration,即前面提到的两个参数均生效的场景
  • 2:记录duration和语句:即前面提到的仅log_min_duration_statement生效的场景

另外是否记录慢sql与 log_duration、log_min_duration_sample 这些参数也有关。

  1. /*
  2. * check_log_duration
  3. * Determine whether current command's duration should be logged
  4. * We also check if this statement in this transaction must be logged
  5. * (regardless of its duration).
  6. *
  7. * Returns:
  8. * 0 if no logging is needed
  9. * 1 if just the duration should be logged
  10. * 2 if duration and query details should be logged
  11. *
  12. * If logging is needed, the duration in msec is formatted into msec_str[],
  13. * which must be a 32-byte buffer.
  14. *
  15. * was_logged should be true if caller already logged query details (this
  16. * essentially prevents 2 from being returned).
  17. */
  18. int
  19. check_log_duration(char *msec_str, bool was_logged)
  20. {
  21. if (log_duration || log_min_duration_sample >= 0 ||
  22. log_min_duration_statement >= 0 || xact_is_sampled)
  23. {
  24. long secs;
  25. int usecs;
  26. int msecs;
  27. bool exceeded_duration;
  28. bool exceeded_sample_duration;
  29. bool in_sample = false;
  30. TimestampDifference(GetCurrentStatementStartTimestamp(),
  31. GetCurrentTimestamp(),
  32. &secs, &usecs);
  33. msecs = usecs / 1000;
  34. /*
  35. * This odd-looking test for log_min_duration_* being exceeded is
  36. * designed to avoid integer overflow with very long durations: don't
  37. * compute secs * 1000 until we've verified it will fit in int.
  38. */
  39. exceeded_duration = (log_min_duration_statement == 0 ||
  40. (log_min_duration_statement > 0 &&
  41. (secs > log_min_duration_statement / 1000 ||
  42. secs * 1000 + msecs >= log_min_duration_statement)));
  43. exceeded_sample_duration = (log_min_duration_sample == 0 ||
  44. (log_min_duration_sample > 0 &&
  45. (secs > log_min_duration_sample / 1000 ||
  46. secs * 1000 + msecs >= log_min_duration_sample)));
  47. /*
  48. * Do not log if log_statement_sample_rate = 0. Log a sample if
  49. * log_statement_sample_rate <= 1 and avoid unnecessary random() call
  50. * if log_statement_sample_rate = 1.
  51. */
  52. if (exceeded_sample_duration)
  53. in_sample = log_statement_sample_rate != 0 &&
  54. (log_statement_sample_rate == 1 ||
  55. random() <= log_statement_sample_rate * MAX_RANDOM_VALUE);
  56. if (exceeded_duration || in_sample || log_duration || xact_is_sampled)
  57. {
  58. snprintf(msec_str, 32, "%ld.%03d",
  59. secs * 1000 + msecs, usecs % 1000);
  60. if ((exceeded_duration || in_sample || xact_is_sampled) && !was_logged)
  61. return 2;
  62. else
  63. return 1;
  64. }
  65. }
  66. return 0;
  67. }

参考:

https://www.postgresql.org/docs/current/runtime-config-logging.html#GUC-LOG-STATEMENT
https://www.postgresql.org/docs/current/protocol-flow.html#PROTOCOL-FLOW-EXT-QUERY
https://blog.csdn.net/qq_35423190/article/details/129138740
https://iwmj.wordpress.com/2018/04/03/postgresql-%E5%89%8D%E5%90%8E%E7%AB%AF%E5%8D%8F%E8%AE%AE%E4%B8%AD%E7%9A%84%E6%9F%A5%E8%AF%A2%E6%96%B9%E5%BC%8F%EF%BC%9A%E7%AE%80%E5%8D%95%E6%9F%A5%E8%AF%A2%E3%80%81%E6%89%A9%E5%B1%95%E6%9F%A5%E8%AF%A2/

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/135425
推荐阅读
相关标签
  

闽ICP备14008679号