Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

lock table till the transaction commit

I have two tables A and B related to ledger. Many transactions happen in both A and B parallely.

Problem faced is:

[28] - A insert success for source entity_id: id001
[28] - B update success for destination entity_id: id002
[28] - A insert success for destination entity_id: id002
[71] - B update success for source entity_id: id001                  
[28] - commit success for [28] with amount 100

Thread 71 updates table B before thread 28 commits the changes to the ledger.

It is happening because postgres locks the table when a process is using it.

  1. How can i make thread to wait till the commit is completed by current thread?
  2. How to lock a record in postgres? (so that other accounts in table can be accessed by users)
like image 366
Vamsidhar Avatar asked Apr 14 '26 06:04

Vamsidhar


1 Answers

Postgres locks the record by default. Refer docs for the same.

For the logs provided, the last log must be after committing the transaction. Whenever the transaction is complete, the waiting thread immediately takes over and that must be the reason your logs showed up that way.

like image 113
user3584564 Avatar answered Apr 17 '26 00:04

user3584564



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!