Insulate linear descendant queries
There are two problems that can occur with the linear descendant queries:
- The PG optimizer can produce an awful plan #339434 (closed) (comment 748505298)
- The queries can produce unexpected results when combined with limit and offset !74606 (merged).
The solution for both of these cases is to wrap the descendant query in to a CTE where:
The PG optimizer will optimize the CTE as an opaque black box separate from the main query. Any limit or offset will be contained within the CTE rather than merging with the main query.
Edited by Alex Pooley