pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。
auto_explain模块提供了一种自动记录慢语句执行计划的方法,而无须手工运行EXPLAIN。这对于在大型应用中追查未优化的查询尤其有帮助。
该模块不提供可从 SQL 访问的函数。要使用它,只需把它加载到服务器中。 你可以把它加载到单个会话中:
LOAD 'auto_explain';
(要这样做,必须是超级用户。)更典型的用法是,在 postgresql.conf 的 shared_preload_libraries 中包含 auto_explain,从而把它预加载到所有会话中。这样,无论 查询何时意外变慢,都可以追踪到它们。当然,这要付出额外开销。
有若干配置参数可控制auto_explain的行为。注意,其默认行为是什么都不做,因此如果希望得到任何结果,至少必须设置auto_explain.log_min_duration。
auto_explain.log_min_duration (integer)auto_explain.log_min_duration是会导致记录该语句计划的最小语句执行时间,单位为毫秒。将其设为 0 会记录所有计划。-1(默认值)会禁用计划记录。例如,如果将其设为250ms,则所有运行 250ms 或更久的语句都会被记录。只有超级用户可以更改此设置。
auto_explain.log_analyze (boolean)auto_explain.log_analyze使得在记录执行计划时输出EXPLAIN ANALYZE,而不只是输出EXPLAIN。该参数默认关闭。只有超级用户可以更改此设置。
当该参数开启时,所有已执行语句都会对每个计划节点计时,无论它们 是否实际运行得足够久而最终被记录。这可能对性能造成极其负面的 影响。
auto_explain.log_verbose (boolean)auto_explain.log_verbose 使得在记录执行计划时 输出EXPLAIN VERBOSE,而不只是输出EXPLAIN。 该参数默认关闭。只有超级用户可以更改此设置。
auto_explain.log_nested_statements (boolean)auto_explain.log_nested_statements使嵌套语句(在函数内部执行的语句)也会被纳入记录考虑范围。关闭该参数时,只记录顶层查询计划。该参数默认关闭。只有超级用户可以更改此设置。
为了能在 postgresql.conf 文件中设置这些参数,你需要把 auto_explain 加入 custom_variable_classes。典型用法如下:
# postgresql.conf shared_preload_libraries = 'auto_explain' custom_variable_classes = 'auto_explain' auto_explain.log_min_duration = '3s'
postgres=# LOAD 'auto_explain';
postgres=# SET auto_explain.log_min_duration = 0;
postgres=# SELECT count(*)
FROM pg_class, pg_index
WHERE oid = indrelid AND indisunique;
这可能产生如下日志输出:
LOG: duration: 0.986 ms plan:
Aggregate (cost=14.90..14.91 rows=1 width=0)
-> Hash Join (cost=3.91..14.70 rows=81 width=0)
Hash Cond: (pg_class.oid = pg_index.indrelid)
-> Seq Scan on pg_class (cost=0.00..8.27 rows=227 width=4)
-> Hash (cost=2.90..2.90 rows=81 width=4)
-> Seq Scan on pg_index (cost=0.00..2.90 rows=81 width=4)
Filter: indisunique
STATEMENT: SELECT count(*)
FROM pg_class, pg_index
WHERE oid = indrelid AND indisunique;
Takahiro Itagaki <itagaki.takahiro@oss.ntt.co.jp>
译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。