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 1oJUI9-0006rd-Kh for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Aug 2022 06:25:09 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1oJUI8-0004KX-3n for pgsql-hackers@arkaria.postgresql.org; Thu, 04 Aug 2022 06:25:08 +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 1oJUI7-0004KC-FQ for pgsql-hackers@lists.postgresql.org; Thu, 04 Aug 2022 06:25:07 +0000 Received: from mail-qv1-xf33.google.com ([2607:f8b0:4864:20::f33]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1oJUI3-0000jf-Lm for pgsql-hackers@postgresql.org; Thu, 04 Aug 2022 06:25:06 +0000 Received: by mail-qv1-xf33.google.com with SMTP id y11so14522310qvn.3 for ; Wed, 03 Aug 2022 23:25:03 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:from:to:cc; bh=eSE64poxUOkpNxtudkYVWNcsoFYPuKOveEuYuML3CVU=; b=Vj93ZkF8Z+eke5k8J6S55aE1o1HUoQaRO/etBiXTFuPmQhtYi8aquZU4/DaGiER/Ss 2Vlqiur7KWNQF2X/YlO0tofgtKAozR0Vd0JLPaMv7GNeNn4zFeaxtFO1KR6smRts2ALs w7i4DTceNEuGuvyZ7e8lsvECRHV3J6vCnYxDA= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=user-agent:in-reply-to:content-disposition:mime-version:references :message-id:subject:cc:to:from:date:x-gm-message-state:from:to:cc; bh=eSE64poxUOkpNxtudkYVWNcsoFYPuKOveEuYuML3CVU=; b=LYQPNcgA62ejo3vknRqwQOyUbDyltXg0k2GuuwYXP8/+htMaR1r/UoENu2hPlqrOvF dw7+6DUv/2vWJ5IhXe6hMlLdosXUeV0UcKcO8kfAMkURlIn0aj8al2mBHwohGCW01oA5 Je2YXKHyjJxhavtAUL33bnZiLqu9T7ZUnrHKPQ5y8prAgdFY1qDZ+rDvHjqrTH4J5JdP du/svGiBhtao8HNq3Ha3YHQlqIcdWUxDrZ8n6nSdBs68i9PQ/Zx2LX8fTfeX0VhIWtDC mB+GDRyOfvI7SsYN1KunANmg53BDDgnCofUZjETF3p/F4lY5LN9vAS+XXwssAcwgIMgB fZIg== X-Gm-Message-State: ACgBeo0T4d1Mvam0XuBMgN76uttarT5ziSjA/V1gzZke2+HoEWC6JWqx MaEACbc+pfYeBeOTuWRE5Vb9Ag== X-Google-Smtp-Source: AA6agR6Niyk9hpjjhv450aavk35/+3Wee1T1z9yU8RSxSd2IwL2H7PKcrzElWX7byJCXiTDFxNYuLA== X-Received: by 2002:ad4:5768:0:b0:476:5e06:7514 with SMTP id r8-20020ad45768000000b004765e067514mr223453qvx.3.1659594302490; Wed, 03 Aug 2022 23:25:02 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id cm19-20020a05622a251300b0031ee918e9f9sm128548qtb.39.2022.08.03.23.24.58 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Wed, 03 Aug 2022 23:25:00 -0700 (PDT) Date: Wed, 3 Aug 2022 23:24:56 -0700 From: Noah Misch To: David Steele Cc: pgsql-hackers@postgresql.org, Kyotaro Horiguchi , Don Seiler Subject: Re: Race between KeepFileRestoredFromArchive() and restartpoint Message-ID: <20220804062456.GA3844351@rfd.leadboat.com> References: <20210202151416.GB3304930@rfd.leadboat.com> <20210619203918.GA1189385@rfd.leadboat.com> <20220731061747.GA3692882@rfd.leadboat.com> <20220802143727.GA3796043@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="T4sUOijqQbZv57TR" Content-Disposition: inline In-Reply-To: <20220802143727.GA3796043@rfd.leadboat.com> User-Agent: Mutt/1.5.24 (2015-08-30) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk --T4sUOijqQbZv57TR Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Aug 02, 2022 at 07:37:27AM -0700, Noah Misch wrote: > On Tue, Aug 02, 2022 at 10:14:22AM -0400, David Steele wrote: > > On 7/31/22 02:17, Noah Misch wrote: > > >On Tue, Jul 26, 2022 at 07:21:29AM -0400, David Steele wrote: > > >>On 6/19/21 16:39, Noah Misch wrote: > > >>>On Tue, Feb 02, 2021 at 07:14:16AM -0800, Noah Misch wrote: > > >>>>Recycling and preallocation are wasteful during archive recovery, because > > >>>>KeepFileRestoredFromArchive() unlinks every entry in its path. I propose to > > >>>>fix the race by adding an XLogCtl flag indicating which regime currently owns > > >>>>the right to add long-term pg_wal directory entries. In the archive recovery > > >>>>regime, the checkpointer will not preallocate and will unlink old segments > > >>>>instead of recycling them (like wal_recycle=off). XLogFileInit() will fail. > > >>> > > >>>Here's the implementation. Patches 1-4 suffice to stop the user-visible > > >>>ERROR. Patch 5 avoids a spurious LOG-level message and wasted filesystem > > >>>writes, and it provides some future-proofing. > > >>> > > >>>I was tempted to (but did not) just remove preallocation. Creating one file > > >>>per checkpoint seems tiny relative to the max_wal_size=1GB default, so I > > >>>expect it's hard to isolate any benefit. Under the old checkpoint_segments=3 > > >>>default, a preallocated segment covered a respectable third of the next > > >>>checkpoint. Before commit 63653f7 (2002), preallocation created more files. > > >> > > >>This also seems like it would fix the link issues we are seeing in [1]. > > >> > > >>I wonder if that would make it worth a back patch? > > > > > >Perhaps. It's sad to have multiple people deep-diving into something fixed on > > >HEAD. On the other hand, I'm not eager to spend risk-of-backpatch points on > > >this. One alternative would be adding an errhint like "This is known to > > >happen occasionally during archive recovery, where it is harmless." That has > > >an unpolished look, but it's low-risk and may avoid deep-dive efforts. > > > > I think in this case a HINT might be sufficient to at least keep people from > > wasting time tracking down a problem that has already been fixed. Here's a patch to add that HINT. I figure it's better to do this before next week's minor releases. In the absence of objections, I'll push this around 2022-08-05 14:00 UTC. > > However, there is another issue [1] that might argue for a back patch if > > this patch (as I believe) would fix the issue. > > > [1] https://www.postgresql.org/message-id/CAHJZqBDxWfcd53jm0bFttuqpK3jV2YKWx%3D4W7KxNB4zzt%2B%2BqFg%40mail.gmail.com > > That makes sense. Each iteration of the restartpoint recycle loop has a 1/N > chance of failing. Recovery adds >N files between restartpoints. Hence, the > WAL directory grows without bound. Is that roughly the theory in mind? On further reflection, I don't expect it to happen that way. Each failure message is LOG-level, so the remaining recycles still happen. (The race condition can yield an ERROR under PreallocXlogFiles(), but recycling is already done at that point.) --T4sUOijqQbZv57TR Content-Type: text/plain; charset=us-ascii Content-Disposition: attachment; filename="XLogFileInit6-hint-v1.patch" Author: Noah Misch Commit: Noah Misch Add HINT for restartpoint race with KeepFileRestoredFromArchive(). The five commits ending at cc2c7d65fc27e877c9f407587b0b92d46cd6dd16 closed this race condition for v15+. For v14 through v10, add a HINT to discourage studying the cosmetic problem. Reviewed by FIXME. Discussion: https://postgr.es/m/20220731061747.GA3692882@rfd.leadboat.com diff --git a/src/backend/access/transam/xlog.c b/src/backend/access/transam/xlog.c index 5cdd01f..9c845b7 100644 --- a/src/backend/access/transam/xlog.c +++ b/src/backend/access/transam/xlog.c @@ -3480,7 +3480,10 @@ XLogFileInit(XLogSegNo logsegno, bool *use_existent, bool use_lock) if (fd < 0) ereport(ERROR, (errcode_for_file_access(), - errmsg("could not open file \"%s\": %m", path))); + errmsg("could not open file \"%s\": %m", path), + (AmCheckpointerProcess() ? + errhint("This is known to fail occasionally during archive recovery, where it is harmless.") : + 0))); elog(DEBUG2, "done creating and filling new WAL file"); diff --git a/src/backend/storage/file/fd.c b/src/backend/storage/file/fd.c index 6688dee..e76daff 100644 --- a/src/backend/storage/file/fd.c +++ b/src/backend/storage/file/fd.c @@ -834,7 +834,10 @@ durable_rename_excl(const char *oldfile, const char *newfile, int elevel) ereport(elevel, (errcode_for_file_access(), errmsg("could not link file \"%s\" to \"%s\": %m", - oldfile, newfile))); + oldfile, newfile), + (AmCheckpointerProcess() ? + errhint("This is known to fail occasionally during archive recovery, where it is harmless.") : + 0))); return -1; } unlink(oldfile); @@ -844,7 +847,10 @@ durable_rename_excl(const char *oldfile, const char *newfile, int elevel) ereport(elevel, (errcode_for_file_access(), errmsg("could not rename file \"%s\" to \"%s\": %m", - oldfile, newfile))); + oldfile, newfile), + (AmCheckpointerProcess() ? + errhint("This is known to fail occasionally during archive recovery, where it is harmless.") : + 0))); return -1; } #endif --T4sUOijqQbZv57TR--