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 1tYcLR-00CRiB-M0 for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Jan 2025 02:44:26 +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 1tYcLP-006BqG-S5 for pgsql-hackers@arkaria.postgresql.org; Fri, 17 Jan 2025 02:44:24 +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 1tYcLP-006Bq8-Hc for pgsql-hackers@lists.postgresql.org; Fri, 17 Jan 2025 02:44:24 +0000 Received: from mail-pl1-x630.google.com ([2607:f8b0:4864:20::630]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.96) (envelope-from ) id 1tYcLN-000roV-2E for pgsql-hackers@postgresql.org; Fri, 17 Jan 2025 02:44:23 +0000 Received: by mail-pl1-x630.google.com with SMTP id d9443c01a7336-2164b1f05caso30198915ad.3 for ; Thu, 16 Jan 2025 18:44:21 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1737081859; x=1737686659; darn=postgresql.org; 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=77l25068cZ1g0U6GG5fwFB8eIhbF2tz8rbW3RBIkEUM=; b=VU91gmgfVoTgXIUhM4FJaTL+Zu161IpOb5y4rf2Ux+QkmFTUv7wBMKSVYbfNc+uI95 1pEeugiX0e3F8gtamGk6U2oVfsQZ3JL+C8o0kgPvOWAO5y4hPPRjWWh4h5NcFTX346AW dRDIB4UxNUC6GOTFXn8sHNophdne2+ptk41Ww= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1737081859; x=1737686659; 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=77l25068cZ1g0U6GG5fwFB8eIhbF2tz8rbW3RBIkEUM=; b=OSnsK//ihzw8brL9Rakads/nuSyH0sbsqlZXLeOIytWlqLmMeanWaRDWT0ANEuwhC+ A4uTnwTpDbp5RB//nOA3JjUeO2HVrDOVZUTndL/zfUj+pLaX0QqjNLLkpBgmV/3Phtk9 G4df1E/x4GMBAaNr/XEebAYkOLT+mTvokZ7pbcSfMC8XvOmrrWObG1GYe9vqx2ZqJrJY GFbpeRxxLAETnNC2RR6W5hAeNfETSbyfCUWUfXL+ZjQKiL3EKrcueu8r6YVlUhCwr7io CcWBDINhWtMXmhn7Zoggwr1rlAwNJdtFKyCkuRcQJet2RAbC3MOWci73eCT1v5klklOz Zl5A== X-Forwarded-Encrypted: i=1; AJvYcCXs7bwKxJ0GBFa/zvenNCpjtSHhP90FxiC12FudPi8xWVQ8rtPeeRlseyc/LzLjKtrZipXOgNU97uvFqT2/@postgresql.org X-Gm-Message-State: AOJu0Yzw8nh1MT1xB+INhKbgPa22LgrWjjyZRv/CJb9n7nRXUA9wRBeS RnO3NtbPPu/AqSNkxTpHD2zLT2zXjZ526IcEd+FraGW+OYi0GaS2SM+hfSEN3A== X-Gm-Gg: ASbGnctJdYqwJCeqP0fofLzMLrwFsRNWjGXmaMt5VfZmjMUWcvQHez6558R3S3bt/Zu dTqqfVbuOGa35BR7U/fufctKrBpOkNUVCdSgHgZ35kxhE+aNRwilMkEHfU9k+mtqnVYi3S7lxu3 0lHSbhve+6FIGBobbop+i+LNgdf2Ov344gJVYVeF0oBPsRK86CopRXXjAfjjIbViT6ICfurunlL V5393hkAZvV+TbvWL1g17RgUGsIbxNW4okT3+hhbEHEASDkZxE= X-Google-Smtp-Source: AGHT+IEZ1+ZCdQ95XW2/OKr4utrAEBL+eMZ4zjhi7pvAa/EzM8JWxjHrrjjZwDGGzzcSTboRKde+iw== X-Received: by 2002:a17:903:703:b0:216:31aa:1308 with SMTP id d9443c01a7336-21c355e35c9mr13310855ad.34.1737081859287; Thu, 16 Jan 2025 18:44:19 -0800 (PST) Received: from google.com ([2600:1702:a20:5750::46]) by smtp.gmail.com with ESMTPSA id d9443c01a7336-21c2ceb8cf0sm6499875ad.68.2025.01.16.18.44.18 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Thu, 16 Jan 2025 18:44:18 -0800 (PST) Date: Thu, 16 Jan 2025 18:44:16 -0800 From: Noah Misch To: Michael Paquier Cc: Vitaly Davydov , pgsql-hackers Subject: Re: An improvement of ProcessTwoPhaseBuffer logic Message-ID: <20250117024416.8c.nmisch@google.com> References: <152dda-676d7200-61-659acf0@102179597> <16aa85-676ec500-3d-23db9b40@59039559> <20250116010051.f3.nmisch@google.com> <20250116205254.65.nmisch@google.com> <20250117005221.05.nmisch@google.com> MIME-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Disposition: inline In-Reply-To: User-Agent: Mutt/2.2.12 (2023-09-09) List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk On Fri, Jan 17, 2025 at 11:04:03AM +0900, Michael Paquier wrote: > On Thu, Jan 16, 2025 at 04:52:21PM -0800, Noah Misch wrote: > > In other words, the root problem that led to commits e358425 and 7e125b2 was > > recovery interpreting pg_twophase file content before reaching consistency. > > We can't make the ProcessTwoPhaseBuffer() checks safe before reaching > > consistency. Is that right? Once ProcessTwoPhaseBuffer() stops running > > before consistency, it won't strictly need the range checks. We may as well > > have something like those range checks as a best-effort way to detect base > > backup spec violations. > > Oh, yeah, it seems like you have a very good point here. > ProcessTwoPhaseBuffer() is an effort to use a unique path for the > handling of this 2PC data, and we cannot do that safely at this stage. > The problem is restoreTwoPhaseData(), which is called very early in Agreed. > the recovery process, while all the other code paths calling > ProcessTwoPhaseBuffer() are done when we're doing reading WAL after > we've reached consistency as far as I can see. This is a wrong > concept since 728bd991c3c4. 5a1dfde8334b has made that much harder to > think about, while not integrating fully things. It also looks like > we may be able to just get rid of the CLOG checks entirely if we > overwrite existing files as long as we are not in a consistent state, > as you say. Hmm. > > I agree that e358425 and 7e125b2 are weird attempts that just try to > work around the real issue, and that they're making some cases wrong > while on it with potential file removals. > > +typedef void (*TwoPhaseCallback) (FullTransactionId fxid, uint16 info, > void *recdata, uint32 len); > > Based on your latest patch, I doubt that we'll be able to do any of > that in the back-branches. That's much nicer in the long term to show > what this code relies on. The twophase.c bits of that patch turned out to be orthogonal to the key issue, so that's fine. I suspect the signature changes would be okay to back-patch to v17, subject to PGXN confirmation of no extension using those APIs. (The TwoPhaseCallback list is hard-coded; extensions can't add callbacks.) That said, the rationale for back-patching fxid stuff is gone. > I've been thinking about the strategy to use here, and here are some > rough steps: > - Let's first revert e358425 and 7e125b2. This indeed needs to be > reworked, and there is a release coming by. +1. e358425 is borderline, but it's hard to rule out ways of it making recovery unlink pg_twophase files improperly. Released code may also be doing that, but that code's seven years of history suggests it does so infrequently if at all. We don't have as much evidence about what the frequency would be under e358425. > - Your refactoring around xid8funcs.c is a good idea on its own. This > can be an independent patch. I got here on a yak shave for postgr.es/m/20250111214454.9a.nmisch@google.com. I expect that project will still want FullTransactionIdFromAllowableAt(). If so, I'll include it in that thread's patch series. > - Let's figure out how much surgery is required with a focus on HEAD > for now (I'm feeling that more non-backpatchable pieces are going to > be needed here), then extract the relevant pieces that could be > backpatchable. Hard to say if this is going to be doable at this > stage, or even worth doing, but it will be possible to dig into the > details once we're happy with the state of HEAD. My first intuition > is that the risk of touching the back-branches may not be worth the > potential reward based on the lack of field complaints (not seen > customer cases, tbh): high risk, low reward. Let's see how it turns out. My first intuition would be to target a back-patch, because a corner-case PITR failure is the kind of thing that can go unreported and yet be hard to forgive. I'm not confident I could fix both that other thread's data loss bug and $SUBJECT in time for the 2024-02 releases. (By $SUBJECT, I mean the seven-year-old bug of interpreting pg_twophase before recovery consistency, which has a higher chance of causing spurious recovery failure starting in v17.) Would your or someone else like to fix $SUBJECT, before or after 2024-02 releases? I'd make time to review a patch. > Another point to consider is if we'd better switch 2PC files to store > a fxid rather than a xid.. Perhaps that's not necessary, but if we're > using FullTransactionIds across the full stack of twophase.c that > could make the whole better with even less xid <-> fxid translations > in all these paths. There is always the counter-argument of the extra > 4 bytes in the 2PC files and the records, though. Yes, perhaps so. It could also be an option to store it only in the pg_twophase file, not in the WAL record. Files are a lot rarer. Similarly, I wondered if pg_twophase files should contain the LSN of the XLOG_XACT_PREPARE record, which would make the file's oldness unambiguous in a way fxid doesn't achieve. I didn't come up with a problem whose solution requires it, though. Thanks, nm