This article is half-done without your Comment! *** Please share your thoughts via Comment ***
While creating a function, You can define the attributes like IMMUTABLE, STABLE, VOLATILE and COST. These attributes inform the query optimizer about the behaviour of the function.
IMMUTABLE: It indicates that the function cannot modify the database and always returns the same result when given the same argument values; that is, it does not do database lookups or otherwise use information not directly present in its argument list.
STABLE: It indicates that the function cannot modify the database, and that within a single table scan it will consistently return the same result for the same argument values, but that its result could change across SQL statements. This is the appropriate selection for functions whose results depend on database lookups, parameter variables (such as the current time zone), etc.
VOLATILE: It indicates that the function value can change even within a single table scan so that no optimizations can make.
COST: It declares the cost per row of the result, which is used by the query planner to find the cheapest plan. The default is COST 100. COST measured in CPU cycles. A higher COST number means more costly. A COST will give the planner a better idea of which strategy to use.
1 2 3 4 5 6 7 8 9 10 |
CREATE FUNCTION fn_test() RETURNS as $$ declare -- declare variables... begin -- do operations... end $$ language plpgsql VOLATILE COST 10000; |
Leave a Reply