Dr. Ibrar Ahmed

HomePostgreSQLArticle

PostgreSQL Mechanics

PostgreSQL 19: The Join Stat Fix We Waited 10 Years For

Dr. Ibrar Ahmed3 min readFrom the lecture notes

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
01

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

02

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.

03

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.

Inspect the system
sql
SELECT * FROM pg_stats LIMIT 20;
04

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.

05

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.

Verification 2
sql
SELECT * FROM pg_stats_ext LIMIT 20;
06

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.

07

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.

Verification 3
sql
SELECT * FROM pg_stats_ext_exprs LIMIT 20;
08

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