Function 1 - estimating the number of rows

The following function estimates the total number of rows using a mathematical procedure called extrapolation. In other words, we take the average number of bytes per row resulting from the last statistics collection, and we apply it to the current table size:

CREATE OR REPLACE FUNCTION estimated_row_count(text) RETURNS bigint LANGUAGE sql AS $$ SELECT (CASE WHEN reltuples > 0 THEN               pg_relation_size($1)*reltuples/(8192*relpages)              ELSE 0              END)::bigint FROM pg_class WHERE oid = $1::regclass; $$;

Get PostgreSQL Administration Cookbook, 9.5/9.6 Edition now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.