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 1kmUsg-0005Ib-8X for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Dec 2020 04:45:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1kmUsf-0007PO-6p for pgsql-hackers@arkaria.postgresql.org; Tue, 08 Dec 2020 04:45:41 +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 1kmUse-0007PH-Tr for pgsql-hackers@lists.postgresql.org; Tue, 08 Dec 2020 04:45:41 +0000 Received: from mail-pg1-x535.google.com ([2607:f8b0:4864:20::535]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1kmUsc-0002qV-00 for pgsql-hackers@postgresql.org; Tue, 08 Dec 2020 04:45:39 +0000 Received: by mail-pg1-x535.google.com with SMTP id n7so11087392pgg.2 for ; Mon, 07 Dec 2020 20:45:37 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=date:message-id:to:cc:subject:from:in-reply-to:references :user-agent:mime-version:content-transfer-encoding; bh=8q/SjlnVZ+O83FS61R7Xed38PaRBQLDB62Tg1/WZIfs=; b=gOTR0XkoqqFbDwmogxyt8D7wFARe95A3xovpvnpfvcSPNZNP3XEMb5uQccasUEIIvv msNRjNzfjX1axyhAkRTs66ix/EFqb93FolIA7BBM4f+KzYo38Q4Rbv6yvoyZCUXjXcdB MY07O/oXhv/deLXWAVTpMsFXeTRCMjF7VQLqnSgwnCc/XAm8P187DUqipgyAnyNwmFcT 10kxj5nyEB3G3yw/pg2Ncbp6Z+Ynfr+/0X2BMqn+cN1Cbmsuy5pznaO8OY03cxtoK3xd vi4S3TF3fyH/cjrhekd6VwHh342KTCR7LX0Q6PI5fTGreS4N/+W8R0KXD4CerKksCb43 3E2A== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:date:message-id:to:cc:subject:from:in-reply-to :references:user-agent:mime-version:content-transfer-encoding; bh=8q/SjlnVZ+O83FS61R7Xed38PaRBQLDB62Tg1/WZIfs=; b=in/2WCcAp0243lDz8xY8CCekZiuU9KNW3u+8CGBh6ArkaxSa36/blYtRd6XfbjoVB3 XAxpWoYRYz06GT99313Zd4lwxdfhak+8S7HWQ0uhG1qXh+jlEue1SYiCqp8I+5xsrAHt 8mPwcwPQUUlmsWuO350kdh7h6sdvFWcaLCab8N6M+6hA2cE1pLD2Pkok9qosNSCTmKe3 wN/Y1dV2Q7p2OMemM15J5NJZaCdg/JUrZe3Em2KejH6sUZAqyhWRN5J2tCTdG8kSqcS8 zR5GD2t4mgxqo+aakZEXQYvEAsfyNQjulpSsthnwHlwCRkIXDd9gMI6mShAHaBuRCVOJ ztvQ== X-Gm-Message-State: AOAM531geqht2LTZ0moQvP+o3Mw6SlvQhNa+xODHq+4+6hPiYAuKsbeI 4htvFfT9QxRNP0b2vfF45NE= X-Google-Smtp-Source: ABdhPJy8CghEsAKec7ARoXxpak1WKwDydz2MgZgUrbRrlKnFOqMd4jw1mhBCXIjqmrzr5d3pxlYGBQ== X-Received: by 2002:a62:c505:0:b029:19d:c3fe:6d92 with SMTP id j5-20020a62c5050000b029019dc3fe6d92mr16737458pfg.47.1607402736887; Mon, 07 Dec 2020 20:45:36 -0800 (PST) Received: from localhost (KD036014041111.ppp-bb.dion.ne.jp. [36.14.41.111]) by smtp.gmail.com with ESMTPSA id c14sm7963733pfp.167.2020.12.07.20.45.34 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 07 Dec 2020 20:45:36 -0800 (PST) Date: Tue, 08 Dec 2020 13:45:33 +0900 (JST) Message-Id: <20201208.134533.202575181178574697.horikyota.ntt@gmail.com> To: hlinnaka@iki.fi Cc: pgsql-hackers@postgresql.org, sfrost@snowman.net, alexk@hintbits.com, michael.paquier@gmail.com Subject: Re: pg_rewind race condition just after promotion From: Kyotaro Horiguchi In-Reply-To: <9f568c97-87fe-a716-bd39-65299b8a60f4@iki.fi> References: <9f568c97-87fe-a716-bd39-65299b8a60f4@iki.fi> User-Agent: Mew version 6.8 on Emacs 26.1 Mime-Version: 1.0 Content-Type: Text/Plain; charset=us-ascii Content-Transfer-Encoding: 7bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk At Mon, 7 Dec 2020 20:13:25 +0200, Heikki Linnakangas wrote in > There's a race condition between the checkpoint at promotion and > pg_rewind. When a server is promoted, the startup process writes an > end-of-recovery checkpoint that includes the new TLI, and the server > is immediate opened for business. The startup process requests the > checkpointer process to perform a checkpoint, but it can take a few > seconds or more to complete. If you run pg_rewind, using the just > promoted server as the source, pg_rewind will think that the server is > still on the old timeline, because it only looks at TLI in the control > file's copy of the checkpoint record. That's not updated until the > checkpoint is finished. > > This isn't a new issue. Stephen Frost first reported it back 2015 > [1]. Back then, it was deemed just a small annoyance, and we just > worked around it in the tests by issuing a checkpoint command after > promotion, to wait for the checkpoint to finish. I just ran into it > again today, with the new pg_rewind test, and silenced it in the > similar way. I (or we) faced that and avoided that by checking for history file on the primary. > I think we should fix this properly. I'm not sure if it can lead to a > broken cluster, but at least it can cause pg_rewind to fail > unnecessarily and in a user-unfriendly way. But this is actually > pretty simple to fix. pg_rewind looks at the control file to find out > the timeline the server is on. When promotion happens, the startup > process updates minRecoveryPoint and minRecoveryPointTLI fields in the > control file. We just need to read it from there. Patch attached. Looks fine to me. A bit concerned about making sourceHistory needlessly file-local but on the other hand unifying sourceHistory and targetHistory looks better. For the test part, that change doesn't necessariry catch the failure of the current version, but I *believe* the prevous code is the result of an actual failure in the past so the test probablistically (or dependently on platforms?) hits the failure if it happned. > I think we should also backpatch this. Back in 2015, we decided that > we can live with this, but it's always been a bit bogus, and seems > simple enough to fix. I don't think this changes any successful behavior and it just saves the failure case so +1 for back-patching. > Thoughts? > > [1] > https://www.postgresql.org/message-id/20150428180253.GU30322%40tamriel.snowman.net regards. -- Kyotaro Horiguchi NTT Open Source Software Center