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 1rhncU-00DQm5-Gh for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Mar 2024 09:31:27 +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 1rhncT-007fak-2K for pgsql-bugs@arkaria.postgresql.org; Wed, 06 Mar 2024 09:31:25 +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 1rhncS-007faT-NZ for pgsql-bugs@lists.postgresql.org; Wed, 06 Mar 2024 09:31:25 +0000 Received: from mail-wm1-x32f.google.com ([2a00:1450:4864:20::32f]) by magus.postgresql.org with esmtps (TLS1.3) tls TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256 (Exim 4.94.2) (envelope-from ) id 1rhncN-0033fR-Nf for pgsql-bugs@lists.postgresql.org; Wed, 06 Mar 2024 09:31:24 +0000 Received: by mail-wm1-x32f.google.com with SMTP id 5b1f17b1804b1-412e92deb18so17912935e9.1 for ; Wed, 06 Mar 2024 01:31:19 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=aiven.io; s=google; t=1709717478; x=1710322278; darn=lists.postgresql.org; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:from:to:cc:subject:date :message-id:reply-to; bh=UKumuxhH2K9EvQKTkRzBwM0kdUXOBaUx2QmTVNkvsF0=; b=NN7r0IZ+IeUmN9irWVOVvN5zzai2LxCIddncqMZqixXIViuZrYj4DgjjAPKoG9Sogy gYVnp0ja50ixNzmPW2qyXpYITXtxnQP3RRyRSfVRKA/wFGJ+wy3e6kXfBFRBEnf/U2hz sDQrbNQYInc0X2OPla6iO8lHb48XeZLNHFGHc= X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1709717478; x=1710322278; h=content-transfer-encoding:mime-version:references:in-reply-to :message-id:date:subject:cc:to:from:x-gm-message-state:from:to:cc :subject:date:message-id:reply-to; bh=UKumuxhH2K9EvQKTkRzBwM0kdUXOBaUx2QmTVNkvsF0=; b=UAlPW1bxxdNXfm8Ag09B45Aqyiu5Lqo7n8z25EyJt+KDloV5EMcKSpI1zi1wsR1EjQ yzc5vN+CUyjeCYmpnPlfQe+EWRuUu9GRB1gQu/L+mslOXJgcxcPUckeRs9wjpFlGBSdT aLNuI6yUz5Wn5SDmHyxX7fxF25GLBjdU8F7Cv7wUVT4Z0n2RP14+7qt805h94LIrN3W3 5zkXQhHPe4JxXFmoinSzdpmivx2giY6R3R5xeH/7NtFyoz1vTpjsadgLq94XvKxg7iai gFuja/Hiyp8glW9uAG77QgtVK8pR2tH1+12KuGyOTKYaBlBVc8TIJmgaXaX3dDdhQF0O mX9w== X-Gm-Message-State: AOJu0Yx4nsQdrvFUlnLxTgOTiqasLye38rty2vKsuICtdSmxVMx3ERqQ 0aUmx+WcnkMqA/mbq2ghpkoc6k2E1cygwvEagzQ8CV05YSxj3CcnrgU0Xbz/AEo= X-Google-Smtp-Source: AGHT+IG/mK0ffilv1thmVyRLEoI1/LTse1BQODlMl2qUFHmIr2NE/YPyWozekUUNFy22B7aEKKdRAA== X-Received: by 2002:a05:600c:35d6:b0:412:e70a:7015 with SMTP id r22-20020a05600c35d600b00412e70a7015mr4891159wmq.25.1709717478508; Wed, 06 Mar 2024 01:31:18 -0800 (PST) Received: from aivenlaptop.localnet ([45.13.105.93]) by smtp.gmail.com with ESMTPSA id p29-20020a05600c1d9d00b00412ef3a68b5sm3614902wms.16.2024.03.06.01.31.17 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 06 Mar 2024 01:31:17 -0800 (PST) From: Ronan Dunklau To: Noah Misch , Michael Paquier Cc: pgsql-bugs Subject: Re: FSM Corruption (was: Could not read block at end of the relation) Date: Wed, 06 Mar 2024 10:31:17 +0100 Message-ID: <1958756.PYKUYFuaPT@aivenlaptop> In-Reply-To: <3609363.R56niFO833@aivenlaptop> References: <3609363.R56niFO833@aivenlaptop> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="nextPart22206681.EfDdHjke4D" Content-Transfer-Encoding: 7Bit List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk This is a multi-part message in MIME format. --nextPart22206681.EfDdHjke4D Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset="iso-8859-1" Le mardi 5 mars 2024, 00:05:03 CET Noah Misch a =E9crit : > 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 det= ect=20 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. =46or the corrupted-FSM case it is not great performance wise, as we will e= xtend=20 the relation in small steps every time we find a non existing block in the = =46SM,=20 until the actual relation size matches what is recorded in the FSM. But sin= ce=20 those seldom happen, I figured it was better to keep the code really simple= for=20 a bugfix. I wanted to test the impact in terms of performance, and I thought about th= e=20 worst possible case for this. Then, run a pgbench doing insertions in the table. With the attached patch = the=20 worst case I could come up with is:=20 - remember which page we last inserted into - notice we don't have enough space - ask the FSM for a block - now have to compare that to the actual relation size So I came up with the following initialization steps: - create a table with vacuum_truncate =3D off, with a tuple size big enoug= h that=20 it's impossible to fit two tuples on the same page - insert lots of tuple in it until it reaches a decent size - delete them all - vacuum - all of this fitting in shared_buffers As in: CREATE TABLE test_perf (c1 char(5000)); ALTER TABLE test_perf ALTER c1 SET STORAGE PLAIN; ALTER TABLE test_perf SET (VACUUM_TRUNCATE =3D off); INSERT INTO test_perf (c1) SELECT 'c' FROM generate_series(1, 1000000); DELETE FROM test_perf; VACUUM test_perf; Then I ran pgbench with a single client, with a script only inserting the s= ame=20 value over and over again, for 1000000 transactions (initial table size). I noticed no difference running with or without the patch, but maybe someon= e=20 else can try to run that or find another adversarial case ?=20 Best regards, =2D- Ronan Dunklau --nextPart22206681.EfDdHjke4D Content-Disposition: attachment; filename="0001-Detect-invalid-FSM-when-finding-a-block.patch" Content-Transfer-Encoding: 7Bit Content-Type: text/x-patch; charset="x-UTF_8J"; name="0001-Detect-invalid-FSM-when-finding-a-block.patch"