On head: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------- Limit (cost=532028.06..532028.06 rows=1 width=44) (actual time=49213.313..49213.314 rows=1 loops=1) -> Sort (cost=532028.06..532480.72 rows=181066 width=44) (actual time=49213.311..49213.311 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=514691.25..531122.73 rows=181066 width=44) (actual time=45469.589..49191.025 rows=27840 loops=1) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=514691.25..517615.42 rows=1169665 width=44) (actual time=45469.337..46220.575 rows=2375516 loops=1) Sort Key: part.p_brand, part.p_type, part.p_size Sort Method: quicksort Memory: 285943kB -> Hash Join (cost=118466.06..396802.68 rows=1169665 width=44) (actual time=6431.135..26289.132 rows=2375516 loops=1 ) Hash Cond: (partsupp.ps_partkey = part.p_partkey) -> Seq Scan on partsupp (cost=2959.85..239600.71 rows=7999762 width=16) (actual time=247.486..13048.239 rows=1 5991520 loops=1) Filter: (NOT (hashed SubPlan 1)) Rows Removed by Filter: 8480 SubPlan 1 -> Seq Scan on supplier (cost=0.00..2959.80 rows=20 width=4) (actual time=0.479..247.334 rows=106 loop s=1) Filter: ((s_comment)::text ~~ '%Customer%Complaints%'::text) Rows Removed by Filter: 199894 -> Hash (cost=108195.63..108195.63 rows=584847 width=40) (actual time=6182.670..6182.670 rows=594194 loops=1) Buckets: 1048576 Batches: 1 Memory Usage: 51237kB -> Seq Scan on part (cost=0.00..108195.63 rows=584847 width=40) (actual time=0.058..5649.112 rows=594194 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: 3405806 Planning time: 1.165 ms Execution time: 49221.879 ms (25 rows) With patches: QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------- ------------------------------------------------------- Limit (cost=509008.82..509008.82 rows=1 width=44) (actual time=37272.962..37272.963 rows=1 loops=1) -> Sort (cost=509008.82..509461.49 rows=181066 width=44) (actual time=37272.960..37272.960 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=491672.02..508103.49 rows=181066 width=44) (actual time=33548.001..37250.009 rows=27840 loops=1) Group Key: part.p_brand, part.p_type, part.p_size -> Sort (cost=491672.02..494596.18 rows=1169665 width=44) (actual time=33547.593..34301.735 rows=2375516 loops=1) Sort Key: part.p_brand, part.p_type, part.p_size Sort Method: quicksort Memory: 285943kB -> Gather (cost=119466.06..373783.45 rows=1169665 width=44) (actual time=6563.283..14142.004 rows=2375516 loops=1) Workers Planned: 2 Workers Launched: 2 -> Hash Join (cost=118466.06..255816.95 rows=1169665 width=44) (actual time=6846.209..13804.511 rows=791839 lo ops=3) Hash Cond: (partsupp.ps_partkey = part.p_partkey) -> Parallel Seq Scan on partsupp (cost=2959.85..122937.51 rows=3333234 width=16) (actual time=245.699..4 741.889 rows=5330507 loops=3) Filter: (NOT (hashed SubPlan 1)) Rows Removed by Filter: 2827 SubPlan 1 -> Seq Scan on supplier (cost=0.00..2959.80 rows=20 width=4) (actual time=0.474..245.544 rows=10 6 loops=3) Filter: ((s_comment)::text ~~ '%Customer%Complaints%'::text) Rows Removed by Filter: 199894 -> Hash (cost=108195.63..108195.63 rows=584847 width=40) (actual time=6599.562..6599.562 rows=594194 loo ps=3) Buckets: 1048576 Batches: 1 Memory Usage: 51237kB -> Seq Scan on part (cost=0.00..108195.63 rows=584847 width=40) (actual time=0.062..6067.231 rows= 594194 loops=3) 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: 3405806 Planning time: 1.100 ms Execution time: 37281.209 ms (28 rows)