Postgres 中的查询读取了多少磁盘页面?

How many disk pages are read by a query in Postgres?

我想知道当 运行 单个 Postgres 查询时从磁盘(而不是从缓存)读取了多少页(table + 索引,如果有的话)。更好的是,如果有任何方法可以从 EXPLAIN ANALYZE 中提取此信息。

当您添加 buffers 选项时,该信息可用:explain (analyze, buffers) select ...

例如

explain (analyze, buffers)
select *
from salaries s
  join employees e on e.emp_no = s.emp_no
where s.emp_no in ('10001', '20001', '30001', '40001', '50001', '99999', '99996');

QUERY PLAN                                                                                                                         
-----------------------------------------------------------------------------------------------------------------------------------
Nested Loop  (cost=0.85..1016.67 rows=81 width=51) (actual time=0.152..18.530 rows=96 loops=1)                                     
  Buffers: shared hit=407 read=5                                                                                                   
  I/O Timings: read=15.340                                                                                                         
  ->  Index Scan using salaries_pkey on salaries s  (cost=0.43..349.03 rows=81 width=20) (actual time=0.082..0.332 rows=96 loops=1)
        Index Cond: ((emp_no)::text = ANY ('{10001,20001,30001,40001,50001,99999,99996}'::text[]))                                 
        Buffers: shared hit=28                                                                                                     
  ->  Index Scan using employees_pkey on employees e  (cost=0.42..8.24 rows=1 width=31) (actual time=0.187..0.187 rows=1 loops=96) 
        Index Cond: ((emp_no)::text = (s.emp_no)::text)                                                                            
        Buffers: shared hit=379 read=5                                                                                             
        I/O Timings: read=15.340                                                                                                   
Planning Time: 256.640 ms                                                                                                          
Execution Time: 18.628 ms                                                                                                          

您可以看到总共需要 412 页(=块)。其中 5 个必须从文件系统中获取(“read=5”)——需要这 5 个是因为 employees_pkey

上的索引扫描

an extension 应该将真正的磁盘读取与 FS 缓存读取分开,但它似乎只提供聚合数据,就像 pg_stat_statements 那样,而不是像 EXPLAIN 这样的单独执行(ANALYZE, BUFFERS) 确实如此。

您也可以使用 set log_executor_stats TO on;,也许与 set client_min_messages TO log; 结合使用以获得每次执行的顶级实际磁盘读取。不过,这里的用户体验非常糟糕。