↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / 锁

PostgreSQL 版本定义:18

当前阅读 PG 18·选择有来源记录的版本

12 / 12 条已记录定义实心方块表示该版本有定义;选择方块可阅读对应版本。

表锁兼容性 8 × 8

行表示请求的锁,列表示其他事务已持有的同对象锁。表锁名称中的 ROW 不代表行锁。

  • × 冲突
  • · 兼容
表锁冲突矩阵. × table conflict; · compatible; ● conflict on the same row only.
Held →Requested ↓ASRSRXSUESSRXXAX
ACCESS SHARE
ROW SHARE
ROW EXCLUSIVE
SHARE UPDATE EXCLUSIVE
SHARE
SHARE ROW EXCLUSIVE
EXCLUSIVE
ACCESS EXCLUSIVE

Select a cell to compare requested and held modes. Use the arrow keys to move through the matrix.

表锁与行锁组合 14 × 14

缩进项同时取得上级表锁及所列行锁。这些显示组合保留原始 12 种锁模式,不代表锁强度排序。

  • × 表锁冲突,不同行也会冲突
  • ● 仅在同一行冲突
  • · 锁模式兼容
表锁与行锁组合矩阵. × table conflict; · compatible; ● conflict on the same row only.
Held →Requested ↓ASROW SHAREROW EXCLUSIVESUESSRXXAX
Table onlyTable onlyFKSFSFNKUFUTable onlyFNKUFUTable onlyTable onlyTable onlyTable onlyTable only
ACCESS SHARETable only
ROW SHARETable only
FOR KEY SHARE
FOR SHARE
FOR NO KEY UPDATE
FOR UPDATE
ROW EXCLUSIVETable only
FOR NO KEY UPDATE
FOR UPDATE
SHARE UPDATE EXCLUSIVETable only
SHARETable only
SHARE ROW EXCLUSIVETable only
EXCLUSIVETable only
ACCESS EXCLUSIVETable only

Select a cell to compare requested and held modes. Use the arrow keys to move through the matrix.

12 种锁模式

表锁

ROW SHARE

Conflicts with the EXCLUSIVE and ACCESS EXCLUSIVE lock modes. The SELECT command acquires a lock of this mode on all tables on which one of the FOR UPDATE, FOR NO KEY UPDATE, FOR SHARE, or FOR KEY SHARE options is specified (in addition to ACCESS SHARE locks on any other tables that are referenced without any explicit FOR ... locking option).

SELECT FOR KEY SHARE · SELECT FOR SHARE · SELECT FOR NO KEY UPDATE · SELECT FOR UPDATE · INSERT (foreign-key check) · UPDATE (foreign-key check) · COPY … FROM (foreign-key check) · MERGE (foreign-key check) · LOCK TABLE … IN ROW SHARE MODE · ALTER TABLE … VALIDATE CONSTRAINT (referenced table) ·

详情 →

Conflicts with the SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. The commands UPDATE, DELETE, INSERT, and MERGE acquire this lock mode on the target table (in addition to ACCESS SHARE locks on any other referenced tables). In general, this lock mode will be acquired by any command that modifies data in a table.

INSERT · UPDATE · DELETE · COPY … FROM · MERGE · LOCK TABLE … IN ROW EXCLUSIVE MODE · UPDATE (non-key update) · UPDATE (key update) · MERGE … WHEN MATCHED THEN UPDATE (non-key) · MERGE … WHEN MATCHED THEN DELETE / UPDATE (key) · INSERT … ON CONFLICT DO UPDATE (non-key) · INSERT … ON CONFLICT DO UPDATE (key) · INSERT (foreign-key check) · UPDATE (foreign-key check) · COPY … FROM (foreign-key check) · MERGE (foreign-key check) ·

详情 →
详情 →

Conflicts with the ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. This mode protects a table against concurrent data changes, and is self-exclusive so that only one session can hold it at a time. Acquired by CREATE TRIGGER and some forms of ALTER TABLE.

