HiveBrain v1.2.0
Get Started
← Back to all entries
patternsqlMinor

Row Locking in Postgres

Submitted by: @import:stackexchange-dba··
0
Viewed 0 times
rowlockingpostgres

Problem

I have table named 'car'

Table "db145004db.car"
Column | Type | Collation | Nullable | Default
----------+--------+-----------+----------+---------
car_no | bigint | | not null |
car_name | text | | |
Indexes:
"car_pkey" PRIMARY KEY, btree (car_no)

Table Content:

car_no | car_name
--------+------------
1 | first_car
2 | second_car
3 | third_car

I ran this following two transaction,

Transaction 1:

begin ;
select * from car where car_no=2 for update;

Transaction 2:

begin ;
select * from car where car_no=2 for update; (waiting for first transaction to release lock)

My 'pg_locks' table looks like:

locktype | database | relation | page | tuple | virtualxid | transactionid | classid | objid | objsubid | virtualtransaction | pid | mode | granted | fastpath
---------------+----------+----------+------+-------+------------+---------------+---------+-------+----------+--------------------+-------+---------------------+---------+----------
relation | 103777 | 143022 | | | | | | | | 4/75 | 11043 | AccessShareLock | t | t
relation | 103777 | 143016 | | | | | | | | 4/75 | 11043 | RowShareLock | t | t
virtualxid | | | | | 4/75 | | | | | 4/75 | 11043 | ExclusiveLock | t | t
relation | 103777 | 143022 | | | | | | | | 5/144 | 11150 | AccessShareLock | t | t
relation | 103777 | 143016 | | | | | | | | 5/144 | 11150 | RowShareLock | t | t
virtualxid | | | | | 5/144

Solution

I came across the postgres docs regarding locking , which states that


Although tuples are a lockable type of object, information about row-level locks is stored on disk, not in memory, and therefore row-level locks normally do not appear in this view. If a transaction is waiting for a row-level lock, it will usually appear in the view as waiting for the permanent transaction ID of the current holder of that row lock.

I got my answer.

Context

StackExchange Database Administrators Q#216475, answer score: 3

Revisions (0)

No revisions yet.