Cookbook recipe

Speed up COUNT(*) on large tables

Applies to PostgreSQL 13–17 Last reviewed May 2026 Grounded in source
Estimated investigation4 min

Scenario

SELECT COUNT(*) on a 200M-row table takes minutes. Exact counts are expensive in MVCC; pick the right trade-off. Diagnose it For an approximate live count, read the planner estimate: SELECT reltuples::bigint AS approx_rows FROM pg_class WHERE…

Investigation Path

SELECT COUNT(*) on a 200M-row table takes minutes. Exact counts are expensive in MVCC; pick the right trade-off.

Diagnose it

For an approximate live count, read the planner estimate:

SELECT reltuples::bigint AS approx_rows
FROM pg_class WHERE relname = 'events';

Why it happens

Because of MVCC, PostgreSQL must verify visibility of each row to count exactly, so COUNT(*) scans the table (or a covering index). There is no maintained row counter.

This is a Pro lesson

Get every Learning Pathway and cookbook recipe — grounded in PostgreSQL source code, with diagnostics, fixes, and prevention for each topic.

Continue this lesson to learn:

  • How to fix it
  • Prevent it next time
  • Related & next steps
  • All 36 Learning Pathway lessons
  • 170+ cookbook recipes
  • Source-grounded diagnostics & fixes

Secure checkout Cancel anytime Source-grounded

Career Impact

This scenario builds production judgment and operational confidence under pressure.

Open Career Dashboard →

Keep going

Related & next steps

Was this helpful?

← All cookbook recipes