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 1rhwDt-00EAVQ-Sm for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Mar 2024 18:42:38 +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 1rhwDs-00D8DN-Fz for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Mar 2024 18:42:36 +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 1rhwDs-00D8Cs-0O for pgsql-bugs@lists.postgresql.org; Wed, 06 Mar 2024 18:42:36 +0000 Received: from mail-yw1-x1136.google.com ([2607:f8b0:4864:20::1136]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rhwDp-00383I-Lh for pgsql-bugs@lists.postgresql.org; Wed, 06 Mar 2024 18:42:35 +0000 Received: by mail-yw1-x1136.google.com with SMTP id 00721157ae682-608342633b8so394737b3.1 for ; Wed, 06 Mar 2024 10:42:33 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1709750552; x=1710355352; darn=lists.postgresql.org; h=user-agent:in-reply-to:content-transfer-encoding :content-disposition:mime-version:references:message-id:subject:cc :to:from:date:from:to:cc:subject:date:message-id:reply-to; bh=FrZ5am/ARM8jK2aJ8XEtiU6DLmQVI/lpJVw8o2Xj96c=; b=PLKjYgG5ZpgelRW9ZCqT0Vgp2Nshb+ShftS/UjWWVdYlKRdrcG5gaK+mYSYUH0wDNt HoKP/Qz7EA6JUZTYn/XSJp7rh/jNJgSHaqtZ92PQHGzdFX44iOZRR6uCVVCBqdx3Cxnd iKF3XVCl+/fGojmchnNp9n8X0MUTN1UskDSn0= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709750552; x=1710355352; h=user-agent:in-reply-to:content-transfer-encoding :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=FrZ5am/ARM8jK2aJ8XEtiU6DLmQVI/lpJVw8o2Xj96c=; b=wPJeD6TvIJAdVmmnDUkRGun3xzTFKXYeBbg0NZVH57ESTnERUNIwnIGeJAOSpkYb13 adKvZkx43I5uA88NRub0ZgYA50GToNBT8r1mOULZ9owTRflmHeqs4ctWItYNgBXoEyVz p6gTTviktxD9qtefJVs5yp+Gv0C8tXt2zelcDIUGSuWTqEKnj9i+C0ZNtVFdryLtskh+ N/5gZig+1CjURj2E/EQ4u+Ko/xQZXVzXERsiYl+7aOODEA3CW9EPiqPwxSgfROCRzpBW WTLH+ItzSlwIxgwRtFG4wMCgBBzdio0emLzO4tviszsIdzlnewdyfmQTFSY77DsnhvXT pudQ== X-Forwarded-Encrypted: i=1; AJvYcCWMr+pRQlBd4J8lCG53SkCHvAj+jyQchx+xPkk2k99epWtDixjanh9Fb7MH4MZ9qC3flnVK2L32Hepts7rhaQ5T173fETSegAxK7qzBSFVN X-Gm-Message-State: AOJu0Yw8Drimg5UIqGS8d8qE+IyAWdKOazvUUIYTJiJnxjz51vWTXzIE Bcjh7Pvw4/4a+n3UpWC5cVhgwuAdc2EXfTElV8uF8bf09QACISUkBaeyajbvcw== X-Google-Smtp-Source: AGHT+IGS+Jt/Nj5b3H93jzFepaQDm8T6QL1Wl60bSahBDEVyl0wA2GLbR/Jgs6TX4hoWNoMWUo04xg== X-Received: by 2002:a0d:e006:0:b0:609:e89e:7ff9 with SMTP id j6-20020a0de006000000b00609e89e7ff9mr468186ywe.34.1709750552119; Wed, 06 Mar 2024 10:42:32 -0800 (PST) Received: from google.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id x8-20020a0dd508000000b00609c886b655sm112229ywd.22.2024.03.06.10.42.30 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 06 Mar 2024 10:42:31 -0800 (PST) Date: Wed, 6 Mar 2024 10:42:28 -0800 From: Noah Misch To: Ronan Dunklau Cc: Michael Paquier , pgsql-bugs Subject: Re: FSM Corruption (was: Could not read block at end of the relation) Message-ID: <20240306184228.a7.nmisch@google.com> References: <3609363.R56niFO833@aivenlaptop> <1958756.PYKUYFuaPT@aivenlaptop> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <1958756.PYKUYFuaPT@aivenlaptop> 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 Wed, Mar 06, 2024 at 10:31:17AM +0100, Ronan Dunklau wrote: > Le mardi 5 mars 2024, 00:05:03 CET Noah Misch a écrit : > > I would guess this one is more risky from a performance perspective, since > > we'd be adding to a hotter path under RelationGetBufferForTuple(). Still, > > it's likely fine. > > I ended up implementing this in the attached patch. The idea is that we detect > if the FSM returns a page past the end of the relation, and ignore it. > In that case we will fallback through the extension mechanism. > > For the corrupted-FSM case it is not great performance wise, as we will extend > the relation in small steps every time we find a non existing block in the FSM, > until the actual relation size matches what is recorded in the FSM. But since > those seldom happen, I figured it was better to keep the code really simple for > a bugfix. That could be fine. Before your message, I assumed we would treat this like the case of a full page. That is, we'd call RecordAndGetPageWithFreeSpace(), which would remove the FSM entry and possibly return another. That could be problematic if another backend is extending the relation; we don't want to erase the extender's new FSM entry. I'm parking this topic for the moment. > I wanted to test the impact in terms of performance, and I thought about the > worst possible case for this. > [test details] > > I noticed no difference running with or without the patch, but maybe someone > else can try to run that or find another adversarial case ? The patch currently adds an lseek() whenever the FSM finds a block. I see relevant-looking posts from mailing list searches with subsets of these terms: RelationGetNumberOfBlocks lseek benchmark overhead. I bet we should reduce this cost add. Some ideas: - No need for a system call if the FSM-returned value is low enough with respect to any prior RelationGetNumberOfBlocks() call. - We'd be happy and efficient with a ReadBufferMode such that ReadBufferExtended() returns NULL after a 0-byte read, with all other errors handled normally. That sounds like the specification of RBM_NORMAL_NO_LOG. Today's implementation of RBM_NORMAL_NO_LOG isn't that, but perhaps one RBM could satisfy both sets of needs. On Wed, Mar 06, 2024 at 12:08:38PM +0100, Ronan Dunklau wrote: > Subject: [PATCH 2/2] Detect invalid FSM when finding a block. > > Whenever the FSM points to a block past the end of the relation, detect > it and fallback to the relation extension mechanism. > > This may arise when a FPI for the FSM has been triggered, and we end up > WAL-logging a page of the FSM pointing to newly extended blocks in the > relation which have never been WAL-logged. > --- > src/backend/access/heap/hio.c | 20 +++++++++++++++++++- Each GetPageWithFreeSpace() caller would need this, not just heap. Please evaluate whether this logic belongs inside freespace.c vs. in each caller. freespace.c comments and/or freespace/README may need updates. Please evaluate header comments of freespace.c functions whose callers you are changing, and evaluate comments about truncation.