Sub topic 3
What pg_class knows: reltuples and relpages
Prerequisites
You should know basic SQL, including SELECT and WHERE, and understand that a table can have indexes. You should also be able to run ANALYZE and read a simple EXPLAIN plan.
PostgreSQL uses a cost-based planner. Before running a query, the planner compares possible plans and estimates the work each plan will require. Two important estimates describe the table itself:
- reltuples is the estimated number of live rows in the relation.
- relpages is the estimated number of disk pages used by the relation.
A relation is a PostgreSQL object that stores rows, such as a table or materialized view. These values are stored in the system catalog named pg_class.
Inspecting reltuples and relpages
The following query inspects a table named orders:
SELECT
oid::regclass AS relation,
relkind,
reltuples,
relpages
FROM pg_class
WHERE oid = 'public.orders'::regclass;The regclass conversion displays the object name instead of an internal object identifier. The relkind column tells you what kind of relation you are viewing. A value of r usually means an ordinary table, while i means an index.
| Column | Meaning | How to use it |
|---|---|---|
| reltuples | Estimated live row count | Check the base row-count estimate |
| relpages | Estimated number of storage pages | Approximate the relation's physical size |
| relkind | Relation type | Confirm that the object is a table or index |
The values are estimates, not current counters. A table with 1,000,000 rows may show a reltuples value that is slightly different from 1,000,000. After many inserts, updates, or deletes, the estimate can become stale.
relpages counts storage pages, not rows. PostgreSQL commonly uses pages of 8 kilobytes, although the value depends on the server build. A table can use more pages after updates because old row versions may remain until vacuuming reclaims their space.
How PostgreSQL refreshes these values
ANALYZE collects a sample of table rows and updates planner statistics. VACUUM also updates important table metadata while cleaning up obsolete row versions. You can run both operations together:
VACUUM (ANALYZE) public.orders;Afterward, query pg_class again:
SELECT
reltuples,
relpages
FROM pg_class
WHERE oid = 'public.orders'::regclass;A newly created table may have reltuples set to a negative value or another unhelpful estimate until PostgreSQL has inspected it. This is one reason to analyze a table after loading a large initial dataset.
The planner uses reltuples and relpages as starting points. It can adjust estimates using the relation's current size and other statistics. For example, reltuples helps estimate how many rows a sequential scan will read. Column statistics then help estimate how many rows survive a WHERE condition.
Reading the values correctly
Treat reltuples as a planner estimate, not as a replacement for COUNT. A COUNT query reads enough table data to produce an exact result, while pg_class provides a quick approximation.
Treat relpages as a storage estimate, not as a logical row count. Two tables with the same number of rows can use different numbers of pages because their rows can have different widths, null values, indexes, and amounts of reclaimable space.
Checking a table and its indexes together
This query shows all relations whose names begin with orders:
SELECT
oid::regclass AS relation,
relkind,
reltuples,
relpages
FROM pg_class
WHERE oid::regclass::text LIKE 'public.orders%';The result may include the table and indexes. Comparing them can help you understand that each index has its own relpages value. An index's reltuples value describes its index entries, while the table's reltuples value describes table rows.
Exercise
A table has reltuples = 500000 and relpages = 62000 after a bulk delete. What should you do before investigating a suspected planner row-count error?
Show answer
Run VACUUM (ANALYZE) on the table, then inspect reltuples again and compare the planner estimate with EXPLAIN (ANALYZE).