How Latches are implemented in PostgreSQL?

PostgreSQL uses latches to provide synchronization between different threads accessing shared resources. Latches are lightweight and efficient mechanisms that provide mutual exclusion between threads. Here is a table describing all the PostgreSQL latches, their purpose, and the shared resources they protect:

Latch namePurposeShared resources protected
BgWriterUsed by the background writer to write dirty buffersAccess to the shared buffer pool
CheckpointerUsed by the checkpoint process to write dirty buffersAccess to the shared buffer pool
WALWriteUsed to synchronize WAL writesAccess to the WAL (Write-Ahead Log) buffer
WALInsertLockUsed to serialize inserts into the WAL bufferAccess to the WAL buffer
WALBufMappingUsed to synchronize access to the WAL buffer mappingAccess to the WAL buffer mapping structure
ProcArrayLockUsed to synchronize access to the ProcArray structureAccess to the ProcArray structure, which tracks the status of all processes in the system
SInvalReadUsed to synchronize access to shared-invalidation messagesAccess to the shared-invalidation message queue
SInvalWriteUsed to serialize updates to the shared-invalidation messageAccess to the shared-invalidation message queue
ProcSignalBarrierUsed to synchronize signaling of processesEnsures that signals sent to processes are properly delivered
BtreeVacuumLockUsed to synchronize access to B-tree pages during vacuumAccess to B-tree pages during vacuum
BtreeSplitLockUsed to synchronize access to B-tree pages during splitsAccess to B-tree pages during splits
BtreeDeleteLockUsed to synchronize access to B-tree pages during deletionAccess to B-tree pages during deletion
LWLockTrancheUsed to synchronize access to LWLocksAccess to LWLocks, which are used for finer-grained locking of shared resources
ProcArrayGroupUsed to synchronize process group changesAccess to the process group structure, which tracks groups of processes and their status
SyncScanUsed to synchronize access to sync-scan functionalityAccess to the sync-scan functionality, which is used to scan multiple indexes in parallel
ExtensionLockUsed to synchronize access to shared extension objectsAccess to shared extension objects, which are used to implement extensions in PostgreSQL
AutovacuumWorkItemUsed to synchronize access to autovacuum work itemsAccess to autovacuum work items, which are used to schedule vacuum operations for tables and indexes
AsyncioQueueLockUsed to synchronize access to the asyncio queueAccess to the asyncio queue, which is used for asynchronous I/O operations
ReplicationSlotAllocationLockUsed to synchronize replication slot allocationAccess to the replication slot allocation process, which is used to allocate replication slots for standby servers
ReplicationSlotControlLockUsed to synchronize replication slot controlAccess to the replication slot control process, which is used to manage replication slots and cleanup WAL files
ReplicationOriginLockUsed to synchronize access to replication originsAccess to the replication origin structure, which is used to track the origins of replication streams

These latches are a critical part of PostgreSQL’s architecture, providing efficient and effective synchronization of shared resources across multiple threads. It is important to understand the purpose and use of each latch to ensure

About Shiv Iyer 455 Articles
Open Source Database Systems Engineer with a deep understanding of Optimizer Internals, Performance Engineering, Scalability and Data SRE. Shiv currently is the Founder, Investor, Board Member and CEO of multiple Database Systems Infrastructure Operations companies in the Transaction Processing Computing and ColumnStores ecosystem. He is also a frequent speaker in open source software conferences globally.