Analyze on a Mostly-Null Column
Comments
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Ranked by a public rubric, not by votes: what a comment adds counts, not who wrote it. How comments are ranked · Comment rules
Summary
Radim Marek of boringSQL shows why PostgreSQL's n_distinct for a mostly-NULL column swings between runs of ANALYZE. On a 10-million-row lab table where 0.2% of rows are non-NULL, the sample holds about 60 values. If none repeat, analyze.c declares the column unique, and about 75 of 200 runs did. That flips the join plan, and a stable fix is a higher statistics target or a manual n_distinct. The tests cover one synthetic table on PostgreSQL 18.7.