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 1gW0Lw-0002US-Jm for pgsql-general@arkaria.postgresql.org; Sun, 09 Dec 2018 14:46:41 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gW0Kv-0007Nl-9a for pgsql-general@arkaria.postgresql.org; Sun, 09 Dec 2018 14:45:37 +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 1gW0Ks-0007N6-O9 for pgsql-general@lists.postgresql.org; Sun, 09 Dec 2018 14:45:36 +0000 Received: from out1-smtp.messagingengine.com ([66.111.4.25]) by makus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gW0Kp-0005jD-0A for pgsql-general@lists.postgresql.org; Sun, 09 Dec 2018 14:45:32 +0000 Received: from compute6.internal (compute6.nyi.internal [10.202.2.46]) by mailout.nyi.internal (Postfix) with ESMTP id CDE44216A6; Sun, 9 Dec 2018 09:45:29 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute6.internal (MEProxy); Sun, 09 Dec 2018 09:45:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aklaver.com; h= subject:to:references:from:message-id:date:mime-version :in-reply-to:content-type:content-transfer-encoding; s=fm1; bh=/ rTxpjQqlg+vRcr/30qT8dGOuQsTzJwuZoK90j0+C0U=; b=STvw4FC8XuzmPTvfK Yi0b4HLl71zRK/os/+9aA7bafYj2o8OArPwXWYNkOCXecjKiyiONCargCLWhIguu cLqG2/H+xlbutiRphWUvyIr3vx2XwMJkIZi6vDWo/7bbaGVcxzTWd4Mw92sBuQ6q xErqFvDdE2arPSANI1v1vtEL7XTzJoFa8zyGLJJoZMUdvASIOiK1oM80ay4CKgnz 98zMBT4fobRk4rKznWcoU9ljaaJteSbWIZYMi0hquXkfG9uCt1mRaqQNZeKExUkv pjpEM5wFNUmCGxoFEgxz+JAr/kMg26jMSP24mtZdimJ0/6NBPl9YtNxp8Ki8pKqN 45UBA== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=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=fm1; bh=/rTxpjQqlg+vRcr/30qT8dGOuQsTzJwuZoK90j0+C 0U=; b=vNRobqlCMuzhV2yHKWiFeLQ/ggb6LBe6b9JwDqJGgdwXYIricVfwSd7Ft KJX8tgNRejckyYXOuzXyoSkK3r2NhX0JIRDp8z+9/YVNoAFbgzlOYoKaEn2MNdPR bI+XyzmVjdAmM2Se2npDWo/noBulHN5tLHnfLYVWyS5Ds2Q1TNDOfBg7svTmO0G8 1B6acAnDgApevE+tG9Ss9zEnWA1mO0hw9hSMRPfvxpSVMkO6SMQyEUj/s3op8mVz vBXg1/ZHTAHXOPk1LDrw0dqXovRjb5lk/hXs6EM7LPFci9FXLqtWgiQ7sHxCNytx z3sWY/5q7NC47A71W5cD8FyNaVV8A== X-ME-Sender: X-ME-Proxy-Cause: gggruggvucftvghtrhhoucdtuddrgedtkedrudegfedgieelucetufdoteggodetrfdotf fvucfrrhhofhhilhgvmecuhfgrshhtofgrihhlpdfquhhtnecuuegrihhlohhuthemucef tddtnecusecvtfgvtghiphhivghnthhsucdlqddutddtmdenucfjughrpefuvfhfhffkff gfgggjtgfgsehtjeertddtfeejnecuhfhrohhmpeetughrihgrnhcumfhlrghvvghruceo rggurhhirghnrdhklhgrvhgvrhesrghklhgrvhgvrhdrtghomheqnecuffhomhgrihhnpe hgihhthhhusgdrtghomhenucfkphepjedurddvuddvrddufeegrdefnecurfgrrhgrmhep mhgrihhlfhhrohhmpegrughrihgrnhdrkhhlrghvvghrsegrkhhlrghvvghrrdgtohhmne cuvehluhhsthgvrhfuihiivgeptd X-ME-Proxy: Received: from [192.168.1.3] (unknown [71.212.134.3]) by mail.messagingengine.com (Postfix) with ESMTPA id 77B67E4535; Sun, 9 Dec 2018 09:45:28 -0500 (EST) Subject: =?UTF-8?Q?Re=3a_Errors_with_schema_migration_and_logical_replicatio?= =?UTF-8?B?biDigJQgZXhwZWN0ZWQ/?= To: Mike Lissner , pgsql-general@lists.postgresql.org References: From: Adrian Klaver Message-ID: <42bcb35c-9173-bb9e-bda6-ea5c5950119b@aklaver.com> Date: Sun, 9 Dec 2018 06:45:27 -0800 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.3.0 MIME-Version: 1.0 In-Reply-To: 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 12/8/18 11:26 PM, Mike Lissner wrote: > Hi, first time poster. > > I just ran into a rather messy problem when doing a schema migration > with logical replication. I'm not entirely sure what went wrong, why, or > how to prevent it in the future. The migration I ran was pretty simple > (though auto-generated by Django): > > And after running this migration, I started getting this error on the > subscriber: > > |2018-12-09 05:59:45 UTC::@:[13373]:LOG: logical replication apply > worker for subscription "replicasubscription" has started 2018-12-09 > 05:59:45 UTC::@:[13373]:ERROR: null value in column > "recap_sequence_number" violates not-null constraint 2018-12-09 05:59:45 > UTC::@:[13373]:DETAIL: Failing row contains (48064261, 2018-12-07 > 04:48:40.388377+00, 2018-12-07 04:48:40.388402+00, null, 576, , 4571214, > null, null). 2018-12-09 05:59:45 UTC::@:[6342]:LOG: worker process: > logical replication worker for subscription 18390 (PID 13373) exited > with exit code 1| > > > So, my migration created a new column with a null constraint and somehow > the subscriber got data that violated that. I don't know how that's > possible since this was a new column and it was never nullable. The above seems to be the crux of the problem, how did NULL get into the column data? The DDL migration did what it was supposed to do. > > I applied the above migration simultaneously on my publisher and > subscriber thinking that postgresql was smart enough to do the right > thing. I think the subscriber finished first (it has less traffic). And it did as far as the schema changes where concerned. > > The docs hint that postgresql might be smart enough to not worry about > the order you do migrations: > > > /Logical replication is robust when schema definitions change in a > live database:/ When the schema is changed on the publisher and > replicated data starts arriving at the subscriber but does not fit into > the table schema, replication will error until the schema is updated. > > And it even hints that doing a migration on the subscriber first is a > good thing in some cases: > > > In many cases, intermittent errors can be avoided by applying > additive schema changes to the subscriber first. > > But I'm now supremely skeptical that doing anything at the subscriber > first is a good idea. Are the docs wrong? Does the above error make > sense? Is the process for schema migrations documented somewhere beyond > the above? > > I have lots of questions because I thought this would have gone smoother > than it did. > > As for the fix: I made the column nullable on the subscriber and I'm > waiting for it to catch up. Once it does I'll re-sync its schema with > the publisher. Anybody interested in following along with all this (or > finding this later and having questions) can follow the issue here: > > https://github.com/freelawproject/courtlistener/issues/919 > > Thank you for the lovely database! I hope this is helpful. > > Mike -- Adrian Klaver adrian.klaver@aklaver.com