Lwlock:BufferMapping

This indicates the heavy activity in shared_buffers.
BufferMapping is a PostgreSQL wait event that occurs when a backend process is waiting to acquire a lock on the shared buffer mapping structure—specifically,
the hash partition lock that protects the buffer lookup table (the buffer mapping lock). This lock is required when locating or pinning a data page in shared buffers,
such as during reads or writes of table or index pages. Contention on BufferMapping typically arises under high concurrency when many sessions simultaneously access different pages,
causing serialized access to the buffer lookup mechanism. This wait event is classified under the LWLock (lightweight lock) category and can indicate a need to increase shared_buffers, adjust access patterns,
or investigate query concurrency bottlenecks.

What does it mean?

To quickly find a data page in memory, PostgreSQL uses a Hash Table. This table maps a "Page ID" (which table and which block number) to a "Buffer ID" (where it is in RAM).
Because this hash table is a shared resource used by every single connection, it is split into 128 partitions (in standard builds). Each partition is protected by an LWLock (Lightweight Lock).

What causes it?

  1. If your shared_buffers is too small to hold the data your queries need, PostgreSQL must constantly "evict" old pages to make room for new ones.
    Every time a page is moved in or out, the hash table must be updated.
    If 100 sessions are all trying to swap pages at the same time, they will collide on the BufferMapping locks.
  2. Queries that perform full table scans on very large tables rapidly "cycle" through the buffer pool. This creates a high volume of requests to the hash table, increasing the likelihood of lock contention
  3. High concurrency: A large number of simultaneous connections accessing different data pages can lead to increased contention for the BufferMapping locks.
  4. Excessive indexes and bloated indexes and unpartitioned huge tables are the common reasons.

How to fix it?