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 2016 August PostgreSQL 9.5: Using FOR UPDATE SKIP LOCKED Option SELECT only Committed Records

PostgreSQL 9.5: Using FOR UPDATE SKIP LOCKED Option SELECT only Committed Records

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

PostgreSQL 9.5 introduced one of the powerful feature called SKIP LOCKED which is used for SKIP uncommitted data.

I am not going to make this more complex topic, explaining by one practical example.

In our project we have implemented thread mechanism. Different threads are running and taking a fixed set of records from the Database.
These threads basically implemented for performing Reverse Geo Coding on the stored Latitude and Longitude.
I am giving 10000 records to each thread in each batch.
Now the average execution time of each thread is 5 second to 7 second.

Now, one batch of 10000 records taken by thread_1 for 5 second to 7 second that same records should not taken by any other thread because that first 10000 records already under the process by thead_1.
Thread_2 has taken next 10000 records which are not taken by thread_1.

Now with the PostgreSQL 9.5, we do not require any additional locking mechanism or isolation level to achieve this scenario.
We can very easily achieve using FOR UPDATE SKIP LOCKED option. When we SELECT data using this option, it will skip all running transactions (uncommitted transaction) and return only required records to be processed.

Below is a full demonstration of this.

First create a table with sample data:

1
2
3
4
5
6
7
8
9
CREATE TABLE tbl_Employee
(
EmpID INT
,EmpName CHARACTER VARYING
);
 
INSERT INTO tbl_Employee
VALUES (1,'Anvesh'),(2,'Roy')
,(3,'Benny'),(4,'Nivu');

Now we are going to test this option with the help of two different sessions.

Open a new session and execute below queries in Session 1:
We have inserted four sample records and now will update one of record in OPEN TRANSACTION with out committing anything.

1
2
3
4
BEGIN TRANSACTION
 
UPDATE tbl_Employee SET EmpName = 'Martin'
WHERE EmpID = 3;

Open another session and execute below queries in Session 2:
First SELECT with out any option:

If you do not know, please read about the MVCC.

What is Multi Version Concurrency Control (MVCC)

PostgreSQL has a default MVCC architecture, in which you can select last committed row version.
In the above query we have updated one record for EmpID=3, but when I execute simple SELECT statement then it returned the old value of EmpID=3.

This data is based on last committed row version.

1
2
3
4
5
6
7
8
9
10
SELECT *FROM tbl_Employee;
 
/* Result:
EmpID EmpName
--------------------
1 Anvesh
2 Roy
4 Nivu
3 Benny
*/


Now SELECT with NOWAIT option:

When you are trying to SELECT running transaction, It will raise the error like : can not obtain lock on row. You can not SELECT EmpID=3, but you can SELECT EmpID IN (1,2,4).

Please find out below query and the result.

1
2
3
4
5
6
7
8
9
SELECT *FROM tbl_Employee WHERE EmpID=3 FOR UPDATE NOWAIT;
SELECT *FROM tbl_Employee FOR UPDATE NOWAIT;
 
/* Result:
********** Error **********
 
ERROR: could not obtain lock on row in relation "tbl_employee"
SQL state: 55P03
*/

Now finally, we test SKIP LOCKED option:

A SKIP LOCKED option simply sikped the records or rows which are the part of ROW Level Locking.
You can find out in the below result, EmpID=3 is not in result because it is not committed yet and running in different session.

1
2
3
4
5
6
7
8
9
/* Result:
SELECT *FROM tbl_Employee FOR UPDATE SKIP LOCKED;
 
EmpID EmpName
--------------------
1 Anvesh
2 Roy
4 Nivu
*/

Aug 14, 2016Anvesh Patel
PostgreSQL 9.5: How to Concatenate and Overwrite JSON DocumentPostgreSQL 9.5: Insert IF not Exists, Update IF Exists (Insert ON CONFLICT option)
Comments: 1
  1. Marais
    June 29, 2017 at 6:00 am

    yes working for me, last week got a issue of lock in Postgresql and found this alternate solution, thank you

Anvesh Patel
Anvesh Patel

Database Engineer

August 14, 2016 PostgreSQLAnvesh Patel, database, database research and development, dbrnd, FOR UPDATE SKIP LOCKED, lock, MVCC, plpgsql, Postgres Query, postgresql, postgresql 9.5, PostgreSQL Administrator, PostgreSQL Error, PostgreSQL Programming, PostgreSQL Tips and Tricks, SKIP LOCK
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....