Re: Function use in query - Mailing list pgsql-general

From David Johnston
Subject Re: Function use in query
Date
Msg-id 1370528625081-5758159.post@n5.nabble.com
Whole thread Raw
In response to Re: Function use in query  (Ioana Danes <ioanasoftware@yahoo.ca>)
Responses Re: Function use in query
List pgsql-general
Ioana Danes wrote
>
> If I will have to filter the  tmp_Cashdrawer table then it executes the
> function for the all the cash drawers and then filter out the result which
> again is not efficient...

Hm????

SELECT function_call(...)
FROM tbl
WHERE tbl.pk = ...;

That should only cause function_call to execute a single time.  If it is not
I'd suggest providing the actual query as well as the "EXPLAIN" plan for it.
It is possible that making the function "cost more" might be necessary but
doubtful.

You can force the table filter to be executed first by using a simple
sub-select:

SELECT function_call(...)
FROM (SELECT * FROM tbl WHERE tbl.pk = ...) filtered_tbl;

Though it is possible the planner would re-arrange this to the simple form
and still cause a problem.  Explain is your friend.

Combine that with the "WITH" if you need to expand the results of
function_call without causing it to execute multiple times - once for each
column being expanded.

David J.






--
View this message in context: http://postgresql.1045698.n5.nabble.com/Function-use-in-query-tp5758051p5758159.html
Sent from the PostgreSQL - general mailing list archive at Nabble.com.


pgsql-general by date:

Previous
From: Ioana Danes
Date:
Subject: Re: Function use in query
Next
From: François Beausoleil
Date:
Subject: User postgres unable to revoke privileges?