CREATE TRIGGER · LOCK TABLE … IN SHARE ROW EXCLUSIVE MODE · ALTER TABLE … ENABLE / DISABLE TRIGGER · ALTER TABLE … ADD FOREIGN KEY [NOT VALID] (referencing table) · ALTER TABLE … ADD FOREIGN KEY [NOT VALID] (referenced table) ·

详情 →
表锁

EXCLUSIVE

Conflicts with the ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE lock modes. This mode allows only concurrent ACCESS SHARE locks, i.e., only reads from the table can proceed in parallel with a transaction holding this lock mode. Acquired by REFRESH MATERIALIZED VIEW CONCURRENTLY.

REFRESH MATERIALIZED VIEW CONCURRENTLY · LOCK TABLE … IN EXCLUSIVE MODE ·

详情 →

Conflicts with locks of all modes (ACCESS SHARE, ROW SHARE, ROW EXCLUSIVE, SHARE UPDATE EXCLUSIVE, SHARE, SHARE ROW EXCLUSIVE, EXCLUSIVE, and ACCESS EXCLUSIVE). This mode guarantees that the holder is the only transaction accessing the table in any way. Acquired by the DROP TABLE, TRUNCATE, REINDEX, CLUSTER, VACUUM FULL, and REFRESH MATERIALIZED VIEW (without CONCURRENTLY) commands. Many forms of ALTER INDEX and ALTER TABLE also acquire a lock at this level. This is also the default lock mode for LOCK TABLE statements that do not specify a mode explicitly.

REFRESH MATERIALIZED VIEW (without CONCURRENTLY) · DROP TABLE · DROP INDEX (without CONCURRENTLY) · TRUNCATE · CLUSTER · VACUUM FULL · VACUUM (tail truncation) · REINDEX (index being rebuilt) · LOCK TABLE … IN ACCESS EXCLUSIVE MODE · ALTER TABLE … ADD COLUMN · ALTER TABLE … DROP COLUMN · ALTER TABLE … ALTER COLUMN … SET / DROP DEFAULT · ALTER TABLE … ALTER COLUMN … TYPE · ALTER TABLE … ALTER COLUMN … SET STORAGE · ALTER TABLE … ALTER COLUMN … SET COMPRESSION · ALTER TABLE … SET TABLESPACE · ALTER TABLE … SET LOGGED / UNLOGGED · ALTER TABLE … SET ACCESS METHOD · ALTER TABLE … DROP CONSTRAINT · ALTER TABLE … ALTER CONSTRAINT · ALTER TABLE … ALTER COLUMN … SET NOT NULL · ALTER TABLE … ALTER COLUMN … DROP NOT NULL · ALTER TABLE … ENABLE / DISABLE RULE · ALTER TABLE … ENABLE / DISABLE ROW LEVEL SECURITY · ALTER TABLE … FORCE / NO FORCE ROW LEVEL SECURITY · ALTER TABLE … ALTER COLUMN … DROP EXPRESSION · ALTER TABLE … ALTER COLUMN … SET EXPRESSION · ALTER TABLE … ALTER COLUMN … SET GENERATED / RESTART / SET sequence_option · ALTER TABLE … ALTER COLUMN … ADD GENERATED … AS IDENTITY · ALTER TABLE … ALTER COLUMN … DROP IDENTITY · ALTER TABLE … INHERIT / NO INHERIT (child table) · ALTER TABLE … OWNER TO · ALTER TABLE … REPLICA IDENTITY · ALTER TABLE … RENAME · ALTER TABLE … ADD CONSTRAINT (CHECK / UNIQUE / PRIMARY KEY / EXCLUDE) · ALTER TABLE … ATTACH PARTITION (partition being attached) · ALTER TABLE … ATTACH PARTITION (default partition) · ALTER TABLE … DETACH PARTITION (parent table) · ALTER TABLE … DETACH PARTITION (partition being detached) · ALTER TABLE … DETACH PARTITION … CONCURRENTLY (partition, final phase) · ALTER INDEX … SET TABLESPACE · ALTER INDEX … SET / RESET (fillfactor = …) · ALTER INDEX … ATTACH PARTITION ·

