postgresql/src/backend/storage
Michael Paquier db4d12fc97 Fix procLatch ownership race in ProcKill()
DisownLatch() was executed after the PGPROC entry of the process
terminated is pushed back into a freelist.  A newly-forked backend that
recycles the slot could call OwnLatch() and PANIC with a "latch already
owned by PID", taking down the server.

There were two scenarios related to lock groups where this issue could
be reached:
* A follower pushes the leader's PGPROC back to the freelist while the
leader has not yet called DisownLatch() in its own ProcKill().
* A leader outliving all its followers pushes its own PGPROC onto the
freelist before reaching DisownLatch(), which would be the most common
scenario.

This issue is fixed by calling SwitchBackToLocalLatch() and
DisownLatch() at an earlier phase of ProcKill(), before any freelist
manipulation happens, so that the slot of the backend terminated is
never exposed as owning a latch.

Note that pgstat_reset_wait_event_storage() is kept at a later stage.
An upcoming commit will take advantage of that by introducing a test
able to check the original PANIC scenario.

Author: Vlad Lesin <vladlesin@gmail.com>
Reviewed-by: Andrey Borodin <x4mmm@yandex-team.ru>
Reviewed-by: Michael Paquier <michael@paquier.xyz>
Discussion: https://postgr.es/m/d2983796-2603-41b7-a66e-fc8489ddb954@gmail.com
Backpatch-through: 14
2026-05-27 17:20:00 +09:00
..
buffer Assert lack of hazardous buffer locks before possible catalog read. 2025-12-16 16:13:56 -08:00
file Make safeguard against incorrect flags for fsync more portable. 2025-07-01 12:08:57 -04:00
freespace Mark modified the FSM buffer as dirty during recovery 2026-05-03 20:26:07 +03:00
ipc Make palloc_array() and friends safe against integer overflow. 2026-05-11 05:13:51 -07:00
large_object Fix snapshot reference leak if lo_export fails. 2021-11-03 10:54:33 +02:00
lmgr Fix procLatch ownership race in ProcKill() 2026-05-27 17:20:00 +09:00
page Initial pgindent and pgperltidy run for v14. 2021-05-12 13:14:10 -04:00
smgr Fix 'unexpected data beyond EOF' on replica restart 2026-01-15 20:58:05 +02:00
sync Fix race between DROP TABLESPACE and checkpointing. 2022-03-16 17:20:50 +13:00
Makefile Refactor the fsync queue for wider use. 2019-04-04 23:38:38 +13:00