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 1iTPZN-0002kE-Bz for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Nov 2019 12:10:21 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iTPZL-0006iA-VE for pgsql-hackers@arkaria.postgresql.org; Sat, 09 Nov 2019 12:10:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iTPZL-0006hr-LZ for pgsql-hackers@lists.postgresql.org; Sat, 09 Nov 2019 12:10:19 +0000 Received: from forward102j.mail.yandex.net ([5.45.198.243]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iTPZJ-0000LY-De for pgsql-hackers@postgreSQL.org; Sat, 09 Nov 2019 12:10:19 +0000 Received: from mxback20o.mail.yandex.net (mxback20o.mail.yandex.net [IPv6:2a02:6b8:0:1a2d::71]) by forward102j.mail.yandex.net (Yandex) with ESMTP id BA82CF21BBB; Sat, 9 Nov 2019 15:10:15 +0300 (MSK) Received: from myt4-947ef0d4a658.qloud-c.yandex.net (myt4-947ef0d4a658.qloud-c.yandex.net [2a02:6b8:c00:885:0:640:947e:f0d4]) by mxback20o.mail.yandex.net (mxback/Yandex) with ESMTP id s8AdUSFB1G-AFXqU359; Sat, 09 Nov 2019 15:10:15 +0300 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yandex.ru; s=mail; t=1573301415; bh=kZ+lMmMGiqywf8AAmStFKjdI7uM6ptEcO6iC6vLSMEU=; h=In-Reply-To:Subject:CC:To:From:References:Date:Message-ID; b=e4qsWJoD25V2c6Nh3a4f+8jGTeHRDjRoZ1AyFp07haBQjUdlHVtfQ+axLn5Y2pQoi YXQTKKlHN/PXNFphVYXWwzUZlAE6flm8F2HBagActyBKBIpn6IWmxE+UiRxAOCGqs3 osxxwqLrxknps0kyqtCT8bX5XLusY52DvdrbnpXM= Authentication-Results: mxback20o.mail.yandex.net; dkim=pass header.i=@yandex.ru Received: by myt4-947ef0d4a658.qloud-c.yandex.net (smtp/Yandex) with ESMTPSA id bEOKX61ALj-AE00lF3V; Sat, 09 Nov 2019 15:10:14 +0300 (using TLSv1.2 with cipher ECDHE-RSA-AES128-SHA256 (128/128 bits)) (Client certificate not present) Date: Sat, 9 Nov 2019 14:10:13 +0200 From: Eugen Konkov Message-ID: <322267502.20191109141013@yandex.ru> To: Bruce Momjian CC: PostgreSQL-development , pgsql-docs@lists.postgresql.org Subject: Re: Does 'instead of delete' trigger support modification of OLD In-Reply-To: <247609768.20191109140502@yandex.ru> References: <919823407.20191029175436@yandex.ru> <20191106185935.GE1843@momjian.us> <1771593631.20191107112032@yandex.ru> <352093181.20191107112429@yandex.ru> <20191107212655.GA487@momjian.us> <20191107222818.GA18712@momjian.us> <247609768.20191109140502@yandex.ru> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Hello Eugen, Saturday, November 9, 2019, 2:05:02 PM, you wrote: > Hello Bruce, > Friday, November 8, 2019, 12:28:18 AM, you wrote: >> On Thu, Nov 7, 2019 at 04:26:55PM -0500, Bruce Momjian wrote: >>> On Thu, Nov 7, 2019 at 11:24:29AM +0200, Eugen Konkov wrote: >>> > >> As far as allowing DELETE to modify the trigger row for RETURNING, I am >>> > >> not sure how much work it would take to allow that, but it seems like it >>> > >> is a valid requite, and if so, I can add it to the TODO list. >>> > >>> > > Yes, Add please into TODO the feature to "allowing DELETE to modify the trigger row >>> > > for RETURNING". Becuase, as I have described at first letter, without >>> > > this the RETURNING rows **does not correspond actually deleted data** >>> > >>> > > Thank you. >>> >>> I have added a TODO item: >>> >>> Allow DELETE triggers to modify rows, for use by RETURNING >> Thinking some more on this, I now don't think a TODO makes sense, so I >> have removed it. >> Triggers are designed to check and modify input data, and since DELETE >> has no input data, it makes no sense. In the attached SQL script, you >> can see that only the BEFORE INSERT trigger fires, so there is no way >> even with INSERT to change what is passed after the write to RETURNING. >> What you can do is to modify the returning expression, which is what I >> have done for the last query --- hopefully that will help you. > You lost my idea. First of all I am talking about views and an > INSTEAD OF triggers. > INSERT/UPDATE operation present which data is added into DB > DELETE operation present which data is deleted from DB > (in my case I am not deleted exact that data which matched by where. > See example below) > Thus INSTEAD OF INSERT/UPDATE triggers are designed to check and modify input data > eg. we can insert/update something different then incoming data (here > we are modifying NEW) > Thus INSTEAD OF DELETE triggers are designed to check and delete **output** data > eg. we can delete something different then underlaid data (here we are > modifying OLD) > for example, we have next data: 1 2 3 4 5 6 7 8 > it is not presented by eight rows, but instead it is presented as one > row with range data type: [1..8] > When we insert data we will not get new row, we change current: > insert into table values ( 9 ) will result > [1..9] > instead of > [1..8] > 9 > So lets look into INSTEAD OF DELETE trigger when we deleting > data: > delete from table where x in ( 5, 6, 7 ); > after deleting this we should get: > [1..4] > [8..9] > thus > with t1 as ( delete from table where x in ( 5, 6, 7 ) returning * ) > select * from t1 > should return: > [5..7] > instead of > [1..9] > because we does not delete ALL [1..9], we just delete ONLY [5..7] > Thus I need to change matched row OLD.x from [1..9] to [5..7] > Please reread my first letter. There I describe more real life example > when I am manipulating bi-temporal data. > where some value exist at given period: > id | app_period | value > 7 [2019-01-01, 2019-04-05) 207 > And I am deleting third month: [ 2019-03-01, 2019-04-01 ) > with t1 as ( delete from table where app_period && [ 2019-03-01, > 2019-04-01 ) returning * ) > select * from t1; > 7 [ 2019-03-01, 2019-04-01 ) 207 > select * from table; > 7 [ 2019-01-01, 2019-03-01 ) 207 > 7 [ 2019-04-01, 2019-04-05 ) 207 Here when data is deleted the next row is matched: 7 [2019-01-01, 2019-04-05) 207 and assigned to OLD. Because I am deleting data ONLY from [ 2019-03-01, 2019-04-01 ) period I am required to change OLD: OLD.app_period = [ 2019-03-01, 2019-04-01 ) So I should get: > 7 [ 2019-03-01, 2019-04-01 ) 207 instead of > 7 [2019-01-01, 2019-04-05) 207 -- Best regards, Eugen Konkov