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