PostgreSQL码农集散地

PostgreSQL 18 preview - 新增 EXPLAIN 扩展信息钩子

PostgreSQL 18 preview - 新增 EXPLAIN 扩展信息钩子

这两个补丁为 PostgreSQL 的 EXPLAIN 命令增加了强大的扩展性,允许开发者创建自定义的扩展,以提供更详细和更有用的查询计划信息。

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

Make it possible for loadable modules to add EXPLAIN options.  
author Robert Haas <[email protected]>   
Tue, 18 Mar 2025 12:41:12 +0000 (08:41 -0400)  
committer Robert Haas <[email protected]>   
Tue, 18 Mar 2025 12:41:12 +0000 (08:41 -0400)  
commit c65bc2e1d14a2d4daed7c1921ac518f2c5ac3d17  
tree ffb12e651e2d3603d3f11aef0449d90bd82ac087 tree  
parent 9d6db8bec19413cd0167f1e59d1af005a997bd3e commit | diff  
Make it possible for loadable modules to add EXPLAIN options.  

Modules can use RegisterExtensionExplainOption to register new  
EXPLAIN options, and GetExplainExtensionId, GetExplainExtensionState,  
and SetExplainExtensionState to store related state inside the  
ExplainState object.  

Since this substantially increases the amount of code that needs  
to handle ExplainState-related tasks, move a few bits of existing  
code to a new file explain_state.c and add the rest of this  
infrastructure there.  

See the comments at the top of explain_state.c for further  
explanation of how this mechanism works.  

This does not yet provide a way for such such options to do anything  
useful. The intention is that we'll add hooks for that purpose in a  
separate commit.  

Discussion: http://postgr.es/m/CA+TgmoYSzg58hPuBmei46o8D3SKX+SZoO4K_aGQGwiRzvRApLg@mail.gmail.com  
Reviewed-by: Srinath Reddy <[email protected]>  
Reviewed-by: Andrei Lepikhov <[email protected]>  
Reviewed-by: Tom Lane <[email protected]>  
Reviewed-by: Sami Imseih <[email protected]>  

https://git.postgresql.org/gitweb/?p=postgresql.git;a=commit;h=4fd02bf7cf94c3b6807dcf0b13e076de94f1e4ff

Add some new hooks so extensions can add details to EXPLAIN.  
author Robert Haas <[email protected]>   
Tue, 18 Mar 2025 13:09:34 +0000 (09:09 -0400)  
committer Robert Haas <[email protected]>   
Tue, 18 Mar 2025 13:28:01 +0000 (09:28 -0400)  
commit 4fd02bf7cf94c3b6807dcf0b13e076de94f1e4ff  
tree 56c34e32e6b83c07fc41fa11cc3a08ca403f791e tree  
parent f76892c9ff7e5f2dcb4073310d1a5273f47d1d9d commit | diff  
Add some new hooks so extensions can add details to EXPLAIN.  

Specifically, add a per-node hook that is called after the per-node  
information has been displayed but before we display children, and a  
per-query hook that is called after existing query-level information  
is printed. This assumes that extension-added information should  
always go at the end rather than the beginning or the middle, but  
that seems like an acceptable limitation for simplicity. It also  
assumes that extensions will only want to add information, not remove  
or reformat existing details; those also seem like acceptable  
restrictions, at least for now.  

If multiple EXPLAIN extensions are used, the order inwhich any  
additional details are printed is likely to depend on the order in
which the modules are loaded. That seems OK, since the user may  
have opinions about the order inwhich output should appear, and the  
extension author can't really know whether their stuff is more or  
less important to a particular user than some other extension.  

Discussion: http://postgr.es/m/CA+TgmoYSzg58hPuBmei46o8D3SKX+SZoO4K_aGQGwiRzvRApLg@mail.gmail.com  
Reviewed-by: Srinath Reddy <[email protected]>  
Reviewed-by: Andrei Lepikhov <[email protected]>  
Reviewed-by: Tom Lane <[email protected]>  
Reviewed-by: Sami Imseih <[email protected]>  

AI 解读

这两个补丁共同为 PostgreSQL 增加了允许可加载模块(扩展)向 EXPLAIN 命令添加自定义选项和信息的机制。

补丁 1:Make it possible for loadable modules to add EXPLAIN options. (允许可加载模块添加 EXPLAIN 选项)

  • 核心功能:  这个补丁引入了允许扩展注册新的 EXPLAIN 选项的基础设施。
  • 具体实现:
    • RegisterExtensionExplainOption 函数:用于注册新的 EXPLAIN 选项。
    • GetExplainExtensionId、GetExplainExtensionState、SetExplainExtensionState 函数:用于在 ExplainState 对象中存储与扩展相关的状态信息。 ExplainState 对象是 EXPLAIN 命令执行期间用于存储各种信息的结构。
    • 将与 ExplainState 相关的代码移动到新的文件 explain_state.c 中,以提高代码组织性和可维护性。
  • 重要说明:  这个补丁只提供了注册选项的基础设施,但没有提供让这些选项实际执行任何操作的机制。  后续的补丁会添加相应的钩子函数。

补丁 2:Add some new hooks so extensions can add details to EXPLAIN. (添加一些新的钩子函数,以便扩展可以向 EXPLAIN 添加详细信息)

  • 核心功能:  这个补丁添加了钩子函数,允许扩展在 EXPLAIN 命令的输出中添加自定义信息。
  • 具体实现:
    • per-node hook (节点级别钩子):  在每个节点的详细信息显示之后,但在显示子节点之前调用。  允许扩展在每个查询计划节点的输出中添加信息。
    • per-query hook (查询级别钩子):  在现有的查询级别信息打印之后调用。  允许扩展在整个查询计划的输出中添加信息。
  • 设计考虑:
    • 扩展添加的信息总是添加到输出的末尾,而不是开头或中间。  这简化了实现,并且对于大多数用例来说是可以接受的限制。
    • 扩展只能添加信息,不能删除或重新格式化现有的信息。  这也是为了简化实现而做出的限制。
    • 如果使用了多个 EXPLAIN 扩展,它们添加的信息的顺序取决于模块的加载顺序。  这允许用户控制输出的顺序,并且扩展作者无法确定他们的信息比其他扩展的信息更重要。

总结:

这两个补丁共同为 PostgreSQL 增加了强大的扩展性,允许可加载模块通过 EXPLAIN 命令提供更详细、更定制化的查询计划信息。  第一个补丁建立了注册 EXPLAIN 选项的基础设施,而第二个补丁则添加了钩子函数,允许扩展实际向输出中添加信息。  这种设计允许扩展以一种可控和一致的方式增强 EXPLAIN 命令的功能。

关键术语解释:

  • EXPLAIN:  PostgreSQL 的一个命令,用于显示查询的执行计划。
  • 可加载模块 (Loadable Module) / 扩展 (Extension):  可以动态加载到 PostgreSQL 服务器中的代码,用于扩展其功能。
  • 钩子函数 (Hook Function):  在特定事件发生时调用的函数,允许扩展修改或增强 PostgreSQL 的行为。
  • 查询计划 (Query Plan):  PostgreSQL 执行查询的步骤的描述。
  • 节点 (Node):  查询计划中的一个步骤。
  • ExplainState:  一个用于存储 EXPLAIN 命令执行期间各种信息的结构。

总而言之,这两个补丁为 PostgreSQL 的 EXPLAIN 命令增加了强大的扩展性,允许开发者创建自定义的扩展,以提供更详细和更有用的查询计划信息。