详情 →

Behaves similarly to FOR SHARE, except that the lock is weaker: SELECT FOR UPDATE is blocked, but not SELECT FOR NO KEY UPDATE. A key-shared lock blocks other transactions from performing DELETE or any UPDATE that changes the key values, but not other UPDATE, and neither does it prevent SELECT FOR NO KEY UPDATE, SELECT FOR SHARE, or SELECT FOR KEY SHARE.

SELECT FOR KEY SHARE · INSERT (foreign-key check) · UPDATE (foreign-key check) · COPY … FROM (foreign-key check) · MERGE (foreign-key check) ·

详情 →
行锁

FOR SHARE

Behaves similarly to FOR NO KEY UPDATE, except that it acquires a shared lock rather than exclusive lock on each retrieved row. A shared lock blocks other transactions from performing UPDATE, DELETE, SELECT FOR UPDATE or SELECT FOR NO KEY UPDATE on these rows, but it does not prevent them from performing SELECT FOR SHARE or SELECT FOR KEY SHARE.

SELECT FOR SHARE ·

详情 →

FOR UPDATE causes the rows retrieved by the SELECT statement to be locked as though for update. This prevents them from being locked, modified or deleted by other transactions until the current transaction ends. That is, other transactions that attempt UPDATE, DELETE, SELECT FOR UPDATE, SELECT FOR NO KEY UPDATE, SELECT FOR SHARE or SELECT FOR KEY SHARE of these rows will be blocked until the current transaction ends; conversely, SELECT FOR UPDATE will wait for a concurrent transaction that has run any of those commands on the same row, and will then lock and return the updated row (or no row, if the row was deleted). Within a REPEATABLE READ or SERIALIZABLE transaction, however, an error will be thrown if a row to be locked has changed since the transaction started. For further discussion see Data Consistency Checks at the Application Level. The FOR UPDATE lock mode is also acquired by any DELETE on a row, and also by an UPDATE that modifies the values of certain columns. Currently, the set of columns considered for the UPDATE case are those that have a unique index on them that can be used in a foreign key (so partial indexes and expressional indexes are not considered), but this may change in the future.

SELECT FOR UPDATE · UPDATE (key update) · DELETE · MERGE … WHEN MATCHED THEN DELETE / UPDATE (key) · INSERT … ON CONFLICT DO UPDATE (key) ·

详情 →

命令与锁模式

命令可在不同对象和执行阶段取得不同锁;链接保留所选 PostgreSQL 版本。

