PostgreSQL: Count

SELECT COUNT(*) FROM table_name;

will always be slow because MVCC.

Counting estimate

Replace table_name with real table name:

SELECT reltuples::BIGINT AS estimate
FROM pg_class
WHERE oid = 'schema_name.table_name'::regclass;

You can get schema_name by

 SELECT nspname FROM pg_catalog.pg_namespace;

Read Also