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 1kcCIF-0003Km-Kx for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Nov 2020 18:53:31 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kcCIE-0002wZ-KV for pgsql-hackers@arkaria.postgresql.org; Mon, 09 Nov 2020 18:53:30 +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 1kcCIE-0002wS-Du for pgsql-hackers@lists.postgresql.org; Mon, 09 Nov 2020 18:53:30 +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 1kcCIC-0004i2-G9 for pgsql-hackers@lists.postgresql.org; Mon, 09 Nov 2020 18:53:30 +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 0A9IrPq31992986; Mon, 9 Nov 2020 13:53:25 -0500 From: Tom Lane To: Alexey Kondratov cc: PostgreSQL Hackers , Robert Haas Subject: Re: Misuse of TimestampDifference() in the autoprewarm feature of pg_prewarm In-reply-to: <3b1c053a21c07c1ed5e00be3b2b855ef@postgrespro.ru> References: <3b1c053a21c07c1ed5e00be3b2b855ef@postgrespro.ru> Comments: In-reply-to Alexey Kondratov message dated "Mon, 09 Nov 2020 19:45:18 +0300" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <1992984.1604948005.1@sss.pgh.pa.us> Content-Transfer-Encoding: quoted-printable Date: Mon, 09 Nov 2020 13:53:25 -0500 Message-ID: <1992985.1604948005@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Alexey Kondratov writes: > After fixing this issue I have noticed that it still dumps blocks twice = > at each timeout (here I set autoprewarm_interval to 15s): > ... > This happens because at timeout time we were using continue, but = > actually we still have to wait the entire autoprewarm_interval after = > successful dumping. I don't think your 0001 is correct. It would be okay if apw_dump_now() could be counted on to take negligible time, but we shouldn't assume that should we? I agree that the "continue" seems a bit bogus, because it's skipping the ResetLatch call at the bottom of the loop; it's not quite clear to me whether that's a good thing or not. But the general idea of the existing code seems to be to loop around and make a fresh calculation of how-long-to-wait, and that doesn't seem wrong. 0002 seems like a pretty clear bug fix, though I wonder if this is exactly what we want to do going forward. It seems like a very large fraction of the callers of TimestampDifference would like to have the value in msec, which means we're doing a whole lot of expensive and error-prone arithmetic to break down the difference to sec/usec and then put it back together again. Let's get rid of that by inventing, say TimestampDifferenceMilliseconds(...). BTW, I see another bug of a related ilk. Look what postgres_fdw/connection.c is doing: TimestampDifference(now, endtime, &secs, µsecs); /* To protect against clock skew, limit sleep to one minut= e. */ cur_timeout =3D Min(60000, secs * USECS_PER_SEC + microsec= s); /* Sleep until there's something to do */ wc =3D WaitLatchOrSocket(MyLatch, WL_LATCH_SET | WL_SOCKET_READABLE | WL_TIMEOUT | WL_EXIT_ON_PM_DEATH, PQsocket(conn), cur_timeout, PG_WAIT_EXTENSION); WaitLatchOrSocket's timeout is measured in msec not usec. I think the comment about "clock skew" is complete BS, and the Min() calculation was put in as a workaround by somebody observing that the sleep waited too long, but not understanding why. regards, tom lane