So, we get this plan with the flag enabled: explain SELECT COUNT(*) FROM ((SELECT BugTask.assignee, BugTask.bug, BugTask.bugwatch, BugTask.date_assigned, BugTask.date_closed, BugTask.date_confirmed, BugTask.date_fix_committed, BugTask.date_fix_released, BugTask .date_incomplete, BugTask.date_inprogress, BugTask.date_left_closed, BugTask.date_left_new, BugTask.date_triaged, BugTask.datecreated, BugTask.distribution, BugTask.distroseries, BugTask.heat, BugTask.id, BugTask.importance, BugTask.milestone, BugTask.owner, BugTask.product, BugTask.productseries, BugTask.sourcepackagename, BugTask.status, BugTask.statusexplanation, BugTask.targetnamecache FROM BugTask LEFT JOIN Product ON BugTask.product = Product.id AND Product.active, Bug WHERE Bug.id = BugTask.bug AND BugTask.assignee = 100 AND ((BugTask.status = 10) OR (BugTask.status = 15) OR (BugTask.status = 20) OR (BugTask.status = 21) OR (BugTask.status = 22) OR (BugTask.status = 25)) AND Bug.duplicateof is NULL AND (Bugtask.product IS NULL OR Product.active = TRUE) AND (Bug.private = FALSE OR EXISTS ( SELECT BugSubscription.bug FROM BugSubscription, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugSubscription.person AND BugSubscription.bug = Bug.id UNION SELECT BugTask.bug FROM BugTask, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugTask.assignee AND BugTask.bug = Bug.id )) ) UNION (SELECT BugTask.assignee, BugTask.bug, BugTask.bugwatch, BugTask.date_assigned, BugTask.date_closed, BugTask.date_confirmed, BugTask.date_fix_committed, BugTask.date_fix_released, BugTask.date_incomplete, BugTask.date_inprogress, BugTask.date_left_closed, BugTask.date_left_new, BugTask.date_triaged, BugTask.datecreated, BugTask.distribution, BugTask.distroseries, BugTask.heat, BugTask.id, BugTask.importance, BugTask.milestone, BugTask.owner, BugTask.product, BugTask.productseries, BugTask.sourcepackagename, BugTask.status, BugTask.statusexplanation, BugTask.targetnamecache FROM BugTask LEFT JOIN Product ON BugTask.product = Product.id AND Product.active, Bug, BugSubscription WHERE Bug.id = BugTask.bug AND ((BugTask.status = 10) OR (BugTask.status = 15) OR (BugTask.status = 20) OR (BugTask.status = 21) OR (BugTask.status = 22) OR (BugTask.status = 25)) AND Bug.duplicateof is NULL AND Bug.id = BugSubscription.bug AND BugSubscription.person = 100 AND (Bugtask.product IS NULL OR Product.active = TRUE) AND (Bug.private = FALSE OR EXISTS ( SELECT BugSubscription.bug FROM BugSubscription, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugSubscription.person AND BugSubscription.bug = Bug.id UNION SELECT BugTask.bug FROM BugTask, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugTask.assignee AND BugTask.bug = Bug.id )) ) UNION (SELECT BugTask.assignee, BugTask.bug, BugTask.bugwatch, BugTask.date_assigned, BugTask.date_closed, BugTask.date_confirmed, BugTask.date_fix_committed, BugTask.date_fix_released, BugTask.date_incomplete, BugTask.date_inprogress, BugTask.date_left_closed, BugTask.date_left_new, BugTask.date_triaged, BugTask.datecreated, BugTask.distribution, BugTask.distroseries, BugTask.heat, BugTask.id, BugTask.importance, BugTask.milestone, BugTask.owner, BugTask.product, BugTask.productseries, BugTask.sourcepackagename, BugTask.status, BugTask.statusexplanation, BugTask.targetnamecache FROM BugTask LEFT JOIN Product ON BugTask.product = Product.id AND Product.active, Bug WHERE Bug.id = BugTask.bug AND BugTask.owner = 100 AND ((BugTask.status = 10) OR (BugTask.status = 15) OR (BugTask.status = 20) OR (BugTask.status = 21) OR (BugTask.status = 22) OR (BugTask.status = 25)) AND Bug.duplicateof is NULL AND (Bugtask.product IS NULL OR Product.active = TRUE) AND BugTask.bug = Bug.id AND Bug.owner = 100 AND (Bug.private = FALSE OR EXISTS ( SELECT BugSubscription.bug FROM BugSubscription, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugSubscription.person AND BugSubscription.bug = Bug.id UNION SELECT BugTask.bug FROM BugTask, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugTask.assignee AND BugTask.bug = Bug.id )) ) UNION (SELECT BugTask.assignee, BugTask.bug, BugTask.bugwatch, BugTask.date_assigned, BugTask.date_closed, BugTask.date_confirmed, BugTask.date_fix_committed, BugTask.date_fix_released, BugTask.date_incomplete, BugTask.date_inprogress, BugTask.date_left_closed, BugTask.date_left_new, BugTask.date_triaged, BugTask.datecreated, BugTask.distribution, BugTask.distroseries, BugTask.heat, BugTask.id, BugTask.importance, BugTask.milestone, BugTask.owner, BugTask.product, BugTask.productseries, BugTask.sourcepackagename, BugTask.status, BugTask.statusexplanation, BugTask.targetnamecache FROM BugTask LEFT JOIN Product ON BugTask.product = Product.id AND Product.active, Bug WHERE Bug.id = BugTask.bug AND ((BugTask.status = 10) OR (BugTask.status = 15) OR (BugTask.status = 20) OR (BugTask.status = 21) OR (BugTask.status = 22) OR (BugTask.status = 25)) AND Bug.duplicateof is NULL AND (Bugtask.product IS NULL OR Product.active = TRUE) AND EXISTS (SELECT True FROM BugMessage WHERE Bug.id = BugMessage.bug AND BugMessage.index > 0 AND BugMessage.owner = 100) AND (Bug.private = FALSE OR EXISTS ( SELECT BugSubscription.bug FROM BugSubscription, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugSubscription.person AND BugSubscription.bug = Bug.id UNION SELECT BugTask.bug FROM BugTask, TeamParticipation WHERE TeamParticipation.person = 2 AND TeamParticipation.team = BugTask.assignee AND BugTask.bug = Bug.id )) )) AS BugTask JOIN Bug ON BugTask.bug = Bug.id; QUERY PLAN ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ Aggregate (cost=1234731.14..1234731.15 rows=1 width=0) -> Hash Join (cost=1210833.58..1234247.22 rows=193566 width=0) Hash Cond: (bugtask.bug = public.bug.id) -> Subquery Scan bugtask (cost=554361.59..569846.87 rows=193566 width=4) -> Unique (cost=554361.59..567911.21 rows=193566 width=280) -> Sort (cost=554361.59..554845.50 rows=193566 width=280) Sort Key: public.bugtask.assignee, public.bugtask.bug, public.bugtask.bugwatch, public.bugtask.date_assigned, public.bugtask.date_closed, public.bugtask.date_confirmed, public.bugtask.date_fix_committed, public.bugtask.date_fix_released, public.bugtask.date_incomplete, public.bugtask.date_inprogress, public.bugtask.date_left_closed, public.bugtask.date_left_new, public.bugtask.date_triaged, public.bugtask.datecreated, public.bugtask.distribution, public.bugtask.distroseries, public.bugtask.heat, public.bugtask.id, public.bugtask.importance, public.bugtask.milestone, public.bugtask.owner, public.bugtask.product, public.bugtask.productseries, public.bugtask.sourcepackagename, public.bugtask.status, public.bugtask.statusexplanation, public.bugtask.targetnamecache -> Append (cost=3193.87..515812.11 rows=193566 width=280) -> Nested Loop (cost=3193.87..73534.16 rows=1303 width=280) -> Hash Left Join (cost=3193.87..13694.69 rows=1303 width=280) Hash Cond: (public.bugtask.product = public.product.id) Filter: ((public.bugtask.product IS NULL) OR public.product.active) -> Bitmap Heap Scan on bugtask (cost=144.10..10627.43 rows=1429 width=280) Recheck Cond: (assignee = 100) Filter: ((status = 10) OR (status = 15) OR (status = 20) OR (status = 21) OR (status = 22) OR (status = 25)) -> Bitmap Index Scan on bugtask__assignee__idx (cost=0.00..143.74 rows=4304 width=0) Index Cond: (assignee = 100) -> Hash (cost=2776.19..2776.19 rows=21886 width=5) -> Seq Scan on product (cost=0.00..2776.19 rows=21886 width=5) Filter: active -> Index Scan using bug_pkey on bug (cost=0.00..45.91 rows=1 width=4) Index Cond: (public.bug.id = public.bugtask.bug) Filter: ((public.bug.duplicateof IS NULL) AND ((NOT public.bug.private) OR (SubPlan 4))) SubPlan 4 -> Unique (cost=39.33..39.34 rows=2 width=4) -> Sort (cost=39.33..39.33 rows=2 width=4) Sort Key: public.bugsubscription.bug -> Append (cost=0.00..39.32 rows=2 width=4) -> Nested Loop (cost=0.00..26.48 rows=1 width=4) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..7.26 rows=3 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..12.82 rows=1 width=4) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.42 rows=1 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugtask.assignee) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..98481.42 rows=1499 width=280) -> Nested Loop Left Join (cost=0.00..29671.05 rows=1499 width=284) Filter: ((public.bugtask.product IS NULL) OR public.product.active) -> Nested Loop (cost=0.00..29160.88 rows=1644 width=284) -> Index Scan using bugsubscription_person_idx on bugsubscription (cost=0.00..7478.40 rows=3945 width=4) Index Cond: (person = 100) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..5.48 rows=1 width=280) Index Cond: (public.bugtask.bug = public.bugsubscription.bug) Filter: ((public.bugtask.status = 10) OR (public.bugtask.status = 15) OR (public.bugtask.status = 20) OR (public.bugtask.status = 21) OR (public.bugtask.status = 22) OR (public.bugtask.status = 25)) -> Index Scan using product_pkey on product (cost=0.00..0.30 rows=1 width=5) Index Cond: (public.bugtask.product = public.product.id) Filter: public.product.active -> Index Scan using bug_pkey on bug (cost=0.00..45.89 rows=1 width=4) Index Cond: (public.bug.id = public.bugtask.bug) Filter: ((public.bug.duplicateof IS NULL) AND ((NOT public.bug.private) OR (SubPlan 3))) SubPlan 3 -> Unique (cost=39.33..39.34 rows=2 width=4) -> Sort (cost=39.33..39.33 rows=2 width=4) Sort Key: public.bugsubscription.bug -> Append (cost=0.00..39.32 rows=2 width=4) -> Nested Loop (cost=0.00..26.48 rows=1 width=4) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..7.26 rows=3 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..12.82 rows=1 width=4) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.42 rows=1 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugtask.assignee) AND (public.teamparticipation.person = 2)) -> Nested Loop Left Join (cost=0.00..49419.31 rows=711 width=280) Filter: ((public.bugtask.product IS NULL) OR public.product.active) -> Nested Loop (cost=0.00..47308.54 rows=780 width=280) -> Index Scan using bug_owner_idx on bug (cost=0.00..42464.09 rows=780 width=4) Index Cond: (owner = 100) Filter: ((duplicateof IS NULL) AND ((NOT private) OR (SubPlan 2))) SubPlan 2 -> Unique (cost=39.33..39.34 rows=2 width=4) -> Sort (cost=39.33..39.33 rows=2 width=4) Sort Key: public.bugsubscription.bug -> Append (cost=0.00..39.32 rows=2 width=4) -> Nested Loop (cost=0.00..26.48 rows=1 width=4) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..7.26 rows=3 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..12.82 rows=1 width=4) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.42 rows=1 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugtask.assignee) AND (public.teamparticipation.person = 2)) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.20 rows=1 width=280) Index Cond: (public.bugtask.bug = public.bug.id) Filter: ((public.bugtask.owner = 100) AND ((public.bugtask.status = 10) OR (public.bugtask.status = 15) OR (public.bugtask.status = 20) OR (public.bugtask.status = 21) OR (public.bugtask.status = 22) OR (public.bugtask.status = 25))) -> Index Scan using product_pkey on product (cost=0.00..2.69 rows=1 width=5) Index Cond: (public.bugtask.product = public.product.id) Filter: public.product.active -> Hash Join (cost=216449.03..292441.56 rows=190053 width=280) Hash Cond: (public.bugtask.bug = public.bug.id) -> Hash Left Join (cost=13260.49..88189.71 rows=251706 width=280) Hash Cond: (public.bugtask.product = public.product.id) Filter: ((public.bugtask.product IS NULL) OR public.product.active) -> Bitmap Heap Scan on bugtask (cost=10210.72..81759.72 rows=276144 width=280) Recheck Cond: ((status = 10) OR (status = 15) OR (status = 20) OR (status = 21) OR (status = 22) OR (status = 25)) -> BitmapOr (cost=10210.72..10210.72 rows=308640 width=0) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..5843.19 rows=184232 width=0) Index Cond: (status = 10) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..565.49 rows=17739 width=0) Index Cond: (status = 15) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..1792.13 rows=56490 width=0) Index Cond: (status = 20) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..959.11 rows=30221 width=0) Index Cond: (status = 21) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..169.28 rows=5311 width=0) Index Cond: (status = 22) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..467.30 rows=14647 width=0) Index Cond: (status = 25) -> Hash (cost=2776.19..2776.19 rows=21886 width=5) -> Seq Scan on product (cost=0.00..2776.19 rows=21886 width=5) ...skipping... Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..12.82 rows=1 width=4) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.42 rows=1 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugtask.assignee) AND (public.teamparticipation.person = 2)) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.20 rows=1 width=280) Index Cond: (public.bugtask.bug = public.bug.id) Filter: ((public.bugtask.owner = 100) AND ((public.bugtask.status = 10) OR (public.bugtask.status = 15) OR (public.bugtask.status = 20) OR (public.bugtask.status = 21) OR (public.bugtask.status = 22) OR (public.bugtask.status = 25))) -> Index Scan using product_pkey on product (cost=0.00..2.69 rows=1 width=5) Index Cond: (public.bugtask.product = public.product.id) Filter: public.product.active -> Hash Join (cost=216449.03..292441.56 rows=190053 width=280) Hash Cond: (public.bugtask.bug = public.bug.id) -> Hash Left Join (cost=13260.49..88189.71 rows=251706 width=280) Hash Cond: (public.bugtask.product = public.product.id) Filter: ((public.bugtask.product IS NULL) OR public.product.active) -> Bitmap Heap Scan on bugtask (cost=10210.72..81759.72 rows=276144 width=280) Recheck Cond: ((status = 10) OR (status = 15) OR (status = 20) OR (status = 21) OR (status = 22) OR (status = 25)) -> BitmapOr (cost=10210.72..10210.72 rows=308640 width=0) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..5843.19 rows=184232 width=0) Index Cond: (status = 10) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..565.49 rows=17739 width=0) Index Cond: (status = 15) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..1792.13 rows=56490 width=0) Index Cond: (status = 20) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..959.11 rows=30221 width=0) Index Cond: (status = 21) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..169.28 rows=5311 width=0) Index Cond: (status = 22) -> Bitmap Index Scan on bugtask__status__idx (cost=0.00..467.30 rows=14647 width=0) Index Cond: (status = 25) -> Hash (cost=2776.19..2776.19 rows=21886 width=5) -> Seq Scan on product (cost=0.00..2776.19 rows=21886 width=5) Filter: active -> Hash (cost=202859.04..202859.04 rows=26360 width=8) -> Nested Loop (cost=35978.83..202859.04 rows=26360 width=8) -> HashAggregate (cost=35978.83..36016.24 rows=3741 width=4) -> Bitmap Heap Scan on bugmessage (cost=597.90..35912.93 rows=26360 width=4) Recheck Cond: ((owner = 100) AND (index > 0)) -> Bitmap Index Scan on bugmessage__owner__index__idx (cost=0.00..591.31 rows=26360 width=0) Index Cond: ((owner = 100) AND (index > 0)) -> Index Scan using bug_pkey on bug (cost=0.00..44.59 rows=1 width=4) Index Cond: (public.bug.id = bugmessage.bug) Filter: ((public.bug.duplicateof IS NULL) AND ((NOT public.bug.private) OR (SubPlan 1))) SubPlan 1 -> Unique (cost=39.33..39.34 rows=2 width=4) -> Sort (cost=39.33..39.33 rows=2 width=4) Sort Key: public.bugsubscription.bug -> Append (cost=0.00..39.32 rows=2 width=4) -> Nested Loop (cost=0.00..26.48 rows=1 width=4) -> Index Scan using bugsubscription_bug_idx on bugsubscription (cost=0.00..7.26 rows=3 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugsubscription.person) AND (public.teamparticipation.person = 2)) -> Nested Loop (cost=0.00..12.82 rows=1 width=4) -> Index Scan using bugtask__bug__idx on bugtask (cost=0.00..6.42 rows=1 width=8) Index Cond: (bug = $0) -> Index Scan using teamparticipation_team_key on teamparticipation (cost=0.00..6.39 rows=1 width=4) Index Cond: ((public.teamparticipation.team = public.bugtask.assignee) AND (public.teamparticipation.person = 2)) -> Hash (cost=644777.55..644777.55 rows=712755 width=4) -> Seq Scan on bug (cost=0.00..644777.55 rows=712755 width=4)