<?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>Function Optimization &#8211; Database Research &amp; Development</title>
	<atom:link href="https://www.dbrnd.com/tag/function-optimization/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>Tue, 09 Oct 2018 19:59:51 +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: 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>
