Skip to content

Latest commit

 

History

History
168 lines (56 loc) · 4.89 KB

20180325_04.md

File metadata and controls

168 lines (56 loc) · 4.89 KB

PostgreSQL 11 preview - pg_stat_statements 增强,支持执行计划翻转记录 - pg_store_plans extension

作者

digoal

日期

2018-03-25

标签

PostgreSQL , pg_stat_statements , 执行计划 , 翻转 , pg_stat_plans


背景

pg_stat_statements是PG的一个用于收集SQL统计信息的插件,可以帮助我们分析TOP SQL。

《PostgreSQL 如何查找TOP SQL (例如IO消耗最高的SQL)》

另一方面,我们可能也想知道SQL曾经执行时是否出现过较差的执行计划,使用较差的执行计划执行了多少次等信息。

通过pg_stat_plans这个插件可以记录SQL的执行计划变化,不同执行计划下SQL的平均执行时间,执行次数等。

例如,下面是某个SQL的统计信息,一共使用了两种PLAN,调用次数和平均执行时间都被记录了下来。

plan_ids            | {2758236521,1252991393}  
calls_histogram     | {1,5}  
avg_time_histogram  | {21.224,0.016}  

执行计划如下

postgres=# select planid,query,query_valid,pg_stat_plans_explain(planid, userid, dbid) from pg_stat_plans where dbid=12788 and query ~ 'test';    
-[ RECORD 1 ]---------+-------------------------------------------------------------------  
planid                | 2758236521    
query                 | select * from test where id=1;    
query_valid           | f    
pg_stat_plans_explain | ***** Existing entry's planid (2758236521) and explain of original SQL query string planid (1252991393) differ *****    
                      | Index Only Scan using idx_test on test  (cost=0.00..2.30 rows=1 width=4)    
                      |   Index Cond: (id = 1)    
-[ RECORD 5 ]---------+-----------------------------------------------------------------------------------------------------  
planid                | 1252991393    
query                 | select * from test where id=1;    
query_valid           | t    
pg_stat_plans_explain | Index Only Scan using idx_test on test  (cost=0.00..2.30 rows=1 width=4)    
                      |   Index Cond: (id = 1)    

PostgreSQL 11,将要把这个功能整合到pg_stat_statements中。patch 如下。

https://www.postgresql.org/message-id/flat/9e43fd8f-4d35-4b9d-545c-f9011cd4aa5d@uni-muenster.de#9e43fd8f-4d35-4b9d-545c-f9011cd4aa5d@uni-muenster.de

https://commitfest.postgresql.org/17/1470/

参考

https://www.postgresql.org/message-id/flat/9e43fd8f-4d35-4b9d-545c-f9011cd4aa5d@uni-muenster.de#9e43fd8f-4d35-4b9d-545c-f9011cd4aa5d@uni-muenster.de

https://commitfest.postgresql.org/17/1470/

custom extension pg_stat_plan

https://github.com/2ndQuadrant/pg_stat_plans

custom extension pg_store_plans

https://github.com/ossc-db/pg_store_plans

《执行计划维度统计\判断执行计划翻转\统计每种执行计划的统计信息 use pg_stat_plans in PostgreSQL 9.0, 9.1 and 9.2》

您的愿望将传达给PG kernel hacker、数据库厂商等, 帮助提高数据库产品质量和功能, 说不定下一个PG版本就有您提出的功能点. 针对非常好的提议,奖励限量版PG文化衫、纪念品、贴纸、PG热门书籍等,奖品丰富,快来许愿。开不开森.

digoal's wechat