plan of original query - looks identical at quick glance Limit (cost=16707.17..149356.52 rows=76 width=921) (actual time=170689.101..172503.304 rows=10 loops=1) -> Nested Loop Semi Join (cost=16707.17..48683307.80 rows=27883 width=921) (actual time=170689.099..172503.290 rows=10 loops=1) Join Filter: (public.bugtask.id = public.bugtask.id) -> Nested Loop (cost=0.00..18311341.12 rows=55765 width=921) (actual time=727.239..8917.468 rows=44262 loops=1) -> Index Scan using bugtask__datecreated__idx on bugtask (cost=0.00..99933.00 rows=73926 width=161) (actual time=397.501..5236.896 rows=152188 loops=1) Filter: ((importance = 5) AND (distribution = 1) AND (status = 10)) -> Index Scan using bug_pkey on bug (cost=0.00..246.33 rows=1 width=760) (actual time=0.022..0.023 rows=0 loops=152188) Index Cond: (bug.id = public.bugtask.bug) Filter: ((bug.duplicateof IS NULL) AND ((NOT bug.private) OR (SubPlan 2))) SubPlan 2 -> HashAggregate (cost=244.40..244.46 rows=6 width=4) (actual time=0.346..0.346 rows=1 loops=3132) CTE teams -> Index Scan using teamparticipation_person_idx on teamparticipation (cost=0.00..178.02 rows=132 width=4) (actual time=0.135..4.342 rows=142 loops=1) Index Cond: (person = 2) -> Append (cost=4.29..66.36 rows=6 width=4) (actual time=0.150..0.335 rows=1 loops=3132) -> Hash Semi Join (cost=4.29..8.68 rows=1 width=4) (actual time=0.091..0.099 rows=1 loops=3132) Hash Cond: (bugsubscription.person = teams.team) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..4.39 rows=3 width=8) (actual time=0.071..0.089 rows=4 loops=3132) Index Cond: (bug = $1) -> Hash (cost=2.64..2.64 rows=132 width=4) (actual time=4.526..4.526 rows=142 loops=1) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (actual time=0.142..4.441 rows=142 loops=1) -> Nested Loop Semi Join (cost=0.00..8.23 rows=1 width=4) (actual time=0.087..0.087 rows=0 loops=3132) Join Filter: (public.bugtask.assignee = teams.team) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..3.94 rows=1 width=8) (actual time=0.025..0.025 rows=1 loops=3132) Index Cond: (bug = $1) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (actual time=0.001..0.029 rows=142 loops=3261) -> Result (cost=0.00..12.11 rows=1 width=4) (actual time=0.022..0.022 rows=0 loops=3132) One-Time Filter: ($2 IS FALSE) -> Nested Loop Semi Join (cost=0.00..12.11 rows=1 width=4) (actual time=0.019..0.019 rows=0 loops=3107) Join Filter: (public.product.owner = teams.team) -> Nested Loop (cost=0.00..7.82 rows=1 width=8) (actual time=0.016..0.017 rows=0 loops=3107) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..3.94 rows=1 width=8) (actual time=0.010..0.010 rows=1 loops=3107) Index Cond: (bug = $1) -> Index Scan using product_pkey on product (cost=0.00..3.87 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=3231) Index Cond: (public.product.id = public.bugtask.product) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (actual time=0.000..0.036 rows=110 loops=30) -> Result (cost=0.00..12.11 rows=1 width=4) (actual time=0.015..0.015 rows=0 loops=3132) One-Time Filter: ($2 IS FALSE) -> Nested Loop Semi Join (cost=0.00..12.11 rows=1 width=4) (actual time=0.013..0.013 rows=0 loops=3107) Join Filter: (productseries.owner = teams.team) -> Nested Loop (cost=0.00..7.82 rows=1 width=8) (actual time=0.012..0.012 rows=0 loops=3107) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..3.94 rows=1 width=8) (actual time=0.006..0.007 rows=1 loops=3107) Index Cond: (bug = $1) -> Index Scan using productseries_pkey on productseries (cost=0.00..3.87 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=3231) Index Cond: (productseries.id = public.bugtask.productseries) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (never executed) -> Result (cost=0.00..12.11 rows=1 width=4) (actual time=0.080..0.080 rows=0 loops=3132) One-Time Filter: ($2 IS FALSE) -> Nested Loop Semi Join (cost=0.00..12.11 rows=1 width=4) (actual time=0.079..0.079 rows=0 loops=3107) Join Filter: (public.distribution.owner = teams.team) -> Nested Loop (cost=0.00..7.82 rows=1 width=8) (actual time=0.026..0.027 rows=1 loops=3107) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..3.94 rows=1 width=8) (actual time=0.006..0.007 rows=1 loops=3107) Index Cond: (bug = $1) -> Index Scan using distribution_pkey on distribution (cost=0.00..3.87 rows=1 width=8) (actual time=0.013..0.014 rows=1 loops=3231) Index Cond: (public.distribution.id = public.bugtask.distribution) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (actual time=0.001..0.023 rows=142 loops=3144) -> Result (cost=0.00..13.06 rows=1 width=4) (actual time=0.023..0.023 rows=0 loops=3132) One-Time Filter: ($2 IS FALSE) -> Nested Loop Semi Join (cost=0.00..13.06 rows=1 width=4) (actual time=0.022..0.022 rows=0 loops=3107) Join Filter: (public.distribution.owner = teams.team) -> Nested Loop (cost=0.00..8.77 rows=1 width=8) (actual time=0.019..0.019 rows=0 loops=3107) -> Nested Loop (cost=0.00..7.82 rows=1 width=8) (actual time=0.014..0.014 rows=0 loops=3107) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..3.94 rows=1 width=8) (actual time=0.008..0.009 rows=1 loops=3107) Index Cond: (bug = $1) -> Index Scan using distrorelease_pkey on distroseries (cost=0.00..3.87 rows=1 width=8) (actual time=0.001..0.001 rows=0 loops=3231) Index Cond: (distroseries.id = public.bugtask.distroseries) -> Index Scan using distribution_pkey on distribution (cost=0.00..0.94 rows=1 width=8) (actual time=0.002..0.003 rows=1 loops=57) Index Cond: (public.distribution.id = distroseries.distribution) -> CTE Scan on teams (cost=0.00..2.64 rows=132 width=4) (actual time=0.000..0.020 rows=142 loops=57) -> Materialize (cost=16707.17..16949.10 rows=24193 width=4) (actual time=0.000..1.526 rows=12163 loops=44262) -> Append (cost=2.48..16682.98 rows=24193 width=4) (actual time=0.198..56.975 rows=12165 loops=1) -> Nested Loop (cost=2.48..99.60 rows=11 width=4) (actual time=0.040..0.040 rows=0 loops=1) -> Index Scan using product__bug_supervisor__idx on product (cost=0.00..3.87 rows=1 width=4) (actual time=0.039..0.039 rows=0 loops=1) Index Cond: (bug_supervisor = 343381) -> Bitmap Heap Scan on bugtask (cost=2.48..95.09 rows=52 width=8) (never executed) Recheck Cond: (public.bugtask.product = public.product.id) -> Bitmap Index Scan on bugtask__product__bug__key (cost=0.00..2.47 rows=52 width=0) (never executed) Index Cond: (public.bugtask.product = public.product.id) -> Nested Loop (cost=3.50..922.99 rows=6651 width=4) (actual time=0.156..54.746 rows=12165 loops=1) -> Bitmap Heap Scan on structuralsubscription (cost=3.50..185.88 rows=187 width=8) (actual time=0.094..0.585 rows=187 loops=1) Recheck Cond: (subscriber = 343381) -> Bitmap Index Scan on structuralsubscription__subscriber__idx (cost=0.00..3.45 rows=187 width=0) (actual time=0.074..0.074 rows=188 loops=1) Index Cond: (subscriber = 343381) -> Index Scan using bugtask__distribution_sourcepackage__heat__idx on bugtask (cost=0.00..3.93 rows=1 width=12) (actual time=0.018..0.275 rows=65 loops=187) Index Cond: ((public.bugtask.distribution = structuralsubscription.distribution) AND (public.bugtask.sourcepackagename = structuralsubscription.sourcepackagename)) -> Nested Loop (cost=0.00..15418.46 rows=17531 width=4) (actual time=0.060..0.060 rows=0 loops=1) -> Index Scan using distribution__bug_supervisor__idx on distribution (cost=0.00..3.87 rows=1 width=4) (actual time=0.060..0.060 rows=0 loops=1) Index Cond: (bug_supervisor = 343381) -> Index Scan using bugtask__distribution_sourcepackage__heat__idx on bugtask (cost=0.00..15168.06 rows=19723 width=8) (never executed) Index Cond: (public.bugtask.distribution = public.distribution.id) Total runtime: 172504.603 ms