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 1rkRt7-00CnoS-2E for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Mar 2024 16:55:33 +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 1rkRt5-008JMZ-F5 for pgsql-bugs@arkaria.postgresql.org; Wed, 13 Mar 2024 16:55:31 +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 1rkRt5-008JJc-5r for pgsql-bugs@lists.postgresql.org; Wed, 13 Mar 2024 16:55:31 +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 1rkRt2-004L4c-D2 for pgsql-bugs@lists.postgresql.org; Wed, 13 Mar 2024 16:55:30 +0000 Received: by mail-yw1-x1136.google.com with SMTP id 00721157ae682-60a3c48e70fso900137b3.1 for ; Wed, 13 Mar 2024 09:55:28 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=leadboat.com; s=google; t=1710348927; x=1710953727; 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=iXT393B0eiPl2cZeiBHx1CTSz4PcC6m8UWjTmw9yo2E=; b=CV1vGneKst5iow2tivdBY7nW4rDqMGFrhy/MuvovSTG2onJqq588zF2mC0hwdVz4wS O8A3/GSYEFPWau1s5zEIbQa+RXLACEyM2jmgv157Oc4Un9TAWgqmHjzuI3wNiHH039Qj AO2yKkWAFjXnuZejf3MvenRAAKUylg0TMjmFo= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1710348927; x=1710953727; 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=iXT393B0eiPl2cZeiBHx1CTSz4PcC6m8UWjTmw9yo2E=; b=pbUMNLStzGxCOHrgwx8kQ8B6OBNv4KsNQs3nY6t3/RbMDIYXO4Xum6cHjcjABR2FnW DEQRUh3gaFn+UAXmsxuinD26LdKTflsvUjJt47487jbo3CAsYLQj8OcpA3NbixZk++gJ eFtDiwX2nNfcPuknSNOtzbaKFtaV6VLOol9BMkM5KW8opcO4eihyxUB73VXK3h9o5tCS UgzUaUgg/I7tN32BJSI2ZdhTgRVzDR8etAvSou5Ru/UvURLfI40PLr9RB42Uyf2P0q1G YZ/JOetnqe5gtdTGQQ1+serMg3g4Cdz62IfO9ZkpFlNP5958cwprXY7x/88kG0I+tKhD FBlw== X-Forwarded-Encrypted: i=1; AJvYcCWZyDEuSGkrE6tbH6xiOZWTqLHqkHVsPxqvtuK21iLIKg9g1FhLvu8UKCqPEI80umqpUSUa9m3ZJqdvZfuitsQe2jRnG03TtMBm9tC1bb5k X-Gm-Message-State: AOJu0YyyaR3fUZOFKiOSE8kQ1XdRytgkTwsyrI4O1XQcpQ6SuKI+paog 32p41P8rLGv88WgORnr8KMg/jDrJfMwi5rokqFZJFsFhLTijwSYeB+yk7GTWVw== X-Google-Smtp-Source: AGHT+IGNbj6jBpgPnSW53x1b/xlRbt9xTKt7isklB50oEd+ufwO9bI+Ozn6viaZZ+G9aHrbSuyOvuw== X-Received: by 2002:a0d:e007:0:b0:609:86b8:d24a with SMTP id j7-20020a0de007000000b0060986b8d24amr3347697ywe.18.1710348926147; Wed, 13 Mar 2024 09:55:26 -0700 (PDT) Received: from rfd.leadboat.com ([2600:1702:a20:5750::48]) by smtp.gmail.com with ESMTPSA id cm10-20020a05690c0c8a00b00607fc6858c3sm2486888ywb.82.2024.03.13.09.55.25 (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Wed, 13 Mar 2024 09:55:25 -0700 (PDT) Date: Wed, 13 Mar 2024 09:55:23 -0700 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: <20240313165523.6a@rfd.leadboat.com> References: <1970118.yKVeVyVuyW@aivenlaptop> <20240312220807.05.nmisch@google.com> <3225659.5fSG56mABF@aivenlaptop> MIME-Version: 1.0 Content-Type: text/plain; charset=iso-8859-1 Content-Disposition: inline Content-Transfer-Encoding: 8bit In-Reply-To: <3225659.5fSG56mABF@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 13, 2024 at 10:37:21AM +0100, Ronan Dunklau wrote: > Le mardi 12 mars 2024, 23:08:07 CET Noah Misch a écrit : > > > I would argue this is fine, as corruptions don't happen often, and FSM is > > > not an exact thing anyway. If we record a block as full, it will just be > > > unused until the next vacuum adds it back to the FSM with a correct > > > value. > > If this happened only under FSM corruption, it would be fine. I was > > thinking about normal extension, with an event sequence like this: > > > > - RelationExtensionLockWaiterCount() returns 10. > > - Backend B1 extends by 10 pages. > > - B1 records 9 pages in the FSM. > > - B2, B3, ... B11 wake up and fetch from the FSM. In each of those > > backends, fsm_does_block_exist() returns false, because the cached relation > > size is stale. Those pages remain unused until VACUUM re-records them in > > the FSM. > > > > PostgreSQL added the multiple-block extension logic (commit 719c84c) from > > hard-won experience bottlenecking there. If fixing $SUBJECT will lose 1% of > > that change's benefit, that's probably okay. If it loses 20%, we should > > work to do better. While we could rerun the 2016 benchmark to see how the > > patch regresses it, there might be a simple fix. fsm_does_block_exist() > > could skip the cache when blknumber>=cached, like this: > > > > return((BlockNumberIsValid(smgr->smgr_cached_nblocks[MAIN_FORKNUM]) > && > > blknumber < smgr- > >smgr_cached_nblocks[MAIN_FORKNUM]) || > > blknumber < RelationGetNumberOfBlocks(rel)); > > > > How do you see it? That still leaves us with an avoidable lseek in B2, B3, > > ... B11, but that feels tolerable. I don't know how to do better without > > switching to the ReadBufferMode approach. > That makes sense. I'm a bit worried about triggering additional lseeks when we > in fact have other free pages in the map, that would pass the test. I'm not > sure how relevant it is given the way we search the FSM with fp_next_slot > though... That's a reasonable thing to worry about. We could do wrong by trying too hard to use an FSM slot, and we could do wrong by not trying hard enough. > To address that, I've given a bit of thought about enabling / disabling the > auto-repair behaviour with a flag in GetPageWithFreeSpace to distinguish the > cases where we know we have a somewhat up-to-date value compared to the case > where we don't (as in, for heap, try without repair, then get an uptodate > value to try the last block, and if we need to look at the FSM again then ask > for it to be repaired) but it brings way too much complexity and would need > careful thought for each AM. > > So please find attached a patch with the change you propose. > > Do you have a link to the benchmark you mention though to evaluate the patch > against it ? Here is one from the thread that created commit 719c84c: https://www.postgresql.org/message-id/flat/CA%2BTgmob7xED4AhoqLspSOF0wCMYEomgHfuVdzNJnwWVoE_c60g%40mail.gmail.com There may be other benchmarks earlier in that thread.