Timing overhead

Assume you're executing a simple query to count all the customers in the database, and want to time how long this takes:

dellstore2=# \timing
Timing is on.
dellstore2=# SELECT count(*) FROM customers;
 count 
-------
 20000
Time: 7.994 ms

You may then be curious to know which query plan was used to get this result. The amount of time taken to determine that may shock you:

dellstore2=# EXPLAIN ANALYZE SELECT count(*) FROM customers;
QUERY PLAN                                                     
----------
Aggregate  (cost=726.00..726.01 rows=1 width=0) (actual time=68.836..68.838 rows=1 loops=1)
        ->  Seq Scan on customers  (cost=0.00..676.00 rows=20000 width=0) (actual time=0.012..33.609 rows=20000 loops=1)
  Total runtime: 68.934 ms
  Time: 69.837 ms
  

This fairly trivial query was ...

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.