# LWLock: WALInsert

> Waiting to insert WAL data into a memory buffer
---

<div class="event-kicker">PostgreSQL wait event dossier</div>
<div class="event-identity">
  <span>Class<strong>LWLock</strong></span>
  <span>Event<strong><code>WALInsert</code></strong></span>
  <span>Versions<strong>PG 13-18</strong></span>
  <span>Evidence<strong>3 source location(s)</strong></span>
</div>

## Official description {#official-description}

Waiting to insert WAL data into a memory buffer

| PG 13 | PG 14 | PG 15 | PG 16 | PG 17 | PG 18 |
| :---: | :---: | :---: | :---: | :---: | :---: |
| ✓ | ✓ | ✓ | ✓ | ✓ | ✓ |

## Trigger mechanism {#mechanism}

A backend must hold one of the WAL insertion locks while reserving and copying a WAL record into shared WAL buffers. High concurrent WAL producers can queue when insertion critical sections lengthen.

## Normal or trouble? {#normal-vs-trouble}

- <span class="severity-normal">**Normal:**</span> Transient waits are normal during concurrent writes and commit bursts.
- <span class="severity-watch">**Investigate:**</span> A recurring foreground pile-up indicates WAL insertion has become a serialization point, often with very high write concurrency, full-page images, or slow buffer turnover.

## Diagnostic SQL {#diagnostic-sql}

```sql {title="Sessions waiting on LWLock/WALInsert"}
SELECT pid, backend_type, usename, datname, application_name,
       state, now() - query_start AS query_age,
       now() - xact_start AS xact_age,
       wait_event_type, wait_event,
       pg_blocking_pids(pid) AS blocking_pids,
       left(query, 160) AS query
FROM pg_stat_activity
WHERE wait_event_type = 'LWLock'
  AND wait_event = 'WALInsert'
ORDER BY query_age DESC NULLS LAST;
```

```sql {title="Current LWLock cohort"}
SELECT wait_event, count(*) AS waiting_sessions,
       count(*) FILTER (WHERE state = 'active') AS active_waiters,
       max(now() - query_start) AS oldest_query
FROM pg_stat_activity
WHERE wait_event_type = 'LWLock'
GROUP BY wait_event
ORDER BY waiting_sessions DESC, wait_event;
```

```sql {title="Lock and relation context for these sessions"}
SELECT a.pid, l.locktype, l.mode, l.granted, l.fastpath,
       d.datname, n.nspname, c.relname,
       l.page, l.tuple, l.virtualxid, l.transactionid,
       l.classid, l.objid, l.objsubid
FROM pg_stat_activity AS a
LEFT JOIN pg_locks AS l ON l.pid = a.pid
LEFT JOIN pg_database AS d ON d.oid = l.database
LEFT JOIN pg_class AS c
  ON c.oid = l.relation
 AND l.database = (
       SELECT oid FROM pg_database WHERE datname = current_database()
     )
LEFT JOIN pg_namespace AS n ON n.oid = c.relnamespace
WHERE a.wait_event_type = 'LWLock'
  AND a.wait_event = 'WALInsert'
ORDER BY a.pid, l.granted, l.locktype, l.mode;
```

## Response {#response}

1. Rank statements by WAL generation and call rate.
2. Check checkpoint/full-page-image timing and concurrent writer count.
3. Batch tiny writes, reduce needless index churn, and address WAL write latency.

## Source evidence {#source}

- [PostgreSQL 18.6 · src/backend/access/transam/xlog.c:1399](https://github.com/postgres/postgres/blob/REL_18_6/src/backend/access/transam/xlog.c#L1399) — `WALInsert`
- [PostgreSQL 18.6 · src/backend/storage/lmgr/lwlock.c:739](https://github.com/postgres/postgres/blob/REL_18_6/src/backend/storage/lmgr/lwlock.c#L739) — `pgstat_report_wait_start`
- [PostgreSQL 18.6 · src/backend/utils/activity/wait_event_names.txt:372](https://github.com/postgres/postgres/blob/REL_18_6/src/backend/utils/activity/wait_event_names.txt#L372) — `WALInsert`

## Related controls and signals {#related}

- **GUCs:** [[guc:wal_buffers]](/guc/wal-buffers/) · [[guc:full_page_writes]](/guc/full-page-writes/) · [[guc:checkpoint_timeout]](/guc/checkpoint-timeout/)
- **Metrics:** [[metric:waiting_sessions]](/metric/waiting-sessions/) · [[metric:wait_event_share]](/metric/wait-event-share/) · [[metric:wal_bytes]](/metric/wal-bytes/) · [[metric:wal_fpi]](/metric/wal-fpi/) · [[metric:statement_wal_bytes]](/metric/statement-wal-bytes/)

## Typical incident pattern {#incident}

A fan-out job issues many single-row commits immediately after a checkpoint; full-page images and writer concurrency turn WAL insertion into the bottleneck.

---

Backlinks:

- [LWLock](/lwlock/)
