forked frompostgres/postgres
- Notifications
You must be signed in to change notification settings - Fork6
Commitcb5c149
committed
Fix misestimation of n_distinct for a nearly-unique column with many nulls.
If ANALYZE found no repeated non-null entries in its sample, it set thecolumn's stadistinct value to -1.0, intending to indicate that the entriesare all distinct. But what this value actually means is that the numberof distinct values is 100% of the table's rowcount, and thus it wasoverestimating the number of distinct values by however many nulls thereare. This could lead to very poor selectivity estimates, as for examplein a recent report from Andreas Joseph Krogh. We should discount thestadistinct value by whatever we've estimated the nulls fraction to be.(That is what will happen if we choose to use a negative stadistinct fora column that does have repeated entries, so this code path was justinconsistent.)In addition to fixing the stadistinct entries stored by several differentANALYZE code paths, adjust the logic where get_variable_numdistinct()forces an "all distinct" estimate on the basis of finding a relevant uniqueindex. Unique indexes don't reject nulls, so there's no reason to assumethat the null fraction doesn't apply.Back-patch to all supported branches. Back-patching is a bit of a judgmentcall, but this problem seems to affect only a few users (else we'd haveidentified it long ago), and it's bad enough when it does happen thatdestabilizing plan choices in a worse direction seems unlikely.Patch by me, with documentation wording suggested by Dean RasheedReport: <VisenaEmail.26.df42f82acae38a58.156463942b8@tc7-visena>Discussion: <16143.1470350371@sss.pgh.pa.us>1 parent71dca40 commitcb5c149
File tree
6 files changed
+34
-21
lines changed- doc/src/sgml
- src
- backend
- commands
- tsearch
- utils/adt
- include/catalog
6 files changed
+34
-21
lines changedLines changed: 3 additions & 3 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
5890 | 5890 |
| |
5891 | 5891 |
| |
5892 | 5892 |
| |
5893 |
| - | |
5894 |
| - | |
5895 |
| - | |
| 5893 | + | |
| 5894 | + | |
| 5895 | + | |
5896 | 5896 |
| |
5897 | 5897 |
| |
5898 | 5898 |
| |
|
Lines changed: 11 additions & 5 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
2043 | 2043 |
| |
2044 | 2044 |
| |
2045 | 2045 |
| |
2046 |
| - | |
2047 |
| - | |
| 2046 | + | |
| 2047 | + | |
| 2048 | + | |
| 2049 | + | |
| 2050 | + | |
2048 | 2051 |
| |
2049 | 2052 |
| |
2050 | 2053 |
| |
| |||
2398 | 2401 |
| |
2399 | 2402 |
| |
2400 | 2403 |
| |
2401 |
| - | |
2402 |
| - | |
| 2404 | + | |
| 2405 | + | |
| 2406 | + | |
| 2407 | + | |
| 2408 | + | |
2403 | 2409 |
| |
2404 | 2410 |
| |
2405 | 2411 |
| |
| |||
2703 | 2709 |
| |
2704 | 2710 |
| |
2705 | 2711 |
| |
2706 |
| - | |
| 2712 | + | |
2707 | 2713 |
| |
2708 | 2714 |
| |
2709 | 2715 |
| |
|
Lines changed: 1 addition & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
295 | 295 |
| |
296 | 296 |
| |
297 | 297 |
| |
298 |
| - | |
| 298 | + | |
299 | 299 |
| |
300 | 300 |
| |
301 | 301 |
| |
|
Lines changed: 3 additions & 1 deletion
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
203 | 203 |
| |
204 | 204 |
| |
205 | 205 |
| |
206 |
| - | |
| 206 | + | |
| 207 | + | |
| 208 | + | |
207 | 209 |
| |
208 | 210 |
| |
209 | 211 |
| |
|
Lines changed: 8 additions & 4 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
4684 | 4684 |
| |
4685 | 4685 |
| |
4686 | 4686 |
| |
| 4687 | + | |
4687 | 4688 |
| |
4688 | 4689 |
| |
4689 | 4690 |
| |
4690 | 4691 |
| |
4691 | 4692 |
| |
4692 | 4693 |
| |
4693 | 4694 |
| |
4694 |
| - | |
| 4695 | + | |
| 4696 | + | |
4695 | 4697 |
| |
4696 | 4698 |
| |
4697 | 4699 |
| |
| |||
4700 | 4702 |
| |
4701 | 4703 |
| |
4702 | 4704 |
| |
| 4705 | + | |
4703 | 4706 |
| |
4704 | 4707 |
| |
4705 | 4708 |
| |
| |||
4723 | 4726 |
| |
4724 | 4727 |
| |
4725 | 4728 |
| |
4726 |
| - | |
| 4729 | + | |
4727 | 4730 |
| |
4728 | 4731 |
| |
4729 | 4732 |
| |
| |||
4745 | 4748 |
| |
4746 | 4749 |
| |
4747 | 4750 |
| |
4748 |
| - | |
| 4751 | + | |
| 4752 | + | |
4749 | 4753 |
| |
4750 | 4754 |
| |
4751 |
| - | |
| 4755 | + | |
4752 | 4756 |
| |
4753 | 4757 |
| |
4754 | 4758 |
| |
|
Lines changed: 8 additions & 7 deletions
Original file line number | Diff line number | Diff line change | |
---|---|---|---|
| |||
57 | 57 |
| |
58 | 58 |
| |
59 | 59 |
| |
60 |
| - | |
61 |
| - | |
62 |
| - | |
63 |
| - | |
64 |
| - | |
65 |
| - | |
66 |
| - | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
| 64 | + | |
| 65 | + | |
| 66 | + | |
| 67 | + | |
67 | 68 |
| |
68 | 69 |
| |
69 | 70 |
| |
|
0 commit comments
Comments
(0)