public inbox for [email protected]  
help / color / mirror / Atom feed
From: feichanghong <[email protected]>
To: [email protected]
Cc: [email protected]
Subject: Improve the efficiency of _bt_killitems.
Date: Fri, 1 Nov 2024 15:19:59 +0800
Message-ID: <[email protected]> (raw)

Hi hackers,

In the _bt_killitems function, the following logic is present: we search to the
right for an index item that matches the heap TID and attempt to mark it as
dead. If that index item has already been marked as dead by other concurrent
processes, we will continue searching. However, there should not be any more
matching index items on the current page.

```
while (offnum <= maxoff)
{
	...
	/*
		* Mark index item as dead, if it isn't already.  Since this
		* happens while holding a buffer lock possibly in shared mode,
		* it's possible that multiple processes attempt to do this
		* simultaneously, leading to multiple full-page images being sent
		* to WAL (if wal_log_hints or data checksums are enabled), which
		* is undesirable.
		*/
	if (killtuple && !ItemIdIsDead(iid))
	{
		/* found the item/all posting list items */
		ItemIdMarkDead(iid);
		killedsomething = true;
		break;			/* out of inner search loop */
	}
	offnum = OffsetNumberNext(offnum);
}
```

Perhaps we should exit the current loop immediately when the killtuple is set,
stopping the search to the right. This could improve efficiency, especially
when the index item to be killed is the first one on the current page:

```
diff --git a/src/backend/access/nbtree/nbtutils.c b/src/backend/access/nbtree/nbtutils.c
index b4ba51357a5..529a3083165 100644
--- a/src/backend/access/nbtree/nbtutils.c
+++ b/src/backend/access/nbtree/nbtutils.c
@@ -4298,11 +4298,14 @@ _bt_killitems(IndexScanDesc scan)
              * to WAL (if wal_log_hints or data checksums are enabled), which
              * is undesirable.
              */
-            if (killtuple && !ItemIdIsDead(iid))
+            if (killtuple)
             {
-                /* found the item/all posting list items */
-                ItemIdMarkDead(iid);
-                killedsomething = true;
+                if (!ItemIdIsDead(iid))
+                {
+                    /* found the item/all posting list items */
+                    ItemIdMarkDead(iid);
+                    killedsomething = true;
+                }
                 break;          /* out of inner search loop */
             }
             offnum = OffsetNumberNext(offnum);
```


Best Regards,
Fei Changhong



view thread (5+ messages)  latest in thread

reply

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Reply to all the recipients using the --to and --cc options:
  reply via email

  To: [email protected]
  Cc: [email protected], [email protected], [email protected]
  Subject: Re: Improve the efficiency of _bt_killitems.
  In-Reply-To: <[email protected]>

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

This inbox is served by agora; see mirroring instructions
for how to clone and mirror all data and code used for this inbox