Received: from malur.postgresql.org ([217.196.149.56]) by arkaria.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRdn7-00008Q-9v for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Nov 2018 13:52:42 +0000 Received: from localhost ([127.0.0.1] helo=malur.postgresql.org) by malur.postgresql.org with esmtp (Exim 4.89) (envelope-from ) id 1gRdn3-0004bt-HC for pgsql-hackers@arkaria.postgresql.org; Tue, 27 Nov 2018 13:52:37 +0000 Received: from magus.postgresql.org ([2a02:c0:301:0:ffff::29]) by malur.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRdn1-0004bm-U8 for pgsql-hackers@lists.postgresql.org; Tue, 27 Nov 2018 13:52:37 +0000 Received: from out4-smtp.messagingengine.com ([66.111.4.28]) by magus.postgresql.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_CBC_SHA1:256) (Exim 4.89) (envelope-from ) id 1gRdmx-0007iJ-8K for pgsql-hackers@postgresql.org; Tue, 27 Nov 2018 13:52:35 +0000 Received: from compute4.internal (compute4.nyi.internal [10.202.2.44]) by mailout.nyi.internal (Postfix) with ESMTP id 2A6F821F23; Tue, 27 Nov 2018 08:52:29 -0500 (EST) Received: from mailfrontend1 ([10.202.2.162]) by compute4.internal (MEProxy); Tue, 27 Nov 2018 08:52:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=paquier.xyz; h= date:from:to:cc:subject:message-id:references:mime-version :content-type:in-reply-to; s=fm3; bh=iWxGpJrfngwJML5WsOtNn3VY9h5 TGSN8CzRDlEIGqpU=; b=H/JhWi1xsUe5XFYH2SYB0kYklZTGEwx2EbrAV4h0fO0 qpvcVNpqHSoeIFFJXTr4Tib0cac7Fg0fYKNigLe8tLKdXYsCrlzB2zmoig2XG6lY 5RyKnRVyEFy8dgONe3BdALk3LcYnE9pxda7i48L6og+5fBXV99fZH69SYpzBQw0z QgCreiMDS6H+blRGQLCwuLGvPrGhDR5l2tabrcL/s92M3nKkqVmAde+RVdTuqysw GGymcJZ6U3HsjiIUJ8/9nijcn4VcHRbrFZLOZ7qGvnXnJ6Lb901GFESsp7UYubJB 5PvZzajRjIcSP6U3+LiNHOt7lsAGpODh2uOlL88Bsdw== DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d= messagingengine.com; h=cc:content-type:date:from:in-reply-to :message-id:mime-version:references:subject:to:x-me-proxy :x-me-proxy:x-me-sender:x-me-sender:x-sasl-enc; s=fm1; bh=iWxGpJ rfngwJML5WsOtNn3VY9h5TGSN8CzRDlEIGqpU=; b=ioqR90Cs170BImBKhPTQDC fzCwKkrUbVeWe0wZu8hRzmPgh1qz0PtUNCr2Ek6lYfDMaNBywQlM8av1p+z5m6kk hKjLxE6hh+TOt9bHgZfMTB/dJuINeG/a8DHHvQtm5bPR5YGVOuzBD2NMuIyC1oD8 VhKmQQH9tGobLTUBGBVVcKNai1Do9oT3JwtTvV3j1nAw9oH87UZHt7R4TdRKrcbn yjsrj0G0Fa7iYhykdouUzrerQd+DtHvpsSK8TeYRC5BdhUukFfgdtJ51XG5A1Vc8 8ljNe8yb1YUhlEDFw9DvXItV9nGRO/pUk63DR74i40ob+b5EmK9TeTrb76KRzFaA == X-ME-Sender: X-ME-Proxy: Received: from paquier.xyz (c137162.net61215.cablenet.ne.jp [61.215.137.162]) by mail.messagingengine.com (Postfix) with ESMTPA id 393BCE4B20; Tue, 27 Nov 2018 08:52:24 -0500 (EST) Date: Tue, 27 Nov 2018 22:52:21 +0900 From: Michael Paquier To: Michael Banck Cc: Andrew Dunstan , Tom Lane , Andres Freund , Postgres hackers , Daniel Gustafsson , Magnus Hagander Subject: Re: pgsql: Add TAP tests for pg_verify_checksums Message-ID: <20181127135221.GG1716@paquier.xyz> References: <20181014224138.GA15473@paquier.xyz> <11414.1539615900@sss.pgh.pa.us> <20181016220900.GA2331@paquier.xyz> <61121849-19c2-ca27-a928-08aacc0f1105@2ndQuadrant.com> <20181019135009.GG2099@paquier.xyz> <1543324145.17247.6.camel@credativ.de> MIME-Version: 1.0 Content-Type: multipart/signed; micalg=pgp-sha512; protocol="application/pgp-signature"; boundary="ibq+fG+Ci5ONsaof" Content-Disposition: inline In-Reply-To: <1543324145.17247.6.camel@credativ.de> List-Id: List-Help: List-Subscribe: List-Post: List-Owner: List-Archive: Precedence: bulk --ibq+fG+Ci5ONsaof Content-Type: multipart/mixed; boundary="yhze8HlyfmXt1APY" Content-Disposition: inline --yhze8HlyfmXt1APY Content-Type: text/plain; charset=us-ascii Content-Disposition: inline On Tue, Nov 27, 2018 at 02:09:05PM +0100, Michael Banck wrote: > I had a quick look at fixing this but did not manage to immediately come > up with a solution, so posting here for now. If you look at another thread, the patch posted on the top would actually solve this issue: https://www.postgresql.org/message-id/20181021134206.GA14282@paquier.xyz Your problem could also be solved with the minimalistic patch attached, so fixing on the way the problems with temporary files present in PGDATA something like the attached could be used... Based on the stale status of the other thread I am unsure what should be done though. -- Michael --yhze8HlyfmXt1APY Content-Type: text/x-diff; charset=us-ascii Content-Disposition: attachment; filename="verify-checksum-tablespace.patch" Content-Transfer-Encoding: quoted-printable diff --git a/src/bin/pg_verify_checksums/pg_verify_checksums.c b/src/bin/pg= _verify_checksums/pg_verify_checksums.c index f0e09bea20..148aa511f6 100644 --- a/src/bin/pg_verify_checksums/pg_verify_checksums.c +++ b/src/bin/pg_verify_checksums/pg_verify_checksums.c @@ -21,6 +21,7 @@ #include "storage/bufpage.h" #include "storage/checksum.h" #include "storage/checksum_impl.h" +#include "storage/fd.h" =20 =20 static int64 files =3D 0; @@ -189,9 +190,22 @@ scan_directory(const char *basedir, const char *subdir) char fn[MAXPGPATH]; struct stat st; =20 - if (!isRelFileName(de->d_name)) + if (strcmp(de->d_name, ".") =3D=3D 0 || + strcmp(de->d_name, "..") =3D=3D 0) continue; =20 + /* Skip temporary files */ + if (strncmp(de->d_name, + PG_TEMP_FILE_PREFIX, + strlen(PG_TEMP_FILE_PREFIX)) =3D=3D 0) + continue; + + /* Skip temporary folders */ + if (strncmp(de->d_name, + PG_TEMP_FILES_DIR, + strlen(PG_TEMP_FILES_DIR)) =3D=3D 0) + return; + snprintf(fn, sizeof(fn), "%s/%s", path, de->d_name); if (lstat(fn, &st) < 0) { @@ -206,6 +220,13 @@ scan_directory(const char *basedir, const char *subdir) *segmentpath; BlockNumber segmentno =3D 0; =20 + /* + * Only normal relation files can be analyzed. Note that this + * skips temporary relations. + */ + if (!isRelFileName(de->d_name)) + continue; + /* * Cut off at the segment boundary (".") to get the segment number * in order to mix it into the checksum. Then also cut off at the diff --git a/src/bin/pg_verify_checksums/t/002_actions.pl b/src/bin/pg_veri= fy_checksums/t/002_actions.pl index 0e1725d9f2..fd64de050e 100644 --- a/src/bin/pg_verify_checksums/t/002_actions.pl +++ b/src/bin/pg_verify_checksums/t/002_actions.pl @@ -5,7 +5,7 @@ use strict; use warnings; use PostgresNode; use TestLib; -use Test::More tests =3D> 36; +use Test::More tests =3D> 42; =20 # Initialize node with checksums enabled. my $node =3D get_new_node('node_checksum'); @@ -54,31 +54,7 @@ command_fails(['pg_verify_checksums', '-D', $pgdata], "fails with online cluster"); =20 # Create table to corrupt and get its relfilenode -$node->safe_psql('postgres', - "SELECT a INTO corrupt1 FROM generate_series(1,10000) AS a; - ALTER TABLE corrupt1 SET (autovacuum_enabled=3Dfalse);"); - -my $file_corrupted =3D $node->safe_psql('postgres', - "SELECT pg_relation_filepath('corrupt1')"); -my $relfilenode_corrupted =3D $node->safe_psql('postgres', - "SELECT relfilenode FROM pg_class WHERE relname =3D 'corrupt1';"); - -# 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 -# corrupted yet. -command_ok(['pg_verify_checksums', '-D', $pgdata, - '-r', $relfilenode_corrupted], - "succeeds for single relfilenode with offline cluster"); - -# Time to create some corruption -open my $file, '+<', "$pgdata/$file_corrupted"; -seek($file, $pageheader_size, 0); -syswrite($file, '\0\0\0\0\0\0\0\0\0'); -close $file; +my $relfilenode_corrupted =3D create_corruption($node, 'corrupt1', 'pg_def= ault'); =20 # Global checksum checks fail $node->command_checks_all([ 'pg_verify_checksums', '-D', $pgdata], @@ -95,6 +71,72 @@ $node->command_checks_all([ 'pg_verify_checksums', '-D',= $pgdata, '-r', [qr/checksum verification failed/], 'fails for corrupted data on single relfilenode'); =20 +# Drop corrupt table again and make sure there is no more corruption +$node->start; +$node->safe_psql('postgres', 'DROP TABLE corrupt1;'); +$node->stop; +$node->command_ok(['pg_verify_checksums', '-D', $pgdata], + 'succeeds again: '.$node->data_dir); + +# Create table to corrupt in a non-default tablespace and get its relfilen= ode +my $tablespace_dir =3D $node->data_dir."/../ts_corrupt_dir"; +mkdir ($tablespace_dir); +$node->start; +$node->safe_psql('postgres', "CREATE TABLESPACE ts_corrupt LOCATION '".$ta= blespace_dir."';"); +$relfilenode_corrupted =3D create_corruption($node, 'corrupt2', 'ts_corrup= t'); +$node->command_checks_all([ 'pg_verify_checksums', '-D', $pgdata], + 1, + [qr/Bad checksums:.*1/], + [qr/checksum verification failed/], + 'fails with corrupted data in non-default tablespace'); + +# Drop corrupt table again and make sure there is no more corruption +$node->start; +$node->safe_psql('postgres', 'DROP TABLE corrupt2;'); +$node->stop; +$node->command_ok(['pg_verify_checksums', '-D', $pgdata], + 'succeeds again'); + +# Utility routine to create a table with corrupted checksums. +# It stops the node (if running), and starts it again. +sub create_corruption +{ + my $node =3D shift; + my $table =3D shift; + my $tablespace =3D shift; + + $node->safe_psql('postgres', + "SELECT a INTO ".$table." FROM generate_series(1,10000) AS a; + ALTER TABLE ".$table." SET (autovacuum_enabled=3Dfalse);"); + + $node->safe_psql('postgres', + "ALTER TABLE ".$table." SET TABLESPACE ".$tablespace.";"); + + my $file_corrupted =3D $node->safe_psql('postgres', + "SELECT pg_relation_filepath('".$table."');"); + 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 + # corrupted yet. + command_ok(['pg_verify_checksums', '-D', $pgdata, + '-r', $relfilenode_corrupted], + "succeeds for single relfilenode with offline cluster"); + + # Time to create some corruption + open my $file, '+<', "$pgdata/$file_corrupted"; + seek($file, $pageheader_size, 0); + syswrite($file, '\0\0\0\0\0\0\0\0\0'); + close $file; + + return $relfilenode_corrupted; +} + # Utility routine to check that pg_verify_checksums is able to detect # correctly-named relation files filled with some corrupted data. sub fail_corrupt --yhze8HlyfmXt1APY-- --ibq+fG+Ci5ONsaof Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQIzBAABCgAdFiEEG72nH6vTowiyblFKnvQgOdbyQH0FAlv9TBUACgkQnvQgOdby QH2A5w//bjntu+MirJO1wkw27AAtTX7mcIgpykIQCFtVwwkvSG5c5Y7e/g3IxaKj YfxT5ib23GGEPlgzUDJRD9RFutNyHXKN+Ra9GIeAd9ZtGal8ucYoAJTXXvz40cex cFEe3yZmYIwDkZfALO9nQ7f+75qb6Bga4DdbDUCgjBq85MwlfsR6Sgbp2LOXRDi4 Z7Ixmf/7geBVuPIDxdcykuqK4PsoQgR4P/WmYbjBxcHzFdc2r7jP90Zhc18T+SHF s+Nx6S7uCgHVGdnxHJ61SglYZM/l1o6LrgWdsGhuvKTl59vSXrjituWRMXcOhzqG Y29DHnIndWkiLoCN3RwGD/XDB7cQOtHEhrJK4codAnnTET0J5LvoXL7Tr9AeF3ZL PwDxRVOHGf7pcpTQWPSw+1P8vL7EUNmQO3r0mz1576EgGbjZbsy7VvxhgWWiArDw uY+tCS8RbrZHSw2jMRohzr40q8Nlihns3DGU5SJCp5iUrdFvHWVvymsBk0Cq334Y NJz4Xjw6xwIpeC+ZzgEgtXYIBqMjgOOocA39IZ0Cv1oWl5qGzVZqlEBN6bqXP1KB 28Z2ImKAXRl7YHybO5XpftC7qc8SwnByWGd0bjBfIB9z1cUJZK/cIBW3OclDn6wT aZDlX5U2tBAs3fAZ97qEzKUTlEzopmWSA9CatdDHGrNb7evCNg0= =V7Xn -----END PGP SIGNATURE----- --ibq+fG+Ci5ONsaof--