Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9EEm-0000ur-Sa for pgsql-general@arkaria.postgresql.org; Thu, 19 Apr 2018 18:24:53 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1f9EEl-0001wz-G2 for pgsql-general@arkaria.postgresql.org; Thu, 19 Apr 2018 18:24:51 +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.89) (envelope-from ) id 1f9EEj-0001wo-FF for pgsql-general@lists.postgresql.org; Thu, 19 Apr 2018 18:24:51 +0000 Received: from out3-smtp.messagingengine.com ([66.111.4.27]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA384:256) (Exim 4.89) (envelope-from ) id 1f9EEg-00030F-57 for pgsql-general@postgresql.org; Thu, 19 Apr 2018 18:24:48 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id 1C3F421803; Thu, 19 Apr 2018 14:24:45 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Thu, 19 Apr 2018 14:24:45 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= content-transfer-encoding:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-sender :x-me-sender:x-sasl-enc; s=fm2; bh=FLJ69fHocuuQDSgOZYG11u++I+L5O XVPfMwskfmUYvU=; b=YUJHR0kO2XHo4wFnHQfAkU5O9fz9/W4IWKGTvO4aSaoPm Q7mNaQGjTNBRWH1VRF3/hB2+H9lZD8x1pS/FqhiL7SsLxRVpoZhXfPW1xzu+7yz1 F0kRmfL/OIRYe32MXgNtjV05HDzxBYDbcEfAm9VvGMNkA/6BnaC6j9PFHvyy6pRA tgIcLZ8f6rzKDh3sr/0CepqqGKdRzeG+aIJTz5j0GpdhSF9DI23/ej9jdkk3+nZl LBmFMR/oNykAQvVWxOXXKNGW0IcDGQVJPlERvJ53UKUgSIgMqz85ggfzrdq59G+2 pFU44rZIrGLty6TVAy9+tFNMmXeZBpO/tkSIkV6Xg== 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-sender:x-me-sender:x-sasl-enc; s=fm2; bh=FLJ69f HocuuQDSgOZYG11u++I+L5OXVPfMwskfmUYvU=; b=O9d5LMFJ0Of98UdkOHonlL Umfy8kFnCKOkhEi8Tz93EeFkVljwonDP1w8b+D96Bu+/LvOSt8dgDDlC5hRKbKyy vb+CVKVWrUACPXa79lW7nzTfyK/RVfwCXuUZMZNo55dgzMdUp3LIk6hy+VqgxV4k bg6Eh71rNurB08UEVjTfp9Hc2SQqmNmN1Tuxj7KkAWL1iWU3OJR7SM+O9A+i/lMs EF4ij+WMqGtcPPPThNaKSkX3cSq5pcziw40XwbZkIfnV7Y0JRNdUQWqLrkBZjnho tP0DJmS9FbnTT4mI2MWAum94jWcmEZ9xXcKBjiTar+67O5AJV7NUYAhXUh0duCsQ == X-ME-Sender: Received: from ranger.suse (c-73-239-117-238.hsd1.wa.comcast.net [73.239.117.238]) by mail.messagingengine.com (Postfix) with ESMTPA id 63EF5E5083; Thu, 19 Apr 2018 14:24:44 -0400 (EDT) Subject: Re: Problem with trigger makes Detail record be invalid To: PegoraroF10 , pgsql-general@postgresql.org References: <1524160534707-0.post@n3.nabble.com> From: Adrian Klaver Message-ID: <7cb5806e-7371-5c19-1c7b-b8ac556bdfb5@aklaver.com> Date: Thu, 19 Apr 2018 11:24:43 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:52.0) Gecko/20100101 Thunderbird/52.7.0 MIME-Version: 1.0 In-Reply-To: <1524160534707-0.post@n3.nabble.com> 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 04/19/2018 10:55 AM, PegoraroF10 wrote: > I´m using Postgres 10 on ubuntu. > > suppose a simple Master/Detail structure like this: > > create table Master(ID integer primary key, name text); > create table Detail(ID integer primary key, Master_ID Integer, OtherInfo > text); > alter table Detail add constraint FKMasterDetail foreign key (Master_ID) > references Master(ID) on update cascade on delete cascade; > > Then insert some records on it: > insert into Master(ID, Name) values(1,'First'), values(2,'Second'); > insert into Detail(ID, Master_ID, OtherInfo) values(1,1,'Detail > Information'), (2,2,'Detail Information2'); > > Then, if I delete on Master will delete on detail too. Fine. > delete from Master where ID=1; > > But now, suppose I have a complex trigger before update or delete that runs > on Detail table. > create function DoAComplexJobOnDetail() returns trigger as $$ > begin > -- Do lots of things then > Return new; --This is the error, because I´m returning new even for > delete; That can be dealt with using TG_OP value to conditionally change what is RETURNed: https://www.postgresql.org/docs/10/static/plpgsql-trigger.html "TG_OP Data type text; a string of INSERT, UPDATE, DELETE, or TRUNCATE telling for which operation the trigger was fired. " See Example 42.4. A PL/pgSQL Trigger Procedure For Auditing at bottom of page. > end;$$ language plpgsql; > create trigger DetailDoAComplexJob before update or delete on Detail for > each row execute procedure DoAComplexJobOnDetail(); > > Then try to delete the other Master record. It will be deleted on Master but > Detail record doesn´t and will obviously become invalid because the foreign > key. > delete from Master where ID=2; > select * from Master; --will show no records. > select * from Detail; --will show one record pointing to Master_ID=2, that > doesn´t exist anymore. > > Is this a bug or it´s mine responsability to check that trigger result ? Without seeing exactly what the trigger function on Detail is doing that is not answerable. > If that trigger responds incorrectly I think that no information could be > executed. > > > > -- > Sent from: http://www.postgresql-archive.org/PostgreSQL-general-f1843780.html > -- Adrian Klaver adrian.klaver@aklaver.com