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 1m8W7g-0005Li-HD for pgsql-novice@arkaria.postgresql.org; Tue, 27 Jul 2021 23:04:30 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1m8W7f-0003UI-Fy for pgsql-novice@arkaria.postgresql.org; Tue, 27 Jul 2021 23:04:27 +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 1m8W7f-0003UA-7l for pgsql-novice@lists.postgresql.org; Tue, 27 Jul 2021 23:04:27 +0000 Received: from mout01.posteo.de ([185.67.36.65]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1m8W7a-0008ML-Cn for pgsql-novice@lists.postgresql.org; Tue, 27 Jul 2021 23:04:26 +0000 Received: from submission (posteo.de [89.146.220.130]) by mout01.posteo.de (Postfix) with ESMTPS id EDB6D240027 for ; Wed, 28 Jul 2021 01:04:16 +0200 (CEST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=posteo.net; s=2017; t=1627427057; bh=31ejA3sYuXn1+JEitLr8iOcg0xSaqRdU6yd9SiPfbyk=; h=From:To:Cc:Subject:Date:From; b=EUG1AHIJIl4+wJOP49F+pqFfzI8YAeo7FQc6hUcE19JTCFlA7ki2Qe8LjMaYM7hey nlKLHbYNzYgZfTetokG9/A5knD2SudvpUdohXIdD79uFYlaOWkcclMxaNUpSbbGdD+ DsdpM8ZKhUlUAm5ICSxeNijbcP/pz312eWYgIVbl/1fDEchs2QCzoHrWWrU/7mQIOG fgftQpWtxR/IR4lGPlJ45qAtY7lLoTU+wMBjNnoQV+xs2QiwpkEXWm0QoBuCT0A6iq M/tt81hqmiEZv2yPc0l+iyn+pNQ5t9JUanK65G2bmVNH0TSP1xl/l7l8oYrSwpg8G7 QI8qrwaXwBIWg== Received: from customer (localhost [127.0.0.1]) by submission (posteo.de) with ESMTPSA id 4GZC680mk6z6tmM; Wed, 28 Jul 2021 01:04:16 +0200 (CEST) From: "Nicolas Mitchell" To: "David G. Johnston" Cc: "hubert depesz lubaczewski" , pgsql-novice@lists.postgresql.org Subject: Re: Trigger function Date: Tue, 27 Jul 2021 23:04:03 +0000 Message-ID: <53180757-855C-4FFF-B5CC-572BAEFD9397@posteo.net> In-Reply-To: References: <30B8A3D0-8A87-40E2-BE40-31A17BA4B944@posteo.net> <20210727063805.GA28983@depesz.com> <2D1DF30A-25BC-48CA-B502-D8B3147B9BEB@posteo.net> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8"; format=flowed Content-Transfer-Encoding: 8bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On 27 Jul 2021, at 23:13, David G. Johnston wrote: > On Tuesday, July 27, 2021, Nicolas Mitchell > wrote: > >> >> But when I have tried this with the following trigger/function >> (BEFORE/AFTER), PG goes into a loop. The two associated sequences >> (object, >> host) are incremented until I break the execution but no insert >> happens in >> either table. My code is causing an endless loop. I’m too green to >> understand why! I’d be grateful for any hints to help me on my way. >> >> CREATE OR REPLACE FUNCTION public.func__host__bi() >> RETURNS trigger AS >> $$ >> begin >> >> INSERT INTO host (name, domain, object) >> VALUES (NEW.name, NEW.domain, (SELECT * FROM object_id)); >> RETURN NEW; >> end >> $$ >> LANGUAGE 'plpgsql' >> >> CREATE TRIGGER trig__host_bi >> BEFORE INSERT <————————————> or AFTER >> INSERT >> ON public."host" >> FOR EACH ROW >> EXECUTE PROCEDURE public.func__host__bi(); >> > > You are getting an infinite cycle because while in the middle of > inserting > a row into host, which provokes the trigger, you go and execute > another > insert command for host, provoking the same trigger, performing yet > another > insert, provoking the same trigger, etc… I see, I think. INSERT going to happen > BEFORE INSERT trigger firing function with > INSERT INTO host > INSERT going to happen > BEFORE INSERT trigger firing function with > etc… > > When you write a trigger for a table you should be executing commands > against the same table. I believe this implies that the code creating a new object (INSERT INTO public.object…) row should reside elsewhere. > > You change the data in the ongoing insert by returning a different row > from > the trigger function (i.e., modify your “return new;” line - or > modify NEW > itself?). I’m absorbing this sentence… Many thanks for your assistance. Nic