<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>IMMUTABLE &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/immutable/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.dbrnd.com</link>
	<description>BIGData &#124; NoSQL &#124; MSSQL &#124; MySQL &#124; PostgreSQL</description>
	<lastBuildDate>Mon, 21 Jan 2019 07:17:21 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0.4</generator>
	<item>
		<title>PostgreSQL: What is IMMUTABLE, STABLE, VOLATILE and COST of Function</title>
		<link>https://www.dbrnd.com/2019/02/postgresql-what-is-immutable-stable-volatile-and-cost-of-function/</link>
					<comments>https://www.dbrnd.com/2019/02/postgresql-what-is-immutable-stable-volatile-and-cost-of-function/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 18 Feb 2019 16:16:06 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[COST]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Function Attribute]]></category>
		<category><![CDATA[IMMUTABLE]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[STABLE]]></category>
		<category><![CDATA[VOLATILE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=4996</guid>

					<description><![CDATA[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 [&#8230;]]]></description>
										<content:encoded><![CDATA[<p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&amp;linkname=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2019%2F02%2Fpostgresql-what-is-immutable-stable-volatile-and-cost-of-function%2F&#038;title=PostgreSQL%3A%20What%20is%20IMMUTABLE%2C%20STABLE%2C%20VOLATILE%20and%20COST%20of%20Function" data-a2a-url="https://www.dbrnd.com/2019/02/postgresql-what-is-immutable-stable-volatile-and-cost-of-function/" data-a2a-title="PostgreSQL: What is IMMUTABLE, STABLE, VOLATILE and COST of Function"></a></p><p>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.</p>
<blockquote><p><strong>IMMUTABLE:</strong> 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.</p></blockquote>
<blockquote><p><strong>STABLE:</strong> 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.</p></blockquote>
<blockquote><p><strong>VOLATILE:</strong> It indicates that the function value can change even within a single table scan so that no optimizations can make.</p></blockquote>
<blockquote><p><strong>COST:</strong> 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.</p></blockquote>
<pre class="crayon-plain-tag">CREATE FUNCTION fn_test()
  RETURNS as
$$
declare
	-- declare variables...
begin	
	-- do operations...	
end
$$ language plpgsql VOLATILE
COST 10000;</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2019/02/postgresql-what-is-immutable-stable-volatile-and-cost-of-function/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>PostgreSQL: Optimize the Function by defining Volatility Classification</title>
		<link>https://www.dbrnd.com/2018/12/postgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable/</link>
					<comments>https://www.dbrnd.com/2018/12/postgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable/#respond</comments>
		
		<dc:creator><![CDATA[Anvesh Patel]]></dc:creator>
		<pubDate>Mon, 31 Dec 2018 16:16:24 +0000</pubDate>
				<category><![CDATA[PostgreSQL]]></category>
		<category><![CDATA[Anvesh Patel]]></category>
		<category><![CDATA[database]]></category>
		<category><![CDATA[database research and development]]></category>
		<category><![CDATA[dbrnd]]></category>
		<category><![CDATA[Function Optimization]]></category>
		<category><![CDATA[Function Volatility]]></category>
		<category><![CDATA[IMMUTABLE]]></category>
		<category><![CDATA[plpgsql]]></category>
		<category><![CDATA[Postgres Query]]></category>
		<category><![CDATA[postgresql]]></category>
		<category><![CDATA[PostgreSQL Administrator]]></category>
		<category><![CDATA[PostgreSQL Error]]></category>
		<category><![CDATA[PostgreSQL Monitoring]]></category>
		<category><![CDATA[PostgreSQL Performance Tuning]]></category>
		<category><![CDATA[PostgreSQL Programming]]></category>
		<category><![CDATA[PostgreSQL Tips and Tricks]]></category>
		<category><![CDATA[STABLE]]></category>
		<category><![CDATA[VOLATILE]]></category>
		<guid isPermaLink="false">https://www.dbrnd.com/?p=7273</guid>

					<description><![CDATA[<div><img width="679" height="186" src="https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-679x186.jpg" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" fetchpriority="high" srcset="https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-679x186.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1030x282.jpg 1030w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-850x233.jpg 850w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1700x466.jpg 1700w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1358x372.jpg 1358w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function.jpg 2000w" sizes="(max-width: 679px) 100vw, 679px" /></div>This is an important article for PostgreSQL Developer because many of the database developers don&#8217;t know about the PostgreSQL Function Volatility. Every function has a volatility classification, with the possibilities being VOLATILE, STABLE, or IMMUTABLE. VOLATILE is the default. In this post, I am not going to write 1000 words on it. Because I believe [&#8230;]]]></description>
										<content:encoded><![CDATA[<div><img width="679" height="186" src="https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-679x186.jpg" class="attachment-post-image size-post-image wp-post-image" alt="" decoding="async" srcset="https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-679x186.jpg 679w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1030x282.jpg 1030w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-850x233.jpg 850w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1700x466.jpg 1700w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function-1358x372.jpg 1358w, https://www.dbrnd.com/wp-content/uploads/2018/03/optimize-function.jpg 2000w" sizes="(max-width: 679px) 100vw, 679px" /></div><p><a class="a2a_button_facebook" href="https://www.addtoany.com/add_to/facebook?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="Facebook" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_twitter" href="https://www.addtoany.com/add_to/twitter?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="Twitter" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_linkedin" href="https://www.addtoany.com/add_to/linkedin?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="LinkedIn" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pinterest" href="https://www.addtoany.com/add_to/pinterest?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="Pinterest" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_reddit" href="https://www.addtoany.com/add_to/reddit?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="Reddit" rel="nofollow noopener" target="_blank"></a><a class="a2a_button_pocket" href="https://www.addtoany.com/add_to/pocket?linkurl=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&amp;linkname=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" title="Pocket" rel="nofollow noopener" target="_blank"></a><a class="a2a_dd addtoany_share_save addtoany_share" href="https://www.addtoany.com/share#url=https%3A%2F%2Fwww.dbrnd.com%2F2018%2F12%2Fpostgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable%2F&#038;title=PostgreSQL%3A%20Optimize%20the%20Function%20by%20defining%20Volatility%20Classification" data-a2a-url="https://www.dbrnd.com/2018/12/postgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable/" data-a2a-title="PostgreSQL: Optimize the Function by defining Volatility Classification"></a></p><p>This is an important article for PostgreSQL Developer because many of the database developers don&#8217;t know about the PostgreSQL Function Volatility. </p>
<p>Every function has a volatility classification, with the possibilities being VOLATILE, STABLE, or IMMUTABLE. VOLATILE is the default.</p>
<blockquote><p>In this post, I am not going to write 1000 words on it. Because I believe in practical so sharing a good practical example here.<br />
But you must read about the PostgreSQL Function Volatility from <a href="https://www.postgresql.org/docs/8.1/static/xfunc-volatility.html" rel="noopener" target="_blank">this</a> official page. </p></blockquote>
<p>Check the below practical example and you will get to know that how VOLATILE function is choosing the bad Query Execution Plan. </p>
<p><strong>Create a sample function:</strong></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_getvalues(int, int)
RETURNS int
AS
$$
  BEGIN
       RETURN 
       	CASE WHEN $1 = $2 THEN $1 ELSE $2 END;
  END;
$$ LANGUAGE 'plpgsql';</pre> </p>
<p><strong>Check the result of sample function:</strong></p>
<pre class="crayon-plain-tag">SELECT * FROM fn_getvalues(50,80);

fn_getvalues
-----------------
80</pre> </p>
<p><strong>Create a sample table with data:</strong></p>
<pre class="crayon-plain-tag">CREATE TABLE tbl_ItemTransactions 
 (
     TranID SERIAL
     ,TransactionDate TIMESTAMPTZ
     ,TransactionName TEXT
 );

CREATE INDEX idx_TranID ON tbl_ItemTransactions (TranID);
 
INSERT INTO tbl_ItemTransactions 
(TransactionDate, TransactionName)
SELECT x, 'dbrnd' 
FROM generate_series('2017-01-01 00:00:00'::timestamptz, '2018-01-31 00:00:00'::timestamptz,'50 seconds'::interval) a(x);</pre> </p>
<p><strong>Check the execution plan for below query:</strong><br />
You can find &#8216;Index Scan&#8217; because the huge table and index applied properly. </p>
<pre class="crayon-plain-tag">EXPLAIN
SELECT *FROM tbl_ItemTransactions
WHERE TranID = 80

'Index Scan using idx_tranid on tbl_itemtransactions  (cost=0.42..8.44 rows=1 width=18)'
'  Index Cond: (tranid = 80)'</pre> </p>
<p><strong>Now, Use the early created function in the same query:</strong><br />
You can find &#8216;Seq Scan&#8217; because the function of the filter is creating a problem. Because VOLATILE is the default volatility classification.</p>
<pre class="crayon-plain-tag">EXPLAIN
SELECT *FROM tbl_ItemTransactions
WHERE TranID = fn_getvalues (50,80);

'Seq Scan on tbl_itemtransactions  (cost=0.00..183520.26 rows=1 width=18)'
'  Filter: (tranid = fn_getvalues(50, 80))'</pre> </p>
<p><strong>Now, Let&#8217;s create an IMMUTABLE function:</strong></p>
<pre class="crayon-plain-tag">CREATE OR REPLACE FUNCTION fn_getvalues(int, int)
RETURNS int
AS
$$
  BEGIN
       RETURN 
       	CASE WHEN $1 = $2 THEN $1 ELSE $2 END;
  END;
$$ LANGUAGE 'plpgsql' IMMUTABLE;</pre> </p>
<p><strong>Check the same query again:</strong><br />
You can find &#8216;Index Scan&#8217; which is expected and correct behaviour.<br />
EXPLAIN</p>
<pre class="crayon-plain-tag">SELECT *FROM tbl_ItemTransactions
WHERE TranID = fn_getvalues (50,80);

'Index Scan using idx_tranid on tbl_itemtransactions  (cost=0.42..8.44 rows=1 width=18)'
'  Index Cond: (tranid = 80)'</pre> </p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.dbrnd.com/2018/12/postgresql-optimize-the-function-by-defining-volatility-classification-immutable-volatile-stable/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
