QUERY PLAN
Aggregate (cost=105.37..105.38 rows=1 width=8)
-> Sort (cost=105.35..105.36 rows=1 width=224)
Sort Key: s.end_at, s.created_at DESC
InitPlan 1 (returns $0)
-> Aggregate (cost=51.07..51.08 rows=1 width=32)
-> Seq Scan on tbl_tasks s_1 (cost=0.00..51.06 rows=2 width=13)
Filter: ((tree_path IS NOT NULL) AND (head_of = 1001) AND (status = '0'::smallint))
-> Group (cost=54.25..54.26 rows=1 width=224)
Group Key: s.id
-> Sort (cost=54.25..54.26 rows=1 width=18)
Sort Key: s.id
-> Seq Scan on tbl_tasks s (cost=0.00..54.24 rows=1 width=18)
Filter: ((parent_id IS NULL) AND (tree_path @> $0) AND (head_of = 1001) AND (status = '0'::smallint))