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 1tad0V-003cWz-MV for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Jan 2025 15:51:08 +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 1tad0U-001HQy-Jh for pgsql-hackers@arkaria.postgresql.org; Wed, 22 Jan 2025 15:51:06 +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 1tad0U-001HQq-An for pgsql-hackers@lists.postgresql.org; Wed, 22 Jan 2025 15:51:06 +0000 Received: from mail-il1-x131.google.com ([2607:f8b0:4864:20::131]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tad0R-000vn7-1g for pgsql-hackers@lists.postgresql.org; Wed, 22 Jan 2025 15:51:05 +0000 Received: by mail-il1-x131.google.com with SMTP id e9e14a558f8ab-3a7dd54af4bso21066315ab.2 for ; Wed, 22 Jan 2025 07:51:03 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1737561062; x=1738165862; 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=VRhCw6dd82j3/oN/3y6ZNOQfPLlLkUpznYVMv59AblI=; b=YZNhe68TEKA9t4lSh0Jml9MgbMoqMdaM/eXUBByNvufxucnf/iS5rxX5oBs8ARrWgc Anz+vG/awohjtveWuTe7TpRJ0S52aK/AcKbma9xLiZdEeeE4UiBT5h2bgzgeTv95pWwy AfsmD/qrP8TL/kjTttnOJs6sCeFBCzybyuLMpF0YkMup288ENAbNtYfV/3xLOyen9fls i6EJhYfApzANsqYBuUh0FvsoNScvbLJNB9Ok2hVQtS4yLSYMAeMtbpqEsVKokN5ty7LI POKtxgpB0xC+hTdFPN5278z6yYiY2wkuKaXPCe3eskR5EHb9VFg+36q+xqkBXclLNKNy Mdrw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737561062; x=1738165862; 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=VRhCw6dd82j3/oN/3y6ZNOQfPLlLkUpznYVMv59AblI=; b=dQNK9RHyQ2arCPr+BJqMOA97DlYc2CPvFxAxg6QBAPwOK5MTvRSs8Ws1UGOJ1wM1RO Ijyj3FKeDoRpS0XCX1IY1EOEcIwfTD6PTfY+KMdat/tddIxDlEBN57nbLq5Nq5w797Ll rMHQG1CtRVupYwVmN05am4fJ82O+h88Ilbr2jfPpf39GSGhF/i1ByGttSvDgAA9K4iuD Ez6J28VGql4emsqDN0O/rkGVfzClA++AWigUyU5+1bb9+0fP1785Uq9z7q0M91e5MAWX hH2tQGdDpCpWAb4icufAT12em8xxao1g3jbJuylyNWuHWOpvINd4fA9Tau+MKcEHTy+z Qmzw== X-Forwarded-Encrypted: i=1; AJvYcCUqGBunapUwn1ssJG+ttnkIKWTuqt2ZOWf7CPkzeY8yCvu37i5yDIYMFSUEXhisX0QqodJqSmBRyInL4lN2@lists.postgresql.org X-Gm-Message-State: AOJu0YzUxPca+cAdIcQd0umGVHOo1Sbdhxlt10+z0B1xdSn1L4m0pGSZ mjzilwLafXVCJeKMyMFix3GQfoRwEjjePv04vD5N2y/U0AGBqNPt X-Gm-Gg: ASbGncuQOdo+WV97FMEBKLHY+cVdKjTojne2boExU3CWxF7RtRI3lTujh1Ce5EhDqb8 IMvOdVC8cxBS1w9jmSbFA0NsGNdMFEHeQg8oOmbpZjavRNVW9+bnndqBPK8vS/yLqEgcDMI9WLF BkcIB12+iURGAm7puY4/e5a63WffI21fPwQRUSj9h7iISv6zxUXpb6GCB+nnUw4DORJ9n67t3Zn kT2zGUXypgeJJ7IMK7X6D+x8+IhrpuNyp9nkpIAYwlgSVX/kyvpWfb8ukplF/s24IVq/Dy5CYOW 20ZrVz3Mpa4MQsJ53u7gY6bC5qVcHI1eit8xXR3wd/uRcMEM6DytdgUI3VDQ X-Google-Smtp-Source: AGHT+IGxzvABNaAdfgiKYsTpiPN7PE7bXFzgqq9goxKnrMQ4im547vhxwGEu1XxLZq146dglo4dkvw== X-Received: by 2002:a05:6e02:170b:b0:3cf:b365:dcf0 with SMTP id e9e14a558f8ab-3cfb365f19bmr27111525ab.19.1737561062141; Wed, 22 Jan 2025 07:51:02 -0800 (PST) Received: from nathan (162-195-168-172.lightspeed.stlsmo.sbcglobal.net. [162.195.168.172]) by smtp.gmail.com with ESMTPSA id 8926c6da1cb9f-4ea754415aasm4161121173.44.2025.01.22.07.51.01 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 22 Jan 2025 07:51:01 -0800 (PST) Date: Wed, 22 Jan 2025 09:50:59 -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: <20220301144044.GG25269@telsasoft.com> <20220301150704.GA1030078@nathanxps13> <20220317231212.GB1415284@nathanxps13> <20220408203003.GA1630183@nathanxps13> <87sepc4zcg.fsf@163.com> 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 Tue, Jan 21, 2025 at 11:23:06AM -0500, Andres Freund wrote: > On 2025-01-21 10:13:14 -0600, Nathan Bossart wrote: >> On Tue, Jan 21, 2025 at 09:52:51AM -0600, Nathan Bossart wrote: >> > On Tue, Jan 21, 2025 at 03:31:27AM +0000, Andy Fan wrote: >> >> 3. Why is the purpose of preallocated_segments directory? what in my >> >> mind is we just prellocate the normal filename so that XLogWrite could >> >> open it directly. This is same as what wal_recycle does and we can reuse >> >> the same strategy to clean up them if they are not needed anymore. >> > >> > The purpose is to limit the use of pre-allocated segments to only >> > situations where WAL recycling is not sufficient. Basically, if writing a >> > record would require a new segment to be created, we can quickly pull a >> > pre-allocated one instead of creating it ourselves. Besides simplifying >> > matters, this prevents a lot of unnecessary pre-allocation, since many >> > workloads will almost never need anything beyond the recycled segments. > > I don't really understand that argument - we should be able to predict rather > precisely whether we need to preallocate or not. We have the recent WAL "fill > rate", we know the end of the WAL and we can easily track how far ahead of the > current point we have allocated. Why preallocate when we have a large reserve > of "future" segments? Why preallocate in a separate directory when we have no > future segments? If we can indeed reliably predict whether we need pre-allocation, then sure, let's just create future segments directly in pg_wal. I'm not sure we could reliably predict whether WAL will be recycled in time, so we might pre-allocate a bit more than necessary, but that's not too terrible. My "pooling" approach was intended to keep the pre-allocation to a minimum (IME you really only need a couple at any given time) and to avoid the guesswork involved in predicting. >> That being said, it would be nice to avoid the fsync() overhead to move a >> pre-allocated WAL into place. My first instinct is that would be >> substantially more complicated and may not actually improve matters all >> that much, but I agree that it's worth exploring. > > FWIW, I've seen the fsyncs around recycling being a rather substantial > bottleneck. To the point of the main benefit of larger segments being the > reduction in number of fsyncs at the end of a checkpoint. I think we should > be able to make the fsyncs a lot more efficient by batching them, first rename > a bunch of files, then fsync them and the directory. The current pattern > bascially requires a separate filesystem jouranl flush for each WAL segment. +1, these kinds of fsync() patterns should be fixed. -- nathan