Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXmEe-00034K-Dr for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Mar 2022 15:52:20 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.92) (envelope-from ) id 1nXmDf-0002uL-6c for pgsql-hackers@arkaria.postgresql.org; Fri, 25 Mar 2022 15:51:19 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXmDe-0002uA-TF for pgsql-hackers@lists.postgresql.org; Fri, 25 Mar 2022 15:51:18 +0000 Received: from sss.pgh.pa.us ([66.207.139.130]) by magus.postgresql.org with esmtps (TLS1.3:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1nXmDc-00041D-KS for pgsql-hackers@postgresql.org; Fri, 25 Mar 2022 15:51:18 +0000 Received: from sss1.sss.pgh.pa.us (localhost [127.0.0.1]) by sss.pgh.pa.us (8.15.2/8.15.2) with ESMTP id 22PFomrJ3261707; Fri, 25 Mar 2022 11:50:48 -0400 From: Tom Lane To: Robert Haas cc: Andres Freund , Daniel Gustafsson , Kyotaro Horiguchi , deniel1495@mail.ru, Ibrar Ahmed , tejeswarm@hotmail.com, hlinnaka , Masahiko Sawada , "pgsql-hackers@postgresql.org" , Daniel Wood Subject: Re: Corruption during WAL replay In-reply-to: References: <3170060.1648171358@sss.pgh.pa.us> <3173721.1648173548@sss.pgh.pa.us> <20220325022010.iqdj37tjqrqwxfrh@alap3.anarazel.de> <20220325024302.lhhi7jaapyetqviv@alap3.anarazel.de> <20220325034301.htu27xf54xjgyoca@alap3.anarazel.de> <3185871.1648181300@sss.pgh.pa.us> <20220325045438.enwakjqhrafzq5f2@alap3.anarazel.de> <20220325052654.3xpbmntatyofau2w@alap3.anarazel.de> <3193652.1648186725@sss.pgh.pa.us> <20220325060737.iayq5cs36jktqlag@alap3.anarazel.de> <3237467.1648216174@sss.pgh.pa.us> <3238876.1648216924@sss.pgh.pa.us> <3241547.1648218889@sss.pgh.pa.us> Comments: In-reply-to Robert Haas message dated "Fri, 25 Mar 2022 10:49:22 -0400" MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----- =_aaaaaaaaaa0" Content-ID: <3261618.1648223387.0@sss.pgh.pa.us> Date: Fri, 25 Mar 2022 11:50:48 -0400 Message-ID: <3261706.1648223448@sss.pgh.pa.us> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Archived-At: Precedence: bulk ------- =_aaaaaaaaaa0 Content-Type: text/plain; charset="us-ascii" Content-ID: <3261618.1648223387.1@sss.pgh.pa.us> Robert Haas writes: > ... It's not > like a 16-bit checksum was state-of-the-art even when we introduced > it. We just did it because we had 2 bytes that we could repurpose > relatively painlessly, and not any larger number. And that's still the > case today, so at least in the short term we will have to choose some > other solution to this problem. Indeed. I propose the attached, which also fixes the unsafe use of seek() alongside syswrite(), directly contrary to what "man perlfunc" says to do. regards, tom lane ------- =_aaaaaaaaaa0 Content-Type: text/x-diff; name="make-checksum-corruption-more-reliable.patch"; charset="us-ascii" Content-ID: <3261618.1648223387.2@sss.pgh.pa.us> Content-Description: make-checksum-corruption-more-reliable.patch Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_checksums/t/002_actions.pl b/src/bin/pg_checksums/= t/002_actions.pl index 62c608eaf6..8c70453a45 100644 --- a/src/bin/pg_checksums/t/002_actions.pl +++ b/src/bin/pg_checksums/t/002_actions.pl @@ -24,6 +24,7 @@ sub check_relation_corruption my $tablespace =3D shift; my $pgdata =3D $node->data_dir; = + # Create table and discover its filesystem location. $node->safe_psql( 'postgres', "CREATE TABLE $table AS SELECT a FROM generate_series(1,10000) AS a; @@ -37,9 +38,6 @@ sub check_relation_corruption my $relfilenode_corrupted =3D $node->safe_psql('postgres', "SELECT relfilenode FROM pg_class WHERE relname =3D '$table';"); = - # Set page header and block size - my $pageheader_size =3D 24; - my $block_size =3D $node->safe_psql('postgres', 'SHOW block_size;'); $node->stop; = # Checksums are correct for single relfilenode as the table is not @@ -55,8 +53,12 @@ sub check_relation_corruption = # Time to create some corruption open my $file, '+<', "$pgdata/$file_corrupted"; - seek($file, $pageheader_size, SEEK_SET); - syswrite($file, "\0\0\0\0\0\0\0\0\0"); + my $pageheader; + sysread($file, $pageheader, 24) or die "sysread failed"; + # This inverts the pd_checksum field (only); see struct PageHeaderData + $pageheader ^=3D "\0\0\0\0\0\0\0\0\xff\xff"; + sysseek($file, 0, 0) or die "sysseek failed"; + syswrite($file, $pageheader) or die "syswrite failed"; close $file; = # Checksum checks on single relfilenode fail ------- =_aaaaaaaaaa0--