pushing order by + limit to union subqueries - Mailing list pgsql-performance

From Paolo Losi
Subject pushing order by + limit to union subqueries
Date
Msg-id CAP=2L=FRza_catqP9LRfJOMybzoorpxSBxSP=J1o9WZnFp1USg@mail.gmail.com
Whole thread Raw
Responses Re: pushing order by + limit to union subqueries
List pgsql-performance
Hi all,
 I've noticed that order by / limit are not distributed to union subqueries
by the planner:

Example:

q1: (select * from t1) union all (select * from t2) order by x limit 10;
q2: (select * from t1 order by x limit 10) union all (select * from t2 order by x limit 10)
      order by x limit 10;

both queries should be equivalent, but the planner provides hugely different
plans. I was expecting that the planner could rewrite the first to the second.
Am I overlooking something? If this is the case, can anyone explain why this
optimization is not performed?

Thanks!
Paolo

pgsql-performance by date:

Previous
From: Josh Berkus
Date:
Subject: Bad cost estimate with FALSE filter condition
Next
From: Tom Lane
Date:
Subject: Re: pushing order by + limit to union subqueries