Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tb1pS-007MI9-3S for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Jan 2025 18:21:22 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.94.2) (envelope-from ) id 1tb1pR-001pqg-6Y for pgsql-hackers@arkaria.postgresql.org; Thu, 23 Jan 2025 18:21:21 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1tb1pQ-001pqY-TD for pgsql-hackers@lists.postgresql.org; Thu, 23 Jan 2025 18:21:20 +0000 Received: from mail-il1-x12b.google.com ([2607:f8b0:4864:20::12b]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tb1pO-0017zU-0w for pgsql-hackers@lists.postgresql.org; Thu, 23 Jan 2025 18:21:20 +0000 Received: by mail-il1-x12b.google.com with SMTP id e9e14a558f8ab-3cf82bd380bso10123965ab.0 for ; Thu, 23 Jan 2025 10:21:18 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737656476; x=1738261276; darn=lists.postgresql.org; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=LXU1zeglFCHhrKM6xqTJOjJ5B7gteyjN9AZtOVxKCww=; b=gwoQu42iCvsIUIiIhT2HsZ1dUWada+YYqd8HeytdeAprT6/M5gYsmcjTYt/e6WbGLl ioMSYy3/GXKqfsmWxeRj8yfXRKczxi8PS6Rk/GqMBUaMua2pljGy8Z/lHe4MPQ4x/vXL xd1SMJKwyAjb6YeNImAVr/sDRsLfMVO70kg22gQhm0hDzdfHKrhnnEdyuHDUit1ZH6SI WXsjqKKDrdT1wKoSzQ4AqriBp3C5ZFipu2EXgQbrNRWCO9iSHdSjja7sWfgMdLcEULJe zKvFt6+JgrWZb2dgTsbCqpmP4C+rszP3Bz7h8C3riqp3d71BOGiGOXGzLHr6sc1HKJgb 2Bdg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737656476; x=1738261276; h=in-reply-to:content-disposition:mime-version:references:message-id :subject:cc:to:from:date:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=LXU1zeglFCHhrKM6xqTJOjJ5B7gteyjN9AZtOVxKCww=; b=dckwdceoG1DMu6R3/9vO9vcpfKGSiymKMgLocRRYq9xNG0TFHXMZvJ9kSklSkO6yQ0 /j20G6n9I4bZV7IOg+GjMo76LHzu4IdaZcVB1Hso3MUC6dpLPLw7pC/Y7mARIR1P0mGY TQoEQ5fZAIVzE+MKOhFllUXW4WHqXYsDwrxFOkI6tVgCimzEr7OEYtLsYCkV1vEiAAyu kHao1PP6HBIfvenuaEdQqNXsHn9fNXPVdHtHUy+9j7t07O7c4fzrhH4k0eKS64EGvDP7 KGDW+cz9CQNuoECUwNfctdXtHbzK/Vn6paRZdfW4u6vyWKFDiHLLusPpNnGr1jIj2lco P/4Q== X-Forwarded-Encrypted: i=1; AJvYcCVBX9/NMF/xWzLCihaLzhZy0p7xmZO2cE0Pxz8bqE6tdzX5RFZZQs13m0Q328ElyA2YQjhZswI8CoxZpJnU@lists.postgresql.org X-Gm-Message-State: AOJu0Yycu/zPYEXiRNX7hn1R9KQhY5X6ovKSv/w4UgtBAycJqg/ievoH L3P1ICFqCFGry+5p+kSWvWyjyudcxcFa9wJGxMfiM2I0QCGroT/G X-Gm-Gg: ASbGncv+TYQnfQkAm3nNKY+mN3MtV+/ycBc5Owm0kl3aYqEVxgdt8NcneFwCTDZ9i2S 9B+UlV6Y4c9dX45Xnwr/DxHzvjjiLQ067efT5gSpvKcstSYaermZv2vQ4/8nXW1Xo58EgVTPQJ1 QhoAiY66P/f6be7hnFF+NIf/UJFhHo1EPPB/IeIkOVq7w8bD58WM3D9jXGQIkx+0wQLqjpzeGUJ VjnNgAxcfanmiO8MSA9F24H/w13LCWCp4BoCPfTwqsMdpgHpFMJzp/duXOo1vH4nrRH/A9p06gj MUbEHJCh61+1NGCWEFqwFrC6oJy/b/ByP38ZVXWjYqrCtyH10RQjSiccf/glsSMAEQ0w/oA= X-Google-Smtp-Source: AGHT+IH3QwSRXbaPdPDvsZ4u8gKeOmiUn/SU3bLMcpztKGqhkyK61Ij38Jbb16xPZeJ9cqKaDst/cw== X-Received: by 2002:a05:6e02:349f:b0:3cf:bb3e:884c with SMTP id e9e14a558f8ab-3cfbb3e8a9fmr44974465ab.16.1737656476341; Thu, 23 Jan 2025 10:21:16 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id e9e14a558f8ab-3cfc73090c2sm1032975ab.0.2025.01.23.10.21.14 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 23 Jan 2025 10:21:15 -0800 (PST) Date: Thu, 23 Jan 2025 12:21:12 -0600 From: Nathan Bossart To: Andres Freund Cc: Andy Fan , Justin Pryzby , Maxim Orlov , Pavel Borisov , "Bossart, Nathan" , Maxim Orlov , pgsql-hackers@lists.postgresql.org Subject: Re: Pre-allocating WAL files Message-ID: References: <20220317231212.GB1415284@nathanxps13> <20220408203003.GA1630183@nathanxps13> <87sepc4zcg.fsf@163.com> <87o6zz4pld.fsf@163.com> <745zvagaf6tfn2zbvtmqvxsr6kmybjbzcw6rsm55jnql4233je@tudeipoewruo> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Wed, Jan 22, 2025 at 01:00:08PM -0500, Andres Freund wrote: > On 2025-01-22 11:43:20 -0600, Nathan Bossart wrote: >> What is the purpose of syncing the file before the rename? > > It's from the general durable_rename() code. The reason it's there that it's > required for "atomically replace a file" use case. Imagine the following: > > create_and_fill("somefile.tmp"); > rename("somefile.tmp", "somefile"); > fsync("somefile.tmp"); > fsync("."); > > If you crash (OS/HW level) in the wrong moment (between rename() taking effect > in-memory and the fsyncs), you might end up with "somefile" pointing to the > *new* file, because the rename took affect, but the new file's content not > having reached disk yet. I.e. "somefile" will be empty. Whether that's > possible depends on filesystem semantics (e.g. on ext4 it's possible with > data=writeback, I think it's always possible on xfs). > > In contrast to that, if you fsync("somefile.tmp") before the rename, a crash > between rename() and the later fsyncs will have "somefile" either pointing to > the *old and valid contents* or the *new and valid contents*, without a chance > for an empty file. Got it, thanks for explaining. If the contents are sync'd before the rename(), do we still need to fsync() it again afterwards, too? I'd expect that to ordinarily not have much to do, but perhaps I'm forgetting about some metadata that isn't covered by the fsync() on the directory. > However, for the case of WAL recycling, we shouldn't need fsync() before the > rename, because we ought to already have done so when creating > (c.f. XLogFileInitInternal() or when recycling it last time. Makes sense. > I suspect the theoretically superfluous fsync() won't have a meaningful > performance impact most of the time though, because > > a) There shouldn't be any dirty data for the file, obviously we need to have > flushed the WAL past the recycled segment > > b) Except for the first to-be-recycled segment, we just fsynced after the last > rename, so there won't be any filesystem journal data that needs to be > flushed > > I'm not entirely sure about a) though - depending on mount options it's > possible that the fsync() will flush file modification times when using > wal_sync_method=fdatasync. But even if that's possibly reachable, I doubt > it'll be common, due to a checkpoint having to complete between the WAL flush > and recycling. Could be worth experimenting with. Yeah, I'm not too worried about the performance impact of some superfluous fsync() calls, either, but I wasn't sure I properly understood the fsync() pattern in durable_rename() (and figured it'd be nice to get it documented in the archives). -- nathan