PostgreSQL 13新特性:explain新增wal選項

PostgreSQL13中,我們通過explain命令查看執行計劃時,新增了一個wal選項,允許我們來跟蹤wal日誌使用相關的統計信息。

語法:

EXPLAIN [ ( option [, ...] ) ] statement
EXPLAIN [ ANALYZE ] [ VERBOSE ] statement

where option can be one of:

    ANALYZE [ boolean ]
    VERBOSE [ boolean ]
    COSTS [ boolean ]
    SETTINGS [ boolean ]
    BUFFERS [ boolean ]
    WAL [ boolean ]
    TIMING [ boolean ]
    SUMMARY [ boolean ]
    FORMAT { TEXT | XML | JSON | YAML }

對於WAL選項的解釋如下:
Include information on WAL record generation. Specifically, include the number of records, number of full page images (fpi) and amount of WAL bytes generated. In text format, only non-zero values are printed. This parameter may only be used when ANALYZE is also enabled. It defaults to FALSE.

需要注意的兩點是:
1、wal選項默認是關閉的,必須配合analyze選項一起使用;
2、如果使用text格式,只會打印非0的wal信息。

例子:
–如果沒有wal信息則在text格式下不顯示:

bill=# explain(analyze,wal) select * from t2 where id > 10 offset 10 limit 10;
                                              QUERY PLAN                                              
------------------------------------------------------------------------------------------------------
 Limit  (cost=0.61..1.22 rows=10 width=36) (actual time=0.008..0.008 rows=0 loops=1)
   ->  Seq Scan on t2  (cost=0.00..25.88 rows=423 width=36) (actual time=0.004..0.005 rows=0 loops=1)
         Filter: (id > 10)
 Planning Time: 0.062 ms
 Execution Time: 0.025 ms
(5 rows)

–改成json格式可以看到相關的信息爲0

bill=# explain(analyze,wal,format json) select * from t2 where id > 10 offset 10 limit 10; 
                QUERY PLAN                 
-------------------------------------------
 [                                        +
   {                                      +
     "Plan": {                            +
       "Node Type": "Limit",              +
       "Parallel Aware": false,           +
       "Startup Cost": 0.47,              +
       "Total Cost": 0.94,                +
       "Plan Rows": 10,                   +
       "Plan Width": 37,                  +
       "Actual Startup Time": 0.026,      +
       "Actual Total Time": 0.028,        +
       "Actual Rows": 10,                 +
       "Actual Loops": 1,                 +
       "WAL Records": 0,                  +
       "WAL FPI": 0,                      +
       "WAL Bytes": 0,                    +
       "Plans": [                         +
         {                                +
           "Node Type": "Seq Scan",       +
           "Parent Relationship": "Outer",+
           "Parallel Aware": false,       +
           "Relation Name": "t2",         +
           "Alias": "t2",                 +
           "Startup Cost": 0.00,          +
           "Total Cost": 47150.93,        +
           "Plan Rows": 998934,           +
           "Plan Width": 37,              +
           "Actual Startup Time": 0.023,  +
           "Actual Total Time": 0.025,    +
           "Actual Rows": 20,             +
           "Actual Loops": 1,             +
           "Filter": "(id > 10)",         +
           "Rows Removed by Filter": 10,  +
           "WAL Records": 0,              +
           "WAL FPI": 0,                  +
           "WAL Bytes": 0                 +
         }                                +
       ]                                  +
     },                                   +
     "Planning": {                        +
       "Planning Time": 0.066             +
     },                                   +
     "Triggers": [                        +
     ],                                   +
     "Execution Time": 0.053              +
   }                                      +
 ]
(1 row)

–有wal日誌信息的情況:

bill=*# explain (analyze ,wal) update t2 set info = 'bill' where id > 10000;
                                                    QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------
 Update on t2  (cost=0.00..47150.93 rows=989595 width=42) (actual time=2204.054..2204.054 rows=0 loops=1)
   WAL: records=1979935 fpi=5781 bytes=142081361
   ->  Seq Scan on t2  (cost=0.00..47150.93 rows=989595 width=42) (actual time=0.034..325.876 rows=990000 loops=1)
         Filter: (id > 10000)
         Rows Removed by Filter: 10000
         WAL: records=1 bytes=112
 Planning Time: 0.066 ms
 Execution Time: 2204.091 ms
(8 rows)

除了explain之外,在PostgreSQL13中,還新增了auto_explain, autovacuum和pg_stat_statements對wal日誌信息的記錄。

參考鏈接:
https://www.postgresql.org/docs/13/sql-explain.html
https://www.postgresql.org/docs/13/auto-explain.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章