Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Analyze tuple locks in PostgreSQL

2025-02-25 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Database >

Share

Shulou(Shulou.com)05/31 Report--

This article mainly introduces "analyzing tuple locks in PostgreSQL". In daily operation, I believe many people have doubts about analyzing tuple locks in PostgreSQL. The editor consulted all kinds of data and sorted out simple and easy-to-use operation methods. I hope it will be helpful to answer the doubts of "analyzing tuple locks in PostgreSQL". Next, please follow the editor to study!

Compared to tables or other database objects, the locks of tuples is not easy to handle. The problem is that the transaction may be at some point in time.

Lock large quantities of tuples, so it is not possible to store these lock information in shared memory. To break through this limitation, PG uses a two-tier mechanism.

Level 1: store lock information in tuple header implementation: store the XID of the current transaction in XMAX, and set a special

Infomask bits to distinguish between deleted tuples and these locked tuples.

Level 2: if there are concurrent operations, use MultiXact.

This mechanism can accommodate any number of tuples locked at the same time.

Example

Inserting data and performing a update,update operation will lock tuples

[local:/data/run/pg12]: 5120 pg12@testdb=# begin;BEGIN [local:/data/run/pg12]: 5120 pg12@testdb=#* insert into t values (1), (2); INSERT 0 2 [local:/data/run/pg12]: 5120 pg12@testdb=#* commit;COMMIT [local:/data/run/pg12]: 5120 pg12@testdb=# begin;BEGIN [local:/data/run/pg12]: 5120 pg12@testdb=#* update t set id = 2 UPDATE 2 [local:/data/run/pg12]: 5120 pg12@testdb=#* [local:/data/run/pg12]: 5120 pg12@testdb=#* select txid_current (); 1512070049 local:/data/run/pg12-[local:/data/run/pg12]: 5120 pg12@testdb=# select xmin,xmax from t; xmin | xmax-+-1512070048 | 1512070049 1512070048 | 1512070049 (2 rows) [local:/data/run/pg12]: 5120 pg12@testdb=#

Lock information

[local:/data/run/pg12]: 5120 pg12@testdb=# select pid,mode,locktype,relation,page,tuple,transactionid from pg_locks where pid pg_backend_pid () Pid | mode | locktype | relation | page | tuple | transactionid-+-1877 | RowExclusiveLock | relation | 74856 | | | | 1877 | ExclusiveLock | virtualxid | 1877 | ExclusiveLock | transactionid | 1512070049 (3 rows) to this point | The study on "analyzing tuple locks in PostgreSQL" is over. I hope to be able to solve your doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Database

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report