Commit 97060928 authored by Robert Haas's avatar Robert Haas

Add pg_amcheck, a CLI for contrib/amcheck.

This makes it a lot easier to run the corruption checks that are
implemented by contrib/amcheck against lots of relations and get
the result in an easily understandable format. It has a wide variety
of options for choosing which relations to check and which checks
to perform, and it can run checks in parallel if you want.

Mark Dilger, reviewed by Peter Geoghegan and by me.

Discussion: http://postgr.es/m/12ED3DA8-25F0-4B68-937D-D907CFBF08E7@enterprisedb.com
Discussion: http://postgr.es/m/BA592F2D-F928-46FF-9516-2B827F067F57@enterprisedb.com
parent 48d67fd8
...@@ -196,6 +196,7 @@ Complete list of usable sgml source files in this directory. ...@@ -196,6 +196,7 @@ Complete list of usable sgml source files in this directory.
<!ENTITY dropuser SYSTEM "dropuser.sgml"> <!ENTITY dropuser SYSTEM "dropuser.sgml">
<!ENTITY ecpgRef SYSTEM "ecpg-ref.sgml"> <!ENTITY ecpgRef SYSTEM "ecpg-ref.sgml">
<!ENTITY initdb SYSTEM "initdb.sgml"> <!ENTITY initdb SYSTEM "initdb.sgml">
<!ENTITY pgamcheck SYSTEM "pg_amcheck.sgml">
<!ENTITY pgarchivecleanup SYSTEM "pgarchivecleanup.sgml"> <!ENTITY pgarchivecleanup SYSTEM "pgarchivecleanup.sgml">
<!ENTITY pgBasebackup SYSTEM "pg_basebackup.sgml"> <!ENTITY pgBasebackup SYSTEM "pg_basebackup.sgml">
<!ENTITY pgbench SYSTEM "pgbench.sgml"> <!ENTITY pgbench SYSTEM "pgbench.sgml">
......
This diff is collapsed.
...@@ -246,6 +246,7 @@ ...@@ -246,6 +246,7 @@
&dropdb; &dropdb;
&dropuser; &dropuser;
&ecpgRef; &ecpgRef;
&pgamcheck;
&pgBasebackup; &pgBasebackup;
&pgbench; &pgbench;
&pgConfig; &pgConfig;
......
...@@ -15,6 +15,7 @@ include $(top_builddir)/src/Makefile.global ...@@ -15,6 +15,7 @@ include $(top_builddir)/src/Makefile.global
SUBDIRS = \ SUBDIRS = \
initdb \ initdb \
pg_amcheck \
pg_archivecleanup \ pg_archivecleanup \
pg_basebackup \ pg_basebackup \
pg_checksums \ pg_checksums \
......
pg_amcheck
/tmp_check/
#-------------------------------------------------------------------------
#
# Makefile for src/bin/pg_amcheck
#
# Portions Copyright (c) 1996-2021, PostgreSQL Global Development Group
# Portions Copyright (c) 1994, Regents of the University of California
#
# src/bin/pg_amcheck/Makefile
#
#-------------------------------------------------------------------------
PGFILEDESC = "pg_amcheck - detect corruption within database relations"
PGAPPICON=win32
EXTRA_INSTALL=contrib/amcheck contrib/pageinspect
subdir = src/bin/pg_amcheck
top_builddir = ../../..
include $(top_builddir)/src/Makefile.global
override CPPFLAGS := -I$(libpq_srcdir) $(CPPFLAGS)
LDFLAGS_INTERNAL += -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport)
OBJS = \
$(WIN32RES) \
pg_amcheck.o
all: pg_amcheck
pg_amcheck: $(OBJS) | submake-libpq submake-libpgport submake-libpgfeutils
$(CC) $(CFLAGS) $^ $(LDFLAGS) $(LDFLAGS_EX) $(LIBS) -o $@$(X)
install: all installdirs
$(INSTALL_PROGRAM) pg_amcheck$(X) '$(DESTDIR)$(bindir)/pg_amcheck$(X)'
installdirs:
$(MKDIR_P) '$(DESTDIR)$(bindir)'
uninstall:
rm -f '$(DESTDIR)$(bindir)/pg_amcheck$(X)'
clean distclean maintainer-clean:
rm -f pg_amcheck$(X) $(OBJS)
rm -rf tmp_check
check:
$(prove_check)
installcheck:
$(prove_installcheck)
This diff is collapsed.
use strict;
use warnings;
use TestLib;
use Test::More tests => 8;
program_help_ok('pg_amcheck');
program_version_ok('pg_amcheck');
program_options_handling_ok('pg_amcheck');
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 76;
# Test set-up
my ($node, $port);
$node = get_new_node('test');
$node->init;
$node->start;
$port = $node->port;
# Load the amcheck extension, upon which pg_amcheck depends
$node->safe_psql('postgres', q(CREATE EXTENSION amcheck));
#########################################
# Test non-existent databases
# Failing to connect to the initial database is an error.
$node->command_checks_all(
[ 'pg_amcheck', 'qqq' ],
1,
[ qr/^$/ ],
[ qr/FATAL: database "qqq" does not exist/ ],
'checking a non-existent database');
# Failing to resolve a database pattern is an error by default.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'qqq', '-d', 'postgres' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no connectable databases to check matching "qqq"/ ],
'checking an unresolvable database pattern');
# But only a warning under --no-strict-names
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-d', 'qqq', '-d', 'postgres' ],
0,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: no connectable databases to check matching "qqq"/ ],
'checking an unresolvable database pattern under --no-strict-names');
# Check that a substring of an existent database name does not get interpreted
# as a matching pattern.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'post', '-d', 'postgres' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no connectable databases to check matching "post"/ ],
'checking an unresolvable database pattern (substring of existent database)');
# Check that a superstring of an existent database name does not get interpreted
# as a matching pattern.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgresql', '-d', 'postgres' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no connectable databases to check matching "postgresql"/ ],
'checking an unresolvable database pattern (superstring of existent database)');
#########################################
# Test connecting with a non-existent user
# Failing to connect to the initial database due to bad username is an error.
$node->command_checks_all(
[ 'pg_amcheck', '-U', 'no_such_user', 'postgres' ],
1,
[ qr/^$/ ],
[ qr/role "no_such_user" does not exist/ ],
'checking with a non-existent user');
# Failing to connect to the initial database due to bad username is an still an
# error under --no-strict-names.
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names', '-U', 'no_such_user', 'postgres' ],
1,
[ qr/^$/ ],
[ qr/role "no_such_user" does not exist/ ],
'checking with a non-existent user under --no-strict-names');
#########################################
# Test checking databases without amcheck installed
# Attempting to check a database by name where amcheck is not installed should
# raise a warning. If all databases are skipped, having no relations to check
# raises an error.
$node->command_checks_all(
[ 'pg_amcheck', 'template1' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/ ],
'checking a database by name without amcheck installed, no other databases');
# Again, but this time with another database to check, so no error is raised.
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'template1', '-d', 'postgres' ],
0,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/ ],
'checking a database by name without amcheck installed, with other databases');
# Again, but by way of checking all databases
$node->command_checks_all(
[ 'pg_amcheck', '--all' ],
0,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/ ],
'checking a database by pattern without amcheck installed, with other databases');
#########################################
# Test unreasonable patterns
# Check three-part unreasonable pattern that has zero-length names
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '..' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no connectable databases to check matching "\.\."/ ],
'checking table pattern ".."');
# Again, but with non-trivial schema and relation parts
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '.foo.bar' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no connectable databases to check matching "\.foo\.bar"/ ],
'checking table pattern ".foo.bar"');
# Check two-part unreasonable pattern that has zero-length names
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '-t', '.' ],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: error: no heap tables to check matching "\."/ ],
'checking table pattern "."');
#########################################
# Test checking non-existent databases, schemas, tables, and indexes
# Use --no-strict-names and a single existent table so we only get warnings
# about the failed pattern matches
$node->command_checks_all(
[ 'pg_amcheck', '--no-strict-names',
'-t', 'no_such_table',
'-t', 'no*such*table',
'-i', 'no_such_index',
'-i', 'no*such*index',
'-r', 'no_such_relation',
'-r', 'no*such*relation',
'-d', 'no_such_database',
'-d', 'no*such*database',
'-r', 'none.none',
'-r', 'none.none.none',
'-r', 'this.is.a.really.long.dotted.string',
'-r', 'postgres.none.none',
'-r', 'postgres.long.dotted.string',
'-r', 'postgres.pg_catalog.none',
'-r', 'postgres.none.pg_class',
'-t', 'postgres.pg_catalog.pg_class', # This exists
],
0,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: no heap tables to check matching "no_such_table"/,
qr/pg_amcheck: warning: no heap tables to check matching "no\*such\*table"/,
qr/pg_amcheck: warning: no btree indexes to check matching "no_such_index"/,
qr/pg_amcheck: warning: no btree indexes to check matching "no\*such\*index"/,
qr/pg_amcheck: warning: no relations to check matching "no_such_relation"/,
qr/pg_amcheck: warning: no relations to check matching "no\*such\*relation"/,
qr/pg_amcheck: warning: no heap tables to check matching "no\*such\*table"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no\*such\*database"/,
qr/pg_amcheck: warning: no relations to check matching "none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "none\.none\.none"/,
qr/pg_amcheck: warning: no connectable databases to check matching "this\.is\.a\.really\.long\.dotted\.string"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.long\.dotted\.string"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.pg_catalog\.none"/,
qr/pg_amcheck: warning: no relations to check matching "postgres\.none\.pg_class"/,
],
'many unmatched patterns and one matched pattern under --no-strict-names');
#########################################
# Test checking otherwise existent objects but in databases where they do not exist
$node->safe_psql('postgres', q(
CREATE TABLE public.foo (f integer);
CREATE INDEX foo_idx ON foo(f);
));
$node->safe_psql('postgres', q(CREATE DATABASE another_db));
$node->command_checks_all(
[ 'pg_amcheck', '-d', 'postgres', '--no-strict-names',
'-t', 'template1.public.foo',
'-t', 'another_db.public.foo',
'-t', 'no_such_database.public.foo',
'-i', 'template1.public.foo_idx',
'-i', 'another_db.public.foo_idx',
'-i', 'no_such_database.public.foo_idx',
],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: warning: no heap tables to check matching "template1\.public\.foo"/,
qr/pg_amcheck: warning: no heap tables to check matching "another_db\.public\.foo"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database\.public\.foo"/,
qr/pg_amcheck: warning: no btree indexes to check matching "template1\.public\.foo_idx"/,
qr/pg_amcheck: warning: no btree indexes to check matching "another_db\.public\.foo_idx"/,
qr/pg_amcheck: warning: no connectable databases to check matching "no_such_database\.public\.foo_idx"/,
qr/pg_amcheck: error: no relations to check/,
],
'checking otherwise existent objets in the wrong databases');
#########################################
# Test schema exclusion patterns
# Check with only schema exclusion patterns
$node->command_checks_all(
[ 'pg_amcheck', '--all', '--no-strict-names',
'-S', 'public',
'-S', 'pg_catalog',
'-S', 'pg_toast',
'-S', 'information_schema',
],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/ ],
'schema exclusion patterns exclude all relations');
# Check with schema exclusion patterns overriding relation and schema inclusion patterns
$node->command_checks_all(
[ 'pg_amcheck', '--all', '--no-strict-names',
'-s', 'public',
'-s', 'pg_catalog',
'-s', 'pg_toast',
'-s', 'information_schema',
'-t', 'pg_catalog.pg_class',
'-S', '*'
],
1,
[ qr/^$/ ],
[ qr/pg_amcheck: warning: skipping database "template1": amcheck is not installed/,
qr/pg_amcheck: error: no relations to check/ ],
'schema exclusion pattern overrides all inclusion patterns');
This diff is collapsed.
This diff is collapsed.
# This regression test checks the behavior of the btree validation in the
# presence of breaking sort order changes.
#
use strict;
use warnings;
use PostgresNode;
use TestLib;
use Test::More tests => 5;
my $node = get_new_node('test');
$node->init;
$node->start;
# Create a custom operator class and an index which uses it.
$node->safe_psql('postgres', q(
CREATE EXTENSION amcheck;
CREATE FUNCTION int4_asc_cmp (a int4, b int4) RETURNS int LANGUAGE sql AS $$
SELECT CASE WHEN $1 = $2 THEN 0 WHEN $1 > $2 THEN 1 ELSE -1 END; $$;
CREATE OPERATOR CLASS int4_fickle_ops FOR TYPE int4 USING btree AS
OPERATOR 1 < (int4, int4), OPERATOR 2 <= (int4, int4),
OPERATOR 3 = (int4, int4), OPERATOR 4 >= (int4, int4),
OPERATOR 5 > (int4, int4), FUNCTION 1 int4_asc_cmp(int4, int4);
CREATE TABLE int4tbl (i int4);
INSERT INTO int4tbl (SELECT * FROM generate_series(1,1000) gs);
CREATE INDEX fickleidx ON int4tbl USING btree (i int4_fickle_ops);
));
# We have not yet broken the index, so we should get no corruption
$node->command_like(
[ 'pg_amcheck', '--quiet', '-p', $node->port, 'postgres' ],
qr/^$/,
'pg_amcheck all schemas, tables and indexes reports no corruption');
# Change the operator class to use a function which sorts in a different
# order to corrupt the btree index
$node->safe_psql('postgres', q(
CREATE FUNCTION int4_desc_cmp (int4, int4) RETURNS int LANGUAGE sql AS $$
SELECT CASE WHEN $1 = $2 THEN 0 WHEN $1 > $2 THEN -1 ELSE 1 END; $$;
UPDATE pg_catalog.pg_amproc
SET amproc = 'int4_desc_cmp'::regproc
WHERE amproc = 'int4_asc_cmp'::regproc
));
# Index corruption should now be reported
$node->command_checks_all(
[ 'pg_amcheck', '-p', $node->port, 'postgres' ],
2,
[ qr/item order invariant violated for index "fickleidx"/ ],
[ ],
'pg_amcheck all schemas, tables and indexes reports fickleidx corruption'
);
...@@ -20,12 +20,12 @@ our (@ISA, @EXPORT_OK); ...@@ -20,12 +20,12 @@ our (@ISA, @EXPORT_OK);
my $insttype; my $insttype;
my @client_contribs = ('oid2name', 'pgbench', 'vacuumlo'); my @client_contribs = ('oid2name', 'pgbench', 'vacuumlo');
my @client_program_files = ( my @client_program_files = (
'clusterdb', 'createdb', 'createuser', 'dropdb', 'clusterdb', 'createdb', 'createuser', 'dropdb',
'dropuser', 'ecpg', 'libecpg', 'libecpg_compat', 'dropuser', 'ecpg', 'libecpg', 'libecpg_compat',
'libpgtypes', 'libpq', 'pg_basebackup', 'pg_config', 'libpgtypes', 'libpq', 'pg_amcheck', 'pg_basebackup',
'pg_dump', 'pg_dumpall', 'pg_isready', 'pg_receivewal', 'pg_config', 'pg_dump', 'pg_dumpall', 'pg_isready',
'pg_recvlogical', 'pg_restore', 'psql', 'reindexdb', 'pg_receivewal', 'pg_recvlogical', 'pg_restore', 'psql',
'vacuumdb', @client_contribs); 'reindexdb', 'vacuumdb', @client_contribs);
sub lcopy sub lcopy
{ {
......
...@@ -54,17 +54,18 @@ my @contrib_excludes = ( ...@@ -54,17 +54,18 @@ my @contrib_excludes = (
# Set of variables for frontend modules # Set of variables for frontend modules
my $frontend_defines = { 'initdb' => 'FRONTEND' }; my $frontend_defines = { 'initdb' => 'FRONTEND' };
my @frontend_uselibpq = ('pg_ctl', 'pg_upgrade', 'pgbench', 'psql', 'initdb'); my @frontend_uselibpq = ('pg_amcheck', 'pg_ctl', 'pg_upgrade', 'pgbench', 'psql', 'initdb');
my @frontend_uselibpgport = ( my @frontend_uselibpgport = (
'pg_archivecleanup', 'pg_test_fsync', 'pg_amcheck', 'pg_archivecleanup', 'pg_test_fsync',
'pg_test_timing', 'pg_upgrade', 'pg_test_timing', 'pg_upgrade',
'pg_waldump', 'pgbench'); 'pg_waldump', 'pgbench');
my @frontend_uselibpgcommon = ( my @frontend_uselibpgcommon = (
'pg_archivecleanup', 'pg_test_fsync', 'pg_amcheck', 'pg_archivecleanup', 'pg_test_fsync',
'pg_test_timing', 'pg_upgrade', 'pg_test_timing', 'pg_upgrade',
'pg_waldump', 'pgbench'); 'pg_waldump', 'pgbench');
my $frontend_extralibs = { my $frontend_extralibs = {
'initdb' => ['ws2_32.lib'], 'initdb' => ['ws2_32.lib'],
'pg_amcheck' => ['ws2_32.lib'],
'pg_restore' => ['ws2_32.lib'], 'pg_restore' => ['ws2_32.lib'],
'pgbench' => ['ws2_32.lib'], 'pgbench' => ['ws2_32.lib'],
'psql' => ['ws2_32.lib'] 'psql' => ['ws2_32.lib']
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment