Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtp (Exim 4.80) (envelope-from ) id 1a6RkU-0005UQ-MU for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Dec 2015 23:32:46 +0000 Received: from localhost ([127.0.0.1] helo=postgresql.org) by malur.postgresql.org with smtp (Exim 4.84) (envelope-from ) id 1a6RkU-000860-8e for pgsql-bugs@arkaria.postgresql.org; Tue, 08 Dec 2015 23:32:46 +0000 Received: from makus.postgresql.org ([2001:4800:1501:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.84) (envelope-from ) id 1a6Rjr-0006QB-79 for pgsql-bugs@postgresql.org; Tue, 08 Dec 2015 23:32:07 +0000 Received: from mail-vk0-x236.google.com ([2607:f8b0:400c:c05::236]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.84) (envelope-from ) id 1a6Rjo-0000yZ-E2 for pgsql-bugs@postgresql.org; Tue, 08 Dec 2015 23:32:06 +0000 Received: by vkha189 with SMTP id a189so32597579vkh.2 for ; Tue, 08 Dec 2015 15:32:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=heroku-com.20150623.gappssmtp.com; s=20150623; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type; bh=KnNVDRsO69uh14JaGKJDOWkF87YH11hh6UQEvlbFJCo=; b=paWP+YaX54Ohv2pgGPEZ3D/2N8Q7l4BdzSuoygvOAt4WhhSNMJNinSRFiI7fRCQc1w efhNPabKYpw8tOAyDNs5bqHAMFovXPzQO3pxwVwUEp8bUHQdTRgVyOVPmCJqrJCrvKVy x2EQo14WkOH2Wc5+zOb9os7BLuAz4KE/3s6Ej7seZg+eFzd04AZxwDLMTNPc8ZjQfT7I f/1SaMUdsgeuLJJWa2Z0Uo3ACdNdgJv5sNk8f4Qq9nmGwSY3tE0wCPYaFnwVHYX4CNnz 1/diFclxVeaiPV10UJExGDO2XkzCMpT/Dc9vO/hHwmcpWYJfWF+gviA90TiqmLp2Aslv 9+LA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20130820; h=x-gm-message-state:mime-version:in-reply-to:references:date :message-id:subject:from:to:cc:content-type; bh=KnNVDRsO69uh14JaGKJDOWkF87YH11hh6UQEvlbFJCo=; b=J2Cw8f+sTVf2sMX8CE+E5cqaUgeN5PUlBx86GxpM2T8kw7kWvfZI4qvLXtSuLTUIVZ ewuiRPCGBC5iJRXL7CyK4o3jpEzsaud64a6+k/H+RoJizDzC5oMq/JsdPQXjtQST+uM4 O26cMxoFGJqwbOr8iLwo5cjvoQ/bvrxBiA8CsT1DPv8VT0HWlFosa5mRx+EFx/udCST9 SV+d9UcfAanGtHAxHgDbLm4aLk3hM8+nMGiN7SAzZcN5eu4wHX0Xn59Rs46QrO76Qom4 buSVA/VgmQWxR0CFMl8a17Rf82jcHUglynpbCnJul0K2xYgC9jlz2ge3UWP2IhiV5hTV ueyw== X-Gm-Message-State: ALoCoQnQ11L8F3u/QTVGXqWjo7MEQ5zP2Vx0e5Dqu0F4M+i9OsDZW0NFZepjUnUlMJpd0KOPHCrOHhwfqyRwxtUMuHgboWmIa+twwYNOHRiLZJTzUz6lBJE= MIME-Version: 1.0 X-Received: by 10.31.169.137 with SMTP id s131mr2033261vke.144.1449617523495; Tue, 08 Dec 2015 15:32:03 -0800 (PST) Received: by 10.31.189.151 with HTTP; Tue, 8 Dec 2015 15:32:03 -0800 (PST) In-Reply-To: <20151208231220.GG28762@awork2.anarazel.de> References: <20151208144440.GU4934@alap3.anarazel.de> <20151208231220.GG28762@awork2.anarazel.de> Date: Tue, 8 Dec 2015 15:32:03 -0800 Message-ID: Subject: Re: Incorrect UPDATE trigger invocation in the UPDATE clause of an UPSERT statement. From: Peter Geoghegan To: Andres Freund Cc: Michael Paquier , Dean Rasheed , Stanislav Grozev , PostgreSQL mailing lists Content-Type: text/plain; charset=UTF-8 X-Pg-Spam-Score: -2.6 (--) List-Archive: List-Help: List-ID: List-Owner: List-Post: List-Subscribe: List-Unsubscribe: X-Mailing-List: pgsql-bugs Precedence: bulk Sender: pgsql-bugs-owner@postgresql.org On Tue, Dec 8, 2015 at 3:12 PM, Andres Freund wrote: > I'm not 100% sure, but I do think it could point to arbitrary tuples: > Consider what happens if a previous update to the same tuple failed and > the new version of the tuple is pruned away, and that space is then > reused for something else. Bang. That's probably only possible if the > xmax is a multixact, as heap_lock_tuple() otherwise would clear out > t_ctid - but that's perfectly possible. That's an alarming possibility. Either way though, it's wrong, and the fix is fairly clear. >> Hmm. You mean having changed things to pass &tuple.t_self to >> ExecUpdate() instead? > > Yes, after that. Passing t_data->t_ctid is just plain wrong. I agree, obviously. >> Could we >> just instruct ExecUpdate() that the caller is ExecOnConflictUpdate(), >> and so it's appropriate to throw a cardinality violation for its >> HeapTupleSelfUpdated case? After all, that case already discriminates >> against updates that are not from the same command in the xact (e.g. >> due to weird before triggers) by throwing an error [1]. I don't think >> we need to throw a cardinality violation unless an UPSERT attempts to >> update a tuple a second time, but not if that occurs within a command >> that happens to contain an UPSERT not directly doing the updating. > > I'm inclined to do the check in ExecOnConflictUpdate() instead - istm > that's easier to understand. We're on the same page. I just happen to think we might as well put the check beside the existing special case check for weird before triggers -- within ExecUpdate()'s HeapTupleSelfUpdated case. That avoids an extra HeapTupleSatisfiesUpdate() call for every UPSERT update. We mostly only call that routine from heapam.c as things are. But I'm hardly going to insist on that. I actually think that there is an argument to be made for doing nothing here, and not allowing a cardinality violation at all. It doesn't seem too inconsistent to follow the old behavior (just accept a redundant second update) in the case where you update the row within an UPSERT after the UPSERT locks the row, but before your UPSERT has a chance to do its UPDATE of that same row (in other words, something happened to your tuple in between). I do prefer to do what we've agreed to, though (whether or not it happens in ExecUpdate()), mostly because it's closer to the other ExecUpdate() HeapTupleSelfUpdated special case. Note the similarity between that case, and this new one. -- Peter Geoghegan -- Sent via pgsql-bugs mailing list (pgsql-bugs@postgresql.org) To make changes to your subscription: http://www.postgresql.org/mailpref/pgsql-bugs