Explain with buffer counts

These examples should have proven to you that looking at the counts of blocks hit and read is valuable for determining whether a query plan is really being executed correctly or not. PostgreSQL 9.0 adds a feature to make this easier than before. Instead of looking at the pg_stat* data as done in the previous example, you can request a count of buffers accessed directly when running EXPLAIN:

EXPLAIN (ANALYZE ON, BUFFERS ON) SELECT count(*) FROM t WHERE v=5;
QUERY PLAN                                                     
----------
Aggregate  (cost=332.92..332.93 rows=1 width=0) (actual time=39.132..39.134 rows=1 loops=1)
       Buffers: shared hit=46 read=23
       ->  Index Scan using i on t  (cost=0.00..308.21 rows=9883 width=0) (actual time=0.069..21.843 rows=9993 loops=1)
    

Get PostgreSQL 10 High Performance now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.