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 1pKrzk-0005aO-2m for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Jan 2023 02:28:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1pKrzh-0005Q4-Bb for pgsql-hackers@arkaria.postgresql.org; Thu, 26 Jan 2023 02:28:05 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pKrzh-0005Pv-2G for pgsql-hackers@lists.postgresql.org; Thu, 26 Jan 2023 02:28:05 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1pKrze-00032P-3A for pgsql-hackers@postgresql.org; Thu, 26 Jan 2023 02:28:04 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 30Q2RvYx2878153; Wed, 25 Jan 2023 21:27:57 -0500 From: Tom Lane To: Nathan Bossart cc: Thomas Munro , "Hayato Kuroda (Fujitsu)" , "pgsql-hackers@postgresql.org" Subject: Re: suppressing useless wakeups in logical/worker.c In-reply-to: <20230126003319.GA1507251@nathanxps13> References: <20221202195503.GD2277157@nathanxps13> <20221205173523.GA2714833@nathanxps13> <20230109174217.GA1112698@nathanxps13> <2557815.1674603908@sss.pgh.pa.us> <20230125235004.GA1327755@nathanxps13> <20230126003319.GA1507251@nathanxps13> Comments: In-reply-to Nathan Bossart message dated "Wed, 25 Jan 2023 16:33:19 -0800" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <2878151.1674700077.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Wed, 25 Jan 2023 21:27:57 -0500 Message-ID: <2878152.1674700077@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Nathan Bossart writes: > I think we might risk overflowing "long" when all the wakeup times are > DT_NOEND: > * This is typically used to calculate a wait timeout for WaitLatch() > * or a related function. The choice of "long" as the result type > * is to harmonize with that. It is caller's responsibility that the > * input timestamps not be so far apart as to risk overflow of "long" > * (which'd happen at about 25 days on machines with 32-bit "long"). > Maybe we can adjust that function or create a new one to deal with this. It'd probably be reasonable to file down that sharp edge by instead specifying that TimestampDifferenceMilliseconds will clamp overflowing differences to LONG_MAX. Maybe there should be a clamp on the underflow side too ... but should it be to LONG_MIN or to zero? BTW, as long as we're discussing roundoff gotchas, I noticed while testing your previous patch that there's some inconsistency between TimestampDifferenceExceeds and TimestampDifferenceMilliseconds. What you submitted at [1] did this: + if (TimestampDifferenceExceeds(last_start, now, + wal_retrieve_retry_interval)) + ... + else + { + long elapsed; + + elapsed =3D TimestampDifferenceMilliseconds(last_start, n= ow); + wait_time =3D Min(wait_time, wal_retrieve_retry_interval = - elapsed); + } and I discovered that that could sometimes busy-wait by repeatedly falling through to the "else", but then calculating elapsed =3D=3D wal_retrieve_retry_interval and hence setting wait_time to zero. I fixed it in the committed version [2] by always computing "elapsed" and then checking if that's strictly less than wal_retrieve_retry_interval, but I bet there's existing code with the same issue. I think we need to take a closer look at making TimestampDifferenceMilliseconds' roundoff behavior match the outcome of TimestampDifferenceExceeds comparisons. regards, tom lane [1] https://www.postgresql.org/message-id/20230110174345.GA1292607%40natha= nxps13 [2] https://git.postgresql.org/gitweb/?p=3Dpostgresql.git&a=3Dcommitdiff&h= =3D5a3a95385