Posts

Showing posts with the label lock modes

Row-level Locks

Image
Introduction Row-level locks are used to protect specific rows when data is being modified. Unlike table locks, Postgres does any row lock storage outside of RAM, writing them directly into the tuples (data rows) on the hard disk to avoid memory overflow. Differences Table-level Lock (8 modes): Protects the table schema and controls broad behaviors, such as full-table reads, table writes, or column alterations. You can imagine these 8 modes as "admission tickets" that control who can enter the building and what they can do inside. Row-level Lock (4 modes): Protects the actual data values inside specific rows to prevent two people from modifying the same record simultaneously. It is like locking individual small rooms inside the building. Connections Although Table-level Lock and Row-level Lock are two distinct sets of lock modes, they always run in parallel and support each other. When you act on a row, Postgres automatically assigns both a table lock and a row lock under...

Table-level Locks

Image
Introduction Lock Modes As mentioned in the previous article, the lock management system is divided into three main levels depending on the locked object: Table-level Locks , Row-level Locks and Page-level Locks . For each lock level, it is further divided into multiple lock modes, for instance, Table-level Locks has eight different lock modes. These lock modes are specific implementations derived from two primary lock types, which are Shared Lock and Exclusive Lock . When a statement executes, Postgres automatically assigns the appropriate lock type, so we rarely need to manually execute lock commands. Understanding these lock modes helps you know which SQL statements can run concurrently and which statements will force others to queue and wait. Below are the details of the eight Table-level Lock Modes in Postgres, ordered from the lowest level (least conflict) to the highest level (blocking everything). Access Share (AS) Activation command: SELECT (pure, without any locking clause...