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 1iDXLJ-0001om-W1 for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 17:14:14 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iDXLI-0004sS-La for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 17:14:12 +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 1iDU4O-0006qy-68 for pgsql-general@lists.postgresql.org; Thu, 26 Sep 2019 13:44:32 +0000 Received: from mx1.riseup.net ([198.252.153.129]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iDU4K-0001hY-SR for pgsql-general@postgresql.org; Thu, 26 Sep 2019 13:44:30 +0000 Received: from bell.riseup.net (bell-pn.riseup.net [10.0.1.178]) (using TLSv1 with cipher ECDHE-RSA-AES256-SHA (256/256 bits)) (Client CN "*.riseup.net", Issuer "COMODO RSA Domain Validation Secure Server CA" (verified OK)) by mx1.riseup.net (Postfix) with ESMTPS id 46fGPQ6MhtzDqjr; Thu, 26 Sep 2019 06:44:26 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=riseup.net; s=squak; t=1569505467; bh=ZXvduZ6pkzb4f40p5kV+dNyxckN1BheqNZEZ80gTRpE=; h=Subject:To:Cc:References:From:Date:In-Reply-To:From; b=UEFG3T48E05J5fCpD0o2Lhda8X2VDYi7BY1vAPDErWVSFB1S+ZtZLDpzenkCQCJ+5 iW05QCXw+PcHv8mHhfeHlnKBlW1vaLMHND3Xy+Onje3e5/1tfmnIS3wxou9iooZDJY ytH4zwOEUE+tK5m4VV2rYZOOXFZXyxhQQAvWxGrQ= X-Riseup-User-ID: 2F8876E962F62660F44266DA6AA98AE30373B60C7E68E2859044CDEE21ECDD71 Received: from [127.0.0.1] (localhost [127.0.0.1]) by bell.riseup.net (Postfix) with ESMTPSA id 46fGPN51xQzJxXP; Thu, 26 Sep 2019 06:44:24 -0700 (PDT) Subject: Re: managing primary key conflicts while restoring data to table with existing data To: Rob Sargent Cc: Adrian Klaver , pgsql-general@postgresql.org References: <399629ce-aefe-d0df-7552-99fc6bc3d958@riseup.net> <920a0616-125c-6b6a-fd51-abd250a6ee9c@aklaver.com> <3dcb8017-9126-26ad-dbc8-99ad31b81d0f@riseup.net> <258FDE31-77A2-4E3B-8BD8-9AB9199999AA@gmail.com> <113DEFAE-E197-4966-B97F-C6F1C3471F75@gmail.com> From: Krishnakant Mane Openpgp: preference=signencrypt Autocrypt: addr=kkmane@riseup.net; prefer-encrypt=mutual; keydata= xsFNBFofqfQBEADJJqpfeolA2X9mCKx6OfZfkaVTs1LQvb2gFctFLT9TSGN37gstOg/Ffq63 npNT2FYvxk0v23JCFu78KLmwni97OGPcTYTWYdjngAKkJ91PxbIWBtEf0b79TstoEe0OaAn4 KAt2rO+EVtMpQy95TaZcZrIFXJeuIWScHtDM64JTk/3uMgOm2sGWZQ+HSULukS6yt+k/Hd5i SAngEtFtRdxj14YqLrYgnZsGbNw4ac71KcbKsGz8HNUP0LLCf5yKOQ7MzTP0y1+M6B1GVx6z qnwO5k6dtKr0dnLsfDsdFUQMRHYKeE0JsVv/qTL18/qQtLXk/93wMMtANPG3z7Y/7AeVaxDd uSuKEZ+GARCBIzdM+Im9fHM66SePgxetwSMLuG8+rhbJhmjnBdmn5bykEjnsX40yhQi3Qi2u w3wYFYiW8AhBIm4X3W0z4VpdquYExeOnbOdTD2c5sylDfdZQl/ntRlHcf2UTFi3A/Zj8XeI2 eTihtFy0dvSgKwRQYvmwHcnKsHFGiZiGInk7Yls8tvGSO9MdNEj+2VtjE0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ZNtfXutOex86 lT1OjQrCucC6GouJNVaErSDllfTHOBL9t5L0sBpLkM6Xyxld7xBxYVc/09ngspW6fyNwXZTa ocBk+fgxEcdazvvZwPlJUGgOO80yXKAmb73D/JFVFhiZ9mv6NnOg7fZL/+WuiQiJ00x92Oh8 rwexCR7E3X88RcorKhHyh7WX6Wcsd3076EF+hFxUlKZWqpA3uIk3IhzgJ8nURsRAqmH4zkVc jT7idx1lDCI= Message-ID: <90a6191b-9b45-eb43-c3da-6587d7731a84@riseup.net> Date: Thu, 26 Sep 2019 19:14:16 +0530 MIME-Version: 1.0 In-Reply-To: <113DEFAE-E197-4966-B97F-C6F1C3471F75@gmail.com> Content-Type: multipart/alternative; boundary="------------5CF03CAA915D32A1318ED42D" Content-Language: en-US List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk This is a multi-part message in MIME format. --------------5CF03CAA915D32A1318ED42D Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit On 26/09/19 6:53 PM, Rob Sargent wrote: >   > > On Sep 26, 2019, at 12:27 AM, Krishnakant Mane > wrote: > >> >> On 26/09/19 12:03 AM, Adrian Klaver wrote: >>> On 9/25/19 8:04 AM, Rob Sargent wrote: >>>> >>>> >>>> On Sep 25, 2019, at 8:24 AM, Krishnakant Mane >>> > wrote: >>>> >>>>> >>>>> On 25/09/19 7:50 PM, Adrian Klaver wrote: >>>>>> On 9/25/19 12:15 AM, Krishnakant Mane wrote: >>>>>>> Hello all, >>>>>>> >>>>>>> I have been using postgresql for an enterprise quality account's >>>>>>> automation and inventory management software called GNUKhata >>>>>>> >>>>>>> >>>>>>> Our team is planning to add backup and restore function in the >>>>>>> software. >>>>>>> >>>>>>> But we don't want to dump the entire database and then restore >>>>>>> the same. >>>>>>> >>>>>>> What we are trying to do is to copy data specific to an >>>>>>> organization. >>>>>>> >>>>>>> The challenge here is that I might copy all data (account heads, >>>>>>> bills, vouchers etc ) for one organization from an instance on >>>>>>> one machine. >>>>>>> >>>>>>> I take the archive in what ever format to another machine and >>>>>>> now attempt to restore. >>>>>>> >>>>>>> The risk here is for example if the primary key value for >>>>>>> orgcode in the organization table is 5, it might conflict with >>>>>>> the data where I am attempting it to be restored. >>>>>>> >>>>>>> Same holds true for bills, invoices etc. >>>>>>> >>>>>>> A certain account head with accountcode 1 might be already >>>>>>> present on the second machine. >>>>>>> >>>>>>> I am not expecting the users to empty all data from the >>>>>>> destination machine before restoring a backup. >>>>>>> >>>>>>> The reason is that an auditor may have many client's data and >>>>>>> one can't predict what primary key values are going to come from >>>>>>> a backup. >>>>>>> >>>>>>> Basically I can even say this is a copy paste instead of a pure >>>>>>> backup and restore. >>>>>>> >>>>>>> Can any one suggest how to handle such conflicts? >>>>>> >>>>>> Hard to say. If the data is held in common tables(bills, >>>>>> vouchers, etc)then the only thing I see happening is changing the >>>>>> PK values to an unused value. That could turn into a nightmare >>>>>> though. Not only that you lose the connection to the original >>>>>> data source. If the data can be broken out into separate tables >>>>>> then I could see placing them in their own schema. >>>>>> >>>>>>> >>>>>>> >>>>>>> --  >>>>>>> Regards, >>>>>>> Krishnakant Mane, >>>>>>> Project Founder and Leader, >>>>>>> GNUKhata >>>>>>> //(Opensource Accounting, Billing and Inventory Management >>>>>>> Software)// >>>>>> >>>>> >>>>> Hi Adrian, >>>>> >>>>> Even I am thinnking to do some kind of upsert with this situation. >>> >>> So to be clear the tables you are working can have records from >>> multiple organizations in a single table? >>> >>>>> >>>>> And I would have to set the pkey to an unassigned value when there >>>>> is conflict. >>> >>> I am seeing nextval() in your future:) >>> >>>>> >>>>> I may also choose to revamp the serial by timestamps but don't >>>>> know if the target customers would like it. >>> >>> I would avoid that. In my opinion timestamps are to too volatile to >>> serve as a PK. If you are going to change I would go with the >>> previous suggestion of UUID: >>> https://www.postgresql.org/docs/11/datatype-uuid.html >>> >>> Not sure your customers would like that either. >>> >> Hi Adrian, >> >> I think I would make them like the uuid idea. >> >> So now what I am thinking is to first revamp the database by first >> removing all the primary key constraints and then deleting all the >> values. >> >> Then loop through the existing data and get uuid in that colum for >> every row. >> >> I might also require to update all the references to this value as >> foreign key in related tables. >> >> But I guess some kind of on update cascade might do well. >> >> I know this would slow down the system, but given that this will be a >> one time process for an individual user (that too if he has existing >> data ), I would take that trade-off. >> >> What do you say? >> >>>>> >>>>> --  >>>>> Regards, >>>>> Krishnakant Mane, >>>>> Project Founder and Leader, >>>>> GNUKhata >>>>> //(Opensource Accounting, Billing and Inventory Management >>>>> Software)// >>>> It would likely be easier to rethink your backup and restore plan. >>>> Putting each restore into its own space would be one tack. >>> >>> >> -- >> Regards, >> Krishnakant Mane, >> Project Founder and Leader, >> GNUKhata >> //(Opensource Accounting, Billing and Inventory Management Software)// > You might think about adding the new UUID column and use the existing > primary key to inform the updates in dependent tables. Then remove the > old PK  column and constraint followed by promoting the UUID to > primary key. This could be safely scripted and applied to all > instances of your data.  > That said, this is only truly necessary of you have production > databases to worry about. Thanks a million, this is the most logical and safe way. yes I have a lot of production databases to worry about. I am only confused about what you mean by "use the existing primary key to inform the updates in dependent tables." Are you refering to a cascading effect? If yes then does it mean I first program my upgrade script to manually go through all new uuid keys and update the same in the depending tables with reference to the old primary key working as foreign key in those tables? -- Regards, Krishnakant Mane, Project Founder and Leader, GNUKhata //(Opensource Accounting, Billing and Inventory Management Software)// --------------5CF03CAA915D32A1318ED42D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 8bit


On 26/09/19 6:53 PM, Rob Sargent wrote:
 

On Sep 26, 2019, at 12:27 AM, Krishnakant Mane <kkmane@riseup.net> wrote:


On 26/09/19 12:03 AM, Adrian Klaver wrote:
On 9/25/19 8:04 AM, Rob Sargent wrote:


On Sep 25, 2019, at 8:24 AM, Krishnakant Mane <kkmane@riseup.net <mailto:kkmane@riseup.net>> wrote:


On 25/09/19 7:50 PM, Adrian Klaver wrote:
On 9/25/19 12:15 AM, Krishnakant Mane wrote:
Hello all,

I have been using postgresql for an enterprise quality account's automation and inventory management software called GNUKhata <https://gnukhata.in>

Our team is planning to add backup and restore function in the software.

But we don't want to dump the entire database and then restore the same.

What we are trying to do is to copy data specific to an organization.

The challenge here is that I might copy all data (account heads, bills, vouchers etc ) for one organization from an instance on one machine.

I take the archive in what ever format to another machine and now attempt to restore.

The risk here is for example if the primary key value for orgcode in the organization table is 5, it might conflict with the data where I am attempting it to be restored.

Same holds true for bills, invoices etc.

A certain account head with accountcode 1 might be already present on the second machine.

I am not expecting the users to empty all data from the destination machine before restoring a backup.

The reason is that an auditor may have many client's data and one can't predict what primary key values are going to come from a backup.

Basically I can even say this is a copy paste instead of a pure backup and restore.

Can any one suggest how to handle such conflicts?

Hard to say. If the data is held in common tables(bills, vouchers, etc)then the only thing I see happening is changing the PK values to an unused value. That could turn into a nightmare though. Not only that you lose the connection to the original data source. If the data can be broken out into separate tables then I could see placing them in their own schema.



-- 
Regards,
Krishnakant Mane,
Project Founder and Leader,
GNUKhata <https://gnukhata.in/>
//(Opensource Accounting, Billing and Inventory Management Software)//


Hi Adrian,

Even I am thinnking to do some kind of upsert with this situation.

So to be clear the tables you are working can have records from multiple organizations in a single table?


And I would have to set the pkey to an unassigned value when there is conflict.

I am seeing nextval() in your future:)


