11
Resource Type: Relation
Modes
Access Share SELECT
Row Share SELECT FOR UPDATE/SHARE
Row Exclusive UPDATE, DELETE, INSERT, MERGE
Share Update Exclusive VACUUM, ANALYZE, ALTER TABLE,
CREATE INDEX CONCURRENTLY
Share CREATE INDEX
Share Row Exclusive CREATE TRIGGER, ALTER TABLE
Exclusive REFRESH MATERIALIZED VIEW
CONCURRENTLY
Access Exclusive DROP, TRUNCATE, REINDEX, VACUUM FULL,
LOCK TABLE, ALTER TABLE,
REFRESH MATERIALIZED VIEW
Relation Locks
allow
concurrent
change
in the table
A particularly important type of locks is relation (tables, indexes, sequences,
etc.) locks. These locks are identified by the relation value in the
pg_locks.locktype column.
They have 8 different modes as shown on the slide together with examples
of SQL commands that use each mode. The compatibility matrix showing
which locks can be acquired simultaneously is provided in the
documentation.
Such a variety of lock modes is to allow as many operations as possible that
refer to the same table (or index, etc.) to run concurrently.
The weakest mode is Access Share, which is acquired by the SELECT
command. It is compatible with any modes except Access Exclusive, the
strongest one. This means that a query does not interfere with other queries,
data changes, or other operations, but prevents operations like dropping the
table while data is being read from it.
Another example: Share mode (along with other stronger modes) is
incompatible with commands that modify data. For example, the CREATE
INDEX command blocks INSERT, UPDATE, and DELETE commands (and
vice versa). Therefore, the CREATE INDEX CONCURRENTLY command
exists, it uses Share Update Exclusive mode, which is compatible with such
data changes (though the command takes longer to complete).