Quick revision
The short answer
PostgreSQL assumes by default that columns are independent, so it multiplies the selectivity of city = 'Paris' by the selectivity of country = 'FR'. Because Paris implies France, the real combined selectivity is about that of city alone; the product, made worse when a frequent city like Paris is missing from the column's statistics, gives 300 rows instead of 400,000. We fix it with extended statistics, CREATE STATISTICS ... (dependencies, mcv) ON city, country, followed by ANALYZE, and for skewed single columns a higher per-column statistics target, then verify with EXPLAIN ANALYZE.
What to remember
In this answer, we saw that PostgreSQL by default treats columns as independent and multiplies their selectivities, which for city and country gives 300 rows instead of about 400,000, because Paris implies France and the combined selectivity is really that of city alone. We fixed the estimate with CREATE STATISTICS using dependencies and mcv followed by ANALYZE, saw that a higher per-column statistics target helps skewed single columns, and verified with EXPLAIN ANALYZE that the estimate and the join strategy above it changed.
Your turn
Answer it aloud as you would in the interview, or type your answer below. Then show one part at a time and compare.
PostgreSQL assumes by default that columns are independent, so it multiplies the selectivity of city = 'Paris' by the selectivity of country = 'FR'. Because Paris implies France, the real combined selectivity is about that of city alone; the product, made worse when a frequent city like Paris is missing from the column's statistics, gives 300 rows instead of 400,000. We fix it with extended statistics, CREATE STATISTICS ... (dependencies, mcv) ON city, country, followed by ANALYZE, and for skewed single columns a higher per-column statistics target, then verify with EXPLAIN ANALYZE.
Why this question is asked
A wrong row estimate is the root of most bad plans, and correlated columns are one of the most common reasons for wrong estimates in real schemas: city and country, model and manufacturer, zip code and state. Interviewers ask this to see whether we know the independence assumption and the tools PostgreSQL gives us to correct it. In this answer, we will see how the planner estimates each condition, reproduce the multiplication that gives 300, explain why it is wrong here, fix it with extended statistics, look at the statistics target for skewed columns, and verify the fix.
1How the Planner Estimates One Condition
For each condition in a WHERE clause, the planner estimates its selectivity, the fraction of rows that match, from the column statistics ANALYZE collected. For equality on a frequent value it uses the most common values list (MCV) and its frequencies. For a value that is not in that list, it spreads the remaining fraction evenly over the other distinct values, which is a rough average.
Let us put concrete numbers on our 10 million addresses. Suppose 6% of them are in France, so country = 'FR' is in the MCV list with a frequency of 0.06, about 600,000 rows, and that estimate is accurate. The city column is different: it has tens of thousands of distinct values, and with the default statistics target the MCV list keeps only 100 of them. If 'Paris' is missing from that list, for example because the statistics were collected before a large import of French addresses, the planner treats it like an average city and estimates, say, 0.0005, about 5,000 rows, when Paris really has 400,000 addresses. So the city estimate is already 80 times too low before we even combine the two conditions.
2Why Multiplying Gives 300 Rows
With two conditions joined by AND, PostgreSQL by default assumes the columns are independent, meaning that knowing one tells us nothing about the other. Under independence, the probability of both is the product of each, so the planner multiplies.
| Estimate | Calculation | Rows |
|---|---|---|
| city = 'Paris', planner | 0.0005 x 10M, average for a non-MCV value | 5,000 |
| Both conditions, planner | 0.0005 x 0.06 x 10M, independence | 300 |
| city = 'Paris', real | Paris is 4% of the table | 400,000 |
| Both conditions, real | Paris implies FR, so same as city alone | 400,000 |
The multiplication is the second mistake. City and country are not independent: every address in Paris is in France, so adding country = 'FR' removes almost no rows, and the combined selectivity is really the selectivity of city alone. Multiplying by 0.06 makes the estimate another 16 times smaller. The planner has no way to know this from per-column statistics, because each column's statistics are collected separately.
Notice that independence alone cannot explain the whole gap: multiplying by an accurate 0.06 can shrink the estimate at most about 16 times, so for the estimate to fall more than a thousand times below reality, the city figure must be wrong as well. The 1,333 times underestimate in the question therefore comes from two causes working together: city statistics that do not reflect how common Paris is, and the independence assumption on two correlated columns. We will fix both.
3Why a 300 Row Estimate Is Dangerous
An estimate of 300 rows changes decisions above this node. If the query joins addresses to orders, the planner may pick a nested loop that does an index lookup into orders for each of the 300 expected addresses, which is cheap for 300 and very slow for 400,000. It may also give a sort or hash only enough memory for 300 rows. So the cost of the mistake is often paid in a node far above the scan where the estimate went wrong.
4Fixing It With Extended Statistics
Since PostgreSQL 10, we can tell ANALYZE to collect statistics about combinations of columns with CREATE STATISTICS. Two kinds help here. dependencies records functional dependencies, how strongly the value of one column determines another, such as city determining country. mcv records a multi-column most common values list, the most frequent (city, country) pairs with their real combined frequencies.
Notice that nothing happens until ANALYZE runs: CREATE STATISTICS only declares what to collect. The dependency degree of about 0.99 for city => country tells the planner that once city is known, the country condition removes almost nothing. Dependencies alone would stop the multiplication, but the estimate would still start from the low city figure of 5,000. The multi-column MCV list goes further: if ('Paris','FR') is among the frequent pairs, the planner reads their real combined frequency directly, which fixes both problems at once and also handles cases that are not simple dependencies. Functional dependencies apply to equality conditions; the MCV list also helps with some other operators.
5Skewed Single Columns and the Statistics Target
Sometimes the problem is in one column: a few values are very frequent and the rest are spread thin. By default ANALYZE keeps 100 most common values and 100 histogram buckets per column, controlled by default_statistics_target. As we saw, if a frequent value like 'Paris' is not in the MCV list, its estimate falls back to an average and can be 80 times off. First we make sure the statistics are fresh with ANALYZE. If a column with very many distinct values still loses important values from its list, raising the target for that column makes ANALYZE sample more rows and keep more values.
A larger target makes ANALYZE slower and planning very slightly slower, so we raise it only for columns where we have seen bad estimates, not globally.
6Verifying the Fix
The proof is in EXPLAIN ANALYZE: the estimated rows for the scan should now be within a small factor of the actual 400,000. Then we look above it. If the join method changed from a nested loop to a hash join and the query time dropped, the estimate was the problem. We should also check a few other city and country combinations, including small ones, because the new statistics affect them all.
- EXPLAIN ANALYZE shows 300 vs 400,000estimate far below actual
- Are the filter columns dependent?YesCREATE STATISTICS (dependencies, mcv)NoRaise SET STATISTICS on the skewed column
- Run ANALYZEstatistics are collected only now
- Estimate close to actual?YesCheck the join method above changedNoLook at the MCV list and target again
4 steps in order. Play them, or step through them one at a time.
7Follow-up questions
What does the ndistinct kind of extended statistics help with?
It records the number of distinct combinations of a group of columns. The planner uses that when it estimates how many groups a GROUP BY city, country will produce, or how many distinct values a multi-column expression has. Without it, the planner multiplies the per-column distinct counts, which for city and country would hugely overestimate the number of groups and could lead it to choose a sort instead of a hash aggregate, or to misjudge memory.
Why does PostgreSQL assume independence at all?
Because it is the only safe default when there is no information about how columns relate, and collecting statistics for every possible combination of columns would be far too expensive. Extended statistics let us add that information only for the column groups where we know it matters.
Can a different index fix this instead?
An index on (city, country) gives a fast access path, but it does not by itself fix the row estimate, which comes from statistics. The planner might still expect 300 rows and make bad choices above the scan. Extended statistics are the tool for estimates; indexes are the tool for access.
8Conclusion
In this answer, we saw that PostgreSQL by default treats columns as independent and multiplies their selectivities, which for city and country gives 300 rows instead of about 400,000, because Paris implies France and the combined selectivity is really that of city alone. We fixed the estimate with CREATE STATISTICS using dependencies and mcv followed by ANALYZE, saw that a higher per-column statistics target helps skewed single columns, and verified with EXPLAIN ANALYZE that the estimate and the join strategy above it changed.
Open with
The planner estimates each condition separately from column statistics, so city = 'Paris' alone and country = 'FR' alone are probably estimated well. The problem is the AND.
Then, if they want more
By default PostgreSQL assumes columns are independent and multiplies the two selectivities. But city and country are strongly correlated: every Paris address is in France, so the country condition removes almost nothing and the real combined selectivity is about the selectivity of city alone.
Multiplying gives a far smaller number, and here the city estimate itself also looks too low, probably stale or missing Paris from its common values, so together we get 300 instead of about 400,000. That matters because everything above the scan, like choosing a nested loop for a join or sizing a hash, is planned for 300 rows.
The fix is extended statistics: CREATE STATISTICS addr_stats with the dependencies and mcv kinds on city and country, and then ANALYZE, because the statistics are only collected then. Dependencies tell the planner that city determines country, and the multi-column MCV list gives the real frequency of common pairs like Paris and FR.
For the city column I would make sure statistics are fresh and, if Paris is still missing from its common values, raise its statistics target, for example to 1000, with ALTER TABLE ... ALTER COLUMN ... SET STATISTICS, and analyze again.
Then I verify with EXPLAIN ANALYZE that the estimate is close to the actual rows and that the join strategy above it changed, for example from a nested loop to a hash join.
About this answer
- Last updated
- 12 November 2026
- Versions it names
- PostgreSQL 10
- Checked
- Its structure and format are checked automatically. Use it as a carefully prepared study note: where a detail matters, confirm it against the documentation below. What the checks cover
- Where the question comes from
- Written by Scrivio for practice. It is not taken from an interview.
- Official documentation
- PostgreSQL documentation, MySQL reference manual
Report an issue with this answer
Copy the line below and send it to the author with what you found. It carries the address of this page.
Issue with "A query filters WHERE city = 'Paris' AND country = 'FR' on a table of 10 million...": https://scrivio-interview-library.netlify.app/databases/query-optimization/f71c8f66f9/