Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVhf3-0008Iq-PK for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2019 19:53:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iVhf2-0003Jt-DJ for pgsql-general@arkaria.postgresql.org; Fri, 15 Nov 2019 19:53:40 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iVhf0-0003Ji-JN for pgsql-general@lists.postgresql.org; Fri, 15 Nov 2019 19:53:40 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iVhex-0008GO-E4 for pgsql-general@postgresql.org; Fri, 15 Nov 2019 19:53:37 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 57F7D21A09; Fri, 15 Nov 2019 14:53:34 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute6.internal (MEProxy); Fri, 15 Nov 2019 14:53:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=H Y2eXFxdlwHoVtnONDXWPRfLNytUSEccQ3b02opSbTg=; b=yQV7h4y2oYxFhymQ0 eeDzQicEgp6Zi543L6RaAIi8kyzip+CU5CLekPX97z7+vysF0d5BE67YdN0ibikQ NbMMhot/lPnFHczCu4kWiRNUOEm+wzdw5oUdHN1DvCMQblZnspxcPFGcSRMVNL4R PzwewPSOPCpq2T1s6gfV3/H7eAnwjPZr+dxfaNqbAaVQqQB9zp7fYNqIxdGpwGDp FxD43J+kS+gJwkkZ8KF9iYWi+iOsKYXQvvTIcsB2W4I/vENJlPjFaeUvzVT4EN1E hosnADCqChzb+eEka242t93XMcneR2kD2NLaDfYxDz0O7H/UhW7zmWlytEZIAcFC RpMcA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=content-transfer-encoding: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=HY2eXFxdlwHoVtnONDXWPRfLNytUSEccQ3b02opSb Tg=; b=RnvfR+JnfHFtktf87tBFm3dlvdfbCmpnwWO5tu1ojrF1Jx5fsMbqWHrNq lHtN0sq7yY2SoPZGt/vkwDsG0g57sqUYt0AGpIJFe7mhkavK48p0Nbekw4d9rpSd o/UFYvo6DSSWkjxd7qV3DZIQ9qFVo+RgSO/BJWgzCmshBElggCjZupQIfOb4GhBl IkZ/PbNR6VtAfxY6+GXwdCIkY0q7lQeiD6T3smfpeDoN9kpIOn8g7vyItkQEP/WN sKRUv6lhUpHqL4D7PVF8PP6RZWVRHw+jmY9WE495IS2+Y3VXvrCJece48VTzovPp It2Hcmfx45wEUQPdu9MwQiQ9duPmA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrudefhedgudefudcutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecukfhppeejuddrvdduvddrudeggedrjeehnecurfgrrhgrmhepmhgrihhlfhhr ohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsth gvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.144.75]) by mail.messagingengine.com (Postfix) with ESMTPA id CCDE53060061; Fri, 15 Nov 2019 14:53:33 -0500 (EST) Subject: Re: access to original-statement predicates in an INSTEAD-OF row trigger To: John Lumby , pgsql general References: From: Adrian Klaver Message-ID: Date: Fri, 15 Nov 2019 11:53:32 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.7.2 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 11/15/19 10:37 AM, John Lumby wrote: > I have an INSTEAD-OF row trigger one of whose purposes is to re-direct the original statement > (which is operating on a VIEW) to a different real base table. > > Suppose the original statement is >           UPDATE myview VW set VW.counter = 11 where VW.primary_key = xxxx and AND VW.counter = 10; > > and my trigger constructs this statement > UPDATE basetable BT set BT.counter = 11 where BT.primary_key = OLD.primary_key; > based on what it finds in OLD tuple and NEW tuple. > > This will never update the wrong row since it specifies the primary key - good. But I have realized there is a problem concerning the returned TAG. > Suppose that, *after* the backend executor started executing the statement but *before* the trigger is fired and this statement is issued, a different transaction updated BT.counter to 11 (or higher). > My trigger still runs the update, but the original statement specified to do so only if the current value of counter is 10. > Or rather, it specified that no row should be found for update if counter <> 10. > > Is there any way my trigger can discover this predicate condition and apply it to its generated statement? Not following. Do you want OLD.primary_key set to 11 or BT.counter set to 12/both/or some other action? > > Or if not, (because I suppose in general such predicates could be very complex) is there some other way of doing this that avoids this problem and that does not require modification of the application? (**) > > I have a feeling this must have come up before but Idon't see any reference. > > postgresqI version 12. > > Cheers, John > > (**) I know a SHARE lock could be obtained by the application running the orginal statement but assume for this question that that is not possible. I am looking for some self-contained way in trigger or similar code. > > > > -- Adrian Klaver adrian.klaver@aklaver.com