Waiting for 8.5 – buffers info for explain

Today small, but (at least for me) really useful patch. Committed on 15th of December by Robert Haas, and written by Itagaki Takahiro, this patch does:

Log Message:
-----------
Add an EXPLAIN (BUFFERS) option to show buffer-usage statistics.
 
This patch also removes buffer-usage statistics from the track_counts
output, since this (or the global server statistics) is deemed to be a better
interface to this information.
 
Itagaki Takahiro, reviewed by Euler Taveira de Oliveira.

What exactly it is? Let's see:

# EXPLAIN ( analyze ON, buffers ON ) SELECT COUNT(*) FROM pg_attribute ;
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=64.70..64.71 ROWS=1 width=0) (actual TIME=0.466..0.466 ROWS=1 loops=1)
   Buffers: shared hit=18 READ=21
   ->  Seq Scan ON pg_attribute  (cost=0.00..59.56 ROWS=2056 width=0) (actual TIME=0.002..0.301 ROWS=2002 loops=1)
         Buffers: shared hit=18 READ=21
 Total runtime: 0.492 ms
(5 ROWS)

As we can see, PostgreSQL had to read 39 pages from disk, 18 of which had been already in shared buffers.

Usually, when running such query for 2nd time, we should see it be a bit faster, because pages are cached. Let's see:

# EXPLAIN ( analyze ON, buffers ON ) SELECT COUNT(*) FROM pg_attribute ;
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Aggregate  (cost=64.70..64.71 ROWS=1 width=0) (actual TIME=0.325..0.325 ROWS=1 loops=1)
   Buffers: shared hit=39
   ->  Seq Scan ON pg_attribute  (cost=0.00..59.56 ROWS=2056 width=0) (actual TIME=0.004..0.171 ROWS=2002 loops=1)
         Buffers: shared hit=39
 Total runtime: 0.353 ms
(5 ROWS)

Nice. Now we see that all pages have been read from cache.

Of course using it for such simple queries doesn't make much sense, but it's just to show expected output.

When adding this capability, it was also propagated to auto explain contrib module – you can now set auto_explain.log_buffers GUC to enable it, but since it requires also ANALYZE – use with caution.

One thought on “Waiting for 8.5 – buffers info for explain”

  1. I just wanted to say: Your blog is way too freaking awesome. I love every single post. I’m too lazy to write it under every one of them, but keep in mind you’re doing great work.

    And if by any chance I happen to meet you on some conference in Poland I definitely owe you some beers for time saved.

Comments are closed.