Postgresql kernel source code analyzes the use of table lock relation lock, session lock session lock application scenarios, and it is no longer difficult to operate the table

​Column content :
postgresql kernel source code analysis
handwritten database toadb
concurrent programming
personal homepage : my homepage
motto: Tian Xingjian, a gentleman strives for self-improvement;

==================================================================

Table lock introduction

When the table is opened or operated, the table relation needs to be locked. The table lock defines 8 levels of mutual exclusion, and there is also a session-level table lock session lock.

ID of the table lock

  • Lock type LOCKTAG_RELATION
  • The value of locktag

locktag_field1 = dboid When relation is a shared table, dboid = 0
locktag_field2 = reloid

Table lock related interface

extern void LockRelationOid(Oid relid, LOCKMODE lockmode);
extern void LockRelationId(LockRelId *relid, LOCKMODE lockmode);
extern bool ConditionalLockRelationOid(Oid relid, LOCKMODE lockmode);
extern void UnlockRelationId(LockRelId *relid, LOCKMODE lockmode);
extern void UnlockRelationOid(Oid relid, LOCKMODE lockmode);

extern void LockRelation(Relation relation, LOCKMODE lockmode);
extern bool ConditionalLockRelation(Relation relation, LOCKMODE lockmode);
extern void UnlockRelation(Relation relation, LOCKMODE lockmode);
extern bool CheckRelationLockedByMe(Relation relation, LOCKMODE lockmode,
									bool orstronger);
extern bool LockHasWaitersRelation(Relation relation, LOCKMODE lockmode);

extern void LockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode);
extern void UnlockRelationIdForSession(LockRelId *relid, LOCKMODE lockmode);

Table lock application method

There are two types of table lock applications. First, lock the records in the system dictionary when opening the table, and then lock the target table;

Of course, when locking, the system dictionary changes will be checked. In postgresql, invalidmessage is used to synchronize the changes;
in fact, LockAcquireExtended is called. If the table lock is obtained, the invalidmessage is first synchronized;

When opening the table for the first time

  • The OID of the table as a parameter

接口有 LockRelationOid,ConditionalLockRelationOid,UnlockRelationOid

Actually call LockAcquireExtended
to lock the data in relcache when opening the table

  • table relid as parameter

The interface has LockRelationId, UnlockRelationId

Actually call LockAcquireExtended to acquire the lock,
which is used to lock the table when opening the table; UnlockRelationId is faster than UnlockRelationOid when unlocking, and the former is recommended

When adding table lock again

  • locktag is initialized using the relation structure

The interface has LockRelation, ConditionalLockRelation, UnlockRelation;

At this time, the table has been opened, and LockAcquireExtended is actually called, and the invalidate message needs to be checked when locking.

use

When accessing tables and indexes, first open to handle
the calling relationship

  • Table open and close call relationship
table_open
	->relation_open
		->LockRelationOid
table_close
	->relation_close
		->UnlockRelationId
  • The opening and closing call relationship of the index is similar to that of the table
index_open
	->relation_open
		->LockRelationOid
index_close
	->relation_close
		->UnlockRelationId

Operation of session lock

The session lock is related to the current session, that is, the lock holding can span multiple transactions;

When releasing, call the interface to release, or it will be released automatically when the session ends, or an error of ERROR level will be released automatically.

The interface LockRelationIdForSession application and UnlockRelationIdForSession release
call LockAcquire to acquire a regular lock, which is actually a package of LockAcquireExtended, which fixes the last two parameters;

The input parameter LockRelId of the session lock specifies dbid, relid;

Purpose of session lock

  • create index

When creating an index in concurrent mode, the session lock will be added again;

  • delete index drop index
  • rebuild index reindex
  • clean table vacuum

end

Thank you very much for your support. Don’t forget to leave your valuable comments while browsing. If you think it is worthy of encouragement, please like and bookmark, I will work harder!

Author email: [email protected]
If there are any mistakes or omissions, please point them out and learn from each other.

Note: Do not reprint without consent!

Guess you like

Origin blog.csdn.net/senllang/article/details/131800184