На каких уровнях можно брать блокировки в базе данных? Какие есть блокировки, не привязанные к таблице?
Asked at
AI answer
The granularity and available types of locks depend on the DBMS. In general, locks can apply to rows, pages, tables, and database objects; some DBMSs also support locks on individual fields. The finer the granularity, the greater the concurrency—but the higher the overhead.
For example, in PostgreSQL, application code can lock rows (SELECT ... FOR UPDATE) and relations—tables, indexes, and other objects (LOCK TABLE). Page locks are mostly internal. Row locks are usually held until the end of the transaction.
Locks that aren’t tied to a specific table are primarily advisory locks: the application chooses a key and uses it to coordinate tasks, for example, to ensure that only one process performs a periodic calculation. PostgreSQL provides both transaction-level and session-level variants (pg_advisory_xact_lock, pg_advisory_lock). It’s important to explicitly release a session-level lock; a transaction-level lock is released when the transaction ends. The DBMS also uses internal locks on transactions and objects, but these aren’t the same as application-level table locks.
Also asked at ozon_tech
- What salary range are you currently considering?
- Tell about transactions: what they are and what are the levels of transaction isolation?
- What transaction isolation levels exist in PostgreSQL?
- Tell me about TCP and UDP, what is the difference?
- Please tell me what you are currently doing, what is your area of responsibility, and what tasks you are working on?
Similar questions
- Which database transaction isolation levels do you know, and how do they differ?
- What transaction isolation levels are there, how do they differ, and why can’t you always use Serializable?
- How many transaction isolation levels are there in PostgreSQL?
- How is Optimistic Locking implemented, and when is it preferable to use it over pessimistic locking?
- What locks exist in PostgreSQL?