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 1kgXAi-0000fT-L2 for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:59:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgXAh-0004FU-L4 for pgsql-general@arkaria.postgresql.org; Sat, 21 Nov 2020 17:59:39 +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 1kgXAg-0004FA-0l for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:59:39 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgXAd-0003HD-AV for pgsql-general@lists.postgresql.org; Sat, 21 Nov 2020 17:59:37 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.nyi.internal (Postfix) with ESMTP id 504715C00AC; Sat, 21 Nov 2020 12:59:34 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute3.internal (MEProxy); Sat, 21 Nov 2020 12:59:34 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=x b6+Atu5k0xZ2SWMDNh/jsHqWMAStIBPmVye7Ln48sA=; b=UibnivcOF2mHnezYq ttb2blU+TzDLAOZ4qBlLnxIfUSr9fhEOvM56Cc8Zsd7FP1UZ/m7gq+i4uiowWqa1 sXBcM/8l3/+fzdU5TlVhc2HpsBX7QTdilLitbCFJ2VtJAHyf218CWqp7DcII6zVc xL65RSG9pT5BrS/2RUqlUMoR0qzd2FKAfgaz1KXRzAQwQRObOPe6d/0KDoVn5WrF Eb2ZgmbGPr8Ltj4n3IRouJ8i/A7jZFTqgo3G9U9R7UIzZC4mDGQsCVBiRFJK6IYt LJjvOHtxsQsSapAQjPuKMwQ0SB7aBI7lUVvROAD3c6dl6n3OZ8GQ/0+EtasyKwjv dgn5A== 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=xb6+Atu5k0xZ2SWMDNh/jsHqWMAStIBPmVye7Ln48 sA=; b=kn4ooQt9jNsLv+4PQfNlGZHcfzB+DH6uVy6AxIyoy+8bTmyA7+IwL+mXf D5faFTiG0r0wLoQlVOoEB0Oq0MT9hR+BGIc7FQqWEirLiBc/kcgfuBQJ9NOhaGxR i3MqagTybo87uOTA1MZ/PFD3Bv23P3CBQvgQ9CWuYYQsAPn7aSXX1ZwFpEtB1JFm fKfhv6qIIkfRoSlumTynY9/Q6JIsAE0U7sc+jmBW5/dKGPCs0raga0o0L9Skjre/ 4xpX+6pqq6cw2l30r90GkxE5ph6bVwbJItzPsN4TtopROsdyj3Bgj0xOF/0GguQH I6fWlwbHLV7YxZjvNlBCJyfcmvnYA== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudegvddguddtkecutefuodetggdotefrod ftvfcurfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfgh necuuegrihhlohhuthemuceftddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmd enucfjughrpefuvfhfhffkffgfgggjtgfgsehtkeertddtfeejnecuhfhrohhmpeetughr ihgrnhcumfhlrghvvghruceorggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtg homheqnecuggftrfgrthhtvghrnhepveevfeevfeeugeffkeekhfegieeufffgteeujedv gffhueejjefhkeejleegtdegnecukfhppeejhedrudejvddrudelrddufeelnecuvehluh hsthgvrhfuihiivgeptdenucfrrghrrghmpehmrghilhhfrhhomheprggurhhirghnrdhk lhgrvhgvrhesrghklhgrvhgvrhdrtghomh 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 A813C3280064; Sat, 21 Nov 2020 12:59:33 -0500 (EST) Subject: Re: INSERT Trigger to check for existing records 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> <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.com> From: Adrian Klaver Message-ID: <6dd7db95-bd18-e945-567a-6fad730d938f@aklaver.com> Date: Sat, 21 Nov 2020 09:59:32 -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: <9db9e54d-f618-bd65-39f6-90f7d78be87d@datasundae.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 9:47 AM, Hagen Finley wrote: > Thanks so much Adrian, > > I like this approach but as you indicated it doesn't actually NULL the > INSERT. It should cause the INSERT not to happen if a row exists with the same values for ndealid, revusd and stage. Are you seeing an INSERT for those conditions? > > Could we UPDATE the existing record (per my fledgling chk UPDATE and > then RETURN NULL? (More proof I don't know what I am talking about ;-). The INSERT won't happen so I'm not sure what you want to check against? > > Hagen > > > On 11/21/20 10:11 AM, Adrian Klaver wrote: >> 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