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 1kgu4P-0005pf-UA for pgsql-general@arkaria.postgresql.org; Sun, 22 Nov 2020 18:26:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgu4M-0002nI-FH for pgsql-general@arkaria.postgresql.org; Sun, 22 Nov 2020 18:26:38 +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 1kgu4L-0002nB-3I for pgsql-general@lists.postgresql.org; Sun, 22 Nov 2020 18:26:38 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgu4D-0006GL-Bh for pgsql-general@lists.postgresql.org; Sun, 22 Nov 2020 18:26:35 +0000 Received: from compute3.internal (compute3.nyi.internal [10.202.2.43]) by mailout.west.internal (Postfix) with ESMTP id D0CDF4E4; Sun, 22 Nov 2020 13:26:26 -0500 (EST) Received: from mailfrontend2 ([10.202.2.163]) by compute3.internal (MEProxy); Sun, 22 Nov 2020 13:26:27 -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=a V5jDD/QSoKbQl9WMs0eaMrUhVWTyE5+UVa7Nn6S2rg=; b=VlKK9+UQ5KYxIcDJ7 2njV7O6eZ1RLwteAbpNScnWTtJ3N3OyOqMW7/mYnpMM9+GsDxV/vmG0Jrgxhz+ag 3GIE271M51iOGjGFMMU7WCj7Lh1ykbsBrad/d8M3HvglGyTp8dy9CJt4jZTYHtFO ouul6EWCPqkAJuf+VgG1fQcfJpAVxXehHT/P98gMQln3HR86sjHqCQygmI3Un6Ii k7oeBVZHX12QURJWJePGNdZfjGcfxHL1mWsniePkL/cVd1y2rx/bfH9uSjeFXq7e sUd0orK0rW4lyh2gG5OHB5j6uj4Bv+gUMFQXlL/FuoXvpU5xTFWRp2Zag6h2tnal 3AAOA== 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=aV5jDD/QSoKbQl9WMs0eaMrUhVWTyE5+UVa7Nn6S2 rg=; b=E9+ELT8zgQmWdaCbo69OcQiKoRVTmJNk4zaPhfKfNppQzKbR7ZPNb4Xea g2MV9OOeGfqp3bb33neSnelflW7gFfLih7JGDeUgt71kgC0V1KxWIepVT2L8oa4D 3mOQQc4JtFP9gAqo47ZPT87N6i2GCfdFJ+V8MGQ70xu9pHl5j8BVGz6Y0ZEzmos9 JuBZ8zVpxweqEM6nt+D/x8EUSUMOP3xlTlFJwWKh3A1rZE12JVzn29sOfneHGZQ7 veZmQQ00FTKmvquqmaNHqdJ0OAbbVxT/ElwlX3+8rjKC2LcXtr3beFnG/pyTOST3 5ffiZ/UWf1yKckcUao23mpH/h+XwQ== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedujedrudeggedgudduiecutefuodetggdotefrod 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 B86F13064AAF; Sun, 22 Nov 2020 13:26:25 -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> <000e01d6c036$fda479b0$f8ed6d10$@datasundae.com> <2ecce3f7-f298-7fd2-f470-5cf9a71b349c@datasundae.com> From: Adrian Klaver Message-ID: <4131ec0f-7dfa-d170-8312-d4fa3fc7ec0f@aklaver.com> Date: Sun, 22 Nov 2020 10:26:24 -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: <2ecce3f7-f298-7fd2-f470-5cf9a71b349c@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/22/20 9:53 AM, Hagen Finley wrote: > Hello Michael, > > Thanks so much for this advice. As I mentioned previously, I'm not very > good at this yet, so forgive me if my response is obtuse. I really love > databases but my sentiments may be unrequited. The overriding issue is lack of a plan. From your first post: "Here’s what I (REALLY) want: Trigger looks at three fields prior to new insert: Deal ID (numeric), revenue (numeric), stage(char) Example: 19743576 22072.37 Commit - 90% 1. If the NEW dealid doesn't match any of the OLD dealids, insert the new row 2. if the NEW dealid, revenue and stage fields ALL match the OLD dealid, revenue and stage, skip (don't insert the NEW row) 3. If the NEW dealid matches an OLD dealid but either the NEW revenue OR the stage fields have changed (don't match OLD record) insert new row (I'll review both rows manually) " And from later post: " I figure I could use the chk field to delete the new inserts I didn't need." From this I come up with the following: 1) Data rules a) If dealid in new data does not exist in old data INSERT row. b) Id dealid is in both new and old data AND revenue OR stage don't match then INSERT and mark for review. c) If new dealid, revenue, stage match old dealid, revenue, stage then do not INSERT. 2) Process the data. Choices a) Use trigger on table sfdc b) Use staging table to hold new data and then process into sfdc table 3) Process the data. Same basic principle for both choices in 2) Flowchart a) In new data search for dealid in table sfdc if it does not exist add data to sfdc. b) If new data dealid does exist in sfdc 1) If revenue or stage field differ mark for review 2) If they do match skip further processing 4) Thoughts about above. a) To me table sfdc should only hold vetted data that is known to be unique per row. b) The data for review 1)b) 3)b) should end up in another review table e.g. sfdc_review. c) Since from OP ' 80% of the records are the same as the existing records from the week before.' it makes sense to use the staging table 2)b) process rather then throwing away a lot of INSERTs. If this makes sense then it comes down to decision in which choice in 2) to use. At that point it is filling in the flowchart with the exact steps to take. > > In reality my table has lots of columns (~30) including a report date > (repdate) and each week's pull has a new repdate ( in this case > 2020-11-02 and 2020-11-09) which could function as a "created on" field. > > To clarify, I would create an unique index on all the columns in the old > report records (2020-11-02)  or just the three I am comparing > (dealid,stage and revenue)? > > In either case, so far in my efforts it looks like the create index > fails because there are lots of rows with the same stage value, and a > few with the same revenue value. > > Create UNIQUE INDEX idx_sfdc > ON sfdc(ndealid, stage, revusd); > > ERROR: could not create unique index "idx_sfdc" DETAIL: Key (ndealid, > stage, revusd)=(19743576, Commit - 90% , 22072.37) is duplicated. SQL > state: 23505 > > I probably could create an unique index on the dealid column as that > should be unique. Would that be enough? It seems like that would insert > ONLY the new records with a new (unique) dealid and that would > definitely by an important step forward. > > I hesitate to admit I have no idea how I would code the "call insert on > conflict (unique index) do nothing" syntax, but I would be excited to learn. > > Best, > > Hagen > > > On 11/22/20 8:54 AM, Michael Lewis wrote: >> If you can modify your insert statement, and live with an extra column >> in the data, no trigger is needed as best I can figure. >> >> Create a unique index over the existing columns, add a "created_on" >> field and call insert on conflict (unique index) do nothing. >> >> This should give the behavior you want. -- Adrian Klaver adrian.klaver@aklaver.com