Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khVh3-000271-Kl for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 10:37:05 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khVh1-0002QI-G8 for pgsql-hackers@arkaria.postgresql.org; Tue, 24 Nov 2020 10:37:03 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1khVh1-0002Pk-7G for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 10:37:03 +0000 Received: from mail.postgrespro.ru ([93.174.131.139]) by makus.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1khVgy-0008UA-7u for pgsql-hackers@lists.postgresql.org; Tue, 24 Nov 2020 10:37:02 +0000 Received: from [192.168.1.70] (ppp91-79-61-252.pppoe.mtu-net.ru [91.79.61.252]) (using TLSv1.2 with cipher ECDHE-RSA-AES128-GCM-SHA256 (128/128 bits)) (Client did not present a certificate) by mail.postgrespro.ru (Postfix) with ESMTPSA id B46DE21C3C00; Tue, 24 Nov 2020 13:36:58 +0300 (MSK) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=postgrespro.ru; s=mail; t=1606214218; bh=8E7bQ2DZRrqbwUegDst78q492qDrFbfkXczXEojhpfY=; h=Subject:To:Cc:References:From:Date:In-Reply-To; b=Ulik2JBK1NAT9gXZGtMQCZTCTyfTvh1FL/m14VDrah/EvjG0GhlEI55yz4yIC7gCx MbXrigbzQMZXSHAikSitzC7kMqNhBKPHzRH7UeCLvaUF2KNSksDe9hQdNi9J40g5bm tQrgOTRqTaQzeV/qpvhj5vRjKz2hWL56pc6jSByM= Subject: Re: LogwrtResult contended spinlock To: Andres Freund , Alvaro Herrera Cc: pgsql-hackers@lists.postgresql.org, Jaime Casanova References: <20200903183452.GA10634@alvherre.pgsql> <20200904170545.ko4bwixl3yuu5jxz@alap3.anarazel.de> <20200904171355.bldopurt65m2aqn4@alap3.anarazel.de> From: Anastasia Lubennikova Message-ID: <45bc9909-8f6c-74d8-83ac-d86d35e147cc@postgrespro.ru> Date: Tue, 24 Nov 2020 13:36:58 +0300 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <20200904171355.bldopurt65m2aqn4@alap3.anarazel.de> Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 8bit Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 04.09.2020 20:13, Andres Freund wrote: > Hi, > > On 2020-09-04 10:05:45 -0700, Andres Freund wrote: >> On 2020-09-03 14:34:52 -0400, Alvaro Herrera wrote: >>> Looking at patterns like this >>> >>> if (XLogCtl->LogwrtRqst.Write < EndPos) >>> XLogCtl->LogwrtRqst.Write = EndPos; >>> >>> It seems possible to implement with >>> >>> do { >>> XLogRecPtr currwrite; >>> >>> currwrite = pg_atomic_read_u64(LogwrtRqst.Write); >>> if (currwrite > EndPos) >>> break; // already done by somebody else >>> if (pg_atomic_compare_exchange_u64(LogwrtRqst.Write, >>> currwrite, EndPos)) >>> break; // successfully updated >>> } while (true); >>> >>> This assumes that LogwrtRqst.Write never goes backwards, so it doesn't >>> seem good material for a general routine. >>> >>> This *seems* correct to me, though this is muddy territory to me. Also, >>> are there better ways to go about this? >> Hm, I was thinking that we'd first go for reading it without a spinlock, >> but continuing to write it as we currently do. >> >> But yea, I can't see an issue with what you propose here. I personally >> find do {} while () weird and avoid it when not explicitly useful, but >> that's extremely minor, obviously. > Re general routine: On second thought, it might actually be worth having > it. Even just for LSNs - there's plenty places where it's useful to > ensure a variable is at least a certain size. I think I would be in > favor of a general helper function. Do you mean by general helper function something like this? void swap_lsn(XLogRecPtr old_value, XLogRecPtr new_value, bool to_largest) {   while (true) {     XLogRecPtr  currwrite;     currwrite = pg_atomic_read_u64(old_value);     if (to_largest)       if (currwrite > new_value)         break;  /* already done by somebody else */     else       if (currwrite < new_value)         break;  /* already done by somebody else */     if (pg_atomic_compare_exchange_u64(old_value,                        currwrite, new_value))     break;  /* already done by somebody else */   } } which will be called like swap_lsn(XLogCtl->LogwrtRqst.Write, EndPos, true); > > Greetings, > > Andres Freund This CF entry was inactive for a while. Alvaro, are you going to continue working on it? -- Anastasia Lubennikova Postgres Professional: http://www.postgrespro.com The Russian Postgres Company