May 2018
Intermediate to advanced
576 pages
30h 25m
English
We can get a quick estimate of the number of rows in a table using roughly the same calculation that the Postgres optimizer uses:
SELECT (CASE WHEN reltuples > 0 THENpg_relation_size(oid)*reltuples/(8192*relpages)ELSE 0END)::bigint AS estimated_row_countFROM pg_classWHERE oid = 'mytable'::regclass;
This gives us the following output:
estimated_count--------------------- 293(1 row)
It returns a row count very quickly, no matter how large the table that we are examining is.