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 1kgula-0007Rh-L7 for pgsql-general@arkaria.postgresql.org; Sun, 22 Nov 2020 19:11:18 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kgulZ-00072L-Jb for pgsql-general@arkaria.postgresql.org; Sun, 22 Nov 2020 19:11:17 +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 1kgulZ-00072E-Bv for pgsql-general@lists.postgresql.org; Sun, 22 Nov 2020 19:11:17 +0000 Received: from p3plsmtpa07-01.prod.phx3.secureserver.net ([173.201.192.230]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1kgulT-0007Ea-1s for pgsql-general@lists.postgresql.org; Sun, 22 Nov 2020 19:11:16 +0000 Received: from DESKTOPPBOKT6J ([98.38.100.21]) by :SMTPAUTH: with ESMTPSA id gulQkMVV9qwDqgulQkke90; Sun, 22 Nov 2020 12:11:08 -0700 X-CMAE-Analysis: v=2.4 cv=Cr56zl0D c=1 sm=1 tr=0 ts=5fbab7cc a=tZXnaF/PLMojNGgMD7Zm7A==:117 a=tZXnaF/PLMojNGgMD7Zm7A==:17 a=IkcTkHD0fZMA:10 a=SNL7wKpgAAAA:8 a=C-sdMEEzAAAA:8 a=epTmVMiNAAAA:8 a=3gY6YFAKCuEVih10eXsA:9 a=QEXdDO2ut3YA:10 a=uEXi9pYaZPHPrIM342IW:22 a=7WAne9lm5b0o3WrbkWlm:22 a=ndEWmUVY6Yapc0oHF_P4:22 X-SECURESERVER-ACCT: hagen@datasundae.com From: To: "'Adrian Klaver'" , 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> <4131ec0f-7dfa-d170-8312-d4fa3fc7ec0f@aklaver.com> In-Reply-To: <4131ec0f-7dfa-d170-8312-d4fa3fc7ec0f@aklaver.com> Subject: RE: INSERT Trigger to check for existing records Date: Sun, 22 Nov 2020 12:11:05 -0700 Message-ID: <01b201d6c103$3a19cb80$ae4d6280$@datasundae.com> MIME-Version: 1.0 Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Mailer: Microsoft Outlook 16.0 Thread-Index: AQI1vRxzwZPQCHZ4ohRSJ/ouoa4aowLrdCJXAWXtZU8CkAIwLwMR34T1Ak2bFosC2ze7PAHLl6oSAdwS6DQCPNrjmwFOSW2KAulG9xGoTMo5gA== Content-Language: en-us X-CMAE-Envelope: MS4xfAuYw8/R68PquLuNiCiFincYf5Uj5AdhbnoubznN2r9KA92dE0BGNAom9FxKG3yx+BbnlUGG6RmGTgEadNDCdKE6AhedYeoJpIRT+NNO3TE8P9V0zc6X eNNUkbPL4kHOeXojyd5b3IDotBmGaVURwTvMO8tKrQUXUALIUz479fkRTYPlzoJJY+mpyxT1t5pliRTgmO+weEsAs2A4yxF/CbCq3kgYKv6LkMqVu04y86ki rKqKs7e7hZdYS2FPPEYy8g== List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk Thank you Adrian, Your summary looks exactly right to me. I think option 2b looks more in = reach for my limited skillset. Let me see if I can make that work (dubious) and report. Best, Hagen -----Original Message----- From: Adrian Klaver =20 Sent: Sunday, November 22, 2020 11:26 AM To: Hagen Finley ; = pgsql-general@lists.postgresql.org Subject: Re: INSERT Trigger to check for existing records On 11/22/20 9:53 AM, Hagen Finley wrote: > Hello Michael, >=20 > Thanks so much for this advice. As I mentioned previously, I'm not=20 > very good at this yet, so forgive me if my response is obtuse. I=20 > really love databases but my sentiments may be unrequited. The overriding issue is lack of a plan. From your first post: "Here=E2=80=99s 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. >=20 > 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. >=20 > To clarify, I would create an unique index on all the columns in the=20 > old report records (2020-11-02) or just the three I am comparing=20 > (dealid,stage and revenue)? >=20 > In either case, so far in my efforts it looks like the create index=20 > fails because there are lots of rows with the same stage value, and a=20 > few with the same revenue value. >=20 > Create UNIQUE INDEX idx_sfdc > ON sfdc(ndealid, stage, revusd); >=20 > ERROR: could not create unique index "idx_sfdc" DETAIL: Key (ndealid,=20 > stage, revusd)=3D(19743576, Commit - 90% , 22072.37) is duplicated. = SQL > state: 23505 >=20 > I probably could create an unique index on the dealid column as that=20 > should be unique. Would that be enough? It seems like that would=20 > insert ONLY the new records with a new (unique) dealid and that would=20 > definitely by an important step forward. >=20 > I hesitate to admit I have no idea how I would code the "call insert=20 > on conflict (unique index) do nothing" syntax, but I would be excited = to learn. >=20 > Best, >=20 > Hagen >=20 >=20 > On 11/22/20 8:54 AM, Michael Lewis wrote: >> If you can modify your insert statement, and live with an extra=20 >> 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"=20 >> field and call insert on conflict (unique index) do nothing. >> >> This should give the behavior you want. -- Adrian Klaver adrian.klaver@aklaver.com