liveBook · Manning

Lightweight lock

As explained in PostgreSQL Mistakes and How to Avoid Them

A lightweight lock is a PostgreSQL synchronization primitive that protects shared-memory data from conflicting concurrent access.

A lightweight lock (LWLock), sometimes called a latch, protects PostgreSQL shared-memory data so concurrent reads and writes remain consistent. Accessing the same objects from many connections can make these locks heavily contended; because queuing is not fair, lockers may slow one another and reduce throughput. section 6.3

The 2,000-connection mistake

A workload that opens 2,000 clients may appear highly parallel, but a 2,000-client pgbench run produced only 352.1 TPS with 5,535 ms average latency. Lower client counts produced better throughput and latency, showing why unrestricted concurrency can overload shared resources.

Use these operational signals and responses:

  • Check pg_stat_activity for abundant wait_event_type: LWLock entries when excessive concurrency is suspected. section 6.3
  • Restrict client connections and let application requests queue; if that is impossible, place PgBouncer between the application and database. section 6.3

How PostgreSQL Mistakes and How to Avoid Them covers it

How deep it goes
Explains shared-memory protection, unfair contention, monitoring signals, and connection-management responses without detailing lock implementation internals.
Examples
Uses pgbench results comparing 2,000 through 16 clients, including TPS, latency, and LWLock wait observations.
What you can do after
Diagnose/debug with pg_stat_activity and reduce lock pressure by limiting connections or configuring PgBouncer.
How it connects
Connects lightweight locks to MVCC, shared memory, concurrent connections, pg_stat_activity, and connection pooling.

Related concepts

This explainer was generated by Manning's knowledge graph from the text of PostgreSQL Mistakes and How to Avoid Them. The section links point to the parts of the book it draws on.