Adam Brusselback <adambrusselback@xxxxxxxxx> writes: > It would be nice if Postgres optimized this case though because it is > really really common from what i've seen. Since the introduction of the "planner support function" infrastructure, it'd be possible to do this without it being a completely ugly kluge: we could put the logic for it into a planner support function attached to count(any). Currently planner support functions are only called for regular functions, but we could certainly envision adding the ability to do it for aggregates (and window functions too, why not). I'm not particularly planning to do that myself, but if someone else wants to write a patch, have at it. regards, tom lane