Sub topic 13
Special Cases: Partitioned Tables, Temporary Tables, and Stale Statistics
Assume you know basic SQL, can run psql, and can read simple EXPLAIN output. This chapter focuses on cases where PostgreSQL cannot keep planner statistics current in the usual way.
Partitioned tables
A partitioned table is a parent table whose rows are stored in separate child tables called partitions. Each partition has its own physical data and its own statistics.
Run ANALYZE on the parent when you need statistics for queries that use the partitioned table as a whole:
ANALYZE public.events;This updates statistics for the parent and can sample data across its partitions. You should also analyze individual partitions after large changes when a query targets one partition directly:
ANALYZE public.events_2026_11;Autovacuum can analyze ordinary partitions because they are regular tables. However, autovacuum does not automatically analyze the partitioned parent in the same way. The parent's cross-partition statistics can therefore become stale even when every child has recently been analyzed.
Schedule ANALYZE on the parent after loading a new partition, changing the distribution of values, or performing a large data migration.
flowchart TD
A[Partitioned parent] --> B[Analyze parent for global estimates]
C[Individual partition] --> D[Analyze changed partition]
B --> E[Planner chooses a plan]
D --> Ecaption: Where statistics are collected for partitioned data alt: A flowchart showing that both the partitioned parent and changed individual partitions may need ANALYZE before the planner chooses a plan.
Temporary tables
A temporary table exists only in the session that created it. Its data and statistics are also session-local.
Autovacuum workers cannot access another session's temporary tables. After creating or loading one, run ANALYZE yourself:
CREATE TEMP TABLE session_orders (
order_id bigint,
customer_id integer,
status text
);
INSERT INTO session_orders (order_id, customer_id, status)
SELECT g, (g % 1000), CASE
WHEN g % 20 = 0 THEN 'pending'
ELSE 'shipped'
END
FROM generate_series(1, 100000) AS g;
ANALYZE session_orders;Without this command, PostgreSQL may use default assumptions about row counts and value distributions. Those assumptions can produce a poor join order or an inefficient scan.
Temporary-table statistics do not appear in the same useful way to other sessions. Inspect them from the session that owns the temporary table:
SELECT *
FROM pg_stats
WHERE schemaname LIKE 'pg_temp_%'
AND tablename = 'session_orders';The temporary schema name can differ between sessions, so checking pg_class or using EXPLAIN in the same session is often more practical.
Stale statistics
Statistics become stale when the data changes substantially after the last analysis. A table can have the correct indexes and still receive a bad plan if the planner believes that a rare value is common, or a common value is rare.
Check the analysis timestamps and modification count:
SELECT relname,
last_analyze,
last_autoanalyze,
n_mod_since_analyze
FROM pg_stat_user_tables
WHERE relname IN ('orders', 'session_orders');n_mod_since_analyze is a useful warning signal, but it does not measure every kind of distribution change. A bulk insert can change the data shape even when the percentage of changed rows seems small. Compare the estimate with reality:
EXPLAIN (ANALYZE, BUFFERS)
SELECT *
FROM orders
WHERE status = 'pending';If the estimated and actual row counts differ greatly, refresh the statistics and test again:
ANALYZE orders;For a partitioned table, analyze both the affected partition and the parent when the query spans multiple partitions. For a temporary table, analyze it after each large load or rebuild.
A practical checklist
- Analyze the parent after partition changes or major cross-partition data changes.
- Analyze heavily modified partitions directly.
- Always analyze temporary tables after loading substantial data.
- Check
last_autoanalyzeandn_mod_since_analyze. - Use
EXPLAIN (ANALYZE)to confirm whether estimates improved.
Exercise
You create and load a temporary table with 500,000 rows, but PostgreSQL chooses a nested loop with a very small estimated row count. What should you try first?
Show answer
Run ANALYZE on the temporary table in the same session, then run EXPLAIN (ANALYZE) again to compare the new estimates with the actual rows.