POSTGRESQL INTERNALS

Deep dive into query optimization, indexing strategies, and database internals. All measurements from EXPLAIN ANALYZE on the 5.74M row airline dataset.

A revenue lookup that took 381ms now returns in 0.13ms. That is a 3,024x improvement from a single composite index. The dashboard query that joins flights, airports, and aggregates delay rates dropped from 147ms to 0.14ms using a materialized view with targeted indexes. Below you can trace each optimization: what the query planner chose before, what it chooses after, and how much storage each index costs. Twenty indexes consume 300+ MB across the database, but two of them (boarding_passes_pkey at 73 MB) have never been used.

0x
Best speedup (mat view)
0
Active indexes
0
Queries optimized
0.00ms
Fastest query (ms)

Query Performance: Before vs After

Measured with EXPLAIN ANALYZE

Flights from SVO with status Arrived44x
BEFORE
32ms
AFTER
0.71ms
Route delay analysis1x
BEFORE
108ms
AFTER
91ms
Revenue for specific flight3024x
BEFORE
381ms
AFTER
0.13ms
Dashboard query (mat view)1038x
BEFORE
147ms
AFTER
0.14ms

Index Size Analysis

Sorted by size, with usage statistics

IndexSizeScansTuples Read
ticket_flights_pkey91 MB1,894,2981,894,366
boarding_passes_pkey73 MB00
boarding_passes_flight_id_boarding_no_key41 MB00
boarding_passes_flight_id_seat_no_key41 MB147,7315,668,343
tickets_pkey25 MB3829,073
idx_tickets_book_ref16 MB4,4932,499,173
idx_tf_flight_id16 MB201,44012,566,905
bookings_pkey13 MB11593,443
flights_flight_no_scheduled_departure_key2048 kB282,518
flights_pkey1456 kB106132,439
idx_flights_sched_dep928 kB00
idx_flights_dep_status488 kB62262,714
idx_flights_arrived360 kB1291,011,292
seats_pkey48 kB00
mv_route_delay_summary_delay_pct_idx32 kB138
mv_daily_revenue_flight_date_fare_class_idx32 kB00
mv_route_delay_summary_departure_airport_arrival_airport_idx32 kB00
mv_aircraft_utilization_aircraft_code_idx16 kB00
aircrafts_pkey16 kB436
mv_daily_revenue_flight_date_idx16 kB00

PostgreSQL Topics Covered

EXPLAIN Deep Dive
Seq Scan, Index Scan, Index-Only Scan, Bitmap Scan. Hash Join, Nested Loop, Merge Join. Cost model and actual vs estimated rows.
Index Strategies
Composite B-tree, partial indexes, expression indexes, GIN for JSONB, covering indexes with INCLUDE. 4,780x speedup on JSONB search.
Table Partitioning
Range partitioning by month. Partition pruning eliminates 80% of scans. Trade-off: point lookups 5x slower.
Statistics & Monitoring
pg_stat_user_tables, pg_stat_user_indexes, cache hit ratios. 93-100% cache hit across tables.
VACUUM Tuning
Dead tuple lifecycle, VACUUM vs VACUUM FULL, autovacuum thresholds. XID wraparound prevention.
WAL & Checkpoints
Write-Ahead Log config, checkpoint statistics, synchronous commit trade-offs. Cloud SQL constraints.