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 1kgWQA-0007Rf-Fi for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:11:34 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgWQ9-0006vg-FL for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:11:33 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgWQ9-0006vZ-8C for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:11:33 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgWQ5-0003Ta-UM for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:11:32 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id D0FAF5C009A; Sat, 21 Nov 2020 12:11:27 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 21 Nov 2020 12:11:27 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:from:to:references:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=9 AcD4zSJbkdOi4tLBwpgV2AysQbkyXOWFlWnZ1uhotU=; b=Ec9BKYoJpmxwO0VVX WeacSZcSsGFQwWLUOWVVNhzHupnlCBB/wifDgrpOqnYvBKiKFlS+k9tJC6uZ07es dCk4X0DfiDh9NQeYPH2cXVHSb7h/g5AJw4hajbqCW3eAg/1/UopTFpjBk04KD8Dd +Iv5I0xKRmU8Z9ahOQRodnlwDU9Zxm/aVoLiel2zdNJwHF0uJJU7vrOPWTpQ7/MU JthepmDaokH0UJjVwbJXFmK7rIcZqlMEEDmHjG+D6F7A/JdrB3s5UvhQeVReRGaC EQ/mtAh9GQhPjOjLUrRh1zaJmNl9QQf1vCIB760tSPptvzcZo+5FaKT077ObOpJh dWdfA== 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=9AcD4zSJbkdOi4tLBwpgV2AysQbkyXOWFlWnZ1uho tU=; b=QMsmBWavk4eEudrD0GOjTq3onbsNYK25kWDIDutbux9mepYvThfDWNXZ5 4625fOzYwcDfftU/gRjGVrpVpQSC/4t8vWZVoHSr5rEvQTYUSlXTnAY3UUyFSiz9 m3phSOwisAfdEKCsCqmIXoM726pHXEJ5PKWWHdi14sC91Nt8yeVhlIK+Q9m7Jfff qdx9scUyJEE4D9YqzYUcT/GiCuGVv6+/49PE8xOLU2D7W0yuQjipdIH6cpucSKOq O3rn2k6rKwUZosW+22nRZWSJk7njboKYqBFMU/Q6KJJ2lHdBzDKGn5ODUPKVIK2/ ZgG4UFAXo0ZIjwpquKgO/5hjzNUrQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegvddgleelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffhvfhfkffffgggjggtgfesthekredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucggtffrrghtthgvrhhnpefhleekveehgeffgfekjeffhfdujeeukeeivefhudeg keelgfefgeevheefieeiteenucfkphepjeehrddujedvrdduledrudefleenucevlhhush htvghrufhiiigvpedtnecurfgrrhgrmhepmhgrihhlfhhrohhmpegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from [192.168.1.4] (75-172-19-139.tukw.qwest.net [75.172.19.139]) by mail.messagingengine.com (Postfix) with ESMTPA id 3468D3280060; Sat, 21 Nov 2020 12:11:26 -0500 (EST) Subject: Re: INSERT Trigger to check for existing records From: Adrian Klaver To: Hagen Finley , pgsql-general@lists.postgresql.org References: <1fc1fb9e-684f-d028-3f9b-fcfaf1f12f65@aklaver.com> <3e1051a4-98d4-4a59-f382-5b82efa4c095@datasundae.com> <212448c5-e86b-46df-53d6-0149a0abf68e@aklaver.com> Message-ID: Date: Sat, 21 Nov 2020 09:11:25 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:78.0) Gecko/20100101 Thunderbird/78.5.0 MIME-Version: 1.0 In-Reply-To: <212448c5-e86b-46df-53d6-0149a0abf68e@aklaver.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 11/21/20 8:47 AM, Adrian Klaver wrote: > On 11/21/20 8:20 AM, Adrian Klaver wrote: >> On 11/21/20 8:00 AM, Hagen Finley wrote: >>> Hello, > >> >> Instead: >> >> IF NEW.ndealid = OLD.ndealid AND NEW.revusd = OLD.revusd >>          AND NEW.stage = OLD.stage THEN >>      RETURN NULL; --Will cancel INSERT >> ELSE >>      RETURN NEW; >> >> END IF; > > Well this is what happens when I answer BC(before coffee). The above > will not work, if for no other reason then OLD does not exist in an > INSERT. Will try to come up with something that is in the realm of > possibility. Alright caffeine in the blood stream, so something that might actually work: DECLARE match_ct integer; BEGIN SELECT INTO match_ct count(*) FROM sfdc WHERE ndealid = NEW.ndealid AND revusd = NEW.revusd AND stage = NEW.stage; IF match_ct > 0 THEN RETURN NULL; --Will cancel INSERT ELSE RETURN NEW; END IF; END; Though I would also point you at David's solution. Given that you are only looking at ~20% of the records being different it would save you a lot of churning through INSERTs. > >> >>> >>> Hagen >>> >>> Larimer County, CO >>> >> >> > > -- Adrian Klaver adrian.klaver@aklaver.com