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 1jxa2Q-0002yA-FK for pgsql-general@arkaria.postgresql.org; Mon, 20 Jul 2020 17:57:19 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jxa2P-0005LV-92 for pgsql-general@arkaria.postgresql.org; Mon, 20 Jul 2020 17:57: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 1jxa2N-0005LO-EI for pgsql-general@lists.postgresql.org; Mon, 20 Jul 2020 17:57:17 +0000 Received: from wout3-smtp.messagingengine.com ([64.147.123.19]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jxa2F-0005Vj-CD for pgsql-general@lists.postgresql.org; Mon, 20 Jul 2020 17:57:14 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.west.internal (Postfix) with ESMTP id 1291D10DD; Mon, 20 Jul 2020 13:57:04 -0400 (EDT) Received: from mailfrontend2 ([10.202.2.163]) by compute4.internal (MEProxy); Mon, 20 Jul 2020 13:57:04 -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=fm3; bh=E HiZ4kSbr5hkN6YQciL1ou8EPi0frs0Z0STAOo7eQ8U=; b=I3f/T3UkfwlnqNiHB 7cEhg9/A9lOqlItn84TrCyKHft8wDbTrEoRbocU2g0UJSvQIPtavDaioXzplI5sD zHJ8cy9QMQPQ/rhzMo3ouzrZMNdV8OYG/N56mFtSjVb4Pt5nBXg/K3P5pDU3Wawk RmuShM4J65+8tx2hKZx3253ueed1FhuH1vy5pggX5MUMG3SDXMsO+A1kGKu0g6Yq Vqnwya/y2EuV6GORZXyOHvWxMiew8W7wvbvZN9+GVxRNI2Lpr1eOh4dgjPzoP8pv pc/hBLAsgx3NgJUjLXEBRvvGsETxNxWRk3O7XlwfyOScGe8rcAU0B0swBV5ckdaT jfumA== 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=EHiZ4kSbr5hkN6YQciL1ou8EPi0frs0Z0STAOo7eQ 8U=; b=H8ukMhP+cMRmUkJ572z0APBtZqLQxk2UbC33sGPhgr5dZStQs6yKzCjDk p/WYhDe0yczZafZ1lQHtthe5ePIedDLjGrC1ouL3Nf1SXJo5Z+cO3K8l9P+M2ge6 vlqmoa3hT3Got4Hd8SdU3qAsbUb0UYhVkAn8S8G+vhKKzQ4tekwFyGmORtfTakxk UtotIwCPqvK29rZkSR7wVdTgPVx4I+MZH0ls2UOOhkGRyFs+JPmL+a217vQwkppW tIO4YmqxRS4zHG/dk3NEttuO0PzLyGvSEVMFi9zX3JDULywtRQba1Oh+hTrFzKlm nfMkwQvyN9vNrWXGQepC5Ss8kGusg== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgeduiedrgeeggdelhecutefuodetggdotefrodftvf curfhrohhfihhlvgemucfhrghsthforghilhdpqfgfvfdpuffrtefokffrpgfnqfghnecu uegrihhlohhuthemuceftddtnecunecujfgurhepuffvfhfhkffffgggjggtgfesthejre dttdefjeenucfhrhhomheptegurhhirghnucfmlhgrvhgvrhcuoegrughrihgrnhdrkhhl rghvvghrsegrkhhlrghvvghrrdgtohhmqeenucggtffrrghtthgvrhhnpeetjedvteffje euvdelieeffeehuedthffgvdfhhffhkedvudevgedvjedufefgveenucfkphepudejvddr heekrdegiedrudelvdenucevlhhushhtvghrufhiiigvpedtnecurfgrrhgrmhepmhgrih hlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhm X-ME-Proxy: Received: from ranger.suse (unknown [172.58.46.192]) by mail.messagingengine.com (Postfix) with ESMTPA id AF5DF3060272; Mon, 20 Jul 2020 13:57:01 -0400 (EDT) Subject: Re: Logical replication from 11.x to 12.x and "unique key violations" To: Tom Lane , Thomas Kellerer Cc: pgsql-general@lists.postgresql.org References: <76af9b83-74cc-88b5-23a7-0462774f472d@gmx.net> <578230.1595267248@sss.pgh.pa.us> From: Adrian Klaver Message-ID: Date: Mon, 20 Jul 2020 10:56:59 -0700 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:68.0) Gecko/20100101 Thunderbird/68.10.0 MIME-Version: 1.0 In-Reply-To: <578230.1595267248@sss.pgh.pa.us> 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 7/20/20 10:47 AM, Tom Lane wrote: > Thomas Kellerer writes: >>> I have a strange error when using logical replication between a 11.2 >>> source database and a 12.3 target. >>> >>> If I create the publication with all needed tables (about 50) at >>> once, I get "duplicate key value violates unique constraint xxx_pkey" >>> errors during the initial replication (when creating the >>> subscription). >>> >>> When create the publication only with a few tables, the initial data >>> sync works without problems. To replicate all tables, I add the >>> tables incrementally to the publication, and refresh the >>> subscription. >>> >>> If I do it like that (step-by-step) everything works fine. Tables >>> that generated the "duplicate key value" error previously will >>> replicate just fine. The tables are quite small, some of them less >>> then 100 rows. > > I have not looked at the code, but it wouldn't surprise me if the initial > replication just copies all the specified tables in some random order. > If there are FK references involved, the replication would have to be > done with referenced tables first, and I bet there's no logic for that. > (Even if there was, it could not cope with circular references or > self-references.) > > Best bet might be to not install the subscriber's foreign key > constraints till after the initial sync is done. I'm probably missing something, but would that not result in a 'key not found' type of error. The OP is seeing "duplicate key value violates unique constraint xxx_pkey". To me that indicates a doubling up of at least some of the data replication. > > regards, tom lane > > -- Adrian Klaver adrian.klaver@aklaver.com