On head: QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------- Limit (cost=9077419.45..9077419.45 rows=1 width=44) (actual time=812826.044..812826.044 rows=1 loops=1) -> Sort (cost=9077419.45..9077888.20 rows=187500 width=44) (actual time=812826.042..812826.042 rows=1 loops=1) Sort Key: (count(DISTINCT partsupp.ps_suppkey)) DESC, part.p_brand, part.p_type, part.p_size Sort Method: top-N heapsort Memory: 25kB -> GroupAggregate (cost=8857722.29..9076481.95 rows=187500 width=44) (actual time=714206.734..812801.585 rows=27840 loops=1) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=8857722.29..8901099.22 rows=17350773 width=40) (actual time=714203.151..765512.643 rows=35593611 loops=1) Sort Key: part.p_brand, part.p_type, part.p_size Sort Method: external merge Disk: 1815480kB -> Merge Join (cost=44177.40..6744311.66 rows=17350773 width=40) (actual time=3613.368..411214.196 rows=35593611 loops=1) Merge Cond: (part.p_partkey = partsupp.ps_partkey) -> Index Scan using part_pkey on part (cost=0.56..1939335.17 rows=8675385 width=40) (actual time=0.038..113094.996 rows=8902829 loops=1) Filter: ((p_brand <> 'Brand#31'::bpchar) AND ((p_type)::text !~~ 'PROMO BRUSHED%'::text) AND (p_size = ANY ('{41,4,15,12,42,46,32,30}'::integer[]))) Rows Removed by Filter: 51097171 -> Index Only Scan using partsupp_pkey on partsupp (cost=44149.93..4309962.17 rows=120000184 width=8) (actual time=3613.301..226287.744 rows=239882717 lo ops=1) Filter: (NOT (hashed SubPlan 1)) Rows Removed by Filter: 117280 Heap Fetches: 0 SubPlan 1 -> Seq Scan on supplier (cost=0.00..44148.61 rows=300 width=4) (actual time=0.453..3611.403 rows=1466 loops=1) Filter: ((s_comment)::text ~~ '%Customer%Complaints%'::text) Rows Removed by Filter: 2998534 Planning time: 1.271 ms Execution time: 812994.671 ms (24 rows) With patches: QUERY PLAN -------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- ------------------- Limit (cost=7919012.55..7919012.56 rows=1 width=44) (actual time=645732.255..645732.255 rows=1 loops=1) -> Sort (cost=7919012.55..7919481.30 rows=187500 width=44) (actual time=645732.253..645732.253 rows=1 loops=1) Sort Key: (count(DISTINCT partsupp.ps_suppkey)) DESC, part.p_brand, part.p_type, part.p_size Sort Method: top-N heapsort Memory: 25kB -> GroupAggregate (cost=7699315.39..7918075.05 rows=187500 width=44) (actual time=547855.352..645708.990 rows=27840 loops=1) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=7699315.39..7742692.32 rows=17350773 width=40) (actual time=547851.770..598542.606 rows=35593611 loops=1) Sort Key: part.p_brand, part.p_type, part.p_size Sort Method: external merge Disk: 1815480kB -> Gather (cost=45177.40..5585904.76 rows=17350773 width=40) (actual time=3811.864..228451.017 rows=35593611 loops=1) Workers Planned: 6 Workers Launched: 6 -> Merge Join (cost=44177.40..3849827.46 rows=17350773 width=40) (actual time=4275.252..237087.584 rows=5084802 loops=7) Merge Cond: (partsupp.ps_partkey = part.p_partkey) -> Parallel Index Only Scan using partsupp_pkey on partsupp (cost=44149.93..1809958.34 rows=20000031 width=8) (actual time=4274.474..39958.862 rows =34268960 loops=7) Filter: (NOT (hashed SubPlan 1)) Rows Removed by Filter: 16754 Heap Fetches: 0 SubPlan 1 -> Seq Scan on supplier (cost=0.00..44148.61 rows=300 width=4) (actual time=5.186..4272.254 rows=1466 loops=7) Filter: ((s_comment)::text ~~ '%Customer%Complaints%'::text) Rows Removed by Filter: 2998534 -> Index Scan using part_pkey on part (cost=0.56..1939335.17 rows=8675385 width=40) (actual time=0.144..181434.037 rows=12708841 loops=7) Filter: ((p_brand <> 'Brand#31'::bpchar) AND ((p_type)::text !~~ 'PROMO BRUSHED%'::text) AND (p_size = ANY ('{41,4,15,12,42,46,32,30}'::integer []))) Rows Removed by Filter: 72941508 Planning time: 1.350 ms Execution time: 645897.192 ms (27 rows)