PostgreSQL码农集散地

PostgreSQL 18 preview - 新增pg_overexplain 插件, 增加EXPLAIN输出细节

PostgreSQL 18 preview - 新增pg_overexplain 插件, 增加EXPLAIN输出细节

pg_overexplain 插件为PostgreSQL的EXPLAIN命令添加了新的调试选项。

解决了以下问题:

  1. PostgreSQL的计划器(planner)生成的Plan和PlanState树中包含大量信息,但现有的EXPLAIN选项无法显示所有这些信息
  1. 目前开发者在调试计划器时,只能依赖debug_print_plan等工具,但这些工具输出的信息过于冗长

新增功能

  1. EXPLAIN (DEBUG) - 提供更详细的调试信息
  1. EXPLAIN (RANGE_TABLE) - 显示范围表(range table)信息

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=8d5ceb113e3f7ddb627bd40b26438a9d2fa05512

pg_overexplain: Additional EXPLAIN options for debugging.  

author  Robert Haas <[email protected]>    
Wed, 26 Mar 2025 17:52:21 +0000 (13:52 -0400)  
committer   Robert Haas <[email protected]>    
Wed, 26 Mar 2025 17:52:21 +0000 (13:52 -0400)  
commit  8d5ceb113e3f7ddb627bd40b26438a9d2fa05512  
tree    6f837fcfedfc32b5aa0ff72a2ad174a09b782289    tree  
parent  818245506c285e0325141dabb1ced45057937502    commit | diff  
pg_overexplain: Additional EXPLAIN options for debugging.  

There's a fair amount of information in the Plan and PlanState trees  
that isn'
t printed by any existing EXPLAIN option. This means that,  
when working on the planner, it's often necessary to rely on facilities  
such as debug_print_plan, which produce excessively voluminous  
output. Hence, use the new EXPLAIN extension facilities to implement  
EXPLAIN (DEBUG) and EXPLAIN (RANGE_TABLE) as extensions to the core  
EXPLAIN facility.  

A great deal more could be done here, and the specific choices about  
what to print and how are definitely arguable, but this is at least  
a starting point for discussion and a jumping-off point for possible  
future improvements.  

Reviewed-by: Sami Imseih <[email protected]>  
Reviewed-by: Tom Lane <[email protected]>  
Reviweed-by: Andrei Lepikhov <[email protected]> (who didn'
t like it)  
Discussion: http://postgr.es/m/CA+TgmoZfvQUBWQ2P8iO30jywhfEAKyNzMZSR+uc2xr9PZBw6eQ@mail.gmail.com  

AI 解读

解读 pg_overexplain 插件

这个补丁名为"pg_overexplain: Additional EXPLAIN options for debugging",它为PostgreSQL的EXPLAIN命令添加了新的调试选项。

主要目的

补丁解决了以下问题:

  1. PostgreSQL的计划器(planner)生成的Plan和PlanState树中包含大量信息,但现有的EXPLAIN选项无法显示所有这些信息
  2. 目前开发者在调试计划器时,只能依赖debug_print_plan等工具,但这些工具输出的信息过于冗长

新增功能

补丁通过新的EXPLAIN扩展机制实现了两个新选项:

  1. EXPLAIN (DEBUG) - 提供更详细的调试信息
  2. EXPLAIN (RANGE_TABLE) - 显示范围表(range table)信息

设计考虑

补丁作者指出:

  • 这只是个起点,未来可以扩展更多功能
  • 关于显示哪些信息以及如何显示的具体选择是可以讨论的
  • 补丁为未来的改进提供了基础

评审情况

补丁经过了多位开发者的评审:

  • Sami Imseih和Tom Lane给予了正面评价
  • Andrei Lepikhov表示不喜欢这个补丁(注意"Reviweed-by"拼写错误)

讨论链接

补丁的详细讨论可以在邮件列表中找到:邮件讨论

这个补丁为PostgreSQL开发者提供了更灵活的调试工具,有助于更深入地分析查询计划问题,同时避免了现有调试工具输出过于冗长的问题。

解读 PostgreSQL pg_overexplain 扩展文档

https://www.postgresql.org/docs/devel/pgoverexplain.html

这个网页是 PostgreSQL 文档中关于 pg_overexplain 扩展的说明,这是一个为 EXPLAIN 命令提供额外调试选项的扩展模块。

主要功能

pg_overexplain 扩展为 PostgreSQL 的 EXPLAIN 命令添加了新的选项,主要用于调试查询计划:

  1. DEBUG 选项:

  • 语法:EXPLAIN (DEBUG) query
  • 功能:显示查询计划树(Plan tree)和计划状态树(PlanState tree)的详细内部信息
  • 输出包括:计划节点类型、目标列表、限定条件、初始化计划和平行计划等
  • RANGE_TABLE 选项:

    • 语法:EXPLAIN (RANGE_TABLE) query
    • 功能:显示查询的范围表(range table)信息
    • 输出包括:范围表条目、表名、别名、权限信息等

    安装和使用

    1. 安装扩展:

      CREATE EXTENSION pg_overexplain;  
    2. 使用示例:

      EXPLAIN (DEBUG) SELECT * FROM table1 WHEREid = 1;  
      EXPLAIN (RANGE_TABLE) SELECT * FROM table1 JOIN table2 ON table1.id = table2.id;  

    设计目的

    该扩展旨在:

    • 提供比标准 EXPLAIN 更详细的查询计划信息
    • 替代 debug_print_plan 等调试工具,提供更结构化的输出
    • 帮助开发者诊断复杂的查询计划问题

    注意事项

    1. 这些选项主要用于调试目的,输出格式可能随版本变化
    2. DEBUG 选项的输出可能非常详细,适合在复杂查询调试时使用
    3. 该扩展是核心 PostgreSQL 的一部分,但可能不是所有版本都包含

    这个扩展特别适合 PostgreSQL 开发者、数据库管理员和高级用户,用于深入分析查询执行计划问题。