Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSCl6-005A43-In for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 09:40:08 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1sSCl4-006d2r-G7 for pgsql-hackers@arkaria.postgresql.org; Fri, 12 Jul 2024 09:40:06 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSCl3-006d2i-Io for pgsql-hackers@lists.postgresql.org; Fri, 12 Jul 2024 09:40:06 +0000 Received: from fout2-smtp.messagingengine.com ([103.168.172.145]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1sSCkz-001khR-92 for pgsql-hackers@postgresql.org; Fri, 12 Jul 2024 09:40:04 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailfout.nyi.internal (Postfix) with ESMTP id 979A31380164; Fri, 12 Jul 2024 05:39:58 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute5.internal (MEProxy); Fri, 12 Jul 2024 05:39:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:content-type:date:date:feedback-id:feedback-id :from:from:in-reply-to:in-reply-to:message-id:mime-version :reply-to:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm2; t=1720777198; x= 1720863598; bh=+IBRtBGsC/0gbEfbSU4VC4B18OI8T+jtp1KHRH8MqI0=; b=E JAQGpwWrY+UUqmaanYmHfJ/tUAzIbXPSkUVrszVzWAv/LGtoP2GGq2q29IHM4FwZ TWSzwI9CdRRr1mnHGYtqlQoUmCN/Coy9p+QPJgfjDUsPAx7SIYeS6FoWM0d+6KbS 9g8kxhxV91PTFg6IlTJBMoPDfD+VWoq3IQCBtjiOk6Rs+r5RaWSwJLnuUKqDXU0T alazU5dI0PmjYyQttNB0HonSffbZyaTar5UGe8K2/ZDz1Ta0jmxvBtTP4AyKbnse HoGQEKNrZF1GZaDURIHEWAedcopH7RNMzAHRE5hOvS6S1UAnQ+DvqLtJX/q6jNr2 and0Wn89iF2K73X/U155Q== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeeftddrfeeigddugecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenog fuuhhsphgvtghtffhomhgrihhnucdlgeelmdenucfjughrpeffhffvvefukfggtggugfgj sehtkeertddttdejnecuhfhrohhmpeetlhhvrghrohcujfgvrhhrvghrrgcuoegrlhhvhh gvrhhrvgesrghlvhhhrdhnohdqihhprdhorhhgqeenucggtffrrghtthgvrhhnpeeutdet keeluddutdffueelkeffkeehieelveevjeelfeejveeludejvefgtefgveenucffohhmrg hinhepfihorhguphhrvghsshdrtghomhdpvghnthgvrhhprhhishgvuggsrdgtohhmnecu vehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprghlvhhhvg hrrhgvsegrlhhvhhdrnhhoqdhiphdrohhrgh X-ME-Proxy: Feedback-ID: ia2694551:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Fri, 12 Jul 2024 05:39:56 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=alvh.no-ip.org; s=schmee; t=1720777194; bh=MUHa3QmuDq8xNx3uWm89yOr831ZIYVm7UagREibbuWI=; h=Date:From:To:Cc:Subject:In-Reply-To:From; b=iKcMMKdNDLpZHFYg7HUMG+uQpMs0zoeg7oSJZVNaAyqz1xfUhrWD7vqWbiHPTaug+ FbMQV0m1whcCmO/5mkEjvqv51b3oIGt0JzinixNVLljnZZNw30Lg8NUQUU/kGR0tx8 D3VX+QlVQaE33jmHdJ1dfYavBfMgUsesytoTKrmO3WpwYP5T+V4/J4P6xMhmJOP2XA qc34UWzW5po3gPnEjk8W7rwrTh19EKU6I6l2i5TkRPqXWjz6E66FpCsiFAzmju6or8 3AlsOFZslfRPdPXJxOXi+3R9NaMZ8pLL/VFynfRhI5/srDUU8wre4Iyk/eIhnpP9D3 JGTJF3UukNE8Q== Received: by schmee.alvh.no-ip.org (Postfix, from userid 1000) id 8D05A529; Fri, 12 Jul 2024 11:39:54 +0200 (CEST) Date: Fri, 12 Jul 2024 11:39:54 +0200 From: Alvaro Herrera To: Nathan Bossart Cc: Sami Imseih , Bertrand Drouvot , Tom Lane , Robert Haas , "pgsql-hackers@postgresql.org" Subject: Re: Restart pg_usleep when interrupted Message-ID: <202407120939.pr6wpjffmxov@alvherre.pgsql> MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 2024-Jul-11, Nathan Bossart wrote: > I'm imagining something like this: > > struct timespec delay; > TimestampTz end_time; > > end_time = TimestampTzPlusMilliseconds(GetCurrentTimestamp(), msec); > > do > { > long secs; > int microsecs; > > TimestampDifference(GetCurrentTimestamp(), end_time, > &secs, µsecs); > > delay.tv_sec = secs; > delay.tv_nsec = microsecs * 1000; > > } while (nanosleep(&delay, NULL) == -1 && errno == EINTR); This looks nicer. We could deal with clock drift easily (in case the sysadmin winds the clock back) by testing that tv_sec+tv_nsec is not higher than the initial time to sleep. I don't know how common this situation is nowadays, but I remember debugging a system years ago where autovacuum was sleeping for a very long time because of that. I can't remember now if we did anything in the code to cope, or just told sysadmins not to do that anymore :-) FWIW my (Linux's) nanosleep() manpage contains this note: If the interval specified in req is not an exact multiple of the granu‐ larity underlying clock (see time(7)), then the interval will be rounded up to the next multiple. Furthermore, after the sleep completes, there may still be a delay before the CPU becomes free to once again execute the calling thread. It's not clear to me what happens if the time to sleep is zero, so maybe there should be a "if tv_sec == 0 && tv_nsec == 0 then break" statement at the bottom of the loop, to quit without sleeping one more time than needed. For Windows, this [1] looks like an interesting and possibly relevant read (though maybe SleepEx already does what we want to do here.) [1] https://randomascii.wordpress.com/2020/10/04/windows-timer-resolution-the-great-rule-change/ -- Álvaro Herrera 48°01'N 7°57'E — https://www.EnterpriseDB.com/ "Having your biases confirmed independently is how scientific progress is made, and hence made our great society what it is today" (Mary Gardiner)