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 1ncs35-0006dc-7K for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Apr 2022 17:05:27 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1ncs34-0007vb-3b for pgsql-hackers@arkaria.postgresql.org; Fri, 08 Apr 2022 17:05:26 +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 1ncs33-0007vG-Qv for pgsql-hackers@lists.postgresql.org; Fri, 08 Apr 2022 17:05:25 +0000 Received: from mail-pl1-x631.google.com ([2607:f8b0:4864:20::631]) by makus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.92) (envelope-from ) id 1ncs31-0004Ar-Et for pgsql-hackers@postgresql.org; Fri, 08 Apr 2022 17:05:24 +0000 Received: by mail-pl1-x631.google.com with SMTP id d15so8402600pll.10 for ; Fri, 08 Apr 2022 10:05:23 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; h=date:from:to:cc:subject:message-id:references:mime-version :content-disposition:in-reply-to; bh=uErEpCVfUSjavvrw6Wdz3Ul2yN3IqEUfKUfXtfVBG0g=; b=B4uwIS2pJk3DWvcdv+pKAalJhDwsoE2JgtEsukcvnp4ZjenxWtMR6RzrH1iFOpskqr b2a2Cy78AWcxppwIm9s5/pufitvBTxzjKSxlLnWnPTlI64LH6Ln2YCwaTFOmuZEtG8rq EBmXp5vqnjdwIK7NBuHQl34sLMmBZNu+EWEhAxoHEtpr36mQGKkXGwkeN8QFiYlrw27h edO+Rjd1tAdv/J/E4qg28Z0128fmdE75XteLSoOX3vSZT5i8NdJZ159lEJadpm74K3lG EwpTXOXH7fOcct13fcoUP37q20OEmsGovbEChFrZQKgnSBj0BVpXUey4v00gzlgRXp+1 ltRg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:date:from:to:cc:subject:message-id:references :mime-version:content-disposition:in-reply-to; bh=uErEpCVfUSjavvrw6Wdz3Ul2yN3IqEUfKUfXtfVBG0g=; b=aROR4/b7Ei2zW93C0LMZEO6fffZegQqnzewSuNmpAgB0ienUsQUIEZ6n2NCOwND4Rc 2x7IQeNXxdUypKTuRydbPSMp3TJIee1UxeV7PmMitI97tkiajBlYcKq5cuE1NTjLSkRw utIN8QJLamqVgzTRKOmsru7uz5/NMBxwmLtSnaVtSLxecF1AvXP+7Y72Y82AJWxXUMz4 IPfUrYXB1OThK17mNsA52FuBpp3ThTGNZgGdNT2BMPPswFedFvvl+RLzYxDZlU1alXTq +0s2IONl9E09rTO/iBm3pG800ZP3YWaKvjUhAWb5jruqK8xYGG9bZVA+OSvUxpHMysoX YCDA== X-Gm-Message-State: AOAM531pM7C2EZohER8M7SW7OF42Xjongdk8NTWY/dpxSgeOEICp6kF8 MgARFWP9vVt/7hhanv2BMaTrs/fXSsw= X-Google-Smtp-Source: ABdhPJyBuy/sLMUfEKAlSJsXDEJt9oUEEC2mz07PYu24VQmyIi1k7aX6M1nhUqnhByixOLXIvJWlBA== X-Received: by 2002:a17:90b:17cb:b0:1ca:7772:1abe with SMTP id me11-20020a17090b17cb00b001ca77721abemr13436408pjb.162.1649437522239; Fri, 08 Apr 2022 10:05:22 -0700 (PDT) Received: from nathanxps13 ([50.54.155.70]) by smtp.gmail.com with ESMTPSA id e10-20020a6558ca000000b0039cc25511dbsm5693220pgu.0.2022.04.08.10.05.21 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 08 Apr 2022 10:05:21 -0700 (PDT) Date: Fri, 8 Apr 2022 10:05:19 -0700 From: Nathan Bossart To: Robert Haas Cc: "pgsql-hackers@postgresql.org" Subject: Re: avoid multiple hard links to same WAL file after a crash Message-ID: <20220408170519.GA1409463@nathanxps13> References: <20220407182954.GA1231544@nathanxps13> <20220408165312.GB1409034@nathanxps13> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: <20220408165312.GB1409034@nathanxps13> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Apr 08, 2022 at 09:53:12AM -0700, Nathan Bossart wrote: > On Fri, Apr 08, 2022 at 10:38:03AM -0400, Robert Haas wrote: >> I'd actually be in favor of nuking durable_rename_excl() from orbit >> and putting the file-exists tests in the callers. Otherwise, someone >> might assume that it actually has the semantics that its name >> suggests, which could be pretty disastrous. If we don't want to do >> that, then I'd changing to do the stat-then-durable-rename thing >> internally, so we don't leave hard links lying around in *any* code >> path. Perhaps that's the right answer for the back-branches in any >> case, since there could be third-party code calling this function. > > I think there might be another problem. The man page for rename() seems to > indicate that overwriting an existing file also introduces a window where > the old and new path are hard links to the same file. This isn't a problem > for the WAL files because we should never be overwriting an existing one, > but I wonder if it's a problem for other code paths. My guess is that many > code paths that overwrite an existing file are first writing changes to a > temporary file before atomically replacing the original. Those paths are > likely okay, too, as you can usually just discard any existing temporary > files. Ha, so there are only a few callers of durable_rename_excl() in the PostgreSQL tree. One is basic_archive.c, which is already doing a stat() check. IIRC I only used durable_rename_excl() here to handle the case where multiple servers are writing archives to the same location. If that happened, the archiver process would begin failing. If a crash left two hard links to the same file around, we will silently succeed the next time around thanks to the compare_files() check. Besides the WAL installation code, the only other callers are in timeline.c, and both note that the use of durable_rename_excl() is for "paranoidly trying to avoid overwriting an existing file (there shouldn't be one)." So AFAICT basic_archive.c is the only caller with a strong reason for using durable_rename_excl(), and even that might not be worth keeping it around. -- Nathan Bossart Amazon Web Services: https://aws.amazon.com