Harmen <harmen@xxxxxxxxx> writes: > On Sat, Jan 14, 2023 at 11:23:07AM -0500, Tom Lane wrote: >> If you are running a reasonably recent PG version you should be able to >> fix that by setting up "extended statistics" on that pair of columns: > CREATE STATISTICS dist4 (ndistinct) ON deleted, org_id FROM contactsbool; > CREATE STATISTICS dist4b (ndistinct) ON org_id, deleted FROM contactsbool; 1. ndistinct is not the correct stats type for this problem. (I think dependencies is, but generally speaking, it's not worth trying to be smarter than the system about which ones you need. Just create 'em all.) 2. Per the CREATE STATISTICS man page, the order of the columns is not significant, so you're just doubling the amount of work for ANALYZE without gaining anything. I think you will find that CREATE STATISTICS stats1 ON deleted, org_id FROM contactsbool; is enough to fix this. It improved the estimate for me in v14 and HEAD, anyway. regards, tom lane