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 1qyoF4-00BFQp-6r for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Nov 2023 07:05:18 +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 1qyoF2-00H051-4l for pgsql-hackers@arkaria.postgresql.org; Fri, 03 Nov 2023 07:05:16 +0000 Received: from makus.postgresql.org ([2001:4800:3e1:1::229]) by malur.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1qyoF1-00H04H-N7 for pgsql-hackers@lists.postgresql.org; Fri, 03 Nov 2023 07:05:15 +0000 Received: from mail-oi1-x233.google.com ([2607:f8b0:4864:20::233]) by makus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qyoEv-003j3S-P4 for pgsql-hackers@lists.postgresql.org; Fri, 03 Nov 2023 07:05:14 +0000 Received: by mail-oi1-x233.google.com with SMTP id 5614622812f47-3b565722c0eso1049820b6e.2 for ; Fri, 03 Nov 2023 00:05:09 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=j-davis-com.20230601.gappssmtp.com; s=20230601; t=1698995108; x=1699599908; darn=lists.postgresql.org; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:from:to:cc:subject :date:message-id:reply-to; bh=ZdQE8WXaZJqUoKHeXPINYLc8t8EFA/dwbv31KRWHqf4=; b=Efbrjikk7qom+aGzJ7LVGsq429lRk2VHUHp6EjQxqqUaQfIrvZ3LpuAbnKblkmaUZf ZBijSTcunOSWkHllyKprdCH1KNsa/9uVIZKNIJ9Iq1cs3/KEUtpKrxWKIQ8JlKt9tB51 PDXRO0kA7sqnn3IKAFeLysC2BdFu+06RBZszP5W6I8Eq2Z8VK/i22MHT9fPWq5ggLzLM u8fV7W9OZKl8GOK5cYXBsUmTVeOJP12PYbk9XFhzGfY4jS0FnlJY5Ok8SrvTztPgeRxE /0aCpzn4F4dyBQZr0Kxhuo3X4MlcWRGP0GYlNZDv5eiD8gZiTpB0svILJVnpSUR2GF9f 9ZnA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1698995108; x=1699599908; h=mime-version:user-agent:content-transfer-encoding:references :in-reply-to:date:cc:to:from:subject:message-id:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=ZdQE8WXaZJqUoKHeXPINYLc8t8EFA/dwbv31KRWHqf4=; b=sp8uJJjs9HM31ZzuMPjDz9yPU4gb+13hQsKOTRzbuUPR41BPwAxoCdW0TnPgVWCdZe afHY4oSTUpsTK7WYFFywPvDz3jpNzJXeFNeaJKYbN3mbFZByCoIxptQPj31ALW0XAnKH zAL+63cOV+qTEooWkkQQfGBufGPEQZJcC/SN713D0wLCIWK2T6WuS503G3MXLySgtC+1 iw7OqTxyssIwG51HCmIitmcm8Aq5BQdPBnU9oOhUOwEr1txWpxBw34AxM/uPg80DI43Z gjP5XjJkfiqU8oPNIH5UURiQ379PtGf7jjaCyDz73UUz5oOlCtmAUsrij75+vZGYEaol C6ig== X-Gm-Message-State: AOJu0YzfBn2YGZzd/ohcGQf059RWbEYpUR80GI/D0gb34uzXIGEqk3GI bgtg1GdPFKmh1QU+CeHO0K3spg== X-Google-Smtp-Source: AGHT+IEYqqvs/96/HMijSLMAmpdoLHUwBSMm+dwyCqkV57MJrBaSPAY7DlId9dTJcObxDOvDFZ62Ww== X-Received: by 2002:a05:6808:1c6:b0:3b2:e799:97f6 with SMTP id x6-20020a05680801c600b003b2e79997f6mr20542520oic.59.1698995108564; Fri, 03 Nov 2023 00:05:08 -0700 (PDT) Received: from jeff-laptop.lan (c-76-102-242-158.hsd1.ca.comcast.net. [76.102.242.158]) by smtp.gmail.com with ESMTPSA id n23-20020a635c57000000b005b458aa0541sm731001pgm.15.2023.11.03.00.05.07 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 03 Nov 2023 00:05:07 -0700 (PDT) Message-ID: <2ef04861c0f77e7ae78b703770cc2bbbac3d85e6.camel@j-davis.com> Subject: Re: Improve WALRead() to suck data directly from WAL buffers when possible From: Jeff Davis To: Bharath Rupireddy Cc: Andres Freund , Dilip Kumar , Kyotaro Horiguchi , pgsql-hackers@lists.postgresql.org, Nathan Bossart , Masahiko Sawada Date: Fri, 03 Nov 2023 00:05:06 -0700 In-Reply-To: References: <20221212.115717.1183698540772933396.horikyota.ntt@gmail.com> <628b07b79743e2614ab1724f516cbd41560d6551.camel@j-davis.com> <20230114203403.4zpg72fw2qb34awf@awork3.anarazel.de> <0d84c025e38673cb575dcf4987d050fe7b1e1a1f.camel@j-davis.com> <20231011224353.cl7c2s222dw3de4j@awork3.anarazel.de> <5c6e8e60c5c156a9f51ccf850f70829e45a985a1.camel@j-davis.com> <4132fe48f831ed6f73a9eb191af5fe475384969c.camel@j-davis.com> Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable User-Agent: Evolution 3.44.4-0ubuntu2 MIME-Version: 1.0 List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Thu, 2023-11-02 at 22:38 +0530, Bharath Rupireddy wrote: > > I suppose the question is: should reading from the WAL buffers an > > intentional thing that the caller does explicitly by specific > > callers? > > Or is it an optimization that should be hidden from the caller? > >=20 > > I tend toward the former, at least for now. >=20 > Yes, it's an optimization that must be hidden from the caller. As I said, I tend toward the opposite: that specific callers should read from the buffers explicitly in the cases where it makes sense. I don't think this is the most important point right now though, let's sort out the other details. > >=20 > At any given point of time, WAL buffer pages are maintained as a > circularly sorted array in an ascending order from > OldestInitializedPage to InitializedUpTo (new pages are inserted at > this end). I don't see any reference to OldestInitializedPage or anything like it, with or without your patch. Am I missing something? > - Read 6 pages starting from LSN 80. Nothing is read from WAL buffers > as the page at LSN 80 doesn't exist despite other 5 pages starting > from LSN 90 exist. This is what I imagined a "partial hit" was: read the 5 pages starting at 90. The caller would then need to figure out how to read the page at LSN 80 from the segment files. I am not saying we should support this case; perhaps it doesn't matter. I'm just describing why that term was confusing for me. > If a caller asks for an unflushed WAL read > intentionally or unintentionally, XLogReadFromBuffers() reads only 4 > pages starting from LSN 150 to LSN 180 and will leave the remaining 2 > pages for the caller to deal with. This is the partial hit that can > happen. To me that's more like an EOF case. "Partial hit" sounds to me like the data exists but is not available in the cache (i.e. go to the segment files); whereas if it encountered the end, the data is not available at all. > >=20 > WALBufMappingLock protects both xlblocks and WAL buffer pages [1][2]. > I'm not sure how using the memory barrier, not WALBufMappingLock, > prevents writers from replacing WAL buffer pages while readers > reading > the pages. It doesn't *prevent* that case, but it does *detect* that case. We don't want to prevent writers from replacing WAL buffers, because that would mean we are slowing down the critical WAL writing path. Let me explain the potential problem cases, and how the barriers prevent them: Potential problem 1: the page is not yet resident in the cache at the time the memcpy begins. In this case, the first read barrier would ensure that the page is also not yet resident at the time xlblocks[idx] is read into endptr1, and=C2=A0we'd break out of the loop. Potential problem 2: the page is evicted before the memcpy finishes. In this case, the second read barrier would ensure that the page was also evicted before xlblocks[idx] is read into endptr2, and again we'd detect the problem and break out of the loop. I assume here that, if xlblocks[idx] holds the endPtr of the desired page, all of the bytes for that page are resident at that moment. I don't think that's true right now: AdvanceXLInsertBuffers() zeroes the old page before updating=C2=A0xlblocks[nextidx]. I think it needs something like: pg_atomic_write_u64(&XLogCtl->xlblocks[nextidx], InvalidXLogRecPtr); pg_write_barrier(); before the MemSet. I didn't review your latest v14 patch yet. Regards, Jeff Davis