Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f17Ak-0000Sf-6Q for pgsql-committers@arkaria.postgresql.org; Wed, 28 Mar 2018 09:15:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f17Aj-0000G7-8d for pgsql-committers@arkaria.postgresql.org; Wed, 28 Mar 2018 09:15:09 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f17Ai-0000Fs-PM for pgsql-committers@lists.postgresql.org; Wed, 28 Mar 2018 09:15:09 +0000 Received: from mail-oi0-x242.google.com ([2607:f8b0:4003:c06::242]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1f17Af-0008TD-KN for pgsql-committers@lists.postgresql.org; Wed, 28 Mar 2018 09:15:07 +0000 Received: by mail-oi0-x242.google.com with SMTP id 71-v6so1508142oie.12 for ; Wed, 28 Mar 2018 02:15:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :cc; bh=pgTedW6A+X154ucNdiq356+O2r64BO37pNAjs2S1qPo=; b=Z/sfARs0PVZRTO3zlWyq7SSESw8xPJtuI22gD3f6/kQQ94/tLy2Iqhp2Lh9LoAmgus 8Swh3M3RjcKF4a46hKP93TN7YyxouWO/dPsoyvYKkj8UimWgNv9hNut2zjFBpiyFkO7/ gnz6KF881tOgHJtv1qqvGoZkYOfTwu1RvtfKybWRM7TPFIboj6df3Y8MfZ4i3u866Tvv wIOZ+6rwjPjpd+hhqrS4963CGa3nCg+OfcbhJHG2JwQbaDOe3TEzBjZklniHRS+3rewh aaMppYfKmK3WQ5VPs82yveBaKX82EmUXhixD0o9BNZaMaqep+h2ysoKSMP0Eoe8/PY0g IllQ== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:in-reply-to:references:from:date :message-id:subject:to:cc; bh=pgTedW6A+X154ucNdiq356+O2r64BO37pNAjs2S1qPo=; b=epnATJoTEPIusCfWkmc+73/Il9MSFzoji3J33agQB3ympXcCTWO3zk0c/wZATNPbFu eDJy2FAZlixb0zB7SZqrLmmzvkyJlzkTrVEY1U3+UA8oAqp9Fr6URyG3afyEoBm+vm9c 4qfMce9smK2w6Vco1l/vh6QHVRW067Y0DuXk/MxlIMIxCrUkTKkjTKJhjAYmuSJ0qCBu piEw8E5pIGl/mpL6/INzirtliakuhKa+8XQZlitpC1kc1Vpg9Obp0fO9EFdlfL68dZkx dVsMCLzyUnS/mqCN1S9+KCIaoqXngAf6n8eSN8WS2wDBZlZldjKzYPN0DiPwblCm393s Jwnw== X-Gm-Message-State: ALQs6tCZd05Qqtbh05ux7a4mZpqEqgU+1x4mIET4B7cVfeivmntztSrc PqnUURhu51aDoA9yiIx43WCkpJN79m+7P1lF0Zs= X-Google-Smtp-Source: AIpwx49zlmG9KlqYj9U+TL5pFLtkXxKwkBHKid8StFhjqRFYVe4BDff1ythEweinkXNUfaTe+JtEb9rPWd5umF6W4QE= X-Received: by 10.202.236.78 with SMTP id k75mr1622465oih.81.1522228504622; Wed, 28 Mar 2018 02:15:04 -0700 (PDT) MIME-Version: 1.0 Received: by 10.74.202.145 with HTTP; Wed, 28 Mar 2018 02:14:44 -0700 (PDT) In-Reply-To: References: From: Pavan Deolasee Date: Wed, 28 Mar 2018 14:44:44 +0530 Message-ID: Subject: Re: pgsql: Optimize btree insertions for common case of increasing values To: Peter Geoghegan Cc: Andrew Dunstan , pgsql-committers Content-Type: multipart/mixed; boundary="001a1134f8a649275105687571e6" List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --001a1134f8a649275105687571e6 Content-Type: multipart/alternative; boundary="001a1134f8a649274f05687571e4" --001a1134f8a649274f05687571e4 Content-Type: text/plain; charset="UTF-8" On Wed, Mar 28, 2018 at 11:33 AM, Peter Geoghegan 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 --001a1134f8a649274f05687571e4 Content-Type: text/html; charset="UTF-8" Content-Transfer-Encoding: quoted-printable


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():

=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 if (P_ISLEAF(lpageop))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0{
=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 xlinfo =3D XLOG_BTR= EE_INSERT_LEAF;
+
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * Cache the block = information if we just inserted into the
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * rightmost leaf p= age of the index.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (P_RIGHTMOST(lpa= geop))
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0Relat= ionSetTargetBlock(rel, BufferGetBlockNumber(buf));
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0}

rd_smgr can be closed by a relcache flush, so it is in fact possible
that we'll reach smgropen().

I thin= k you're right. I propose that we call=C2=A0R= elationSetTargetBlock right after the critical section ends. That should no= t 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 pro= per recheck.=C2=A0
=C2=A0

As I already pointed out, it's unclear why some of these tests are
used, such as P_INCOMPLETE_SPLIT():

+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0/*
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * Check if the page is still the= rightmost leaf page, has enough
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * free space to accommodate the = new tuple, no split is in progress
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * and the scankey is greater tha= n or equal to the first key on the
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 * page.
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 */
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0if (P_ISLEAF(lpageop) && = P_RIGHTMOST(lpageop) &&
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0!P_IN= COMPLETE_SPLIT(lpageop) &&
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0!P_IG= NORE(lpageop) &&
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0(Page= GetFreeSpace(page) > itemsz) &&
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0PageG= etMaxOffsetNumber(page) >=3D P_FIRSTDATAKEY(lpageop) &&
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0_bt_c= ompare(rel, natts, itup_scankey, page,
+=C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0P_FIRSTDATAKEY(lpageop)) > 0)

The BTP_INCOMPLETE_SPLIT bit means that the page's right sibling's<= br> 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<= br> the page is rightmost within _bt_moveright() or _bt_findinsertloc().

Hmm. I agree. I am sorry, I missed that commen= t during the review process. I checked the code again and=C2=A0BTP_INCOMPLE= TE_SPLIT is always set along with the right-link. So the rightmost page, wh= ich does not have a right-link, must not have=C2=A0
Previously, we agreed that=C2=A0P_IGNORE() is required. So I assume no issues there. The oth= er tests seem required too for us to conclusively decide to use the cached = block.
=C2=A0=C2=A0

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

--
=
=C2=A0Pavan Deolasee =C2=A0 =C2=A0 =C2=A0 =C2=A0 =C2= =A0 =C2=A0 =C2=A0 =C2=A0 =C2=A0 http://www.2ndQuadrant.com/
=C2=A0PostgreSQL Developm= ent, 24x7 Support, Training & Services
--001a1134f8a649274f05687571e4-- --001a1134f8a649275105687571e6 Content-Type: application/octet-stream; name="pg_btree_target_block_v4_delta.patch" Content-Disposition: attachment; filename="pg_btree_target_block_v4_delta.patch" Content-Transfer-Encoding: base64 X-Attachment-Id: f_jfavhff10 ZGlmZiAtLWdpdCBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9uYnRyZWUvbmJ0aW5zZXJ0LmMgYi9zcmMv YmFja2VuZC9hY2Nlc3MvbmJ0cmVlL25idGluc2VydC5jCmluZGV4IGU4NWFiY2ZkNzIuLmVkOWU3 MDNmNzEgMTAwNjQ0Ci0tLSBhL3NyYy9iYWNrZW5kL2FjY2Vzcy9uYnRyZWUvbmJ0aW5zZXJ0LmMK KysrIGI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