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 1ouKTK-0007ct-3J for pgsql-general@arkaria.postgresql.org; Sun, 13 Nov 2022 21:24:58 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ouKTI-0005di-Mv for pgsql-general@arkaria.postgresql.org; Sun, 13 Nov 2022 21:24:56 +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 1ouKTI-0005WL-C1 for pgsql-general@lists.postgresql.org; Sun, 13 Nov 2022 21:24:56 +0000 Received: from out5-smtp.messagingengine.com ([66.111.4.29]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1ouKTF-0004OP-Rl for pgsql-general@postgresql.org; Sun, 13 Nov 2022 21:24:55 +0000 Received: from compute1.internal (compute1.nyi.internal [10.202.2.41]) by mailout.nyi.internal (Postfix) with ESMTP id 030E85C00A2; Sun, 13 Nov 2022 16:24:53 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute1.internal (MEProxy); Sun, 13 Nov 2022 16:24:53 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= cc:cc:content-transfer-encoding:content-type:date:date:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to; s=fm3; t=1668374692; x= 1668461092; bh=Az8ZSstxlV/tDDUka6rlsqq36s5naFT3uTA0HcFsDpo=; b=h 6+BF+HO6BIf+vBdfAOAK+byPyG8+dljtDAx85FqEKRoIlpKvipyHc4PiHkmS3mtJ H7HIHVwlrJLWbz5geeFbBRps7CCLx7zmqsUfpMONvYHNdmRnWbWqvKG504B59P8w wPTGaSOjxCBwMBjPyl1mOIB4ICMwZp7bvsyZSPzKTZ1433UmFSUkc00LLo5h/X/m Ka5kwcOxBESFJTl6/kBnO3AwYcyjEj8ee4VHI4dcwm4yjKGlRj6cgCst1YE3fdCJ YLx9vahmehySZ5kOtPaBvQeI9oQWd7cV/Q7al0KkwMQxEhjMTwvowRr6sCkop4KH Sl/V2oYaWrbvNqYJYiNDg== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:cc:content-transfer-encoding :content-type:date:date:feedback-id:feedback-id:from:from :in-reply-to:in-reply-to:message-id:mime-version:references :reply-to:sender:subject:subject:to:to:x-me-proxy:x-me-proxy :x-me-sender:x-me-sender:x-sasl-enc; s=fm1; t=1668374692; x= 1668461092; bh=Az8ZSstxlV/tDDUka6rlsqq36s5naFT3uTA0HcFsDpo=; b=q DghIvucpp0PJvk5sy+0FHs9UReUt2PfuG0OMWrowDCOfIkMyfnEj+4+e144UMpwV KlmBF0Yyd+hmpNJXBVslMHf35YeDoEdKAFJPUskRG0u3uB/z342KuM+AEEmMsXqz 3DGTfwix4qat0O5zqmror0TyfJ/kBnpuu1ucDZDKLEGPqUG2fBJueUM29qwk61aZ OyTcy6C75EgINaLkBLHfEC3K3r6wbxZukZqLnmo1/cR9/IglZEbJdcy34LKWsHo+ eXamzRaFo/SrW5Caarl8GDWs/UX50e5zsYnbqWep5vPejVcSwp27xavMbGpCjUi7 SPSDFaTaWwPsdQK+axFjg== X-ME-Sender: X-ME-Received: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedvgedrgedtgdduhedtucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepkfffgggfuffvvehfhfgjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepjeegtdfhuefhtefgffdvheduleetgeejudevffek teegjeeiheeuudellefgffffnecuvehluhhsthgvrhfuihiivgeptdenucfrrghrrghmpe hmrghilhhfrhhomheprggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomh X-ME-Proxy: Feedback-ID: i76984098:Fastmail Received: by mail.messagingengine.com (Postfix) with ESMTPA; Sun, 13 Nov 2022 16:24:52 -0500 (EST) Message-ID: <231bbc6b-3e01-9ce7-3560-0025e2361d16@aklaver.com> Date: Sun, 13 Nov 2022 13:24:51 -0800 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.4.2 Subject: Re: ON CONFLICT and WHERE Content-Language: en-US To: Tom Lane Cc: pgsql-general , Peter Geoghegan References: <4cc58ea7-c69d-cb4a-de8a-da66d79f0f5a@aklaver.com> <736683.1668373664@sss.pgh.pa.us> From: Adrian Klaver In-Reply-To: <736683.1668373664@sss.pgh.pa.us> Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 11/13/22 13:07, Tom Lane wrote: > Adrian Klaver writes: >> INSERT INTO books VALUES (12, 0, CURRENT_TIMESTAMP) >> ON CONFLICT (id) >> WHERE updated IS NULL OR updated + INTERVAL '2min' < CURRENT_TIMESTAMP >> DO UPDATE >> SET version = books.version + 1, updated = CURRENT_TIMESTAMP; > >> I have not used WHERE with ON CONFLICT myself so it took longer then I >> care to admit to correct the above to: > >> INSERT INTO books VALUES (12, 0, CURRENT_TIMESTAMP) >> ON CONFLICT (id) >> DO UPDATE >> SET version = books.version + 1, updated = CURRENT_TIMESTAMP >> WHERE books.version IS NULL OR books.updated + INTERVAL '2min' < >> CURRENT_TIMESTAMP; > >> The question is why did the first case just ignore the WHERE instead of >> throwing a syntax error? > > A WHERE placed there is an index_predicate attachment to the ON CONFLICT > clause. It doesn't have any run-time effect other than to allow partial > indexes to be chosen as arbiter indexes. TFM explains > > index_predicate > > Used to allow inference of partial unique indexes. Any indexes > that satisfy the predicate (which need not actually be partial > indexes) can be inferred. > > This strikes me as a bit of a foot-gun. I wonder if we should make > it safer by insisting that the resolved index be partial when there's > a WHERE clause here. (This documentation text is about as clear as > mud, too. What does "inferred" mean here? I think it means "chosen as > arbiter index", but maybe I misunderstand.) Alright I see how another use of WHERE comes into play. I do agree with the clarity of the description, especially after looking at the example: " Insert new distributor if possible; otherwise DO NOTHING. Example assumes a unique index has been defined that constrains values appearing in the did column on a subset of rows where the is_active Boolean column evaluates to true: -- This statement could infer a partial unique index on "did" -- with a predicate of "WHERE is_active", but it could also -- just use a regular unique constraint on "did" INSERT INTO distributors (did, dname) VALUES (10, 'Conrad International') ON CONFLICT (did) WHERE is_active DO NOTHING; " I honestly cannot figure out what that is saying. > > regards, tom lane -- Adrian Klaver adrian.klaver@aklaver.com