Database Research & Development

  • Home
  • NoSQL
    • NoSQL
    • Cassandra
  • Databases
    • Database Theory
    • Database Designing
    • SQL Server Coding Standards
    • SQL Server
    • PostgreSQL
    • MySQL
    • Greenplum
    • Linux
  • Interviews
    • SQL Server Interviews
    • MySQL Interviews
    • SQL Puzzles
  • DBA Scripts
    • SQL Server DBA Scripts
    • PostgreSQL DBA Scripts
    • MySQL DBA Scripts
    • Greenplum DBA Scripts
  • Home
  • Blog Archives !
  • (: Laugh@dbrnd :)
  • Contact Me !
sqlserverinterviews
Home 2018 December PostgreSQL: Optimize the Function by defining Volatility Classification

PostgreSQL: Optimize the Function by defining Volatility Classification

This article is half-done without your Comment! *** Please share your thoughts via Comment ***

This is an important article for PostgreSQL Developer because many of the database developers don’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 in practical so sharing a good practical example here.
But you must read about the PostgreSQL Function Volatility from this official page.

Check the below practical example and you will get to know that how VOLATILE function is choosing the bad Query Execution Plan.

Create a sample function:

1
2
3
4
5
6
7
8
9
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';

Check the result of sample function:

1
2
3
4
5
SELECT * FROM fn_getvalues(50,80);
 
fn_getvalues
-----------------
80

Create a sample table with data:

1
2
3
4
5
6
7
8
9
10
11
12
13
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);

Check the execution plan for below query:
You can find ‘Index Scan’ because the huge table and index applied properly.

1
2
3
4
5
6
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)'

Now, Use the early created function in the same query:
You can find ‘Seq Scan’ because the function of the filter is creating a problem. Because VOLATILE is the default volatility classification.

1
2
3
4
5
6
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))'

Now, Let’s create an IMMUTABLE function:

1
2
3
4
5
6
7
8
9
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;

Check the same query again:
You can find ‘Index Scan’ which is expected and correct behaviour.
EXPLAIN

1
2
3
4
5
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)'

Dec 31, 2018Anvesh Patel
SQL Server: If Table Exists in a Database, don't perform any actionSQL Server: Set IDENTITY to existing Column of a Table

Leave a Reply Cancel reply

CAPTCHA
Refresh

*

Anvesh Patel
Anvesh Patel

Database Engineer

ImageDecember 31, 2018 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, Function Optimization, Function Volatility, IMMUTABLE, plpgsql, Postgres Query, postgresql, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Monitoring, PostgreSQL Performance Tuning, PostgreSQL Programming, PostgreSQL Tips and Tricks, STABLE, VOLATILE
About Me!

I'm Anvesh Patel, a Database Engineer certified by Oracle and IBM. I'm working as a Database Architect, Database Optimizer, Database Administrator, Database Developer. Providing the best articles and solutions for different problems in the best manner through my blogs is my passion. I have more than six years of experience with various RDBMS products like MSSQL Server, PostgreSQL, MySQL, Greenplum and currently learning and doing research on BIGData and NoSQL technology. -- Hyderabad, India.

About DBRND !

dbrnd

This is a personal blog (www.dbrnd.com).

Any views or opinions represented in this blog are personal and belong solely to the blog owner and do not represent those of people, institutions or organizations that the owner may or may not be associated with in professional or personal capacity, unless explicitly stated.

Feel free to challenge me, disagree with me, or tell me I’m completely nuts in the comments section of each blog entry, but I reserve the right to delete any comment for any reason whatsoever (abusive, profane, rude, or anonymous comments) - so keep it polite.

The content of this website is protected by copyright. No portion of this website may be copied or replicated in any form without the written consent of the website owner.

Recent Comments !
  • Anvesh Patel { Sure will do... } – May 27, 12:43 PM
  • Anvesh Patel { Great... } – May 27, 12:41 PM
  • Anvesh Patel { Great... } – May 27, 12:39 PM
  • Anvesh Patel { Great... } – May 27, 12:36 PM
  • Anvesh Patel { Great... } – May 27, 12:28 PM
  • Anvesh Patel { Great... } – May 27, 12:27 PM
  • Anvesh Patel { Great... } – May 27, 12:16 PM
  • Older »
Follow Me !
  • facebook
  • linkedin
  • twitter
  • youtube
  • google
  • flickr
© 2015 – 2019 All rights reserved. Database Research & Development (dbrnd.com)
Posting....