SQL 命令关联模式对象、阶段与条件
SELECTRead referenced tables without a FOR locking clause. 来源
COPY … TORead the source table. COPY (query) TO follows the locks required by that query. 来源
LOCK TABLE … IN ACCESS SHARE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
ALTER TABLE … NO INHERIT (parent table)Take ACCESS SHARE on the parent while removing inheritance. The altered child separately takes ACCESS EXCLUSIVE. 来源
SELECT FOR KEY SHARELock the named table, in addition to the corresponding locks on selected rows. 来源
SELECT FOR SHARELock the named table, in addition to the corresponding locks on selected rows. 来源
SELECT FOR NO KEY UPDATELock the named table, in addition to the corresponding locks on selected rows. 来源
SELECT FOR UPDATELock the named table, in addition to the corresponding locks on selected rows. 来源
INSERT (foreign-key check)The foreign-key check runs SELECT FOR KEY SHARE on the referenced table, taking ROW SHARE there. The writing target separately holds ROW EXCLUSIVE. 来源
UPDATE (foreign-key check)The foreign-key check runs SELECT FOR KEY SHARE on the referenced table, taking ROW SHARE there. The writing target separately holds ROW EXCLUSIVE. 来源
COPY … FROM (foreign-key check)The foreign-key check runs SELECT FOR KEY SHARE on the referenced table, taking ROW SHARE there. The writing target separately holds ROW EXCLUSIVE. 来源
MERGE (foreign-key check)The foreign-key check runs SELECT FOR KEY SHARE on the referenced table, taking ROW SHARE there. The writing target separately holds ROW EXCLUSIVE. 来源
LOCK TABLE … IN ROW SHARE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
ALTER TABLE … VALIDATE CONSTRAINT (referenced table)Foreign-key validation takes ROW SHARE on the referenced table and SHARE UPDATE EXCLUSIVE on the table being validated. 来源
INSERTLock the insertion target. Inserting a new row is not equivalent to locking an existing row FOR UPDATE. 来源
UPDATELock the target table; affected existing rows also receive row locks. 来源
DELETELock the target table; deleted rows also receive FOR UPDATE locks. 来源
COPY … FROMLock the target and insert new rows. This does not generally lock existing rows FOR UPDATE. 来源
MERGELock the target table. Row locking depends on the INSERT, UPDATE or DELETE action actually taken. 来源
LOCK TABLE … IN ROW EXCLUSIVE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
UPDATE (non-key update)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
UPDATE (key update)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
MERGE … WHEN MATCHED THEN UPDATE (non-key)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
MERGE … WHEN MATCHED THEN DELETE / UPDATE (key)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
INSERT … ON CONFLICT DO UPDATE (non-key)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
INSERT … ON CONFLICT DO UPDATE (key)This write variant takes ROW EXCLUSIVE on its target table. Existing-row locks depend on the action actually performed. 来源
INSERT (foreign-key check)The write target takes ROW EXCLUSIVE. The foreign-key check takes ROW SHARE and FOR KEY SHARE on a different object: the referenced table and its matching rows. 来源
UPDATE (foreign-key check)The write target takes ROW EXCLUSIVE. The foreign-key check takes ROW SHARE and FOR KEY SHARE on a different object: the referenced table and its matching rows. 来源
COPY … FROM (foreign-key check)The write target takes ROW EXCLUSIVE. The foreign-key check takes ROW SHARE and FOR KEY SHARE on a different object: the referenced table and its matching rows. 来源
MERGE (foreign-key check)The write target takes ROW EXCLUSIVE. The foreign-key check takes ROW SHARE and FOR KEY SHARE on a different object: the referenced table and its matching rows. 来源
VACUUM (without FULL)Ordinary vacuum holds this mode; truncating empty tail pages can briefly require ACCESS EXCLUSIVE. 来源
ANALYZELock the table being analyzed. 来源
CREATE INDEX CONCURRENTLYLock the indexed table. Building proceeds across transactions and waits for old transactions where necessary. 来源
CREATE STATISTICSLock the table whose extended statistics are being defined. 来源
COMMENT ON (relation object)Lock the object being commented on. Identical modes on different objects do not conflict. 来源
DROP INDEX CONCURRENTLYLock the parent table while removing the index across phases; unlike ordinary DROP INDEX this does not hold ACCESS EXCLUSIVE on the table. 来源
REINDEX … CONCURRENTLYFrom PostgreSQL 12, session-level SHARE UPDATE EXCLUSIVE locks cover the rebuilt index and its table through the build, validation, swap and cleanup transactions. 来源
LOCK TABLE … IN SHARE UPDATE EXCLUSIVE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
ALTER TABLE … ALTER COLUMN … SET STATISTICSChange the column statistics target. 来源
ALTER TABLE … ALTER COLUMN … SET (n_distinct = …)Change column statistics options; RESET has the same requirement. 来源
ALTER TABLE … CLUSTER ONChoose the index for future clustering; this command does not rewrite the table. 来源
ALTER TABLE … SET WITHOUT CLUSTERClear the clustering-index designation. 来源
ALTER TABLE … VALIDATE CONSTRAINTValidate a constraint on this table. Foreign-key validation also accesses the referenced table. 来源
ALTER TABLE … ATTACH PARTITION (parent table)The parent lock is extracted separately for every version. The table being attached additionally requires ACCESS EXCLUSIVE. From PostgreSQL 11 a default partition, if any, is also locked. 来源
ALTER TABLE … DETACH PARTITION … FINALIZE (parent table)Complete an interrupted concurrent detach. The detached partition additionally requires ACCESS EXCLUSIVE. 来源
ALTER TABLE … SET / RESET (fillfactor = …)Each storage parameter specifies its required mode. fillfactor can use SHARE UPDATE EXCLUSIVE. 来源
ALTER TABLE … SET / RESET (autovacuum_* = …)Table autovacuum storage options use SHARE UPDATE EXCLUSIVE. 来源
ALTER TABLE … SET / RESET (toast.autovacuum_* = …)These are TOAST autovacuum options. There is no generic SET TOAST subcommand; other options follow their own lock requirements. 来源
ALTER TABLE … DETACH PARTITION … CONCURRENTLY (parent table)From PostgreSQL 14, concurrent detach uses two transactions. The parent holds SHARE UPDATE EXCLUSIVE and waits for transactions using the partition before finalizing. 来源
ALTER TABLE … DETACH PARTITION … CONCURRENTLY (partition, first phase)In the first phase, parent and partition hold SHARE UPDATE EXCLUSIVE. The partition lock is upgraded after committing and waiting. 来源
ALTER INDEX … RENAMELock the index being renamed. 来源
ALTER TABLE … INHERIT (parent table)Take SHARE UPDATE EXCLUSIVE on the newly designated parent. The altered child separately takes ACCESS EXCLUSIVE. 来源
CREATE INDEX (without CONCURRENTLY)Lock the indexed table, preventing concurrent writes. 来源
REINDEX (parent table)Ordinary REINDEX takes SHARE on the table and ACCESS EXCLUSIVE on the index itself. The index lock is not an ACCESS EXCLUSIVE lock on the whole table. 来源
LOCK TABLE … IN SHARE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
CREATE TRIGGERLock the table that owns the trigger. 来源
LOCK TABLE … IN SHARE ROW EXCLUSIVE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
ALTER TABLE … ENABLE / DISABLE TRIGGEREnable or disable triggers, including the applicable USER, ALL, REPLICA and ALWAYS forms. 来源
ALTER TABLE … ADD FOREIGN KEY [NOT VALID] (referencing table)Both referencing and referenced tables take SHARE ROW EXCLUSIVE. NOT VALID skips the initial data check, not this lock. 来源
ALTER TABLE … ADD FOREIGN KEY [NOT VALID] (referenced table)The referenced table also takes SHARE ROW EXCLUSIVE, not merely ACCESS SHARE. 来源
REFRESH MATERIALIZED VIEW CONCURRENTLYLock the materialized view. Ordinary reads continue; only one refresh can run on that view at a time. 来源
LOCK TABLE … IN EXCLUSIVE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
REFRESH MATERIALIZED VIEW (without CONCURRENTLY)Lock the materialized view, blocking ordinary reads as well. 来源
DROP TABLELock the table being dropped. Dependencies and CASCADE can require additional locks. 来源
DROP INDEX (without CONCURRENTLY)Take ACCESS EXCLUSIVE on the parent table, blocking reads and writes; the index is locked too. 来源
TRUNCATELock each table being truncated. Options and dependencies determine the affected table set. 来源
CLUSTERBlock concurrent reads and writes while rewriting the table. 来源
VACUUM FULLBlock concurrent reads and writes while rewriting the table. 来源
VACUUM (tail truncation)Ordinary VACUUM briefly attempts ACCESS EXCLUSIVE when returning empty tail pages to the operating system. Where supported, disabling truncation avoids this phase. 来源
REINDEX (index being rebuilt)Lock the index itself. Planning also accesses indexes, so ordinary REINDEX can still block queries. 来源
LOCK TABLE … IN ACCESS EXCLUSIVE MODERequest this table mode explicitly. Omitting IN MODE defaults to ACCESS EXCLUSIVE. 来源
ALTER TABLE … ADD COLUMNA rewrite depends on version and default expression. Avoiding the rewrite does not reduce the listed lock mode. 来源
ALTER TABLE … DROP COLUMNDrop a column and change the table definition. 来源
ALTER TABLE … ALTER COLUMN … SET / DROP DEFAULTChange the column default. 来源
ALTER TABLE … ALTER COLUMN … TYPEChange the column type. Whether a rewrite is needed does not change this lock requirement. 来源
ALTER TABLE … ALTER COLUMN … SET STORAGEChange the column storage strategy. 来源
ALTER TABLE … ALTER COLUMN … SET COMPRESSIONChange the column compression method. 来源
ALTER TABLE … SET TABLESPACEMove the table to another tablespace. 来源
ALTER TABLE … SET LOGGED / UNLOGGEDChange table persistence. 来源
ALTER TABLE … SET ACCESS METHODChange the table access method. 来源
ALTER TABLE … DROP CONSTRAINTDrop the constraint and any dependent index as applicable. 来源
ALTER TABLE … ALTER CONSTRAINTChange constraint properties. 来源
ALTER TABLE … ALTER COLUMN … SET NOT NULLSet a not-null constraint. 来源
ALTER TABLE … ALTER COLUMN … DROP NOT NULLDrop a not-null constraint. 来源
ALTER TABLE … ENABLE / DISABLE RULEEnable or disable rules. 来源
ALTER TABLE … ENABLE / DISABLE ROW LEVEL SECURITYEnable or disable row-level security. 来源
ALTER TABLE … FORCE / NO FORCE ROW LEVEL SECURITYChange whether the table owner is subject to row-level security. 来源
ALTER TABLE … ALTER COLUMN … DROP EXPRESSIONDrop a generated-column expression. 来源
ALTER TABLE … ALTER COLUMN … SET EXPRESSIONSet a generated-column expression. 来源
ALTER TABLE … ALTER COLUMN … SET GENERATED / RESTART / SET sequence_optionChange identity-column properties or its implicit sequence options. This is not a standalone SET SEQUENCE subcommand. 来源
ALTER TABLE … ALTER COLUMN … ADD GENERATED … AS IDENTITYAdd identity properties to a column. 来源
ALTER TABLE … ALTER COLUMN … DROP IDENTITYDrop identity properties. 来源
ALTER TABLE … INHERIT / NO INHERIT (child table)This entry describes the altered child table; the inheritance parent is locked separately. 来源
ALTER TABLE … OWNER TOChange table ownership. 来源
ALTER TABLE … REPLICA IDENTITYChange replica identity. 来源
ALTER TABLE … RENAMERename the table, column or constraint. 来源
ALTER TABLE … ADD CONSTRAINT (CHECK / UNIQUE / PRIMARY KEY / EXCLUDE)Ordinary constraint creation. Adding a foreign key instead uses SHARE ROW EXCLUSIVE. 来源
ALTER TABLE … ATTACH PARTITION (partition being attached)The table being attached requires ACCESS EXCLUSIVE. An appropriate CHECK constraint can avoid a validation scan but not this lock. 来源
ALTER TABLE … ATTACH PARTITION (default partition)From PostgreSQL 11, an existing default partition requires ACCESS EXCLUSIVE. An appropriate CHECK constraint can avoid a scan but not the lock on the default partition. Descendant scanning follows validation needs. 来源
ALTER TABLE … DETACH PARTITION (parent table)Ordinary detach without CONCURRENTLY. 来源
ALTER TABLE … DETACH PARTITION (partition being detached)Ordinary detach takes ACCESS EXCLUSIVE on the partition being detached. 来源
ALTER TABLE … DETACH PARTITION … CONCURRENTLY (partition, final phase)The second phase takes ACCESS EXCLUSIVE on the detached partition. CONCURRENTLY is not allowed when a default partition exists. 来源
ALTER INDEX … SET TABLESPACELock the index being moved, not the similarly named table. 来源
ALTER INDEX … SET / RESET (fillfactor = …)Lock the index being modified. 来源
ALTER INDEX … ATTACH PARTITIONFrom PostgreSQL 11, modify the partitioned parent index and the child index being attached. 来源
SELECT FOR KEY SHARELock selected rows, with ROW SHARE on the target table. NOWAIT and SKIP LOCKED affect waiting, not mode compatibility. 来源
INSERT (foreign-key check)Checking a non-null foreign key locks matching rows in the referenced table FOR KEY SHARE. The check can run immediately or be deferred. Cascading actions require their own UPDATE or DELETE locks. 来源
UPDATE (foreign-key check)Checking a non-null foreign key locks matching rows in the referenced table FOR KEY SHARE. The check can run immediately or be deferred. Cascading actions require their own UPDATE or DELETE locks. 来源
COPY … FROM (foreign-key check)Checking a non-null foreign key locks matching rows in the referenced table FOR KEY SHARE. The check can run immediately or be deferred. Cascading actions require their own UPDATE or DELETE locks. 来源
MERGE (foreign-key check)Checking a non-null foreign key locks matching rows in the referenced table FOR KEY SHARE. The check can run immediately or be deferred. Cascading actions require their own UPDATE or DELETE locks. 来源
SELECT FOR SHARELock selected rows, with ROW SHARE on the target table. NOWAIT and SKIP LOCKED affect waiting, not mode compatibility. 来源
SELECT FOR NO KEY UPDATELock selected rows, with ROW SHARE on the target table. NOWAIT and SKIP LOCKED affect waiting, not mode compatibility. 来源
UPDATE (non-key update)UPDATE uses this mode when it does not require FOR UPDATE. Key columns are columns of a unique index usable by a foreign key; partial and expression indexes do not qualify. 来源
MERGE … WHEN MATCHED THEN UPDATE (non-key)The UPDATE branch actually runs and does not need FOR UPDATE. Other MERGE actions can require different row locks. 来源
INSERT … ON CONFLICT DO UPDATE (non-key)Lock the existing conflicting row for UPDATE, even when the DO UPDATE WHERE condition excludes the update. Key-column changes determine the mode. 来源
SELECT FOR UPDATELock selected rows, with ROW SHARE on the target table. NOWAIT and SKIP LOCKED affect waiting, not mode compatibility. 来源
UPDATE (key update)Used when updating key columns of a unique index usable by a foreign key, excluding partial and expression indexes. 来源
DELETELock rows being deleted, with ROW EXCLUSIVE on their table. 来源
MERGE … WHEN MATCHED THEN DELETE / UPDATE (key)The matched action deletes a row or updates key columns. An insert-only action does not use this row lock. 来源
INSERT … ON CONFLICT DO UPDATE (key)The conflict action updates key columns of an existing row. This is not a fixed row lock for every INSERT. 来源

