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 1iD8qV-0002Ir-LR for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 15:04:47 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iD8qU-0006S2-II for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 15:04:46 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iD8qU-0006Rf-6F for pgsql-general@lists.postgresql.org; Wed, 25 Sep 2019 15:04:46 +0000 Received: from mail-io1-xd41.google.com ([2607:f8b0:4864:20::d41]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1iD8qQ-0005Uq-22 for pgsql-general@postgresql.org; Wed, 25 Sep 2019 15:04:45 +0000 Received: by mail-io1-xd41.google.com with SMTP id u8so14670115iom.5 for ; Wed, 25 Sep 2019 08:04:41 -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=OsDRXpJMhCTq56LgWnJxa3LeggO6HBiGAf2+018WjZs=; b=P2Q/7e0r0OgqHc7m8wMcLN2zL7VZLwxI9d/b2W99oW7CAme+hdQrFwLNF9AqGfUVo+ a6z/4kRXFsqSJjSA7VJKJE5sjEz63swyeV+wlXzNxwNFAnAsmikEvkygS3fYHbU3vf2D CNy1BIG7VBdEg1RHz4+XzAsftqdTwbhYYF8BTy375lSokwRdOKaUSUHuynRuQJgP7X3G LqjCXpzIosay2C148hHMGT89uPFJAPE3jnWfOwr41szKVqQBD/cEO9p7a12+U0jgfW97 8wpSaVebb4N6z+TpcYITOka+V8Ll+Q0gMnOmZ7mBV19DHiGcG469EkQM/GUllD7pcoIX NlKw== 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=OsDRXpJMhCTq56LgWnJxa3LeggO6HBiGAf2+018WjZs=; b=lBffy+3Rk+kRUqRtEAblw8coqomSHHNpy0oqN6X4r11LmQR/tLRtqzUqorFLTeXXzT ig5UKPgW0QjQVHySQx7a3hkuruZtsov4CDH2YJe5laZGwWm3Va0qt8VU/NzLMQgd/+WZ vw47n/gHSK7EJEtCEq1on4H4WD7SP1rzUUP3/GEw+OUXlSCxcL8AY1yz7+daNkDSJc7j ZSPoFQqrm/231+3OE4OvtjMs3tThVVU4qFk+WBBjKWjCtvVN8byQgpWlE4MrkVVAvalf 97ZkLVqTJx1rP63H+6PmeSo1GK4ITKpNqD0i/hpOJiVJDMleMnQDZN98M26wbQxmFhFy wYfA== X-Gm-Message-State: APjAAAUytBZcepmgUHXdlmS9jzQC4ElN5DNBDrbQHudVWWWbmaIFtoxD HlbVwQ5q6AePdzVF1H+4NdcJ8W/a9rc= X-Google-Smtp-Source: APXvYqw97QbxMkdC57EZlyCZMgU0mqLI5GsHKQa3U1E8YbYsY5Z8uaJLoM37Y+JCrvivkToHlwP35Q== X-Received: by 2002:a6b:6a01:: with SMTP id x1mr10130626iog.119.1569423879972; Wed, 25 Sep 2019 08:04:39 -0700 (PDT) Received: from [172.31.99.94] ([96.77.187.155]) by smtp.gmail.com with ESMTPSA id s11sm4172724ioc.79.2019.09.25.08.04.39 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 25 Sep 2019 08:04:39 -0700 (PDT) Content-Type: multipart/alternative; boundary=Apple-Mail-7B384BA2-EA3C-4321-B97C-8FF5EAD2E21D 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: <3dcb8017-9126-26ad-dbc8-99ad31b81d0f@riseup.net> Date: Wed, 25 Sep 2019 09:04:38 -0600 Cc: Adrian Klaver , pgsql-general@postgresql.org Content-Transfer-Encoding: 7bit Message-Id: <258FDE31-77A2-4E3B-8BD8-9AB9199999AA@gmail.com> References: <399629ce-aefe-d0df-7552-99fc6bc3d958@riseup.net> <920a0616-125c-6b6a-fd51-abd250a6ee9c@aklaver.com> <3dcb8017-9126-26ad-dbc8-99ad31b81d0f@riseup.net> To: Krishnakant Mane List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --Apple-Mail-7B384BA2-EA3C-4321-B97C-8FF5EAD2E21D Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: quoted-printable > On Sep 25, 2019, at 8:24 AM, Krishnakant Mane wrote: >=20 >=20 >> On 25/09/19 7:50 PM, Adrian Klaver wrote: >>> 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 automat= ion and inventory management software called GNUKhata =20= >>>=20 >>> Our team is planning to add backup and restore function in the software.= =20 >>>=20 >>> But we don't want to dump the entire database and then restore the same.= =20 >>>=20 >>> What we are trying to do is to copy data specific to an organization.=20= >>>=20 >>> The challenge here is that I might copy all data (account heads, bills, v= ouchers 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 attemp= t to restore.=20 >>>=20 >>> 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 attempt= ing 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 on th= e second machine.=20 >>>=20 >>> I am not expecting the users to empty all data from the destination mach= ine before restoring a backup.=20 >>>=20 >>> The reason is that an auditor may have many client's data and one can't p= redict 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 backup a= nd 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, etc)th= en the only thing I see happening is changing the PK values to an unused val= ue. That could turn into a nightmare though. Not only that you lose the conn= ection to the original data source. If the data can be broken out into separ= ate 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 > Even I am thinnking to do some kind of upsert with this situation. >=20 > And I would have to set the pkey to an unassigned value when there is conf= lict. >=20 > I may also choose to revamp the serial by timestamps but don't know if the= target customers would like it. >=20 > --=20 > Regards,=20 > 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 e= ach restore into its own space would be one tack.=20= --Apple-Mail-7B384BA2-EA3C-4321-B97C-8FF5EAD2E21D Content-Type: text/html; charset=utf-8 Content-Transfer-Encoding: 7bit


On Sep 25, 2019, at 8:24 AM, Krishnakant Mane <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.

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

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

--
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.  --Apple-Mail-7B384BA2-EA3C-4321-B97C-8FF5EAD2E21D--