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 1n8DNC-0004Y9-UX for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jan 2022 03:35: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 1n8DNA-0005jf-U0 for pgsql-hackers@arkaria.postgresql.org; Fri, 14 Jan 2022 03:35:28 +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 1n8DN9-0005jV-P0 for pgsql-hackers@lists.postgresql.org; Fri, 14 Jan 2022 03:35:28 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1n8DN2-0002My-E7 for pgsql-hackers@postgresql.org; Fri, 14 Jan 2022 03:35:26 +0000 Received: from compute5.internal (compute5.nyi.internal [10.202.2.45]) by mailout.nyi.internal (Postfix) with ESMTP id 8837E5C0181; Thu, 13 Jan 2022 22:35:19 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute5.internal (MEProxy); Thu, 13 Jan 2022 22:35:19 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=anarazel.de; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=auGOr8eFbhDNrDUgmE31REdPL1x cH7plo4I9dLM7NDk=; b=lHNLul0uCyMZm/bqbORyYylIid8L/Ujow/LCdlQRDXU fO2DW4ABVAYJNjvjnxIHaklGIiNjxbloF5SGNqyXRqJ6ByghF4JA0Ev1F3NN0mIB /EAlujeL/SyDNA/qlIyQUSRgXOwhCvHsDyf1PNmaFKxNhc3ywTf2PTRm7vRIMcgv Qq7Ys4/opAvHU0BNWLZhhXNdzo2rQBrQYLFdqYSzh49jeK92E3Wfo6b9vpX2vzmC qr5tu3QtTDrQ3d56NFmj9884yhMB3a7aB6siK9r7AxjbsOPgJUrqH1p8h+h/oNqd 1L1rKmj0OQY67CHAB+gKvPYoZTjLef/V6Z8CJbPGtDQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=auGOr8 eFbhDNrDUgmE31REdPL1xcH7plo4I9dLM7NDk=; b=OCv7365o37hjD+h8kXebK8 Ub9eJTk/UfUpJqE6kwz1BzqxzJag0V71C1uoOz9gw2ouqsy2UxCM2DKJ4wn1Km/p P3vLhftRvyerRnuvWidW4zvc1SjgwfPLb3OPNgfXRzWDHHeux/NE4Kn0bVAPKMhD mZbVt4Nhg2+0gtNfhBIogmikuRRRzPuWaGpP/SZUSX2Gq1yhpnVcT1eUgxe7CI6k 2fFZ4Iu5VcFjmOap3x1iIHK2ELywL8kzf6GHjRRhQJy9x90F51sGIIvOosi7yIEt crL3N6Eh6rUajMan8fSfUHyzp9XSpaiB6wUYTcgxTlsg6pOJY8gWslm8lNyXp5MA == X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvvddrtdeggdeitdcutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenuc fjughrpeffhffvuffkfhggtggujgesthdtredttddtvdenucfhrhhomheptehnughrvghs ucfhrhgvuhhnugcuoegrnhgurhgvshesrghnrghrrgiivghlrdguvgeqnecuggftrfgrth htvghrnhepudekhfekleeugeevteehleffffejgeelueduleeffeeutdelffeujeffhfeu ffdunecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprg hnughrvghssegrnhgrrhgriigvlhdruggv X-ME-Proxy: Received: by mail.messagingengine.com (Postfix) with ESMTPA; Thu, 13 Jan 2022 22:35:18 -0500 (EST) Date: Thu, 13 Jan 2022 19:35:17 -0800 From: Andres Freund To: Thomas Munro Cc: Maksim Milyutin , "tsunakawa.takay@fujitsu.com" , pgsql-hackers , Tatsuo Ishii , Tom Lane , Zhihong Yu Subject: Re: Add client connection check during the execution of the query Message-ID: <20220114033517.mps5pdfkxeqeiciw@alap3.anarazel.de> References: <20211211051101.mui5qtx3r5ixwkfb@alap3.anarazel.de> <20211213185257.e7f6uyoocauoa4y6@alap3.anarazel.de> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk Hi, On 2022-01-11 22:59:13 +1300, Thomas Munro wrote: > I considered another idea we discussed: if we see a latch event, clear > it and try again so that other events can be revealed (rince and > repeat), but remember if that happens and set the latch at the end. I > think that still requires PG_FINALLY() if you want to guarantee not to > eat a latch event if WaitEventSetWait() throws. This may be a > theoretical point because things must be pretty broken if > WaitEventSetWait() is throwing, but I don't like an egregious lack of > exception safety on principle. I don't think this is a problem. Not because of WaitEventSetWait() never throwing, but because it's "just fine" to have reset the latch in that case. The error will cause control flow to transfer to the next PG_CATCH site. The point of latches is to avoid racy checks for events (or sleeps with short timeouts to handle the races). The documented pattern is: * for (;;) * { * ResetLatch(); * if (work to do) * Do Stuff(); * WaitLatch(); * } Latches only work if there's a very limited amount of things happening between the if (work_to_do) and the WaitLatch(). It definitely is *NOT* be ok to do something like: for (;;) { ResetLatch() if (work_to_do) DoStuff(); PG_TRY(): something_that_may_throw(); PG_CATCH(): something_not_throwing(); WaitLatch(); } For one, elog.c related code might have actually done network IO! During which the latch very well might be reset. So there's just no way any remotely reasonable code can rely on preserving latch state across errors. > I considered another idea we discussed: if we see a latch event, clear > it and try again so that other events can be revealed (rince and > repeat), but remember if that happens and set the latch at the end. The more I think about it, the less I see why we *ever* need to re-arm the latch in pq_check_connection() in this approach. pq_check_connection() is only used from from ProcessInterrupts(), and there's plenty things inside ProcessInterrupts() that can cause latches to be reset (e.g. parallel message processing causing log messages to be sent to the client, causing network IO, which obviously can do a latch reset). It makes sense to use CFI() in a latch loop, but it would have to be at the bottom or top, not between if (work_to_do) and WaitLatch(). Greetings, Andres Freund