Inspecting estimates

However, there is something that should always be done: making sure that estimates and real numbers are reasonably close together. In some cases, the optimizer will make poor decisions because the estimates are way off for some reason. It can happen that estimates are off because the system statistics are not up to date. Running an ANALYZE clause is therefore definitely a good thing to start with. However, optimizer stats are mostly taken care of by the autovacuum daemon, so it is definitely worth considering other options causing bad estimates. Take a look at the following example:

test=# CREATE TABLE t_estimate AS        SELECT * FROM generate_series(1, 10000) AS id;SELECT 10000

After loading 10,000 rows, optimizer statistics ...

Get Mastering PostgreSQL 10 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.