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 1gvln3-0002pi-Ru for pgsql-general@arkaria.postgresql.org; Mon, 18 Feb 2019 16:29:10 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gvln2-0002M5-0I for pgsql-general@arkaria.postgresql.org; Mon, 18 Feb 2019 16:29:08 +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 1gvln1-0002Ly-K8 for pgsql-general@lists.postgresql.org; Mon, 18 Feb 2019 16:29:07 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gvlmy-0002Nt-N8 for pgsql-general@lists.postgresql.org; Mon, 18 Feb 2019 16:29:06 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E84832260B; Mon, 18 Feb 2019 11:29:03 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Mon, 18 Feb 2019 11:29:03 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm2; bh=p 4OR+hh/M43RL5p52ZdoPWks0yuW+3y8oJFcPWxB96U=; b=i9zqaeVc3O+ywpgk2 dOHoLKFfQfm6LuFQxeW98bViXUHgFJmF58d/oNsTWz4Z7z0ioVI51UVrDol4AfIZ bnZCbokTfgXljnxIWqKAd2Id0ic/eN/6mZXk070ELS6VwPv5K2PzK0K9sysZ9aoP UqQLOzVNDXrEgTpZc8Ka/tNKywtVpio4rXXq5GsZKdHb6sQOBJWwTUtQHwChwK9D HxnNOkxfjrWDNeeiG4ENurLLoe7W8pr/Bam0tfaYnwoxDSdGi+UrBsEQgzg2zPVz MpTAfmjAavBfAEpd8M4b3OnktgSeVA//qGRd01kkHwUhLWAIBrePOAn5eXAgrx8X wPdnQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fm2; bh=p4OR+hh/M43RL5p52ZdoPWks0yuW+3y8oJFcPWxB9 6U=; b=soKKnInA2hTo3MlUNu+fgFfrELVP5lzW9MTW04QkbBONlLCcWFAQ1usZU HuC6NhNB5xzYuUcCJ7iAUaA3El6EwDsYGAdfLtIIKr0tzAF+1bcQqb8s9IzCTAy/ hJzO7puYrqAB+B3izbiUEL+Y35uaTeFqMNGOglA7HZbqlEO9bv7E4pm4x6f0dILF rvv2PWnyXJkXGOzD8WPLwuH2m5CJAkPQXSom2EFLPlOkWqSy/JIwPWr3k85rNTHw cjx7YhRSTSmxYiyWpXTEzm9zd0VZElXjDkuQqvpMgn4vgnSm0KQzlrYfHKePEV4R HRVknODtI7JDvJ6R+6dqs0OEZy+Yg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedutddrtddugdehjeculddtuddrgedtledrtddtmd cutefuodetggdotefrodftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfhuthen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgnecukfhppeejuddrvddu vddrudehtddrvddtvdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhklh grvhgvrhesrghklhgrvhgvrhdrtghomhenucevlhhushhtvghrufhiiigvpedt X-ME-Proxy: Received: from [192.168.1.4] (unknown [71.212.150.202]) by mail.messagingengine.com (Postfix) with ESMTPA id B920EE4549; Mon, 18 Feb 2019 11:29:02 -0500 (EST) Subject: Re: Statement-level trigger results in recursion To: Jitendra Loyal Cc: pgsql-general@lists.postgresql.org References: <3459d021-67ca-4d38-1945-1fd576c0f826@aklaver.com> From: Adrian Klaver Message-ID: Date: Mon, 18 Feb 2019 08:29:01 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.5.0 MIME-Version: 1.0 In-Reply-To: Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk On 2/18/19 8:23 AM, Jitendra Loyal wrote: > My bad! > > It is a transition table. Consider the following revised definition of > trigger: > > > CREATE TRIGGER storage_locations_b_u_AS_DML > AFTER UPDATE > ON storage_locations > REFERENCING NEW TABLE AS new_table > OLD TABLE AS old_table > FOR EACH STATEMENT EXECUTE FUNCTION TRG_storage_locations_b_u_AS_DML (); Alright I understand now. Did you see the rest of my previous post about AFTER STATEMENT running regardless of number of rows affected? > > Thanks and regards, > Jiten > > On Mon 18 Feb, 2019, 9:32 PM Adrian Klaver, > wrote: > > On 2/18/19 4:11 AM, Jitendra Loyal wrote: > > > > The AFTER Statement-level Trigger runs into infinite execution when > > another set of rows are affected for the same table through this > > trigger. Consider this use case where a table storage_locations that > > manages a hierarchy of storage_locations in stores, and thus having > > following columns (for simplicity): > > > > > > > > > > storage_location_id SERIAL NOT NULL PRIMARY KEY, > > store_id INTEGER NOT NULL, -- REFERENCES stores > > storage_location_nm VARCHAR (25) NOT NULL, > > parent_storage_location_id INTEGER NULL REFERENCES > storage_locations, > > ---- NULL for root storage locations > > storage_location_path TEXT NOT NULL > > > > > > > > > > > > I have a BEFORE ROW trigger, which updates the > storage_location_path with > > parent's storage_location_path, if any, concatenated with its > > storage_location_name. This works fine - no issues. > > > > I have another AFTER UPDATE STATEMENT-level Trigger and function > definitions > > as below (which updates the storage_path of the children): > > > > > > > > > > CREATE FUNCTION TRG_storage_locations_b_u_AS_DML () > > RETURNS TRIGGER > > AS $$ > > DECLARE > > v_separator VARCHAR (1) = '/'; > > v_cnt INT; > > BEGIN > > -- [ -- Required to prevent infinite recursion > > SELECT COUNT (*) INTO v_cnt > > FROM new_table; > > Where is new_table coming from? > > > > > IF (v_cnt > 0) THEN > > -- ] -- Required to prevent infinite recursion > > UPDATE storage_locations > > SET storage_location_path = COALESCE (i.storage_location_path || > > v_separator, '') || storage_locations.storage_location_nm > > FROM inserted i > > JOIN deleted d > > ON ( i.storage_location_id = d.storage_location_id > > AND i.storage_location_path != d.storage_location_path > > ) > > WHERE storage_locations.parent_storage_location_id = > i.storage_location_id; > > END IF; > > RETURN NULL; > > END > > $$ LANGUAGE plpgsql; > > > > CREATE TRIGGER storage_locations_b_u_AS_DML > > AFTER UPDATE > > ON storage_locations > > REFERENCING NEW TABLE AS inserted > > OLD TABLE AS deleted > > FOR EACH STATEMENT EXECUTE FUNCTION > TRG_storage_locations_b_u_AS_DML (); > > > > Notice that the Trigger is getting called endlessly (if the > number of > > rows in the NEW TABLE are NOT checked). I reckon if there are not > any > > rows, what is the need to call the trigger. Or, may be, I am missing > > something, which I need to learn. > > Yes: > > https://www.postgresql.org/docs/10/sql-createtrigger.html > > "... In contrast, a trigger that is marked FOR EACH STATEMENT only > executes once for any given operation, regardless of how many rows it > modifies (in particular, an operation that modifies zero rows will > still > result in the execution of any applicable FOR EACH STATEMENT triggers)." > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > > Thanks, > > > > > > > > > > > > Jiten > > > > > > > > > > > -- > Adrian Klaver > adrian.klaver@aklaver.com > -- Adrian Klaver adrian.klaver@aklaver.com