From: Pavan Deolasee <pavan.deolasee@gmail.com>
To: Peter Geoghegan <pg@bowt.ie>
Cc: Andrew Dunstan <andrew@dunslane.net>
Cc: pgsql-committers <pgsql-committers@lists.postgresql.org>
Subject: Re: pgsql: Optimize btree insertions for common case of increasing values
Date: Wed, 28 Mar 2018 14:44:44 +0530
Message-ID: <CABOikdMCoXG3pMU7zUjO2wdVudvZ-5SzD7McedzRbitN_VvuUQ@mail.gmail.com> (raw)
In-Reply-To: <CAH2-WzmpBA3TVtQCkkupVhxJ7DNsaosnmB9Maiogw0K58BKCKg@mail.gmail.com>
References: <E1f0Qx3-0004Z5-KN@gemulon.postgresql.org>
<CAH2-WzmpBA3TVtQCkkupVhxJ7DNsaosnmB9Maiogw0K58BKCKg@mail.gmail.com>
On Wed, Mar 28, 2018 at 11:33 AM, Peter Geoghegan <pg@bowt.ie> wrote:
>
>
> This code can allocate memory in a critical section, since
> RelationSetTargetBlock() can call smgropen():
>
> if (P_ISLEAF(lpageop))
> + {
> xlinfo = XLOG_BTREE_INSERT_LEAF;
> +
> + /*
> + * Cache the block information if we just inserted into the
> + * rightmost leaf page of the index.
> + */
> + if (P_RIGHTMOST(lpageop))
> + RelationSetTargetBlock(rel, BufferGetBlockNumber(buf));
> + }
>
> rd_smgr can be closed by a relcache flush, so it is in fact possible
> that we'll reach smgropen().
>
I think you're right. I propose that we call RelationSetTargetBlock right
after the critical section ends. That should not have any adverse side
effect since we're still holding WRITE lock on the page, it's just an hint
anyways and the next time we shall do a proper recheck.
>
> As I already pointed out, it's unclear why some of these tests are
> used, such as P_INCOMPLETE_SPLIT():
>
> + /*
> + * Check if the page is still the rightmost leaf page, has
> enough
> + * free space to accommodate the new tuple, no split is in
> progress
> + * and the scankey is greater than or equal to the first key
> on the
> + * page.
> + */
> + if (P_ISLEAF(lpageop) && P_RIGHTMOST(lpageop) &&
> + !P_INCOMPLETE_SPLIT(lpageop) &&
> + !P_IGNORE(lpageop) &&
> + (PageGetFreeSpace(page) > itemsz) &&
> + PageGetMaxOffsetNumber(page) >=
> P_FIRSTDATAKEY(lpageop) &&
> + _bt_compare(rel, natts, itup_scankey, page,
> + P_FIRSTDATAKEY(lpageop)) > 0)
>
> The BTP_INCOMPLETE_SPLIT bit means that the page's right sibling's
> downlink is missing, but how can a rightmost page have a right sibling
> in the first place? We don't bother to check that when we already know
> the page is rightmost within _bt_moveright() or _bt_findinsertloc().
>
Hmm. I agree. I am sorry, I missed that comment during the review process.
I checked the code again and BTP_INCOMPLETE_SPLIT is always set along with
the right-link. So the rightmost page, which does not have a right-link,
must not have BTP_INCOMPLETE_SPLIT set. That test is thus redundant (though
it should not give any wrong answers).
Previously, we agreed that P_IGNORE() is required. So I assume no issues
there. The other tests seem required too for us to conclusively decide to
use the cached block.
>
> I also noticed that favorable/favourable is misspelled "favourble".
> Also, "workout" is a noun.
>
Fixed in the attached delta.
Please have a look.
Thanks,
Pavan
--
Pavan Deolasee http://www.2ndQuadrant.com/
PostgreSQL Development, 24x7 Support, Training & Services
Attachments:pg_btree_target_block_v4_delta.patch (application/octet-stream, 2.1K) downloaddiff --git a/src/backend/access/nbtree/nbtinsert.c b/src/backend/access/nbtree/nbtinsert.cindex e85abcfd72..ed9e703f71 100644--- a/src/backend/access/nbtree/nbtinsert.c+++ b/src/backend/access/nbtree/nbtinsert.c@@ -167,13 +167,11 @@ top:
* but we need to be consistent */
/*
- * Check if the page is still the rightmost leaf page, has enough- * free space to accommodate the new tuple, no split is in progress- * and the scankey is greater than or equal to the first key on the- * page.+ * Check if the page is still the rightmost valid leaf page, has+ * enough free space to accommodate the new tuple and the scankey+ * is strictly greater than the first key on the page.
*/
if (P_ISLEAF(lpageop) && P_RIGHTMOST(lpageop) &&
- !P_INCOMPLETE_SPLIT(lpageop) &&
!P_IGNORE(lpageop) &&
(PageGetFreeSpace(page) > itemsz) &&
PageGetMaxOffsetNumber(page) >= P_FIRSTDATAKEY(lpageop) &&
@@ -187,9 +185,9 @@ top:
_bt_relbuf(rel, buf);
/*
- * Something did not workout. Just forget about the cached+ * Something did not work out. Just forget about the cached
* block and follow the normal path. It might be set again if
- * the conditions are favourble.+ * the conditions are favourable.
*/
RelationSetTargetBlock(rel, InvalidBlockNumber);
}
@@ -970,16 +968,7 @@ _bt_insertonpg(Relation rel,
XLogRegisterData((char *) &xlrec, SizeOfBtreeInsert);
if (P_ISLEAF(lpageop))
- {
xlinfo = XLOG_BTREE_INSERT_LEAF;
-- /*- * Cache the block information if we just inserted into the- * rightmost leaf page of the index.- */- if (P_RIGHTMOST(lpageop))- RelationSetTargetBlock(rel, BufferGetBlockNumber(buf));- }
else
{
/*
@@ -1032,6 +1021,13 @@ _bt_insertonpg(Relation rel,
END_CRIT_SECTION();
+ /*+ * Cache the block information if we just inserted into the+ * rightmost leaf page of the index.+ */+ if (P_ISLEAF(lpageop) && P_RIGHTMOST(lpageop))+ RelationSetTargetBlock(rel, BufferGetBlockNumber(buf));+
/* release buffers */
if (BufferIsValid(metabuf))
_bt_relbuf(rel, metabuf);
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 from your mail client; the link fills in
the headers below: reply via email
Some clients, webmail especially, drop In-Reply-To and References from
a link, and the reply then starts a new thread: use the mbox below.
To: pgsql-committers@postgresql.org
Cc: pavan.deolasee@gmail.com, pg@bowt.ie, andrew@dunslane.net
Subject: Re: pgsql: Optimize btree insertions for common case of increasing values
In-Reply-To: <CABOikdMCoXG3pMU7zUjO2wdVudvZ-5SzD7McedzRbitN_VvuUQ@mail.gmail.com>
References: <E1f0Qx3-0004Z5-KN@gemulon.postgresql.org> <CAH2-WzmpBA3TVtQCkkupVhxJ7DNsaosnmB9Maiogw0K58BKCKg@mail.gmail.com> <CABOikdMCoXG3pMU7zUjO2wdVudvZ-5SzD7McedzRbitN_VvuUQ@mail.gmail.com>
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
This inbox is served by DDX for PostgreSQL; see mirroring instructions
for how to clone and mirror all data and code used for this inbox