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 1iDUQq-0003Go-B6 for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 14:07:44 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iDUPr-00079E-8I for pgsql-general@arkaria.postgresql.org; Thu, 26 Sep 2019 14:06:43 +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 1iDUPq-000790-Lh for pgsql-general@lists.postgresql.org; Thu, 26 Sep 2019 14:06:42 +0000 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1iDUPj-0001vW-Hw for pgsql-general@postgresql.org; Thu, 26 Sep 2019 14:06:41 +0000 Received: by mail-io1-xd41.google.com with SMTP id q1so6938102ion.1 for ; Thu, 26 Sep 2019 07:06:34 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=kMY1apfQHx3+jLeI1jgqRCHfXt8pyptTirUjl2PcuI0=; b=LqrzzxpJmB/g7qHceqegkwPq7slUR4qF9kXtd/i7JNl6J5HrWgH0Us7uj+9LN+Gz8r 7ShPQdBKmzDqDztW6w4qVHW/3Nd6znY+EGeGGxEEUfrqn/OBIUB7wKl+9RRYNbMaf/xL DpeNrdzPOkgPFEQ3Xaeu74Hdz4+WlaWhCgMwc9An0tpeiU14fElyqoY/wDs/mOxUOzRB ImL9Awbah9YkWzqKLTV2TXYj8V5N/YzgkFiWiz3+WRjychueednkTtwhGqz5buFfgstT +X9BsA6mIwfiSiUikHgYs0cnJqpxJrwl7nb8Z5IGlGe2a9fjLRFpm8Xe6wHNxUJVwfdr t89w== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:mime-version:subject:from:in-reply-to:date:cc :content-transfer-encoding:message-id:references:to; bh=kMY1apfQHx3+jLeI1jgqRCHfXt8pyptTirUjl2PcuI0=; b=E8Zi3dD8uYVplUMoHNFdepfa0eo+yNT8jW+35k9VeBYswerexxWR32O0r2OcQ2qfne ghbNeEQtdXbvQMcAN3syS1vzEIebKcjy3Z1Z34n4f03aRh6dnfF6XN1HdO0I2MEaya4L U+lmiL+7sucY7L5KQ7OgXna2wbUJkNDwX6u2n8a631YE104cCFaCEKFMElhA+2shF4ob qUlx2WYsG8S7gB2cwQIVG2mnIC9gq1Wt0NB1wyIZXvQ0ezy8YK5VJ49WChQP/4WmvqS3 DkQ+6CzJCxRYHvDgYvqRQ/LvAHTCZul5XFatAx2seWy3Gx7Uz08uQg7MmnkQVfh3U3qL 7u2A== X-Gm-Message-State: APjAAAXkiwGUswOErYX+A/lav1sYP1U0wRr87N7bgRGDywfahEtJ6Rmf QDccz4RxTK4HvQU3AhFiLSE= X-Google-Smtp-Source: APXvYqxYwdapBoldUkY2QX9RJDZOnvCLDMM9wNOfVFt7NRtrZ8R8X7mr7tHKYQ9XJ0TmTL9+3I/l7g== X-Received: by 2002:a02:6616:: with SMTP id k22mr3530895jac.129.1569506791047; Thu, 26 Sep 2019 07:06:31 -0700 (PDT) Received: from [10.0.0.10] (c-73-63-59-118.hsd1.ut.comcast.net. [73.63.59.118]) by smtp.gmail.com with ESMTPSA id c19sm692326ila.19.2019.09.26.07.06.30 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 26 Sep 2019 07:06:30 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-A62273A6-8053-4A9B-AE04-DE76E084DE0F Mime-Version: 1.0 (1.0) Subject: Re: managing primary key conflicts while restoring data to table with existing data From: Rob Sargent X-Mailer: iPhone Mail (16G102) In-Reply-To: <90a6191b-9b45-eb43-c3da-6587d7731a84@riseup.net> Date: Thu, 26 Sep 2019 08:06:29 -0600 Cc: Adrian Klaver , pgsql-general@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: <9BD0F10C-EADC-4B2F-AF32-32403C747EAE@gmail.com> 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> <90a6191b-9b45-eb43-c3da-6587d7731a84@riseup.net> To: Krishnakant Mane List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-A62273A6-8053-4A9B-AE04-DE76E084DE0F Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable > On Sep 26, 2019, at 7:44 AM, Krishnakant Mane wrote: >=20 >=20 >> On 26/09/19 6:53 PM, Rob Sargent wrote: >> =20 >>=20 >> On Sep 26, 2019, at 12:27 AM, Krishnakant Mane wrote:= >>=20 >>>=20 >>>> On 26/09/19 12:03 AM, Adrian Klaver wrote: >>>>> On 9/25/19 8:04 AM, Rob Sargent wrote:=20 >>>>>=20 >>>>>=20 >>>>> On Sep 25, 2019, at 8:24 AM, Krishnakant Mane > wrote:=20 >>>>>=20 >>>>>>=20 >>>>>>> On 25/09/19 7:50 PM, Adrian Klaver wrote:=20 >>>>>>>> On 9/25/19 12:15 AM, Krishnakant Mane wrote:=20 >>>>>>>> Hello all,=20 >>>>>>>>=20 >>>>>>>> I have been using postgresql for an enterprise quality account's au= tomation and inventory management software called GNUKhata =20 >>>>>>>>=20 >>>>>>>> Our team is planning to add backup and restore function in the soft= ware.=20 >>>>>>>>=20 >>>>>>>> But we don't want to dump the entire database and then restore the s= ame.=20 >>>>>>>>=20 >>>>>>>> What we are trying to do is to copy data specific to an organizatio= n.=20 >>>>>>>>=20 >>>>>>>> The challenge here is that I might copy all data (account heads, bi= lls, vouchers etc ) for one organization from an instance on one machine.=20= >>>>>>>>=20 >>>>>>>> I take the archive in what ever format to another machine and now a= ttempt to restore.=20 >>>>>>>>=20 >>>>>>>> The risk here is for example if the primary key value for orgcode i= n the organization table is 5, it might conflict with the data where I am at= tempting it to be restored.=20 >>>>>>>>=20 >>>>>>>> Same holds true for bills, invoices etc.=20 >>>>>>>>=20 >>>>>>>> A certain account head with accountcode 1 might be already present o= n the second machine.=20 >>>>>>>>=20 >>>>>>>> I am not expecting the users to empty all data from the destination= machine before restoring a backup.=20 >>>>>>>>=20 >>>>>>>> The reason is that an auditor may have many client's data and one c= an't predict what primary key values are going to come from a backup.=20 >>>>>>>>=20 >>>>>>>> Basically I can even say this is a copy paste instead of a pure bac= kup and restore.=20 >>>>>>>>=20 >>>>>>>> Can any one suggest how to handle such conflicts?=20 >>>>>>>=20 >>>>>>> Hard to say. If the data is held in common tables(bills, vouchers, e= tc)then the only thing I see happening is changing the PK values to an unuse= d 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 s= eparate tables then I could see placing them in their own schema.=20 >>>>>>>=20 >>>>>>>>=20 >>>>>>>>=20 >>>>>>>> --=20 >>>>>>>> Regards,=20 >>>>>>>> Krishnakant Mane,=20 >>>>>>>> Project Founder and Leader,=20 >>>>>>>> GNUKhata =20 >>>>>>>> //(Opensource Accounting, Billing and Inventory Management Software= )//=20 >>>>>>>=20 >>>>>>=20 >>>>>> Hi Adrian,=20 >>>>>>=20 >>>>>> Even I am thinnking to do some kind of upsert with this situation.=20= >>>>=20 >>>> So to be clear the tables you are working can have records from multipl= e organizations in a single table?=20 >>>>=20 >>>>>>=20 >>>>>> And I would have to set the pkey to an unassigned value when there is= conflict.=20 >>>>=20 >>>> I am seeing nextval() in your future:)=20 >>>>=20 >>>>>>=20 >>>>>> I may also choose to revamp the serial by timestamps but don't know i= f the target customers would like it.=20 >>>>=20 >>>> I would avoid that. In my opinion timestamps are to too volatile to ser= ve as a PK. If you are going to change I would go with the previous suggesti= on of UUID:=20 >>>> https://www.postgresql.org/docs/11/datatype-uuid.html=20 >>>>=20 >>>> Not sure your customers would like that either.=20 >>>>=20 >>> Hi Adrian, >>>=20 >>> I think I would make them like the uuid idea. >>>=20 >>> So now what I am thinking is to first revamp the database by first remov= ing all the primary key constraints and then deleting all the values. >>>=20 >>> Then loop through the existing data and get uuid in that colum for every= row. >>>=20 >>> I might also require to update all the references to this value as forei= gn key in related tables. >>>=20 >>> But I guess some kind of on update cascade might do well. >>>=20 >>> I know this would slow down the system, but given that this will be a on= e time process for an individual user (that too if he has existing data ), I= would take that trade-off. >>>=20 >>> What do you say? >>>=20 >>>>>>=20 >>>>>> --=20 >>>>>> Regards,=20 >>>>>> Krishnakant Mane,=20 >>>>>> Project Founder and Leader,=20 >>>>>> GNUKhata =20 >>>>>> //(Opensource Accounting, Billing and Inventory Management Software)/= /=20 >>>>> It would likely be easier to rethink your backup and restore plan. Put= ting each restore into its own space would be one tack.=20 >>>>=20 >>>>=20 >>> --=20 >>> Regards,=20 >>> 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 pri= mary key to inform the updates in dependent tables. Then remove the old PK c= olumn and constraint followed by promoting the UUID to primary key. This cou= ld be safely scripted and applied to all instances of your data.=20 >> That said, this is only truly necessary of you have production databases t= o worry about. >=20 > Thanks a million, this is the most logical and safe way. >=20 > yes I have a lot of production databases to worry about. >=20 > I am only confused about what you mean by "use the existing primary key to= inform the updates in dependent tables." >=20 > Are you refering to a cascading effect? >=20 > If yes then does it mean I first program my upgrade script to manually go t= hrough all new uuid keys and update the same in the depending tables with re= ference to the old primary key working as foreign key in those tables? >=20 >=20 >=20 Yes. You will need the old keys to recreate referential integrity (foreign k= eys, etc) > --=20 > Regards,=20 > Krishnakant Mane, > Project Founder and Leader, > GNUKhata > (Opensource Accounting, Billing and Inventory Management Software) --Apple-Mail-A62273A6-8053-4A9B-AE04-DE76E084DE0F Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit


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


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?


Yes. You will need the old keys to recreate referential integrity (foreign keys, etc)

--
Regards,
Krishnakant Mane,
Project Founder and Leader,
GNUKhata
(Opensource Accounting, Billing and Inventory Management Software)
--Apple-Mail-A62273A6-8053-4A9B-AE04-DE76E084DE0F--