Have you heard of Advisory Locks in PostgreSQL?
AI answer
Yes. Advisory locks in PostgreSQL are application-level locks for which the application chooses the key and defines the meaning of the resource being locked; the database does not automatically associate them with rows or tables. There are transaction-level variants (pg_advisory_xact_lock) and session-level variants (pg_advisory_lock). Transaction-level locks are released when the transaction ends, while session-level locks must be explicitly released or the connection must be closed. They can be used, for example, to prevent the same task from running in parallel. It is important that all participants use the same key space and that the lock is held on the same connection; long-running tasks also require timeouts and failure handling.
Also asked at Коммит (Аутстафф на проект "Ростелекома")
- How to protect the system from creating multiple payments if the user presses the pay button many times?
- How is the N+1 query problem solved in ORM?
- What types of indexes are available in PostgreSQL?
- There are several services (2-3), and there is an end-to-end business scenario passing through all services. If an operation on one of the services in the middle or at the end fails, the system ends up in an inconsistent state. What methods can be used to achieve inter-service transactionality (either the entire scenario is applied, or none of it is), if each service has its own database?
- What databases have you worked with the most?
Similar questions
- How do PostgreSQL's isolation levels differ from the standard ones?
- What is the difference between pessimistic and optimistic locking?
- How can you select data and then modify it, while making sure a concurrent transaction doesn't change it?
- Which type of locking have you mainly used: optimistic or pessimistic?
- What are Advisory Locks in PostgreSQL and how can they be used for business orchestration?