1.0 and 4.0 — random page reads cost 4× sequential.
When does an index scan beat a seq scan?
When selectivity is low (few matching rows S), so few random page fetches outweigh reading all P pages sequentially.
How is equality selectivity estimated?
s≈1/ndistinct, assuming uniform distribution.
Estimated output rows of a node = ?
selectivity × input rows (s⋅T).
What command refreshes the statistics used for estimates?
ANALYZE (populates pg_statistic with n_distinct, MCVs, histogram).
Which direction do you read a Postgres plan tree?
Bottom-up / inside-out: most-indented leaf nodes execute first and feed their parents.
A node shows rows=10 ... actual rows=900000. What's the diagnosis?
Severe misestimation (stale/insufficient stats); likely cause of a bad plan — re-ANALYZE.
Is cost= comparable across different queries/machines?
No — only between alternative plans for the same query on the same config; it's an abstract unit, not time.
Recall Feynman: explain to a 12-year-old
You ask a giant library for "all books about dragons." The librarian can either walk past every shelf (slow but steady), or use the card catalog to jump straight to the dragon books (fast if there are only a few). EXPLAIN is you peeking at the librarian's plan before they start: "Are you going to walk every shelf? How many books do you think you'll find?" If the librarian guesses "2 books" but there are actually 900,000, they made a dumb plan — and you tell them to re-count what's on the shelves (that's ANALYZE). "Cost" is just the librarian's guess of how tiring the trip will be, not minutes.
Dekho, jab tum SELECT likhte ho, tum database ko sirf batate ho ki tumhe kya chahiye — kaise layega yeh database khud decide karta hai. Yeh decision lene wale part ko query optimizer kehte hain, aur EXPLAIN us optimizer ke dimaag ko padhne ki khidki hai. EXPLAIN sirf plan dikhata hai (query chalata nahi), jabki EXPLAIN ANALYZE query actually chalata hai aur estimated vs actual rows/time dono dikhata hai — debugging ke liye yahi sabse powerful tool hai.
Plan ek tree hota hai. Sabse andar (zyada indent wale) nodes pehle chalte hain aur upar wale parent ko rows dete hain — yaani tree ko neeche se upar padho, code ki tarah upar se neeche nahi. Har line par cost=0.00..18334 likha hota — pehla number startup cost (pehli row aane se pehle ka kaam), doosra total cost (last row tak). Sort ka startup cost high hota kyunki use sab rows pehle chahiye. Yaad rakho: cost ek abstract unit hai, seconds nahi — isliye alag-alag queries ka cost compare mat karo, sirf same query ke alag plans compare karo.
Sabse important intuition: Index Scan tabhi jeetta hai jab matching rows kam ho (low selectivity). Sequential scan poori table ko sequentially padhta hai (har page sasta), jabki index random page fetches karta hai (har page 4× mehnga — random_page_cost=4). Agar filter 80% rows match karta hai, to index lagana suicide hai, aur planner sahi me usse ignore karta hai. Isliye "mera index use kyu nahi ho raha" ka jawab aksar yeh hota hai ki planner smart hai.
Aur ek cheez: estimated rows ka stats se aata hai (ANALYZE se banti). Agar rows=10 likha ho par actual rows=900000 ho, samajh jao stats purani/galat hain — ANALYZE chalao. Yeh estimation error hi 90% bure plans ki jadd hai. To pattern: EXPLAIN ANALYZE chalao, estimated vs actual ka gap dekho, aur sabse mehenge node par focus karo.