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 1jxZt3-0002go-MA for pgsql-general@arkaria.postgresql.org; Mon, 20 Jul 2020 17:47:37 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1jxZt2-0000Cz-4r for pgsql-general@arkaria.postgresql.org; Mon, 20 Jul 2020 17:47:36 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jxZt1-0000Ca-TM for pgsql-general@lists.postgresql.org; Mon, 20 Jul 2020 17:47:35 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jxZsz-0002if-U4 for pgsql-general@lists.postgresql.org; Mon, 20 Jul 2020 17:47:34 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 06KHlSvv578231; Mon, 20 Jul 2020 13:47:29 -0400 From: Tom Lane To: Thomas Kellerer cc: pgsql-general@lists.postgresql.org Subject: Re: Logical replication from 11.x to 12.x and "unique key violations" In-reply-to: <76af9b83-74cc-88b5-23a7-0462774f472d@gmx.net> References: <76af9b83-74cc-88b5-23a7-0462774f472d@gmx.net> Comments: In-reply-to Thomas Kellerer message dated "Mon, 20 Jul 2020 16:22:00 +0200" MIME-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-ID: <578229.1595267248.1@sss.pgh.pa.us> Date: Mon, 20 Jul 2020 13:47:28 -0400 Message-ID: <578230.1595267248@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk 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. regards, tom lane