Ken Tanzer <ken.tanzer@gmail.com> writes:
> On Sat, Jul 20, 2019 at 12:25 PM Tom Lane <tgl@sss.pgh.pa.us> wrote:
>> There's been assorted talk about various optimizations to
>> avoid unnecessary duplicate function calls,
> So I had read the sentence below to mean my functions would only get called
> once. But is that sentence only supposed to apply to index scans? Or does
> it mean the planner is allowed to optimize, but it just doesn't know how
> yet?
> A STABLE function cannot modify the database and is guaranteed to return
> the same results given the same arguments for all rows within a single
> statement. *This category allows the optimizer to optimize multiple calls
> of the function to a single call.*
It says "allows", not "requires". But in particular, we've interpreted
that to mean trying to call a stable function (with constant or at least
stable arguments) once per query rather than once per row, as the naive
interpretation of SQL semantics would have us do. Matching up textually
distinct calls has not been on the radar --- it seems fairly expensive
to do, with no return in typical queries, and relatively small return
even if we find a match.
regards, tom lane