HomePostgreSQLArticle
PostgreSQL Mechanics
PostgreSQL 19: The Join Stat Fix We Waited 10 Years For
What you will learn
- For years, pg stats knew a table's name, but not its identity.
- pg stats ext and pg stats ext exprs now carry tableid and
- And a bonus hides in pg stats ext exprs.
- Before, you joined pg stats to pg class on the table name, and
Hook
For years, pg stats knew a table's name, but not its identity.
It now exposes the table's OID and attribute number, right beside
So you can join planner stats straight to the catalogs. No
Old Pain
Here is how it used to hurt. To get a table's OID from pg stats, you built it by hand: schema dot table, cast to regclass, then to oid. Fragile. A duplicate name, or a different search path, and your join pointed at the wrong table.
Extended Stats
pg stats ext and pg stats ext exprs now carry tableid and
tableid is the table's OID. statistics id is the OID of the stats
So you join extended statistics by OID, not by name.
SELECT * FROM pg_stats LIMIT 20;Range Stats
And a bonus hides in pg stats ext exprs. It now shows range type statistics: range length histogram, empty fraction, and range bounds histogram. If you use range columns, the planner's view of them is finally readable.
Oid Joins
Before, you joined pg stats to pg class on the table name, and
Now it is one clean condition: c dot oid equals s dot tableid.
Stable, scriptable, and it will not surprise you in production.
SELECT * FROM pg_stats_ext LIMIT 20;Real Use
In practice, it is a clean key. Filter pg stats by tableid, order by attnum, and you get every column statistic for one table. No quoting, and easy to run across hundreds of tables.
Watch Out
One thing to keep straight. tableid is the table's OID. statistics id is the stats object's OID. Not the same number. Do not join the extended views on tableid when you mean statistics id. The names are still there, for humans.
SELECT * FROM pg_stats_ext_exprs LIMIT 20;Checklist
Filter pg stats by tableid. Jump to one stats object by
statistics id. Read range stats from pg stats ext exprs.
Planner statistics just went from something you describe, to