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 1qUIM2-00CMnC-4V for pgsql-bugs@arkaria.postgresql.org; Fri, 11 Aug 2023 02:58: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 1qUIM0-003K2p-5Z for pgsql-bugs@arkaria.postgresql.org; Fri, 11 Aug 2023 02:58:20 +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 1qUILz-003K2h-U3 for pgsql-bugs@lists.postgresql.org; Fri, 11 Aug 2023 02:58:20 +0000 Received: from mail-yw1-x112a.google.com ([2607:f8b0:4864:20::112a]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1qUILt-001nog-JX for pgsql-bugs@lists.postgresql.org; Fri, 11 Aug 2023 02:58:19 +0000 Received: by mail-yw1-x112a.google.com with SMTP id 00721157ae682-583b0637c04so22548857b3.1 for ; Thu, 10 Aug 2023 19:58:13 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1691722692; x=1692327492; h=user-agent: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=UdOLqWoo9BLCaZdTkMwaLzC/09gwItCOr9AzxyTn9UY=; b=ATPXBMXNwfCqe9in3Z0fGAZXPAWh+f/wxNrHIjbhCDHHfb3DUDBoNZrmE0ipwQuQyn AHvb/8TLypj0kmdFH99Z6/ueLy5hzN0+/vEY2ok8+Vu9MYq5c8nbCFlAZGzAuEz3y8GP j1e7ZtGFKEGI4RZhpVBBS1G98AzSpXG2GonBg= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1691722692; x=1692327492; 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 :subject:date:message-id:reply-to; bh=UdOLqWoo9BLCaZdTkMwaLzC/09gwItCOr9AzxyTn9UY=; b=Llc2VQJaukI5t0r1HEglKsXaCM25cy7NLkFGtBEksbicQACo9b3Sfsmy8TyJt/2Zp1 KVlBpPABlshkx8mu6SN4S1xZ19W+qalEDw2eFjHVSbiROv7F2nVucOZJI0lYnRJNo0KW GHwxOJDrHrg3yI8LKFztJC7BSYnk1hLWqiLfergfbuyU/nlv4CRXZRLBj2UEZwkjE7iT 6CgPqIEdkFGNwaFFFrthqXOqiUJmI4krScehUnKCtkGK8mlJW/0+q/KVykX2F/coCp3u 3sNr7wiw35LAwUXXREB5eaeIuktxxPVsPc64qsruA5876jVRzUkLxQHxrNgebNj8Bare lz8w== X-Gm-Message-State: AOJu0YxDerrj9t6sEHtzQ4S6Drx1tt8qnI+vNnUIo5ArkYzfblZZbAG5 M1dtGu8JAEj1e3jtTOQwR07/pA== X-Google-Smtp-Source: AGHT+IHGmWsnv/7u3JFm38jRWv8fUPUIsAZ6taqkjq6atoU1v2F1emseiULztvEUvWEt5FeH+2ZRXw== X-Received: by 2002:a81:9182:0:b0:56d:2e66:4a3f with SMTP id i124-20020a819182000000b0056d2e664a3fmr4781842ywg.0.1691722691949; Thu, 10 Aug 2023 19:58:11 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::2e]) by smtp.gmail.com with ESMTPSA id x187-20020a817cc4000000b005774338d039sm744101ywc.96.2023.08.10.19.58.10 (version=TLS1_2 cipher=ECDHE-RSA-AES128-GCM-SHA256 bits=128/128); Thu, 10 Aug 2023 19:58:11 -0700 (PDT) Date: Thu, 10 Aug 2023 19:58:08 -0700 From: Noah Misch To: Michael Paquier Cc: Kyotaro Horiguchi , thomas.munro@gmail.com, exclusion@gmail.com, pgsql-bugs@lists.postgresql.org Subject: Re: BUG #17928: Standby fails to decode WAL on termination of primary Message-ID: <20230811025808.GA2261232@rfd.leadboat.com> References: <72bd036d-4f2a-8d50-b56e-6b1e3b9ba0a9@gmail.com> <20230710200012.GB3476234@rfd.leadboat.com> <20230711.155414.939136339819647605.horikyota.ntt@gmail.com> <20230717004905.GE3826297@rfd.leadboat.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: 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 On Thu, Aug 10, 2023 at 04:45:25PM +0900, Michael Paquier wrote: > On Sun, Jul 16, 2023 at 05:49:05PM -0700, Noah Misch wrote: > > On my system, testing unpatched REL_15_2, the test reached its first failure > > at minute 53 and its second failure at minute 189. Perhaps a faster, > > deterministic test would be feasible: > > It took less than five minutes for me to reproduce the failure using > the test case sent by Alexander on HEAD: > 2023-08-10 15:26:37.401 JST [11239] FATAL: invalid memory alloc request size 2021163525 > 2023-08-10 15:26:37.405 JST [11235] LOG: startup process (PID 11239) exited with exit code 1 > > I don't see it after two hours of tests with the patch. That is good. > > - Use pg_logical_emit_message to fill a few segments with 0xFF. > > - CHECKPOINT the primary, so the standby recycles segments. > > - One more pg_logical_emit_message, computing the length from > > pg_current_wal_insert_lsn() such that new message crosses a segment boundary > > and ends 4 bytes before the end of a page. > > - Stop the primary. > > - If the bug is present, the standby will exit. > > Good idea to pollute the data with recycled segments. Using a minimal > WAL segment size whould help here as well in keeping a test cheap, and > two segments should be enough. The alignment calculations and the > header size can be known, but the standby records are an issue for the > predictability of the test when it comes to adjust the length of the > logical message depending on the 8k WAL page, no? Could be. I expect there would be challenges translating that outline into a real test, but I don't know if that will be a major one. The test doesn't need to be 100% deterministic. If it fails 25% of the time and is not the slowest test in the recovery suite, I'd find that good enough. > >>>> [1] https://www.postgresql.org/message-id/20210505010835.umylslxgq4a6rbwg@alap3.anarazel.de > >>> > >>> Regarding [1], is it still worth zeroing recycled pages on standbys and/or > >>> reading the whole header before allocating xl_tot_len? (Are there benefits > >>> other than avoiding a 1G backend allocation or 4G frontend allocation, or is > >>> that benefit worth the cycles?) > >> > >> I believe reading the whole header is the most sensible approach as it > >> can prevent unnecessary memory requests. Another potential solution > >> (or hack) for this specific case is to let XLogWalRcvFlush write a > >> finalizing ((uint32)0) when dying is true. This stabilizes the > >> behavior to "invalid record length.. got 0" when running the TAP test. > > FWIW, I came back to this thread while tweaking the error reporting of > xlogreader.c for the sake of this thread and this proposal is an > improvement to be able to make a distinction between an OOM and an > incorrect record: > https://www.postgresql.org/message-id/ZMh/WV+CuknqePQQ@paquier.xyz > > Anyway, agreed that it's an improvement to remove this check out of > allocate_recordbuf(). Noah, are you planning to work more on that? I can push xl_tot_len-validate-v1.patch, particularly given the testing result you reported today. I'm content for my part to stop there. > >> --- a/src/backend/replication/walreceiver.c > >> +++ b/src/backend/replication/walreceiver.c > >> @@ -989,6 +989,15 @@ XLogWalRcvFlush(bool dying, TimeLineID tli) > >> { > >> Assert(tli != 0); > >> > >> + if (dying) > >> + { > >> + int buflen = sizeof(uint32); > >> + char buf[sizeof(uint32)]; > >> + > >> + memset(buf, 0, buflen); > >> + XLogWalRcvWrite(buf, buflen, LogstreamResult.Write, tli); > >> + } > >> + > >> if (LogstreamResult.Flush < LogstreamResult.Write) > >> { > >> WalRcvData *walrcv = WalRcv; > > > > That's inexpensive in cycles, and it should make the failures in question > > quite rare. If we do that instead of pre-zeroing, I think we'd still want to > > be ready for invalid lengths, for the case of unclean standby shutdown. > > Tweaking the WAL receiver to do more zeroing has been discussed in the > past, but as far as I recall we were not completely sure whether it's > completely free, either. It seems to me that this should have a > discussion on its own, in a new thread. Not sure if we should worry > about archiving, actually, even if the segments should be padded with > zeros beforehand. Okay. > + gotheader = targetRecOff <= XLOG_BLCKSZ - SizeOfXLogRecord ? true : false; Okay.