Skip to content

Commit 242dfcb

Browse files
committed
Avoid killing btree items that are already dead
_bt_killitems marks btree items dead when a scan leaves the page where they live, but it does so with only share lock (to improve concurrency). This was historicall okay, since killing a dead item has no consequences. However, with the advent of data checksums and wal_log_hints, this action incurs a WAL full-page-image record of the page. Multiple concurrent processes would write the same page several times, leading to WAL bloat. The probability of this happening can be reduced by only killing items if they're not already dead, so change the code to do that. The problem could eliminated completely by having _bt_killitems upgrade to exclusive lock upon seeing a killable item, but that would reduce concurrency so it's considered a cure worse than the disease. Backpatch all the way back to 9.5, since wal_log_hints was introduced in 9.4. Author: Masahiko Sawada <masahiko.sawada@2ndquadrant.com> Discussion: https://postgr.es/m/CA+fd4k6PeRj2CkzapWNrERkja5G0-6D-YQiKfbukJV+qZGFZ_Q@mail.gmail.com
1 parent 5da1493 commit 242dfcb

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/backend/access/nbtree/nbtutils.c

+9-1
Original file line numberDiff line numberDiff line change
@@ -1854,7 +1854,15 @@ _bt_killitems(IndexScanDesc scan)
18541854
else if (ItemPointerEquals(&ituple->t_tid, &kitem->heapTid))
18551855
killtuple = true;
18561856

1857-
if (killtuple)
1857+
/*
1858+
* Mark index item as dead, if it isn't already. Since this
1859+
* happens while holding a buffer lock possibly in shared mode,
1860+
* it's possible that multiple processes attempt to do this
1861+
* simultaneously, leading to multiple full-page images being
1862+
* set to WAL (if wal_log_hints or data checksums are enabled),
1863+
* which is undesirable.
1864+
*/
1865+
if (killtuple && !ItemIdIsDead(iid))
18581866
{
18591867
/* found the item/all posting list items */
18601868
ItemIdMarkDead(iid);

0 commit comments

Comments
 (0)