Bernard Frankpitt <frankpit@pop.dn.net> writes:
> The solution that I propose is to include code in the optimizer that
> picks functions with constant arguments out of a qualification
> clause, and evaluates them.
This is something I had on my own to-do list, and I'm glad to see
someone beat me to it. But you've only done half the job: you
should also be folding operators with constant arguments.
Also, you need to be wary of functions like now() and random().
There probably isn't any other way to handle these than to add a
column to pg_proc flagging functions that can't be constant-folded.
> Finally, there is the question of where in the planner should the
> early evaluation occur. It is not obvious to me where the best point
> is, I chose to put it in
> plan/initsplan.c:add_restrict_and_join_to_rels().
I believe it would be best to do it considerably earlier, specifically,
before cnfify(). It might even be worth running the code twice,
once before and once after cnfify.
Also, probably we should apply it to the targetlist as well as the qual.
regards, tom lane