![]() | |
![]() |
| | Thread Tools | Display Modes |
#1
| |||
| |||
|
#2
| |||
| |||
|
|
We have the PL/Python function get_foo(n) that returns an array. If this function is declared IMMUTABLE, then: SELECT * FROM tablenamehere WHERE id = any(get_foo(21)); returns quickly. If the function is declared STABLE, however, PostgreSQL seems to incorrectly run it for every row in the table and it takes forever. |
#3
| |||
| |||
|
|
The function we're running returns an array, and it is non-trivial to compute. We do a simple query with it like "SELECT * FROM tablename WHERE id = any(foo(21))"). When the function is STABLE (or VOLATILE) the function is run to generate the array every time. If the function is IMMUTABLE, the array is computed only once for this query, as we'd expect, and the query is fast. |
#4
| |||
| |||
|
|
Oh, you are misunderstanding the point of IMMUTABLE/STABLE. STABLE essentially gives the planner permission to use the function in an indexscan qualification. It does *not* cause any caching of the function result in other contexts, which is what you seem to be wishing would happen. |
#5
| |||
| |||
|
|
Is caching of results for STABLE/IMMUTABLE functions called with a constant something that would be reasonable to add? |
#6
| |||
| |||
|
|
"Jim C. Nasby" <jnasby (AT) pervasive (DOT) com> writes: Is caching of results for STABLE/IMMUTABLE functions called with a constant something that would be reasonable to add? I tend to think not: the distributed overhead would outweigh the advantages. See previous discussions in the archives. (However, the previous discussions have imagined an explicit cache that stores function names, arguments, and results. Your thought of transforming the call into an InitPlan node might be interesting. The trick is still to know which functions are expensive enough to justify the overhead of an InitPlan.) |
![]() |
| Thread Tools | |
| Display Modes | |
| |