From compatibility to actual waits

  • Ordinary SELECT uses MVCC and takes ACCESS SHARE on referenced tables, without taking these row locks.
  • SELECT FOR also takes ROW SHARE; UPDATE and DELETE also take ROW EXCLUSIVE. A row operation can therefore wait on a table lock.
  • The matrix compares different transactions. Locks are normally held until transaction end; rolling back to a savepoint releases locks acquired since that savepoint.
  • Foreign keys, triggers, additional objects, lock queues and transaction snapshots can introduce waits beyond the listed modes. NOWAIT and SKIP LOCKED change waiting behavior.
  • Advisory locks, predicate locks and internal lightweight locks are separate mechanisms. See Explicit Locking and pg_locks.

Find blocking sessions

pg_blocking_pids() considers both holders of conflicting locks and conflicting requests ahead in the queue.

SELECT pid, usename, wait_event_type, wait_event,
       pg_blocking_pids(pid) AS blocking_pids, query
FROM pg_stat_activity
WHERE cardinality(pg_blocking_pids(pid)) > 0
ORDER BY pid;

Visibility of sessions and query text depends on privileges. Individual row locks normally do not appear in pg_locks; a row-lock wait often appears as waiting for the holding transaction.

来源

PostgreSQL 18 · 753057e340da8f22e57c9a409ea7a235fd6a46bd

本地手册

固定版本源码

来源采集日期: 2026-09-27