EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login
Home Data Science Data Science Tutorials PostgreSQL Tutorial PostgreSQL Lock Table
Secondary Sidebar
PostgreSQL Tutorial
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • Postgres Command-Line
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL BIGINT
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
    • Postgres like query
    • PostgreSQL encode
    • PostgreSQL Cheat Sheet
    • PostgreSQL List Databases
    • PostgreSQL Rename Database
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL INSERT INTO
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL ORDER BY DESC
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL Drop Schema
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL User Password
    • PostgreSQL log_statement
    • PostgreSQL repository
    • PostgreSQL shared_buffer
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL where in array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL CHECK Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • PostgreSQL List Tables
    • PostgreSQL TRUNCATE TABLE
    • PostgreSQL Table Partitioning
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL datediff
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL LENGTH()
    • PostgreSQL blob
    • PostgreSQL Median
    • PostgreSQL kill query
    • PostgreSQL Formatter
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL UUID
    • PostgreSQL Merge
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Show Databases
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL Show Tables
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL Encryption
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL Incremental Backup
    • PostgreSQL JSON vs JSONNB
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions

PostgreSQL Lock Table

By Priya PedamkarPriya Pedamkar

PostgreSQL Lock Table

Introduction to PostgreSQL Lock Table

PostgreSQL lock table is defined as a lock table for access from the user, we can lock the table from read access or write access. A lock is very useful and important in PostgreSQL to prevent the user for modifying a single row or all tables. We can lock the table by using access share, row share, row exclusive, share, share update exclusive, exclusive, share row exclusive, and access exclusive mode in PostgreSQL. Using the lock command we need to specify the table name and the name of the mode which we have applied on the table.

How to Lock Table in PostgreSQL?

Below is the way that defined how to lock the table in PostgreSQL.

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

Below syntax shows how to lock the table:

Lock table name_of_table IN [Mode of locking] [NOWAIT]
  • In the above example, the lock table is defined as a command used to lock the table by which mode we have used at the time of the locking table in PostgreSQL. After applying a lock on the table, it’s not accessible for read or write operations.
  • The name of the table is defined as the table name on which we are applying the lock.
  • Mode of locking is defined as the mode which was used while locking a table in PostgreSQL. If we have not used any lock mode then the default mode of access exclusive is used in PostgreSQL.
  • Now it is defined as do not wait for any lock to be released from the table. If the lock is not acquired then the transaction will directly be aborted without waiting in PostgreSQL.
  • We can acquire a lock explicitly by using the lock command.

Mode of Locks:

  • Access share
  • Row share
  • Row exclusive
  • Share update exclusive
  • Share
  • Share row exclusive
  • Exclusive
  • Access exclusive

The above mode of lock will contain its specified function to lock the table in PostgreSQL as follows:

1. Access Share

  • This lock in PostgreSQL conflicts with the only mode of access exclusive. Select command in PostgreSQL will acquire these locks on a specified table.
  • This is defined as we can only read data from the table we cannot modify the table after acquiring these locks on table.

2. Row Share

  • This lock in PostgreSQL conflicts with the mode of access exclusive and exclusive. The select for share and select for update statement will acquire these types of lock on the table.

3. Row Exclusive

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, and exclusive.
  • The update deletes and inserts will acquire the locks on the table. Basically, these locks are acquired on the lock which modifies the statement in PostgreSQL.

4. Share Update Exclusive

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, share update exclusive, and exclusive.
  • It will acquire command of vacuum, index creation, alter table, and validate command in PostgreSQL.

5. Share

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, share update exclusive, share, and exclusive.
  • This lock mode will acquire locks from the create index command in PostgreSQL.

6. Share Row Exclusive

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, share update exclusive, share row exclusive, share, and exclusive.
  • This mode is not automatically acquired on the table we have applied the same by using the lock command.

7. Exclusive

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, share update exclusive, share row exclusive, share, and exclusive.
  • This lock is acquired by using the refresh materialized view.

8. Access Exclusive

  • This lock in PostgreSQL conflicts with the mode of share row exclusive, share, access exclusive, share update exclusive, share row exclusive, share, access exclusive, and exclusive.
  • While using this lock only the user can access the table who applying a lock on the table.

Examples of PostgreSQL Lock Table

Given below are the examples of PostgreSQL Lock Table:

Example #1

Lock Table by Using Access Share Lock Mode.

The below example shows the lock table by using access share lock mode.

Code:

begin;
lock table stud1 IN ACCESS SHARE MODE;
select * from stud1;

Output:

PostgreSQL Lock Table-1.1

Example #2

Lock Table by Using Row Share Lock Mode.

The below example shows that lock table by using row share lock mode.

Code:

begin;
lock table stud1 IN ROW SHARE MODE;

Output:

row share

Example #3

Lock Table by Using Rowexclusive Lockmode.

Below example shows that lock table by using row exclusive lock mode.

Code:

begin;
lock table stud1 IN ROW EXCLUSIVE MODE;

Output:

PostgreSQL Lock Table-1.3

Example #4

Lock Table by Using Share Updateexclusive Lock Mode.

Below example shows that lock table by using share update exclusive lock mode.

Code:

begin;
lock table stud1 IN SHARE UPDATE EXCLUSIVE MODE;

Output:

Using Share Updateexclusive

Example #5

Lock Table by Using Share Lock Mode.

Below example shows that lock table by using share lock mode.

Code:

begin;
lock table stud1 IN SHARE MODE;

Output:

PostgreSQL Lock Table-1.5

Example #6

Lock Table by Using Share Row Exclusive Lock Mode.

Below example shows that lock table by using share row exclusive lock mode.

Code:

begin;
lock table stud1 IN SHARE ROW EXCLUSIVEMODE;

Output:

Using Share Row Exclusive

Example #7

Lock Table by Using Exclusive Lock Mode.

Below example shows that lock table by using exclusive lock mode.

Code:

begin;
lock table stud1 IN EXCLUSIVE MODE;

Output:

PostgreSQL Lock Table-1.7

Example #8

Lock Table by Using Access Exclusive Lock Mode.

Below example shows that lock table by using access exclusive lock mode.

Code:

begin;
lock table stud1 IN ACCESS EXCLUSIVE MODE;

Output:

PostgreSQL Lock Table-1.8

Recommended Articles

This is a guide to PostgreSQL Lock Table. Here we discuss the introduction and how to lock table in postgresql along with examples respectively. You may also have a look at the following articles to learn more –

  1. PostgreSQL Float
  2. PostgreSQL DELETE
  3. PostgreSQL Import CSV
  4. PostgreSQL String Array
Popular Course in this category
PostgreSQL Course (2 Courses, 1 Project)
  2 Online Courses |  1 Hands-on Project |  7+ Hours |  Verifiable Certificate of Completion
4.5
Price

View Course
Primary Sidebar
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Live Classes
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

ISO 10004:2018 & ISO 9001:2015 Certified

© 2023 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you

Let’s Get Started

By signing up, you agree to our Terms of Use and Privacy Policy.

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

By signing up, you agree to our Terms of Use and Privacy Policy.

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

Loading . . .
Quiz
Question:

Answer:

Quiz Result
Total QuestionsCorrect AnswersWrong AnswersPercentage

Explore 1000+ varieties of Mock tests View more