Re: function(contants) evaluated for every row

From: Michael Glaesemann <grzm(at)seespotcode(dot)net>
To: Marti Raudsepp <marti(at)juffo(dot)org>
Cc: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>, Bruce Momjian <bruce(at)momjian(dot)us>, PostgreSQL-development <pgsql-hackers(at)postgresql(dot)org>
Subject: Re: function(contants) evaluated for every row
Date: 2010-11-25 00:20:37
Message-ID: E05E70E4-721E-4A3A-8DEC-7FF9ADA5A74E@seespotcode.net
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers


On Nov 24, 2010, at 15:28 , Marti Raudsepp wrote:

> On Wed, Nov 24, 2010 at 21:52, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
>> Bruce Momjian <bruce(at)momjian(dot)us> writes:
>>> Notice the to_date()'s were not converted to constants in EXPLAIN so
>>> they are evaluated for every row. to_date() is marked STABLE.
>
>> No. This is per expectation. Only IMMUTABLE functions can be folded to
>> constants in advance of the query.
>
> This is something that has bit me in the past.
>
> I realize that STABLE functions cannot be constant-folded at
> planning-time. But are there good reasons why it cannot called only
> once at execution-time?
>
> As long as *only* STABLE or IMMUTABLE functions are used in a query,
> we can assume that settings like timezone won't change in the middle
> of the execution of a function, thus STABLE function calls can be
> collapsed -- right?

I've seen this as well be a performance issue, in particular with partitioned tables. Out of habit I now write functions that always cache the value of the function in a variable and use the variable in the actual query to avoid this particular "gotcha".

Michael Glaesemann
grzm seespotcode net

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Bruce Momjian 2010-11-25 00:30:56 Re: ALTER TABLE ... IF EXISTS feature?
Previous Message Bruce Momjian 2010-11-24 23:42:00 pgsql: Document that a CHECKPOINT before taking a file system snapshot