Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

is there a way to get postgres show actual i/o for query

Tags:

postgresql

I know that with EXPLAIN ANALYZE I can get the predicted cost and actual execution times (which are in different units, argh!), but is there a way to get Postgres to tell me how much I/O (logical or physical) that it has to do to satisfy a query?

(I'm looking for the equivalent of "set statistics io on" for Sybase or MS SQL Server.)

like image 850
Chris Curvey Avatar asked Jun 02 '11 16:06

Chris Curvey


2 Answers

Starting in PostgreSQL 9.0, you can execute:

EXPLAIN (ANALYZE ON, BUFFERS ON) SELECT ...

And it will show you how the statement interacted with PostgreSQL's cache. In cases where this reports a cache miss, that's going to be an OS call to read something. You can't be sure that's a physical I/O, because it may be in the OS cache. But this is probably more like what you're looking for here than trying to look at the pg_stat_* information.

like image 56
Greg Smith Avatar answered Sep 17 '22 12:09

Greg Smith


This answer is not related directly to a specific query statement, but for helping those who ended here when searching for a way of showing a "disk vs cache":

-- perform a "select pg_stat_reset();" when you want to reset counter statistics
with 
all_tables as
(
SELECT  *
FROM    (
    SELECT  'all'::text as table_name, 
        sum( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk, 
        sum( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
    FROM    pg_statio_all_tables  --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
    ) a
WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
),
tables as 
(
SELECT  *
FROM    (
    SELECT  relname as table_name, 
        ( (coalesce(heap_blks_read,0) + coalesce(idx_blks_read,0) + coalesce(toast_blks_read,0) + coalesce(tidx_blks_read,0)) ) as from_disk, 
        ( (coalesce(heap_blks_hit,0)  + coalesce(idx_blks_hit,0)  + coalesce(toast_blks_hit,0)  + coalesce(tidx_blks_hit,0))  ) as from_cache    
    FROM    pg_statio_all_tables --> change to pg_statio_USER_tables if you want to check only user tables (excluding postgres's own tables)
    ) a
WHERE   (from_disk + from_cache) > 0 -- discard tables without hits
)
SELECT  table_name as "table name",
    from_disk as "disk hits",
    round((from_disk::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% disk hits",
    round((from_cache::numeric / (from_disk + from_cache)::numeric)*100.0,2) as "% cache hits",
    (from_disk + from_cache) as "total hits"
FROM    (SELECT * FROM all_tables UNION ALL SELECT * FROM tables) a
ORDER   BY (case when table_name = 'all' then 0 else 1 end), from_disk desc

enter image description here

like image 36
Christian Avatar answered Sep 19 '22 12:09

Christian