Re: Avoiding execution of some functions by query rewriting

From: Tom Lane <tgl(at)sss(dot)pgh(dot)pa(dot)us>
To: Thomas Girault <toma(dot)girault(at)gmail(dot)com>
Cc: pgsql-hackers(at)postgresql(dot)org
Subject: Re: Avoiding execution of some functions by query rewriting
Date: 2012-05-16 12:35:51
Message-ID: 22457.1337171751@sss.pgh.pa.us
Views: Raw Message | Whole Thread | Download mbox | Resend email
Thread:
Lists: pgsql-hackers

Thomas Girault <toma(dot)girault(at)gmail(dot)com> writes:
> Hello,
> I would like to allow the execution of a function (my_function) only if its
> argument (my_table.x) belongs to a predefined interval (e.g. [100,1000]).

> Let's take the following query example :
> (q) SELECT * FROM my_table WHERE my_function(mytable.x);

> I would like this query automatically rewrites itself to check whether
> "mytable.x" belong to the interval [100,1000] :
> (q') SELECT * FROM my_table WHERE (my_table.x BETWEEN 100 AND 1000) AND
> my_function(my_table.x);

> The command EXPLAIN ANALYSE shows that the second query is really faster
> than the first one.

> How can I change the query execution plan in order to automate the process
> of query rewriting (q into q') ?
> Where can I store suitably the metadata about the interval [100,1000]
> associated to my_function ?

Surely a quick "if" test added to the top of the function would be
a better solution than trying to hack the system to do this. Keep
in mind that any code you add to the planner for such a thing would
fire on every function call, in every query, all the time. It seems
unlikely that the possible benefit of not entering your function at
all would outweigh the distributed penalty from the added planner
overhead.

regards, tom lane

In response to

Browse pgsql-hackers by date

  From Date Subject
Next Message Sandro Santilli 2012-05-16 12:42:45 Re: Interrupting long external library calls
Previous Message Thomas Girault 2012-05-16 12:30:00 Avoiding execution of some functions by query rewriting