optimizer: havingの最適化

subquery_plannerで,havingの条件をwhereに移動する最適化を実行している。(planner.c:294-346行目)
以下の3パターンがある。

(1)where句に移動できないケース
having句に指定したカラムが,集約関数やvolatile関数だったりする場合。
集約関数などの実行結果をフィルタするため,where句へ移動できない。

select bid, count(*) from accounts
group by bid
having count(*) < 10

(2)where句に移動できるケース
having句に指定したカラムがgroup by句にもある場合は,where句に移動できる。
集約計算を実行するレコードを減らせるため性能がよくなる。

変更前

select bid, count(*) from accounts
group by bid
having bid < 10

変更後

select bid, count(*) from accounts
where bid < 10
group by bid

(3)where句にコピーしてhavingにもキープするケース
group by句がなくて,havingを使用する場合。
これは特殊なケースか?現実的なSQLが思いつかない。
以下のようなSQLがこのケースに当てはまる。

変更前

select (select max(bid) from branches)
having (select max(bid) from branches) != 0

変更後

select (select max(bid) from branches)
where (select max(bid) from branches) != 0
having (select max(bid) from branches) != 0