Re: Performance problem in textanycat/anytextcat

From: Robert Haas <robertmhaas(at)gmail(dot)com>
To: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Performance problem in textanycat/anytextcat
Date: 2010-05-16 16:35:45
Message-ID: AANLkTilmvGwrBaHKYGRt3_hJqGICnQJU16izvP6H9YCa@mail.gmail.com
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

On Sat, May 15, 2010 at 9:51 PM, Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us> wrote:
> I noticed by accident that there are some cases where the planner fails
> to inline the SQL functions that underlie the || operator for text vs
> non-text cases.  The reason is that these functions are marked
> immutable, but their expansion involves a coercion to text that might
> not be immutable.  The planner will not inline a function if the
> resulting expression would be more volatile than the function claims
> itself to be, because sometimes the point of such a function is to hide
> the expression's volatility.  In this case, however, we don't want to
> hide the true nature of the expression, and we definitely don't want
> to pay the performance price of calling a SQL function.  That price
> is pretty significant, eg on a rather slow machine I get
>
> regression=# select count(localtimestamp || i::text) from generate_series(1,100000) i;
>  count
> --------
>  100000
> (1 row)
>
> Time: 12512.624 ms
> regression=# update pg_proc set provolatile = 'v' where oid = 2004;
> UPDATE 1
> Time: 7.104 ms
> regression=# select count(localtimestamp || i::text) from generate_series(1,100000) i;
>  count
> --------
>  100000
> (1 row)
>
> Time: 4967.086 ms
>
> so the added overhead more than doubles the cost of this case.
>
> There's also a possibility of an outright wrong behavior, since the
> immutable marking will allow the concatenation of two constants to
> be folded to a constant in contexts where perhaps it shouldn't be.
> Continuing the above example, 'now'::timestamp || 'foo' will be folded
> to a constant on sight, which is wrong because the coercion to text
> depends on DateStyle and ought to react to a later change in DateStyle.
>
> So I think that labeling textanycat/anytextcat as immutable was a
> thinko, and we instead ought to label them volatile so that the planner
> can inline them no matter what the behavior of the underlying text cast
> is.

Couldn't you apply this argument to any built-in immutable function whatsoever?

--
Robert Haas
EnterpriseDB: http://www.enterprisedb.com
The Enterprise Postgres Company

In response to

Responses

Browse pgsql-hackers by date

  From Date Subject
Next Message Andrew Dunstan 2010-05-16 17:11:05 Re: pg_upgrade and extra_float_digits
Previous Message Tom Lane 2010-05-16 16:22:33 Re: [PATCH] Add SIGCHLD catch to psql