I may also choose to revamp the serial by timestamps but don't know if the target customers would like it.

I would avoid that. In my opinion timestamps are to too volatile to serve as a PK. If you are going to change I would go with the previous suggestion of UUID:
https://www.postgresql.org/docs/11/datatype-uuid.html

Not sure your customers would like that either.

Hi Adrian,

I think I would make them like the uuid idea.

So now what I am thinking is to first revamp the database by first removing all the primary key constraints and then deleting all the values.

Then loop through the existing data and get uuid in that colum for every row.

I might also require to update all the references to this value as foreign key in related tables.

But I guess some kind of on update cascade might do well.

I know this would slow down the system, but given that this will be a one time process for an individual user (that too if he has existing data ), I would take that trade-off.

What do you say?


-- 
Regards,
Krishnakant Mane,
Project Founder and Leader,
GNUKhata <https://gnukhata.in/>
//(Opensource Accounting, Billing and Inventory Management Software)//
It would likely be easier to rethink your backup and restore plan. Putting each restore into its own space would be one tack.


--
Regards,
Krishnakant Mane,
Project Founder and Leader,
GNUKhata
(Opensource Accounting, Billing and Inventory Management Software)
You might think about adding the new UUID column and use the existing primary key to inform the updates in dependent tables. Then remove the old PK  column and constraint followed by promoting the UUID to primary key. This could be safely scripted and applied to all instances of your data. 
That said, this is only truly necessary of you have production databases to worry about.


Thanks a million, this is the most logical and safe way.

yes I have a lot of production databases to worry about.

I am only confused about what you mean by "use the existing primary key to inform the updates in dependent tables."

Are you refering to a cascading effect?

If yes then does it mean I first program my upgrade script to manually go through all new uuid keys and update the same in the depending tables with reference to the old primary key working as foreign key in those tables?


--
Regards,
Krishnakant Mane,
Project Founder and Leader,
GNUKhata
(Opensource Accounting, Billing and Inventory Management Software)
--------------5CF03CAA915D32A1318ED42D--