选择 打开 改范围 完整检索页

pgsql.cc 提供对 postgresql.org 官网内容的中文翻译,由 Pigsty 团队维护。

受支持版本: 当前版本 (18) / 17 / 16 / 15 / 14
测试与开发版本: 19 / devel
不受支持的版本: 13 / 12 / 11 / 10 / 9.6 / 9.5 / 9.4 / 9.3 / 9.2 / 9.1 / 9.0
历史版本PostgreSQL 9.1 已于 2016 年 10 月结束社区维护,本页译文保留供仍在使用旧版本的读者参考。新系统请看当前版本

F.3. auto_explain #

auto_explain模块提供了一种自动记录慢语句执行计划的方法,而无须手工运行EXPLAIN。这对于在大型应用中追查未优化的查询尤其有帮助。

该模块不提供可从 SQL 访问的函数。要使用它,只需把它加载到服务器中。 你可以把它加载到单个会话中:

LOAD 'auto_explain';

(要这样做,必须是超级用户。)更典型的用法是,在 postgresql.confshared_preload_libraries 中包含 auto_explain,从而把它预加载到所有会话中。这样,无论 查询何时意外变慢,都可以追踪到它们。当然,这要付出额外开销。

F.3.1. 配置参数

有若干配置参数可控制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_buffers (boolean)

auto_explain.log_buffers 使得在记录执行计划时 输出EXPLAIN (ANALYZE, BUFFERS),而不只是输出EXPLAIN。 该参数默认关闭。只有超级用户可以更改此设置。除非设置了 auto_explain.log_analyze,否则该参数没有效果。

auto_explain.log_format (enum)

auto_explain.log_format选择要使用的EXPLAIN输出格式。允许的值有textxmljsonyaml。默认值为 text。只有超级用户可以更改此设置。

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'

F.3.2. 示例

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: 3.651 ms  plan:
  Query Text: SELECT count(*)
              FROM pg_class, pg_index
              WHERE oid = indrelid AND indisunique;
  Aggregate  (cost=16.79..16.80 rows=1 width=0) (actual time=3.626..3.627 rows=1 loops=1)
    ->  Hash Join  (cost=4.17..16.55 rows=92 width=0) (actual time=3.349..3.594 rows=92 loops=1)
          Hash Cond: (pg_class.oid = pg_index.indrelid)
          ->  Seq Scan on pg_class  (cost=0.00..9.55 rows=255 width=4) (actual time=0.016..0.140 rows=255 loops=1)
          ->  Hash  (cost=3.02..3.02 rows=92 width=4) (actual time=3.238..3.238 rows=92 loops=1)
                Buckets: 1024  Batches: 1  Memory Usage: 4kB
                ->  Seq Scan on pg_index  (cost=0.00..3.02 rows=92 width=4) (actual time=0.008..3.187 rows=92 loops=1)
                      Filter: indisunique

F.3.3. 作者

Takahiro Itagaki

提交更正

译文有误、术语不当或页面显示问题,请到译文仓库 pgsty/pgdoc 报告译文问题。 英文原文本身的问题,请在当前版本的对应页面向上游反馈;上游不再修订已结束维护的版本。