Waiting for 9.4 – Include planning time in EXPLAIN ANALYZE output.

On 29th of January, Robert Haas committed patch:

Include planning time in EXPLAIN ANALYZE output.
 
This doesn't work for prepared queries, but it's not too easy to get
the information in that case and there's some debate as to exactly
what the right thing to measure is, so just do this for now.
 
Andreas Karlsson, with slight doc changes by me.

Planning is the part of query processing that happens before PostgreSQL will even touch data. It is actually what creates the plans that EXPLAIN ANALYZE shows.

For trivial queries – it's fast. For more complicated queries – it can be longer.

Now, with this new patch from Andreas, we can actually see the planning time.

Let's consider simple example:

$ EXPLAIN analyze SELECT relkind, COUNT(*) FROM pg_class GROUP BY 1 ORDER BY 2 DESC;
                                                    QUERY PLAN
-------------------------------------------------------------------------------------------------------------------
 Sort  (cost=14.49..14.50 ROWS=4 width=1) (actual TIME=0.267..0.267 ROWS=4 loops=1)
   Sort KEY: (COUNT(*))
   Sort Method: quicksort  Memory: 25kB
   ->  HashAggregate  (cost=14.41..14.45 ROWS=4 width=1) (actual TIME=0.258..0.259 ROWS=4 loops=1)
         ->  Seq Scan ON pg_class  (cost=0.00..12.94 ROWS=294 width=1) (actual TIME=0.008..0.064 ROWS=297 loops=1)
 Total runtime: 0.320 ms
(6 ROWS)

And now, the same query on another server, with pg 9.4:

$ EXPLAIN analyze SELECT relkind, COUNT(*) FROM pg_class GROUP BY 1 ORDER BY 2 DESC;
                                                    QUERY PLAN                                                     
-------------------------------------------------------------------------------------------------------------------
 Sort  (cost=14.04..14.05 ROWS=4 width=1) (actual TIME=0.272..0.272 ROWS=5 loops=1)
   Sort KEY: (COUNT(*))
   Sort Method: quicksort  Memory: 25kB
   ->  HashAggregate  (cost=13.97..14.01 ROWS=4 width=1) (actual TIME=0.263..0.264 ROWS=5 loops=1)
         GROUP KEY: relkind
         ->  Seq Scan ON pg_class  (cost=0.00..12.31 ROWS=331 width=1) (actual TIME=0.007..0.062 ROWS=322 loops=1)
 Planning TIME: 0.100 ms
 Total runtime: 0.323 ms
(8 ROWS)

Now we see that for this particular query, 0.1ms is used to plan it. Unfortunately, I don't have any real databases on 9.4, so I can't show you longer times, but generally – the more joins/subselects – the longer it will take to plan the query.

This effect is actually the reason we have GEQO.

All in all – it's really nice to have – especially if you have some multi-table join queries. Thanks Andreas and Robert.