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 1iDC6o-0001Co-Al for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 18:33:50 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1iDC6m-0000Bn-US for pgsql-general@arkaria.postgresql.org; Wed, 25 Sep 2019 18:33:48 +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 1iDC6m-0000Bf-Hi for pgsql-general@lists.postgresql.org; Wed, 25 Sep 2019 18:33:48 +0000 Received: from out2-smtp.messagingengine.com ([66.111.4.26]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1iDC6j-0004XQ-Iq for pgsql-general@postgresql.org; Wed, 25 Sep 2019 18:33:46 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id E6AB021ACF; Wed, 25 Sep 2019 14:33:44 -0400 (EDT) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Wed, 25 Sep 2019 14:33:44 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:cc:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=i Nt+JqV0LiwQdOzcUrb3F2F/eDyHDBQSJ/JFG4dyOeI=; b=SFXXaJag5J0nRJ0Bi 7soOXZmyv/n/lrVijqAs8OXn2swGDsxPHLrpHj7eoLqHjbRu/GmsM9UdnTALqIle Lrc9xzRYhqGcKsQwZMJEPOu9Uav2cPyVyz1W5bhRyHHZyNivw2y5AQwyTdLecv65 SUftAyVSR37ilMRiUWh6eRX2/qEF0o4XziLm2zVRJMded5hKfIheP2cvLhs1s8mY 7aGOYTtCf8LhfU50KcS09sVolaaA0t/kZy1DMIrE9N6ReD47Xr4Ywe7UbQ7hYitL O7+YGGT6zHV2FpKJxhhwovfCHfPi56APkhJPmCWF3ZFbQXR+ULhqYygbMEUlmCfC y+ePQ== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc: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=fm3; bh=iNt+JqV0LiwQdOzcUrb3F2F/eDyHDBQSJ/JFG4dyO eI=; b=SCbjE9dzyxe2REHqDtaV7PeTPZoUs8jQOc9HofVkCTKl/ERFgmGH8Tusu EvfMWLNvhAEF/NhkblAc9fk6owPy9vMDXguhYivgTb4N1J6qsxayNzvooimB0xEx dKNz3uPFXtv1P5Ex79sVsZJUaUpHTNgjEan/PjUUV8kwrdlFFqDuA0DYGBSHUuL3 C46LR2/q3lB+tOiot6RC3eMC14+DiMzlVQdsxk+KcBDLTiE9EoxyywWffCMk2Sdo uTwXtiupLHWnakIl+agDtM/h7KKx559mWvCf2ojOcf2quzEHi4P9zncbcbdXtvkg cRyP7zVECOsbSRwH7QzbJS0WygI9A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedufedrfedvgdduvdejucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfqfgfvpdfurfetoffkrfgpnffqhgen uceurghilhhouhhtmecufedttdenucesvcftvggtihhpihgvnhhtshculddquddttddmne cujfgurhepuffvfhfhkffffgggjggtgfesthejredttdefjeenucfhrhhomheptegurhhi rghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtoh hmqeenucffohhmrghinhepphhoshhtghhrvghsqhhlrdhorhhgpdhgnhhukhhhrghtrgdr ihhnnecukfhppeehtddrudeljedrkedtrddvnecurfgrrhgrmhepmhgrihhlfhhrohhmpe grughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmnecuvehluhhsthgvrhfu ihiivgeptd X-ME-Proxy: Received: from ranger.suse (50-197-80-2-static.hfc.comcastbusiness.net [50.197.80.2]) by mail.messagingengine.com (Postfix) with ESMTPA id 13AA580065; Wed, 25 Sep 2019 14:33:43 -0400 (EDT) Subject: Re: managing primary key conflicts while restoring data to table with existing data To: Rob Sargent , Krishnakant Mane Cc: 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> From: Adrian Klaver Message-ID: Date: Wed, 25 Sep 2019 11:33:43 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 MIME-Version: 1.0 In-Reply-To: <258FDE31-77A2-4E3B-8BD8-9AB9199999AA@gmail.com> Content-Type: text/plain; charset=utf-8; format=flowed Content-Language: en-US Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. >> >> -- >> 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. -- Adrian Klaver adrian.klaver@aklaver.com