skip to content

Indexes & Query Planning

PostgreSQL offers B-tree, GIN, GiST, SP-GiST, BRIN and hash indexes, and a cost-based planner chooses among them from statistics. Interviewers hand you EXPLAIN ANALYZE and ask why.

part ofPostgreSQLread it →
on this pageshow

explore

questions

page 2 of 2

In PostgreSQL, what does B-tree deduplication do, and why can adding INCLUDE (price) to an index on books(category_id) make it much larger?

level: seniorimportance: nice to knowfreq 22%

basics

~20 s

Deduplication stores a repeated key once with a sorted list of heap TIDs, shrinking low-cardinality B-trees. It is on by default but never used in INCLUDE indexes, so adding INCLUDE (price) turns every row back into a full index tuple.

open as a page

In PostgreSQL, how do join_collapse_limit, from_collapse_limit and geqo_threshold bound join-order search, and when would you set join_collapse_limit to 1?

level: seniorimportance: nice to knowfreq 24%

basics

~20 s

from_collapse_limit and join_collapse_limit (both 8) stop subqueries and explicit JOINs merging into one larger search; join problems of geqo_threshold (12) items or more go to the genetic optimizer. join_collapse_limit = 1 joins explicit JOINs in written order.

open as a page

On a 600-million-row PostgreSQL delivery_addresses table, pg_stats.n_distinct for customer_id is far below the true count; why does ANALYZE miss it, and how do you fix it durably?

level: seniorimportance: nice to knowfreq 28%

basics

~20 s

ANALYZE extrapolates distinct counts from about 30,000 sampled rows, which fails on huge tables. Measure the true ratio once, set ALTER TABLE ... ALTER COLUMN customer_id SET (n_distinct = -0.05), then ANALYZE; later ANALYZEs keep that override.

open as a page

showing 31–33 of 33