v39-0001-Adding-contrib-module-pg_amcheck.patch

application/octet-stream

Filename: v39-0001-Adding-contrib-module-pg_amcheck.patch
Type: application/octet-stream
Part: 0
Message: Re: new heapcheck contrib module

Patch

Format: format-patch
Series: patch v39-0001
Subject: Adding contrib module pg_amcheck
File+
contrib/Makefile 1 0
contrib/pg_amcheck/.gitignore 3 0
contrib/pg_amcheck/Makefile 29 0
contrib/pg_amcheck/pg_amcheck.c 1822 0
contrib/pg_amcheck/t/001_basic.pl 9 0
contrib/pg_amcheck/t/002_nonesuch.pl 228 0
contrib/pg_amcheck/t/003_check.pl 481 0
contrib/pg_amcheck/t/004_verify_heapam.pl 496 0
contrib/pg_amcheck/t/005_opclass_damage.pl 54 0
doc/src/sgml/contrib.sgml 1 0
doc/src/sgml/filelist.sgml 1 0
doc/src/sgml/pgamcheck.sgml 1029 0
src/tools/msvc/Install.pm 1 1
src/tools/msvc/Mkvcbuild.pm 3 3
src/tools/pgindent/typedefs.list 4 0
From 45badbf39001cc60c855864e6531807818eda6f5 Mon Sep 17 00:00:00 2001
From: Mark Dilger <mark.dilger@enterprisedb.com>
Date: Tue, 16 Feb 2021 13:58:40 -0800
Subject: [PATCH v39 1/2] Adding contrib module pg_amcheck

Adding new contrib module pg_amcheck, which is a command line
interface for running amcheck's verifications against tables and
indexes.
---
 contrib/Makefile                           |    1 +
 contrib/pg_amcheck/.gitignore              |    3 +
 contrib/pg_amcheck/Makefile                |   29 +
 contrib/pg_amcheck/pg_amcheck.c            | 1822 ++++++++++++++++++++
 contrib/pg_amcheck/t/001_basic.pl          |    9 +
 contrib/pg_amcheck/t/002_nonesuch.pl       |  228 +++
 contrib/pg_amcheck/t/003_check.pl          |  481 ++++++
 contrib/pg_amcheck/t/004_verify_heapam.pl  |  496 ++++++
 contrib/pg_amcheck/t/005_opclass_damage.pl |   54 +
 doc/src/sgml/contrib.sgml                  |    1 +
 doc/src/sgml/filelist.sgml                 |    1 +
 doc/src/sgml/pgamcheck.sgml                | 1029 +++++++++++
 src/tools/msvc/Install.pm                  |    2 +-
 src/tools/msvc/Mkvcbuild.pm                |    6 +-
 src/tools/pgindent/typedefs.list           |    4 +
 15 files changed, 4162 insertions(+), 4 deletions(-)
 create mode 100644 contrib/pg_amcheck/.gitignore
 create mode 100644 contrib/pg_amcheck/Makefile
 create mode 100644 contrib/pg_amcheck/pg_amcheck.c
 create mode 100644 contrib/pg_amcheck/t/001_basic.pl
 create mode 100644 contrib/pg_amcheck/t/002_nonesuch.pl
 create mode 100644 contrib/pg_amcheck/t/003_check.pl
 create mode 100644 contrib/pg_amcheck/t/004_verify_heapam.pl
 create mode 100644 contrib/pg_amcheck/t/005_opclass_damage.pl
 create mode 100644 doc/src/sgml/pgamcheck.sgml

diff --git a/contrib/Makefile b/contrib/Makefile
index f27e458482..a72dcf7304 100644
--- a/contrib/Makefile
+++ b/contrib/Makefile
@@ -30,6 +30,7 @@ SUBDIRS = \
 		old_snapshot	\
 		pageinspect	\
 		passwordcheck	\
+		pg_amcheck	\
 		pg_buffercache	\
 		pg_freespacemap \
 		pg_prewarm	\
diff --git a/contrib/pg_amcheck/.gitignore b/contrib/pg_amcheck/.gitignore
new file mode 100644
index 0000000000..c21a14de31
--- /dev/null
+++ b/contrib/pg_amcheck/.gitignore
@@ -0,0 +1,3 @@
+pg_amcheck
+
+/tmp_check/
diff --git a/contrib/pg_amcheck/Makefile b/contrib/pg_amcheck/Makefile
new file mode 100644
index 0000000000..bc61ee7970
--- /dev/null
+++ b/contrib/pg_amcheck/Makefile
@@ -0,0 +1,29 @@
+# contrib/pg_amcheck/Makefile
+
+PGFILEDESC = "pg_amcheck - detects corruption within database relations"
+PGAPPICON = win32
+
+PROGRAM = pg_amcheck
+OBJS = \
+	$(WIN32RES) \
+	pg_amcheck.o
+
+REGRESS_OPTS += --load-extension=amcheck --load-extension=pageinspect
+EXTRA_INSTALL += contrib/amcheck contrib/pageinspect
+
+TAP_TESTS = 1
+
+PG_CPPFLAGS = -I$(libpq_srcdir)
+PG_LIBS_INTERNAL = -L$(top_builddir)/src/fe_utils -lpgfeutils $(libpq_pgport)
+
+ifdef USE_PGXS
+PG_CONFIG = pg_config
+PGXS := $(shell $(PG_CONFIG) --pgxs)
+include $(PGXS)
+else
+SHLIB_PREREQS = submake-libpq
+subdir = contrib/pg_amcheck
+top_builddir = ../..
+include $(top_builddir)/src/Makefile.global
+include $(top_srcdir)/contrib/contrib-global.mk
+endif
diff --git a/contrib/pg_amcheck/pg_amcheck.c b/contrib/pg_amcheck/pg_amcheck.c
new file mode 100644
index 0000000000..ad75bafa39
--- /dev/null
+++ b/contrib/pg_amcheck/pg_amcheck.c
@@ -0,0 +1,1822 @@
+/*-------------------------------------------------------------------------
+ *
+ * pg_amcheck.c
+ *		Detects corruption within database relations.
+ *
+ * Copyright (c) 2017-2021, PostgreSQL Global Development Group
+ *
+ * IDENTIFICATION
+ *	  contrib/pg_amcheck/pg_amcheck.c
+ *
+ *-------------------------------------------------------------------------
+ */
+#include "postgres_fe.h"
+
+#include "catalog/pg_am_d.h"
+#include "catalog/pg_namespace_d.h"
+#include "common/logging.h"
+#include "common/username.h"
+#include "fe_utils/cancel.h"
+#include "fe_utils/option_utils.h"
+#include "fe_utils/parallel_slot.h"
+#include "fe_utils/query_utils.h"
+#include "fe_utils/simple_list.h"
+#include "fe_utils/string_utils.h"
+#include "getopt_long.h"		/* pgrminclude ignore */
+#include "storage/block.h"
+
+/* pg_amcheck command line options controlled by user flags */
+typedef struct amcheckOptions
+{
+	bool		alldb;
+	bool		allrel;
+	bool		excludetbl;
+	bool		excludeidx;
+	bool		echo;
+	bool		quiet;
+	bool		verbose;
+	bool		no_dependents;
+	bool		no_indexes;
+	bool		no_tables;
+	bool		no_toast;
+	bool		reconcile_toast;
+	bool		on_error_stop;
+	bool		parent_check;
+	bool		rootdescend;
+	bool		heapallindexed;
+	bool		strict_names;
+	bool		show_progress;
+	const char *skip;
+	int			jobs;
+	long		startblock;
+	long		endblock;
+	SimplePtrList include;		/* list of PatternInfo structs */
+	SimplePtrList exclude;		/* list of PatternInfo structs */
+} amcheckOptions;
+
+static amcheckOptions opts = {
+	.alldb = false,
+	.allrel = true,
+	.excludetbl = false,
+	.excludeidx = false,
+	.echo = false,
+	.quiet = false,
+	.verbose = false,
+	.no_dependents = false,
+	.no_indexes = false,
+	.no_tables = false,
+	.on_error_stop = false,
+	.parent_check = false,
+	.rootdescend = false,
+	.heapallindexed = false,
+	.no_toast = false,
+	.reconcile_toast = true,
+	.strict_names = true,
+	.show_progress = false,
+	.skip = "none",
+	.jobs = 1,
+	.startblock = -1,
+	.endblock = -1,
+	.include = {NULL, NULL},
+	.exclude = {NULL, NULL},
+};
+
+static const char *progname = NULL;
+
+typedef struct PatternInfo
+{
+	int			pattern_id;		/* Unique ID of this pattern */
+	const char *pattern;		/* Unaltered pattern from the command line */
+	char	   *dbrgx;			/* Database regexp parsed from pattern, or
+								 * NULL */
+	char	   *nsprgx;			/* Schema regexp parsed from pattern, or NULL */
+	char	   *relrgx;			/* Relation regexp parsed from pattern, or
+								 * NULL */
+	bool		tblonly;		/* true if relrgx should only match tables */
+	bool		idxonly;		/* true if relrgx should only match indexes */
+	bool		matched;		/* true if the pattern matched in any database */
+}			PatternInfo;
+
+/* Unique pattern id counter */
+static int	next_id = 1;
+
+typedef struct DatabaseInfo
+{
+	char	   *datname;
+	char	   *amcheck_schema; /* escaped, quoted literal */
+} DatabaseInfo;
+
+typedef struct RelationInfo
+{
+	const DatabaseInfo *datinfo;	/* shared by other relinfos */
+	Oid			reloid;
+	bool		is_table;		/* true if heap, false if btree */
+} RelationInfo;
+
+/*
+ * Query for determining if contrib's amcheck is installed.  If so, selects the
+ * namespace name where amcheck's functions can be found.
+ */
+static const char *amcheck_sql =
+"SELECT n.nspname, x.extversion"
+"\nFROM pg_catalog.pg_extension x"
+"\nJOIN pg_catalog.pg_namespace n"
+"\nON x.extnamespace OPERATOR(pg_catalog.=) n.oid"
+"\nWHERE x.extname OPERATOR(pg_catalog.=) 'amcheck'";
+
+static void prepare_table_command(PQExpBuffer sql, Oid reloid,
+								  const char *nspname);
+static void prepare_btree_command(PQExpBuffer sql, Oid reloid,
+								  const char *nspname);
+static void run_command(ParallelSlot *slot, const char *sql,
+						ConnParams *cparams);
+static bool VerifyHeapamSlotHandler(PGresult *res, PGconn *conn,
+									void *context);
+static bool VerifyBtreeSlotHandler(PGresult *res, PGconn *conn, void *context);
+static void help(const char *progname);
+static void appendDatabasePattern(SimplePtrList *list, const char *pattern,
+								  int encoding);
+static void appendSchemaPattern(SimplePtrList *list, const char *pattern,
+								int encoding);
+static void appendRelationPattern(SimplePtrList *list, const char *pattern,
+								  int encoding);
+static void appendTablePattern(SimplePtrList *list, const char *pattern,
+							   int encoding);
+static void appendIndexPattern(SimplePtrList *list, const char *pattern,
+							   int encoding);
+static void compileDatabaseList(PGconn *conn, SimplePtrList *databases);
+static void compileRelationListOneDb(PGconn *conn, SimplePtrList *relations,
+									 const DatabaseInfo *datinfo);
+
+int
+main(int argc, char *argv[])
+{
+	PGconn	   *conn;
+	SimplePtrListCell *cell;
+	SimplePtrList databases = {NULL, NULL};
+	SimplePtrList relations = {NULL, NULL};
+	bool		failed;
+	const char *prev_datname;
+	int			parallel_workers;
+	ParallelSlot *slots;
+	PQExpBufferData sql;
+	long long int reltotal;
+	long long int relprogress;
+
+	static struct option long_options[] = {
+		/* Connection options */
+		{"host", required_argument, NULL, 'h'},
+		{"port", required_argument, NULL, 'p'},
+		{"username", required_argument, NULL, 'U'},
+		{"no-password", no_argument, NULL, 'w'},
+		{"password", no_argument, NULL, 'W'},
+		{"maintenance-db", required_argument, NULL, 1},
+
+		/* check options */
+		{"all", no_argument, NULL, 'a'},
+		{"dbname", required_argument, NULL, 'd'},
+		{"exclude-dbname", required_argument, NULL, 'D'},
+		{"echo", no_argument, NULL, 'e'},
+		{"heapallindexed", no_argument, NULL, 'H'},
+		{"index", required_argument, NULL, 'i'},
+		{"exclude-index", required_argument, NULL, 'I'},
+		{"jobs", required_argument, NULL, 'j'},
+		{"parent-check", no_argument, NULL, 'P'},
+		{"quiet", no_argument, NULL, 'q'},
+		{"relation", required_argument, NULL, 'r'},
+		{"exclude-relation", required_argument, NULL, 'R'},
+		{"schema", required_argument, NULL, 's'},
+		{"exclude-schema", required_argument, NULL, 'S'},
+		{"table", required_argument, NULL, 't'},
+		{"exclude-table", required_argument, NULL, 'T'},
+		{"verbose", no_argument, NULL, 'v'},
+		{"exclude-indexes", no_argument, NULL, 2},
+		{"exclude-tables", no_argument, NULL, 3},
+		{"exclude-toast", no_argument, NULL, 4},
+		{"exclude-toast-pointers", no_argument, NULL, 5},
+		{"on-error-stop", no_argument, NULL, 6},
+		{"skip", required_argument, NULL, 7},
+		{"startblock", required_argument, NULL, 8},
+		{"endblock", required_argument, NULL, 9},
+		{"rootdescend", no_argument, NULL, 10},
+		{"no-dependents", no_argument, NULL, 11},
+		{"no-strict-names", no_argument, NULL, 12},
+		{"progress", no_argument, NULL, 13},
+
+		{NULL, 0, NULL, 0}
+	};
+
+	int			optindex;
+	int			c;
+
+	/*
+	 * If a maintenance database is specified, that will be used for the
+	 * initial connection.  Failing that, the first plain argument (without a
+	 * flag) will be used.  If neither of those are given, the first database
+	 * specified with -d.
+	 */
+	const char *primary_db = NULL;
+	const char *secondary_db = NULL;
+	const char *tertiary_db = NULL;
+
+	const char *host = NULL;
+	const char *port = NULL;
+	const char *username = NULL;
+	enum trivalue prompt_password = TRI_DEFAULT;
+	int			encoding = pg_get_encoding_from_locale(NULL, false);
+	ConnParams	cparams;
+
+	pg_logging_init(argv[0]);
+	progname = get_progname(argv[0]);
+	set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("contrib"));
+
+	handle_help_version_opts(argc, argv, progname, help);
+
+	/* process command-line options */
+	while ((c = getopt_long(argc, argv, "ad:D:eh:Hi:I:j:p:Pqr:R:s:S:t:T:U:wWv",
+							long_options, &optindex)) != -1)
+	{
+		char	   *endptr;
+
+		switch (c)
+		{
+			case 'a':
+				opts.alldb = true;
+				break;
+			case 'd':
+				if (tertiary_db == NULL)
+					tertiary_db = optarg;
+				appendDatabasePattern(&opts.include, optarg, encoding);
+				break;
+			case 'D':
+				appendDatabasePattern(&opts.exclude, optarg, encoding);
+				break;
+			case 'e':
+				opts.echo = true;
+				break;
+			case 'h':
+				host = pg_strdup(optarg);
+				break;
+			case 'H':
+				opts.heapallindexed = true;
+				break;
+			case 'i':
+				opts.allrel = false;
+				appendIndexPattern(&opts.include, optarg, encoding);
+				break;
+			case 'I':
+				opts.excludeidx = true;
+				appendIndexPattern(&opts.exclude, optarg, encoding);
+				break;
+			case 'j':
+				opts.jobs = atoi(optarg);
+				if (opts.jobs < 1)
+				{
+					pg_log_error("number of parallel jobs must be at least 1");
+					exit(1);
+				}
+				break;
+			case 'p':
+				port = pg_strdup(optarg);
+				break;
+			case 'P':
+				opts.parent_check = true;
+				break;
+			case 'q':
+				opts.quiet = true;
+				break;
+			case 'r':
+				opts.allrel = false;
+				appendRelationPattern(&opts.include, optarg, encoding);
+				break;
+			case 'R':
+				opts.excludeidx = true;
+				opts.excludetbl = true;
+				appendRelationPattern(&opts.exclude, optarg, encoding);
+				break;
+			case 's':
+				opts.allrel = false;
+				appendSchemaPattern(&opts.include, optarg, encoding);
+				break;
+			case 'S':
+				appendSchemaPattern(&opts.exclude, optarg, encoding);
+				break;
+			case 't':
+				opts.allrel = false;
+				appendTablePattern(&opts.include, optarg, encoding);
+				break;
+			case 'T':
+				opts.excludetbl = true;
+				appendTablePattern(&opts.exclude, optarg, encoding);
+				break;
+			case 'U':
+				username = pg_strdup(optarg);
+				break;
+			case 'w':
+				prompt_password = TRI_NO;
+				break;
+			case 'W':
+				prompt_password = TRI_YES;
+				break;
+			case 'v':
+				opts.verbose = true;
+				pg_logging_increase_verbosity();
+				break;
+			case 1:
+				primary_db = pg_strdup(optarg);
+				break;
+			case 2:
+				opts.no_indexes = true;
+				break;
+			case 3:
+				opts.no_tables = true;
+				break;
+			case 4:
+				opts.no_toast = true;
+				break;
+			case 5:
+				opts.reconcile_toast = false;
+				break;
+			case 6:
+				opts.on_error_stop = true;
+				break;
+			case 7:
+				if (pg_strcasecmp(optarg, "all-visible") == 0)
+					opts.skip = "all visible";
+				else if (pg_strcasecmp(optarg, "all-frozen") == 0)
+					opts.skip = "all frozen";
+				else
+				{
+					fprintf(stderr, "invalid skip options");
+					exit(1);
+				}
+				break;
+			case 8:
+				opts.startblock = strtol(optarg, &endptr, 10);
+				if (*endptr != '\0')
+				{
+					fprintf(stderr,
+							"relation starting block argument contains garbage characters");
+					exit(1);
+				}
+				if (opts.startblock > (long) MaxBlockNumber)
+				{
+					fprintf(stderr,
+							"relation starting block argument out of bounds");
+					exit(1);
+				}
+				break;
+			case 9:
+				opts.endblock = strtol(optarg, &endptr, 10);
+				if (*endptr != '\0')
+				{
+					fprintf(stderr,
+							"relation ending block argument contains garbage characters");
+					exit(1);
+				}
+				if (opts.startblock > (long) MaxBlockNumber)
+				{
+					fprintf(stderr,
+							"relation ending block argument out of bounds");
+					exit(1);
+				}
+				break;
+			case 10:
+				opts.rootdescend = true;
+				opts.parent_check = true;
+				break;
+			case 11:
+				opts.no_dependents = true;
+				break;
+			case 12:
+				opts.strict_names = false;
+				break;
+			case 13:
+				opts.show_progress = true;
+				break;
+			default:
+				fprintf(stderr,
+						"Try \"%s --help\" for more information.\n",
+						progname);
+				exit(1);
+		}
+	}
+
+	if (opts.endblock >= 0 && opts.endblock < opts.startblock)
+	{
+		pg_log_error("relation ending block argument precedes starting block argument");
+		exit(1);
+	}
+
+	/* non-option arguments specify database names */
+	while (optind < argc)
+	{
+		if (secondary_db == NULL)
+			secondary_db = argv[optind];
+		appendDatabasePattern(&opts.include, argv[optind], encoding);
+		optind++;
+	}
+
+	/* fill cparams except for dbname, which is set below */
+	cparams.pghost = host;
+	cparams.pgport = port;
+	cparams.pguser = username;
+	cparams.prompt_password = prompt_password;
+	cparams.override_dbname = NULL;
+
+	setup_cancel_handler(NULL);
+
+	/* choose the database for our initial connection */
+	if (primary_db)
+		cparams.dbname = primary_db;
+	else if (secondary_db != NULL)
+		cparams.dbname = secondary_db;
+	else if (tertiary_db != NULL)
+		cparams.dbname = tertiary_db;
+	else
+	{
+		const char *default_db;
+
+		if (getenv("PGDATABASE"))
+			default_db = getenv("PGDATABASE");
+		else if (getenv("PGUSER"))
+			default_db = getenv("PGUSER");
+		else
+			default_db = get_user_name_or_exit(progname);
+
+		/*
+		 * Users expect the database name inferred from the environment to get
+		 * checked, not just get used for the initial connection.
+		 */
+		appendDatabasePattern(&opts.include, default_db, encoding);
+
+		cparams.dbname = default_db;
+	}
+
+	conn = connectMaintenanceDatabase(&cparams, progname, opts.echo);
+	compileDatabaseList(conn, &databases);
+	disconnectDatabase(conn);
+
+	if (databases.head == NULL)
+	{
+		fprintf(stderr, "%s: no databases to check\n", progname);
+		exit(0);
+	}
+
+	/*
+	 * Compile a list of all relations spanning all databases to be checked.
+	 */
+	for (cell = databases.head; cell; cell = cell->next)
+	{
+		PGresult   *result;
+		int			ntups;
+		const char *amcheck_schema;
+		DatabaseInfo *dat = (DatabaseInfo *) cell->ptr;
+
+		cparams.override_dbname = dat->datname;
+
+		/*
+		 * Test that this function works, but for now we're not using the list
+		 * 'relations' that it builds.
+		 */
+		conn = connectDatabase(&cparams, progname, opts.echo, false, true);
+
+		/*
+		 * Verify that amcheck is installed for this next database.  User
+		 * error could result in a database not having amcheck that should
+		 * have it, but we also could be iterating over multiple databases
+		 * where not all of them have amcheck installed (for example,
+		 * 'template1').
+		 */
+		result = executeQuery(conn, amcheck_sql, opts.echo);
+		if (PQresultStatus(result) != PGRES_TUPLES_OK)
+		{
+			/* Querying the catalog failed. */
+			pg_log_error("database \"%s\": %s\n",
+						 PQdb(conn), PQerrorMessage(conn));
+			pg_log_error("query was: %s", amcheck_sql);
+			PQclear(result);
+			disconnectDatabase(conn);
+			continue;
+		}
+		ntups = PQntuples(result);
+		if (ntups == 0)
+		{
+			/* Querying the catalog succeeded, but amcheck is missing. */
+			fprintf(stderr,
+					"%s: skipping database \"%s\": amcheck is not installed\n",
+					progname, PQdb(conn));
+			disconnectDatabase(conn);
+			continue;
+		}
+		amcheck_schema = PQgetvalue(result, 0, 0);
+		if (opts.verbose)
+			fprintf(stderr,
+					"%s: in database \"%s\": using amcheck version \"%s\" in schema \"%s\"\n",
+					progname, PQdb(conn), PQgetvalue(result, 0, 1),
+					amcheck_schema);
+		dat->amcheck_schema = PQescapeIdentifier(conn, amcheck_schema,
+												 strlen(amcheck_schema));
+		PQclear(result);
+
+		compileRelationListOneDb(conn, &relations, dat);
+		disconnectDatabase(conn);
+	}
+
+	/*
+	 * Check that all inclusion patterns matched at least one schema or
+	 * relation that we can check.
+	 */
+	for (failed = false, cell = opts.include.head; cell; cell = cell->next)
+	{
+		PatternInfo *pat = (PatternInfo *) cell->ptr;
+
+		if (!pat->matched && (pat->nsprgx != NULL || pat->relrgx != NULL))
+		{
+			failed = opts.strict_names;
+
+			if (!opts.quiet)
+			{
+				if (pat->tblonly)
+					fprintf(stderr, "%s: no tables to check for \"%s\"\n",
+							progname, pat->pattern);
+				else if (pat->idxonly)
+					fprintf(stderr, "%s: no btree indexes to check for \"%s\"\n",
+							progname, pat->pattern);
+				else if (pat->relrgx == NULL)
+					fprintf(stderr, "%s: no relations to check in schemas for \"%s\"\n",
+							progname, pat->pattern);
+				else
+					fprintf(stderr, "%s: no relations to check for \"%s\"\n",
+							progname, pat->pattern);
+			}
+		}
+	}
+
+	if (failed)
+		exit(1);
+
+	/*
+	 * We cannot use more workers than the user specified with --jobs, but if
+	 * that value exceeds the number of relations, using that many needlessly
+	 * opens extra unused database connections.
+	 *
+	 * Set parallel_workers to the lesser of opts.jobs and the number of
+	 * relations.
+	 */
+	for (cell = relations.head, parallel_workers = 0;
+		 cell != NULL && parallel_workers < opts.jobs;
+		 cell = cell->next, parallel_workers++)
+		;
+
+	if (opts.show_progress)
+	{
+		/* Count the total number of relations */
+		reltotal = 0;
+		for (cell = relations.head; cell; cell = cell->next)
+			reltotal++;
+	}
+
+	/*
+	 * ParallelSlots based vent loop follows.
+	 *
+	 * We use server-side parallelism to check up to parallel_workers
+	 * relations in parallel.  As a slot becomes free, we reuse the slot's
+	 * connection if it is open and is connected to the next relation's
+	 * database.  Otherwise, we close the slot's connection (if open) and
+	 * connect again.  The relations list was computed in database order, so
+	 * this strategy should not result in unreasonably many connects and
+	 * disconnects.
+	 *
+	 * Per database, the relations are sorted in relpages order, largest
+	 * first, but the cluster-global largest objects may be far down the list
+	 * if databases with smaller objects were processed first.  We have traded
+	 * off the desire to keep reconnections low against the desire to start
+	 * processing the cluster-global largest objects first irrespective of
+	 * database order.
+	 */
+	prev_datname = NULL;
+	failed = false;
+	slots = NULL;
+	initPQExpBuffer(&sql);
+	for (relprogress = 0, cell = relations.head; cell; cell = cell->next)
+	{
+		ParallelSlot *free_slot;
+		RelationInfo *rel;
+
+		Assert(cell);
+		Assert(cell->ptr);
+
+		rel = (RelationInfo *) cell->ptr;
+
+		Assert(rel->datinfo);
+		Assert(rel->datinfo->datname);
+		Assert(rel->datinfo->amcheck_schema);
+
+		if (CancelRequested)
+		{
+			failed = true;
+			goto finish;
+		}
+
+		/*
+		 * The relations list is in database sorted order.  If this next
+		 * relation is in a different database than the last one seen, we are
+		 * about to start checking this database.  Note that other slots may
+		 * still be working on relations from prior databases.
+		 */
+		if (!opts.quiet && (!prev_datname || strcmp(rel->datinfo->datname,
+													prev_datname) != 0))
+			fprintf(stderr, "%s: checking database \"%s\"\n",
+					progname, rel->datinfo->datname);
+		prev_datname = rel->datinfo->datname;
+
+		if (opts.show_progress)
+		{
+			fprintf(stderr,
+					ngettext("%s: %lld/%lld relation checked\n",
+							 "%s: %lld/%lld relations checked\n",
+							 relprogress),
+					progname, relprogress, reltotal);
+			relprogress++;
+		}
+
+		/*
+		 * Setup the slots if this is our first time through.
+		 *
+		 * There is an inefficiency here that would require redesigning
+		 * parallel slots to fix:  If the number of relations to be checked in
+		 * the first database is less than parallel_workers, we will open more
+		 * connections to the first database than necessary, and will close
+		 * some of them unused as we move on to relations in other databases.
+		 * If this happened for *every* database, it would need fixing, but
+		 * since it only happens for the first, we tolerate this for now.
+		 */
+		if (slots == NULL)
+		{
+			cparams.override_dbname = rel->datinfo->datname;
+			conn = connectDatabase(&cparams, progname, opts.echo, false, true);
+			slots = ParallelSlotsSetup(&cparams, progname, opts.echo, conn,
+									   parallel_workers);
+		}
+
+		/*
+		 * Get a parallel slot for the next amcheck command, blocking if
+		 * necessary until one is available, or until a previously issued slot
+		 * command fails, indicating that we should abort checking the
+		 * remaining objects.
+		 */
+		free_slot = ParallelSlotsGetIdle(slots, parallel_workers);
+		if (!free_slot)
+		{
+			/*
+			 * Something failed.  We don't need to know what it was, because
+			 * the handler should already have emitted the necessary error
+			 * messages.
+			 */
+			failed = true;
+			goto finish;
+		}
+
+		/*
+		 * If the slot is connected to the wrong database for checking this
+		 * next relation, reconnect to the right one.
+		 */
+		if (strcmp(PQdb(free_slot->connection), rel->datinfo->datname) != 0)
+		{
+			disconnectDatabase(free_slot->connection);
+			free_slot->connection = connectDatabase(&cparams, progname,
+													opts.echo, false, true);
+		}
+
+		/*
+		 * Execute the appropriate amcheck command for this relation using our
+		 * slot's database connection.  We do not wait for the command to
+		 * complete, nor do we perform any error checking, as that is done by
+		 * the parallel slots and our handler callback functions.
+		 */
+		if (rel->is_table)
+		{
+			prepare_table_command(&sql, rel->reloid,
+								  rel->datinfo->amcheck_schema);
+			ParallelSlotSetHandler(free_slot, VerifyHeapamSlotHandler,
+								   sql.data);
+			run_command(free_slot, sql.data, &cparams);
+		}
+		else
+		{
+			prepare_btree_command(&sql, rel->reloid,
+								  rel->datinfo->amcheck_schema);
+			ParallelSlotSetHandler(free_slot, VerifyBtreeSlotHandler, NULL);
+			run_command(free_slot, sql.data, &cparams);
+		}
+	}
+	termPQExpBuffer(&sql);
+
+	/*
+	 * Wait for all slots to complete, or for one to indicate that an error
+	 * occurred.  Like above, we rely on the handler emitting the necessary
+	 * error messages.
+	 */
+	if (slots && !ParallelSlotsWaitCompletion(slots, parallel_workers))
+		failed = true;
+
+finish:
+	if (slots)
+	{
+		ParallelSlotsTerminate(slots, parallel_workers);
+		pg_free(slots);
+	}
+
+	if (failed)
+		exit(1);
+}
+
+/*
+ * prepare_table_command
+ *
+ * Creates a SQL command for running amcheck checking on the given heap
+ * relation.  The command is phrased as a SQL query, with column order and
+ * names matching the expectations of VerifyHeapamSlotHandler, which will
+ * receive and handle each row returned from the verify_heapam() function.
+ *
+ * sql: buffer into which the table checking command will be written
+ * reloid: relation of the table to be checked
+ * amcheck_schema: escaped and quoted name of schema in which amcheck contrib
+ * module is installed
+ */
+static void
+prepare_table_command(PQExpBuffer sql, Oid reloid, const char *amcheck_schema)
+{
+	resetPQExpBuffer(sql);
+	appendPQExpBuffer(sql,
+					  "SELECT n.nspname, c.relname, v.blkno, v.offnum, "
+					  "v.attnum, v.msg"
+					  "\nFROM %s.verify_heapam("
+					  "\nrelation := %u,"
+					  "\non_error_stop := %s,"
+					  "\ncheck_toast := %s,"
+					  "\nskip := '%s'",
+					  amcheck_schema,
+					  reloid,
+					  opts.on_error_stop ? "true" : "false",
+					  opts.reconcile_toast ? "true" : "false",
+					  opts.skip);
+	if (opts.startblock >= 0)
+		appendPQExpBuffer(sql, ",\nstartblock := %ld", opts.startblock);
+	if (opts.endblock >= 0)
+		appendPQExpBuffer(sql, ",\nendblock := %ld", opts.endblock);
+	appendPQExpBuffer(sql, "\n) v,"
+					  "\npg_catalog.pg_class c"
+					  "\nJOIN pg_catalog.pg_namespace n"
+					  "\nON c.relnamespace OPERATOR(pg_catalog.=) n.oid"
+					  "\nWHERE c.oid OPERATOR(pg_catalog.=) %u",
+					  reloid);
+}
+
+/*
+ * prepare_btree_command
+ *
+ * Creates a SQL command for running amcheck checking on the given btree index
+ * relation.  The command does not select any columns, as btree checking
+ * functions do not return any, but rather return corruption information by
+ * raising errors, which VerifyBtreeSlotHandler expects.
+ *
+ * sql: buffer into which the table checking command will be written
+ * reloid: relation of the table to be checked
+ * amcheck_schema: escaped and quoted name of schema in which amcheck contrib
+ * module is installed
+ */
+static void
+prepare_btree_command(PQExpBuffer sql, Oid reloid, const char *amcheck_schema)
+{
+	resetPQExpBuffer(sql);
+	if (opts.parent_check)
+		appendPQExpBuffer(sql,
+						  "SELECT %s.bt_index_parent_check("
+						  "\nindex := '%u'::regclass,"
+						  "\nheapallindexed := %s,"
+						  "\nrootdescend := %s)",
+						  amcheck_schema,
+						  reloid,
+						  (opts.heapallindexed ? "true" : "false"),
+						  (opts.rootdescend ? "true" : "false"));
+	else
+		appendPQExpBuffer(sql,
+						  "SELECT %s.bt_index_check("
+						  "\nindex := '%u'::regclass,"
+						  "\nheapallindexed := %s)",
+						  amcheck_schema,
+						  reloid,
+						  (opts.heapallindexed ? "true" : "false"));
+}
+
+/*
+ * run_command
+ *
+ * Sends a command to the server without waiting for the command to complete.
+ * Logs an error if the command cannot be sent, but otherwise any errors are
+ * expected to be handled by a ParallelSlotHandler.
+ *
+ * If reconnecting to the database is necessary, the cparams argument may be
+ * modified.
+ *
+ * slot: slot with connection to the server we should use for the command
+ * sql: query to send
+ * cparams: connection parameters in case the slot needs to be reconnected
+ */
+static void
+run_command(ParallelSlot *slot, const char *sql, ConnParams *cparams)
+{
+	if (opts.echo)
+		printf("%s\n", sql);
+
+	if (PQsendQuery(slot->connection, sql) == 0)
+	{
+		pg_log_warning("error sending command to database \"%s\": %s",
+					   PQdb(slot->connection),
+					   PQerrorMessage(slot->connection));
+
+		/* reconnect, in case we merely lost the connection */
+		cparams->override_dbname = PQdb(slot->connection);
+		disconnectDatabase(slot->connection);
+		slot->connection = connectDatabase(cparams, progname,
+										   opts.echo, false, true);
+
+		/* retry the command, but give up if the retry fails */
+		if (PQsendQuery(slot->connection, sql) == 0)
+		{
+			pg_log_error("retry: error sending command to database \"%s\": %s",
+						 PQdb(slot->connection),
+						 PQerrorMessage(slot->connection));
+			pg_log_error("command was: %s", sql);
+
+			/*
+			 * The retry failed, and continuing to retry risks an infinite
+			 * loop.  But subsequent remaining commands, particularly if they
+			 * are for a different database, may succeed, so we do not exit
+			 * here.
+			 */
+		}
+	}
+}
+
+/*
+ * VerifyHeapamSlotHandler
+ *
+ * ParallelSlotHandler that receives results from a table checking command
+ * created by prepare_table_command and outputs the results for the user.
+ *
+ * res: result from an executed sql query
+ * conn: connection on which the sql query was executed
+ * context: the sql query being handled, as a cstring
+ */
+static bool
+VerifyHeapamSlotHandler(PGresult *res, PGconn *conn, void *context)
+{
+	int			ntups = PQntuples(res);
+
+	if (PQresultStatus(res) == PGRES_TUPLES_OK)
+	{
+		int			i;
+
+		for (i = 0; i < ntups; i++)
+		{
+			if (!PQgetisnull(res, i, 4))
+				printf("relation %s.%s.%s, block %s, offset %s, attribute %s\n    %s\n",
+					   PQdb(conn),
+					   PQgetvalue(res, i, 0),	/* schema */
+					   PQgetvalue(res, i, 1),	/* relname */
+					   PQgetvalue(res, i, 2),	/* blkno */
+					   PQgetvalue(res, i, 3),	/* offnum */
+					   PQgetvalue(res, i, 4),	/* attnum */
+					   PQgetvalue(res, i, 5));	/* msg */
+
+			else if (!PQgetisnull(res, i, 3))
+				printf("relation %s.%s.%s, block %s, offset %s\n    %s\n",
+					   PQdb(conn),
+					   PQgetvalue(res, i, 0),	/* schema */
+					   PQgetvalue(res, i, 1),	/* relname */
+					   PQgetvalue(res, i, 2),	/* blkno */
+					   PQgetvalue(res, i, 3),	/* offnum */
+				/* attnum is null: 4 */
+					   PQgetvalue(res, i, 5));	/* msg */
+
+			else if (!PQgetisnull(res, i, 2))
+				printf("relation %s.%s.%s, block %s\n    %s\n",
+					   PQdb(conn),
+					   PQgetvalue(res, i, 0),	/* schema */
+					   PQgetvalue(res, i, 1),	/* relname */
+					   PQgetvalue(res, i, 2),	/* blkno */
+				/* offnum is null: 3 */
+				/* attnum is null: 4 */
+					   PQgetvalue(res, i, 5));	/* msg */
+
+			else if (!PQgetisnull(res, i, 1))
+				printf("relation %s.%s.%s\n    %s\n",
+					   PQdb(conn),
+					   PQgetvalue(res, i, 0),	/* schema */
+					   PQgetvalue(res, i, 1),	/* relname */
+				/* blkno is null:  2 */
+				/* offnum is null: 3 */
+				/* attnum is null: 4 */
+					   PQgetvalue(res, i, 5));	/* msg */
+
+			else
+				printf("%s.%s\n",
+					   PQdb(conn),
+					   PQgetvalue(res, i, 5));	/* msg */
+		}
+	}
+	else if (PQresultStatus(res) != PGRES_TUPLES_OK)
+	{
+		printf("%s: %s\n", PQdb(conn), PQerrorMessage(conn));
+		printf("%s: query was: %s\n", PQdb(conn), (const char *) context);
+	}
+
+	return true;
+}
+
+/*
+ * VerifyBtreeSlotHandler
+ *
+ * ParallelSlotHandler that receives results from a btree checking command
+ * created by prepare_btree_command and outputs them for the user.  The results
+ * from the btree checking command is assumed to be empty, but when the results
+ * are an error code, the useful information about the corruption is expected
+ * in the connection's error message.
+ *
+ * res: result from an executed sql query
+ * conn: connection on which the sql query was executed
+ * context: unused
+ */
+static bool
+VerifyBtreeSlotHandler(PGresult *res, PGconn *conn, void *context)
+{
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+		printf("%s: %s\n", PQdb(conn), PQerrorMessage(conn));
+
+	return true;
+}
+
+/*
+ * help
+ *
+ * Prints help page for the program
+ *
+ * progname: the name of the executed program, such as "pg_amcheck"
+ */
+static void
+help(const char *progname)
+{
+	printf("%s checks objects in a PostgreSQL database for corruption.\n\n", progname);
+	printf("Usage:\n");
+	printf("  %s [OPTION]... [DBNAME]\n", progname);
+	printf("\nTarget Options:\n");
+	printf("  -a, --all                 check all databases\n");
+	printf("  -d, --dbname=DBNAME       check specific database(s)\n");
+	printf("  -D, --exclude-dbname=DBNAME do NOT check specific database(s)\n");
+	printf("  -i, --index=INDEX         check specific index(es)\n");
+	printf("  -I, --exclude-index=INDEX do NOT check specific index(es)\n");
+	printf("  -r, --relation=RELNAME    check specific relation(s)\n");
+	printf("  -R, --exclude-relation=RELNAME do NOT check specific relation(s)\n");
+	printf("  -s, --schema=SCHEMA       check specific schema(s)\n");
+	printf("  -S, --exclude-schema=SCHEMA do NOT check specific schema(s)\n");
+	printf("  -t, --table=TABLE         check specific table(s)\n");
+	printf("  -T, --exclude-table=TABLE do NOT check specific table(s)\n");
+	printf("      --exclude-indexes     do NOT perform any index checking\n");
+	printf("      --exclude-tables      do NOT check any tables\n");
+	printf("      --exclude-toast       do NOT check any toast tables nor toast indexes\n");
+	printf("      --no-dependents       do NOT automatically check dependent objects\n");
+	printf("      --no-strict-names     do NOT require patterns to match objects\n");
+	printf("\nIndex Checking Options:\n");
+	printf("  -H, --heapallindexed      check all heap tuples are found within indexes\n");
+	printf("  -P, --parent-check        check index parent/child relationships\n");
+	printf("      --rootdescend         search from root page to refind tuples\n");
+	printf("\nTable Checking Options:\n");
+	printf("      --exclude-toast-pointers do NOT follow relation toast pointers\n");
+	printf("      --on-error-stop       stop checking at end of first corrupt page\n");
+	printf("      --skip=OPTION         do NOT check \"all-frozen\" or \"all-visible\" blocks\n");
+	printf("      --startblock=BLOCK    begin checking table(s) at the given block number\n");
+	printf("      --endblock=BLOCK      check table(s) only up to the given block number\n");
+	printf("\nConnection options:\n");
+	printf("  -h, --host=HOSTNAME       database server host or socket directory\n");
+	printf("  -p, --port=PORT           database server port\n");
+	printf("  -U, --username=USERNAME   user name to connect as\n");
+	printf("  -w, --no-password         never prompt for password\n");
+	printf("  -W, --password            force password prompt\n");
+	printf("  --maintenance-db=DBNAME   alternate maintenance database\n");
+	printf("\nOther Options:\n");
+	printf("  -e, --echo                show the commands being sent to the server\n");
+	printf("  -j, --jobs=NUM            use this many concurrent connections to the server\n");
+	printf("  -q, --quiet               don't write any messages\n");
+	printf("  -v, --verbose             write a lot of output\n");
+	printf("  -V, --version             output version information, then exit\n");
+	printf("      --progress            show progress information\n");
+	printf("  -?, --help                show this help, then exit\n");
+
+	printf("\nRead the description of the amcheck contrib module for details.\n");
+	printf("\nReport bugs to <%s>.\n", PACKAGE_BUGREPORT);
+	printf("%s home page: <%s>\n", PACKAGE_NAME, PACKAGE_URL);
+}
+
+/*
+ * appendDatabasePattern
+ *
+ * Adds to a list the given pattern interpreted as a database name pattern.
+ *
+ * list: the list to be appended
+ * pattern: the database name pattern
+ * encoding: client encoding for parsing the pattern
+ */
+static void
+appendDatabasePattern(SimplePtrList *list, const char *pattern, int encoding)
+{
+	PQExpBufferData buf;
+	PatternInfo *info = (PatternInfo *) palloc0(sizeof(PatternInfo));
+
+	info->pattern_id = next_id++;
+
+	initPQExpBuffer(&buf);
+	patternToSQLRegex(encoding, NULL, NULL, &buf, pattern, false);
+	info->pattern = pattern;
+	info->dbrgx = pstrdup(buf.data);
+
+	termPQExpBuffer(&buf);
+
+	simple_ptr_list_append(list, info);
+}
+
+/*
+ * appendSchemaPattern
+ *
+ * Adds to a list the given pattern interpreted as a schema name pattern.
+ *
+ * list: the list to be appended
+ * pattern: the schema name pattern
+ * encoding: client encoding for parsing the pattern
+ */
+static void
+appendSchemaPattern(SimplePtrList *list, const char *pattern, int encoding)
+{
+	PQExpBufferData buf;
+	PatternInfo *info = (PatternInfo *) palloc0(sizeof(PatternInfo));
+
+	info->pattern_id = next_id++;
+
+	initPQExpBuffer(&buf);
+	patternToSQLRegex(encoding, NULL, NULL, &buf, pattern, false);
+	info->pattern = pattern;
+	info->nsprgx = pstrdup(buf.data);
+	termPQExpBuffer(&buf);
+
+	simple_ptr_list_append(list, info);
+}
+
+/*
+ * appendRelationPatternHelper
+ *
+ * Adds to a list the given pattern interpreted as a relation pattern.
+ *
+ * list: the list to be appended
+ * pattern: the relation name pattern
+ * encoding: client encoding for parsing the pattern
+ * tblonly: whether the pattern should only be matched against heap tables
+ * idxonly: whether the pattern should only be matched against btree indexes
+ */
+static void
+appendRelationPatternHelper(SimplePtrList *list, const char *pattern,
+							int encoding, bool tblonly, bool idxonly)
+{
+	PQExpBufferData dbbuf;
+	PQExpBufferData nspbuf;
+	PQExpBufferData relbuf;
+	PatternInfo *info = (PatternInfo *) palloc0(sizeof(PatternInfo));
+
+	info->pattern_id = next_id++;
+
+	initPQExpBuffer(&dbbuf);
+	initPQExpBuffer(&nspbuf);
+	initPQExpBuffer(&relbuf);
+
+	patternToSQLRegex(encoding, &dbbuf, &nspbuf, &relbuf, pattern, false);
+	info->pattern = pattern;
+	if (dbbuf.data[0])
+		info->dbrgx = pstrdup(dbbuf.data);
+	if (nspbuf.data[0])
+		info->nsprgx = pstrdup(nspbuf.data);
+	if (relbuf.data[0])
+		info->relrgx = pstrdup(relbuf.data);
+
+	termPQExpBuffer(&dbbuf);
+	termPQExpBuffer(&nspbuf);
+	termPQExpBuffer(&relbuf);
+
+	info->tblonly = tblonly;
+	info->idxonly = idxonly;
+
+	simple_ptr_list_append(list, info);
+}
+
+/*
+ * appendRelationPattern
+ *
+ * Adds to a list the given pattern interpreted as a relation pattern, to be
+ * matched against both tables and indexes.
+ *
+ * list: the list to be appended
+ * pattern: the relation name pattern
+ * encoding: client encoding for parsing the pattern
+ */
+static void
+appendRelationPattern(SimplePtrList *list, const char *pattern, int encoding)
+{
+	appendRelationPatternHelper(list, pattern, encoding, false, false);
+}
+
+/*
+ * appendTablePattern
+ *
+ * Adds to a list the given pattern interpreted as a relation pattern, to be
+ * matched only against tables.
+ *
+ * list: the list to be appended
+ * pattern: the relation name pattern
+ * encoding: client encoding for parsing the pattern
+ */
+static void
+appendTablePattern(SimplePtrList *list, const char *pattern, int encoding)
+{
+	appendRelationPatternHelper(list, pattern, encoding, true, false);
+}
+
+/*
+ * appendIndexPattern
+ *
+ * Adds to a list the given pattern interpreted as a relation pattern, to be
+ * matched only against indexes.
+ *
+ * list: the list to be appended
+ * pattern: the relation name pattern
+ * encoding: client encoding for parsing the pattern
+ */
+static void
+appendIndexPattern(SimplePtrList *list, const char *pattern, int encoding)
+{
+	appendRelationPatternHelper(list, pattern, encoding, false, true);
+}
+
+/*
+ * appendDbPatternCTE
+ *
+ * Appends to the buffer the body of a Common Table Expression (CTE) containing
+ * the database portions filtered from the list of patterns expressed as three
+ * columns:
+ *
+ *     id: the unique pattern ID
+ *     pat: the full user specified pattern from the command line
+ *     rgx: the database regular expression parsed from the pattern
+ *
+ * Patterns without a database portion are skipped.  Patterns with more than
+ * just a database portion are optionally skipped, depending on argument
+ * 'inclusive'.
+ *
+ * buf: the buffer to be appended
+ * patterns: the list of patterns to be inserted into the CTE
+ * conn: the database connection
+ * inclusive: whether to include patterns with schema and/or relation parts
+ */
+static void
+appendDbPatternCTE(PQExpBuffer buf, const SimplePtrList *patterns,
+				   PGconn *conn, bool inclusive)
+{
+	SimplePtrListCell *cell;
+	const char *comma;
+	bool		have_values;
+
+	comma = "";
+	have_values = false;
+	for (cell = patterns->head; cell; cell = cell->next)
+	{
+		PatternInfo *info = (PatternInfo *) cell->ptr;
+
+		if (info->dbrgx != NULL &&
+			(inclusive || (info->nsprgx == NULL && info->relrgx == NULL)))
+		{
+			if (!have_values)
+				appendPQExpBufferStr(buf, "\nVALUES");
+			have_values = true;
+			appendPQExpBuffer(buf, "%s\n(%d, ", comma, info->pattern_id);
+			appendStringLiteralConn(buf, info->pattern, conn);
+			appendPQExpBufferStr(buf, ", ");
+			appendStringLiteralConn(buf, info->dbrgx, conn);
+			appendPQExpBufferStr(buf, ")");
+			comma = ",";
+		}
+	}
+
+	if (!have_values)
+		appendPQExpBufferStr(buf, "\nSELECT NULL, NULL, NULL WHERE false");
+}
+
+/*
+ * compileDatabaseList
+ *
+ * Compiles a list of databases to check based on the user supplied options,
+ * sorted to preserve the order they were specified on the command line.  In
+ * the event that multiple databases match a single command line pattern, they
+ * are secondarily sorted by name.
+ *
+ * conn: connection to the initial database
+ * databases: the list onto which databases should be appended
+ */
+static void
+compileDatabaseList(PGconn *conn, SimplePtrList *databases)
+{
+	PGresult   *res;
+	PQExpBufferData sql;
+	int			ntups;
+	int			i;
+	bool		fatal;
+
+	initPQExpBuffer(&sql);
+
+	/* Append the include patterns CTE. */
+	appendPQExpBufferStr(&sql, "WITH include_raw (id, pat, rgx) AS (");
+	appendDbPatternCTE(&sql, &opts.include, conn, true);
+
+	/* Append the exclude patterns CTE. */
+	appendPQExpBufferStr(&sql, "\n),\nexclude_raw (id, pat, rgx) AS (");
+	appendDbPatternCTE(&sql, &opts.exclude, conn, false);
+	appendPQExpBufferStr(&sql, "\n),");
+
+	/*
+	 * Append the database CTE, which includes whether each database is
+	 * connectable and also joins against exclude_raw to determine whether
+	 * each database is excluded.
+	 */
+	appendPQExpBufferStr(&sql,
+						 "\ndatabase (datname) AS ("
+						 "\nSELECT d.datname"
+						 "\nFROM pg_catalog.pg_database d"
+						 "\nLEFT OUTER JOIN exclude_raw e"
+						 "\nON d.datname ~ e.rgx"
+						 "\nWHERE d.datallowconn"
+						 "\nAND e.id IS NULL"
+						 "\n),"
+
+	/*
+	 * Append the include_pat CTE, which joins the include_raw CTE against the
+	 * databases CTE to determine if all the inclusion patterns had matches,
+	 * and whether each matched pattern had the misfortune of only matching
+	 * excluded or unconnectable databases.
+	 */
+						 "\ninclude_pat (id, pat, checkable) AS ("
+						 "\nSELECT i.id, i.pat,"
+						 "\nCOUNT(*) FILTER ("
+						 "\nWHERE d IS NOT NULL"
+						 "\n) AS checkable"
+						 "\nFROM include_raw i"
+						 "\nLEFT OUTER JOIN database d"
+						 "\nON d.datname ~ i.rgx"
+						 "\nGROUP BY i.id, i.pat"
+						 "\n),"
+
+	/*
+	 * Append the filtered_databases CTE, which selects from the database CTE
+	 * optionally joined against the include_raw CTE to only select databases
+	 * that match an inclusion pattern.  This appears to duplicate what the
+	 * include_pat CTE already did above, but here we want only databses, and
+	 * there we wanted patterns.
+	 */
+						 "\nfiltered_databases (datname) AS ("
+						 "\nSELECT DISTINCT d.datname"
+						 "\nFROM database d");
+	if (!opts.alldb)
+		appendPQExpBufferStr(&sql,
+							 "\nINNER JOIN include_raw i"
+							 "\nON d.datname ~ i.rgx");
+	appendPQExpBufferStr(&sql,
+						 "\n)"
+
+	/*
+	 * Select the checkable databases and the unmatched inclusion patterns.
+	 */
+						 "\nSELECT pat, datname"
+						 "\nFROM ("
+						 "\nSELECT id, pat, NULL::TEXT AS datname"
+						 "\nFROM include_pat"
+						 "\nWHERE checkable = 0"
+						 "\nUNION ALL"
+						 "\nSELECT NULL, NULL, datname"
+						 "\nFROM filtered_databases"
+						 "\n) AS combined_records"
+						 "\nORDER BY id NULLS LAST, datname");
+
+	res = executeQuery(conn, sql.data, opts.echo);
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+	{
+		pg_log_error("query failed: %s", PQerrorMessage(conn));
+		pg_log_error("query was: %s", sql.data);
+		disconnectDatabase(conn);
+		exit(1);
+	}
+	termPQExpBuffer(&sql);
+
+	ntups = PQntuples(res);
+	for (fatal = false, i = 0; i < ntups; i++)
+	{
+		const char *pat = NULL;
+		const char *datname = NULL;
+
+		if (!PQgetisnull(res, i, 0))
+			pat = PQgetvalue(res, i, 0);
+		if (!PQgetisnull(res, i, 1))
+			datname = PQgetvalue(res, i, 1);
+
+		if (pat != NULL)
+		{
+			/*
+			 * Current record pertains to an inclusion pattern that matched no
+			 * checkable databases.
+			 */
+			fatal = opts.strict_names;
+			fprintf(stderr, "%s: no checkable database: \"%s\"\n",
+					progname, pat);
+		}
+		else
+		{
+			/* Current record pertains to a database */
+			Assert(datname != NULL);
+
+			DatabaseInfo *dat = (DatabaseInfo *) palloc0(sizeof(DatabaseInfo));
+
+			/* This database is included.  Add to list */
+			if (opts.verbose)
+				fprintf(stderr, "%s: including database: \"%s\"\n", progname,
+						datname);
+
+			dat->datname = pstrdup(datname);
+			simple_ptr_list_append(databases, dat);
+		}
+	}
+	PQclear(res);
+
+	if (fatal)
+	{
+		disconnectDatabase(conn);
+		exit(1);
+	}
+}
+
+/*
+ * appendRelPatternRawCTE
+ *
+ * Appends to the buffer the body of a Common Table Expression (CTE) containing
+ * the patterns from the given list as seven columns:
+ *
+ *     id: the unique pattern ID
+ *     pat: the full user specified pattern from the command line
+ *     dbrgx: the database regexp parsed from the pattern, or NULL if the
+ *            pattern had no database part
+ *     nsprgx: the namespace regexp parsed from the pattern, or NULL if the
+ *             pattern had no namespace part
+ *     relrgx: the relname regexp parsed from the pattern, or NULL if the
+ *             pattern had no relname part
+ *     tbl: true if the pattern applies only to tables (not indexes)
+ *     idx: true if the pattern applies only to indexes (not tables)
+ *
+ * buf: the buffer to be appended
+ * patterns: the list of patterns to be inserted into the CTE
+ * conn: the database connection
+ */
+static void
+appendRelPatternRawCTE(PQExpBuffer buf, const SimplePtrList *patterns,
+					   PGconn *conn)
+{
+	SimplePtrListCell *cell;
+	const char *comma;
+	bool		have_values;
+
+	comma = "";
+	have_values = false;
+	for (cell = patterns->head; cell; cell = cell->next)
+	{
+		PatternInfo *info = (PatternInfo *) cell->ptr;
+
+		if (!have_values)
+			appendPQExpBufferStr(buf, "\nVALUES");
+		have_values = true;
+		appendPQExpBuffer(buf, "%s\n(%d::INTEGER, ", comma, info->pattern_id);
+		appendStringLiteralConn(buf, info->pattern, conn);
+		appendPQExpBufferStr(buf, "::TEXT, ");
+		if (info->dbrgx == NULL)
+			appendPQExpBufferStr(buf, "NULL");
+		else
+			appendStringLiteralConn(buf, info->dbrgx, conn);
+		appendPQExpBufferStr(buf, "::TEXT, ");
+		if (info->nsprgx == NULL)
+			appendPQExpBufferStr(buf, "NULL");
+		else
+			appendStringLiteralConn(buf, info->nsprgx, conn);
+		appendPQExpBufferStr(buf, "::TEXT, ");
+		if (info->relrgx == NULL)
+			appendPQExpBufferStr(buf, "NULL");
+		else
+			appendStringLiteralConn(buf, info->relrgx, conn);
+		if (info->tblonly)
+			appendPQExpBufferStr(buf, "::TEXT, true::BOOLEAN");
+		else
+			appendPQExpBufferStr(buf, "::TEXT, false::BOOLEAN");
+		if (info->idxonly)
+			appendPQExpBufferStr(buf, ", true::BOOLEAN");
+		else
+			appendPQExpBufferStr(buf, ", false::BOOLEAN");
+		appendPQExpBufferStr(buf, ")");
+		comma = ",";
+	}
+
+	if (!have_values)
+		appendPQExpBufferStr(buf,
+							 "\nSELECT NULL::INTEGER, NULL::TEXT, NULL::TEXT,"
+							 "\nNULL::TEXT, NULL::TEXT, NULL::BOOLEAN,"
+							 "\nNULL::BOOLEAN"
+							 "\nWHERE false");
+}
+
+/*
+ * appendRelPatternFilteredCTE
+ *
+ * Appends to the buffer a Common Table Expression (CTE) which selects
+ * all patterns from the named raw CTE, filtered by database.  All patterns
+ * which have no database portion or whose database portion matches our
+ * connection's database name are selected, with other patterns excluded.
+ *
+ * The basic idea here is that if we're connected to database "foo" and we have
+ * patterns "foo.bar.baz", "alpha.beta" and "one.two.three", we only want to
+ * use the first two while processing relations in this database, as the third
+ * one is not relevant.
+ *
+ * buf: the buffer to be appended
+ * raw: the name of the CTE to select from
+ * filtered: the name of the CTE to create
+ * conn: the database connection
+ */
+static void
+appendRelPatternFilteredCTE(PQExpBuffer buf, const char *raw,
+							const char *filtered, PGconn *conn)
+{
+	appendPQExpBuffer(buf,
+					  "\n%s (id, pat, nsprgx, relrgx, tbl, idx) AS ("
+					  "\nSELECT id, pat, nsprgx, relrgx, tbl, idx"
+					  "\nFROM %s r"
+					  "\nWHERE (r.dbrgx IS NULL"
+					  "\nOR ",
+					  filtered, raw);
+	appendStringLiteralConn(buf, PQdb(conn), conn);
+	appendPQExpBufferStr(buf, " ~ r.dbrgx)");
+	appendPQExpBufferStr(buf,
+						 "\nAND (r.nsprgx IS NOT NULL"
+						 "\nOR r.relrgx IS NOT NULL)"
+						 "\n),");
+}
+
+/*
+ * compileRelationListOneDb
+ *
+ * Compiles a list of relations to check within the currently connected
+ * database based on the user supplied options, sorted by descending size,
+ * and appends them to the given relations list.
+ *
+ * The cells of the constructed list contain all information about the relation
+ * necessary to connect to the database and check the object, including which
+ * database to connect to, where contrib/amcheck is installed, and the Oid and
+ * type of object (table vs. index).  Rather than duplicating the database
+ * details per relation, the relation structs use references to the same
+ * database object, provided by the caller.
+ *
+ * conn: connection to this next database, which should be the same as in 'dat'
+ * relations: list onto which the relations information should be appended
+ * dat: the database info struct for use by each relation
+ */
+static void
+compileRelationListOneDb(PGconn *conn, SimplePtrList *relations,
+						 const DatabaseInfo *dat)
+{
+	PGresult   *res;
+	PQExpBufferData sql;
+	int			ntups;
+	int			i;
+	const char *datname;
+
+	initPQExpBuffer(&sql);
+	appendPQExpBufferStr(&sql, "WITH");
+
+	/* Append CTEs for the relation inclusion patterns, if any */
+	if (!opts.allrel)
+	{
+		appendPQExpBufferStr(&sql,
+							 "\ninclude_raw (id, pat, dbrgx, nsprgx, relrgx, tbl, idx) AS (");
+		appendRelPatternRawCTE(&sql, &opts.include, conn);
+		appendPQExpBufferStr(&sql, "\n),");
+		appendRelPatternFilteredCTE(&sql, "include_raw", "include_pat", conn);
+	}
+
+	/* Append CTEs for the relation exclusion patterns, if any */
+	if (opts.excludetbl || opts.excludeidx)
+	{
+		appendPQExpBufferStr(&sql,
+							 "\nexclude_raw (id, pat, dbrgx, nsprgx, relrgx, tbl, idx) AS (");
+		appendRelPatternRawCTE(&sql, &opts.exclude, conn);
+		appendPQExpBufferStr(&sql, "\n),");
+		appendRelPatternFilteredCTE(&sql, "exclude_raw", "exclude_pat", conn);
+	}
+
+	/* Append the relation CTE. */
+	appendPQExpBufferStr(&sql,
+						 "\nrelation (id, pat, oid, reltoastrelid, relpages, tbl, idx) AS ("
+						 "\nSELECT DISTINCT ON (c.oid");
+	if (!opts.allrel)
+		appendPQExpBufferStr(&sql, ", ip.id) ip.id, ip.pat,");
+	else
+		appendPQExpBufferStr(&sql, ") NULL::INTEGER AS id, NULL::TEXT AS pat,");
+	appendPQExpBuffer(&sql,
+					  "\nc.oid, c.reltoastrelid, c.relpages,"
+					  "\nc.relam = %u AS tbl,"
+					  "\nc.relam = %u AS idx"
+					  "\nFROM pg_catalog.pg_class c"
+					  "\nINNER JOIN pg_catalog.pg_namespace n"
+					  "\nON c.relnamespace = n.oid",
+					  HEAP_TABLE_AM_OID, BTREE_AM_OID);
+	if (!opts.allrel)
+		appendPQExpBuffer(&sql,
+						  "\nINNER JOIN include_pat ip"
+						  "\nON (n.nspname ~ ip.nsprgx OR ip.nsprgx IS NULL)"
+						  "\nAND (c.relname ~ ip.relrgx OR ip.relrgx IS NULL)"
+						  "\nAND (c.relam = %u OR NOT ip.tbl)"
+						  "\nAND (c.relam = %u OR NOT ip.idx)",
+						  HEAP_TABLE_AM_OID, BTREE_AM_OID);
+	if (opts.excludetbl || opts.excludeidx)
+		appendPQExpBuffer(&sql,
+						  "\nLEFT OUTER JOIN exclude_pat e"
+						  "\nON (n.nspname ~ e.nsprgx OR e.nsprgx IS NULL)"
+						  "\nAND (c.relname ~ e.relrgx OR e.relrgx IS NULL)"
+						  "\nAND (c.relam = %u OR NOT e.tbl)"
+						  "\nAND (c.relam = %u OR NOT e.idx)",
+						  HEAP_TABLE_AM_OID, BTREE_AM_OID);
+
+	if (opts.excludetbl || opts.excludeidx)
+		appendPQExpBufferStr(&sql, "\nWHERE e.pat IS NULL");
+	else
+		appendPQExpBufferStr(&sql, "\nWHERE true");
+
+	if (opts.no_toast)
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relnamespace != %u",
+						  PG_TOAST_NAMESPACE);
+	if (opts.no_tables)
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relam = %u"
+						  "\nAND c.relkind = 'i'",
+						  BTREE_AM_OID);
+	else if (opts.no_indexes)
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relam = %u"
+						  "\nAND c.relkind IN ('r', 'm', 't')",
+						  HEAP_TABLE_AM_OID);
+	else
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relam IN (%u, %u)"
+						  "\nAND c.relkind IN ('r', 'm', 't', 'i')"
+						  "\nAND ((c.relam = %u AND c.relkind IN ('r', 'm', 't')) OR"
+						  "\n(c.relam = %u AND c.relkind = 'i'))",
+						  HEAP_TABLE_AM_OID, BTREE_AM_OID,
+						  HEAP_TABLE_AM_OID, BTREE_AM_OID);
+
+	appendPQExpBufferStr(&sql,
+						 "\nORDER BY c.oid"
+						 "\n)");
+
+	if (!opts.no_dependents && !opts.no_toast)
+	{
+		/*
+		 * Include a CTE for toast tables associated with primary tables
+		 * selected above, filtering by exclusion patterns (if any) that match
+		 * toast table names.
+		 */
+		appendPQExpBufferStr(&sql,
+							 ",\ntoast (oid, relpages) AS ("
+							 "\nSELECT t.oid, t.relpages"
+							 "\nFROM pg_catalog.pg_class t"
+							 "\nINNER JOIN relation r"
+							 "\nON r.reltoastrelid = t.oid");
+		if (opts.excludetbl)
+			appendPQExpBufferStr(&sql,
+								 "\nLEFT OUTER JOIN exclude_pat e"
+								 "\nON ('pg_toast' ~ e.nsprgx OR e.nsprgx IS NULL)"
+								 "\nAND (t.relname ~ e.relrgx OR e.relrgx IS NULL)"
+								 "\nAND e.tbl"
+								 "\nWHERE e.id IS NULL");
+		appendPQExpBufferStr(&sql,
+							 "\n)");
+	}
+	if (!opts.no_dependents && !opts.no_indexes)
+	{
+		/*
+		 * Include a CTE for btree indexes associated with primary tables
+		 * selected above, filtering by exclusion patterns (if any) that match
+		 * btree index names.
+		 */
+		appendPQExpBuffer(&sql,
+						  ",\nindex (oid, relpages) AS ("
+						  "\nSELECT c.oid, c.relpages"
+						  "\nFROM relation r"
+						  "\nINNER JOIN pg_catalog.pg_index i"
+						  "\nON r.oid = i.indrelid"
+						  "\nINNER JOIN pg_catalog.pg_class c"
+						  "\nON i.indexrelid = c.oid");
+		if (opts.excludeidx)
+			appendPQExpBufferStr(&sql,
+								 "\nINNER JOIN pg_catalog.pg_namespace n"
+								 "\nON c.relnamespace = n.oid"
+								 "\nLEFT OUTER JOIN exclude_pat e"
+								 "\nON (n.nspname ~ e.nsprgx OR e.nsprgx IS NULL)"
+								 "\nAND (c.relname ~ e.relrgx OR e.relrgx IS NULL)"
+								 "\nAND e.idx"
+								 "\nWHERE e.id IS NULL");
+		else
+			appendPQExpBufferStr(&sql,
+								 "\nWHERE true");
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relam = %u"
+						  "\nAND c.relkind = 'i'",
+						  BTREE_AM_OID);
+		if (opts.no_toast)
+			appendPQExpBuffer(&sql,
+							  "\nAND c.relnamespace != %u",
+							  PG_TOAST_NAMESPACE);
+		appendPQExpBufferStr(&sql, "\n)");
+	}
+
+	if (!opts.no_dependents && !opts.no_toast && !opts.no_indexes)
+	{
+		/*
+		 * Include a CTE for btree indexes associated with toast tables of
+		 * primary tables selected above, filtering by exclusion patterns (if
+		 * any) that match the toast index names.
+		 */
+		appendPQExpBuffer(&sql,
+						  ",\ntoast_index (oid, relpages) AS ("
+						  "\nSELECT c.oid, c.relpages"
+						  "\nFROM toast t"
+						  "\nINNER JOIN pg_catalog.pg_index i"
+						  "\nON t.oid = i.indrelid"
+						  "\nINNER JOIN pg_catalog.pg_class c"
+						  "\nON i.indexrelid = c.oid");
+		if (opts.excludeidx)
+			appendPQExpBufferStr(&sql,
+								 "\nLEFT OUTER JOIN exclude_pat e"
+								 "\nON ('pg_toast' ~ e.nsprgx OR e.nsprgx IS NULL)"
+								 "\nAND (c.relname ~ e.relrgx OR e.relrgx IS NULL)"
+								 "\nAND e.idx"
+								 "\nWHERE e.id IS NULL");
+		else
+			appendPQExpBufferStr(&sql,
+								 "\nWHERE true");
+		appendPQExpBuffer(&sql,
+						  "\nAND c.relam = %u"
+						  "\nAND c.relkind = 'i'"
+						  "\n)",
+						  BTREE_AM_OID);
+	}
+
+	/*
+	 * Roll-up distinct rows from CTEs.
+	 *
+	 * Relations that match more than one pattern may occur more than once in
+	 * the list, and indexes and toast for primary relations may also have
+	 * matched in their own right, so we rely on UNION to deduplicate the
+	 * list.
+	 */
+	appendPQExpBuffer(&sql,
+					  "\nSELECT id, tbl, idx, oid"
+					  "\nFROM (");
+	appendPQExpBufferStr(&sql,
+	/* Inclusion patterns that failed to match */
+						 "\nSELECT id, tbl, idx,"
+						 "\nNULL::OID AS oid,"
+						 "\nNULL::INTEGER AS relpages"
+						 "\nFROM relation"
+						 "\nWHERE id IS NOT NULL"
+						 "\nUNION"
+	/* Primary relations */
+						 "\nSELECT NULL::INTEGER AS id,"
+						 "\ntbl, idx,"
+						 "\noid, relpages"
+						 "\nFROM relation");
+	if (!opts.no_dependents && !opts.no_toast)
+		appendPQExpBufferStr(&sql,
+							 "\nUNION"
+		/* Toast tables for primary relations */
+							 "\nSELECT NULL::INTEGER AS id, TRUE AS tbl,"
+							 "\nFALSE AS idx, oid, relpages"
+							 "\nFROM toast");
+	if (!opts.no_dependents && !opts.no_indexes)
+		appendPQExpBufferStr(&sql,
+							 "\nUNION"
+		/* Indexes for primary relations */
+							 "\nSELECT NULL::INTEGER AS id, FALSE AS tbl,"
+							 "\nTRUE AS idx, oid, relpages"
+							 "\nFROM index");
+	if (!opts.no_dependents && !opts.no_toast && !opts.no_indexes)
+		appendPQExpBufferStr(&sql,
+							 "\nUNION"
+		/* Indexes for toast relations */
+							 "\nSELECT NULL::INTEGER AS id, FALSE AS tbl,"
+							 "\nTRUE AS idx, oid, relpages"
+							 "\nFROM toast_index");
+	appendPQExpBufferStr(&sql,
+						 "\n) AS combined_records"
+						 "\nORDER BY relpages DESC NULLS FIRST, oid");
+
+	res = executeQuery(conn, sql.data, opts.echo);
+	if (PQresultStatus(res) != PGRES_TUPLES_OK)
+	{
+		pg_log_error("query failed: %s", PQerrorMessage(conn));
+		pg_log_error("query was: %s", sql.data);
+		disconnectDatabase(conn);
+		exit(1);
+	}
+	termPQExpBuffer(&sql);
+
+	/*
+	 * Allocate a single copy of the database name to be shared by all nodes
+	 * in the object list, constructed below.
+	 */
+	datname = pstrdup(PQdb(conn));
+
+	ntups = PQntuples(res);
+	for (i = 0; i < ntups; i++)
+	{
+		int			pattern_id = 0;
+		bool		tbl = false;
+		bool		idx = false;
+		Oid			oid = InvalidOid;
+
+		if (!PQgetisnull(res, i, 0))
+			pattern_id = atoi(PQgetvalue(res, i, 0));
+		if (!PQgetisnull(res, i, 1))
+			tbl = (PQgetvalue(res, i, 1)[0] == 't');
+		if (!PQgetisnull(res, i, 2))
+			idx = (PQgetvalue(res, i, 2)[0] == 't');
+		if (!PQgetisnull(res, i, 3))
+			oid = atooid(PQgetvalue(res, i, 3));
+
+		if (pattern_id > 0)
+		{
+			/*
+			 * Current record pertains to an inclusion pattern.  Find the
+			 * pattern in the list and update its counts.  If we expected a
+			 * large number of command-line inclusion pattern arguments, the
+			 * datastructure here might need to be more efficient, but we
+			 * expect the list to be short.
+			 */
+			SimplePtrListCell *cell;
+			bool		found;
+
+			for (found = false, cell = opts.include.head; cell; cell = cell->next)
+			{
+				PatternInfo *info = (PatternInfo *) cell->ptr;
+
+				if (info->pattern_id == pattern_id)
+				{
+					info->matched = true;
+					found = true;
+					break;
+				}
+			}
+			if (!found)
+			{
+				pg_log_error("internal error: received unexpected pattern_id %d",
+							 pattern_id);
+				exit(1);
+			}
+		}
+		else
+		{
+			/* Current record pertains to a relation */
+
+			RelationInfo *rel = (RelationInfo *) palloc0(sizeof(RelationInfo));
+
+			Assert(OidIsValid(oid));
+			Assert(!(tbl && idx));
+
+			rel->datinfo = dat;
+			rel->reloid = oid;
+			rel->is_table = tbl;
+
+			simple_ptr_list_append(relations, rel);
+		}
+	}
+	PQclear(res);
+}
diff --git a/contrib/pg_amcheck/t/001_basic.pl b/contrib/pg_amcheck/t/001_basic.pl
new file mode 100644
index 0000000000..dfa0ae9e06
--- /dev/null
+++ b/contrib/pg_amcheck/t/001_basic.pl
@@ -0,0 +1,9 @@
+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');
diff --git a/contrib/pg_amcheck/t/002_nonesuch.pl b/contrib/pg_amcheck/t/002_nonesuch.pl
new file mode 100644
index 0000000000..c8f5862372
--- /dev/null
+++ b/contrib/pg_amcheck/t/002_nonesuch.pl
@@ -0,0 +1,228 @@
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More tests => 66;
+
+# 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 connecting to a non-existent database
+
+# Failing to connect to the initial database is an error.
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'qqq' ],
+	qr/database "qqq" does not exist/,
+	'checking a non-existent database');
+
+# Failing to resolve a secondary database name is also an error, though since
+# the string is treated as a pattern, the error message looks different.
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'postgres', 'qqq' ],
+	qr/pg_amcheck: no checkable database: "qqq"/,
+	'checking a non-existent database');
+
+# Failing to connect to the initial database is still an error when using
+# --no-strict-names.
+command_fails_like(
+	[ 'pg_amcheck', '--no-strict-names', '-p', $port, 'qqq' ],
+	qr/database "qqq" does not exist/,
+	'checking a non-existent database with --no-strict-names');
+
+# But failing to resolve secondary database names is not an error when using
+# --no-strict-names.  We should still see the message, but as a non-fatal
+# warning
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-p', $port, '-d', 'no_such_database', 'postgres', 'qqq' ],
+	0,
+	[ ],
+	[ qr/pg_amcheck: checking database "postgres"/,
+	  qr/no checkable database: "qqq"/ ],
+	'checking a non-existent secondary database with --no-strict-names');
+
+# Check that a substring of an existent database name does not get interpreted
+# as a matching pattern.
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'post' ],
+	qr/database "post" does not exist/,
+	'checking a non-existent primary database (substring of existent database)');
+
+# And again, but testing the secondary database name rather than the primary
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'postgres', 'post' ],
+	qr/pg_amcheck: no checkable database: "post"/,
+	'checking a non-existent secondary database (substring of existent database)');
+
+# Likewise, check that a superstring of an existent database name does not get
+# interpreted as a matching pattern.
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'postresql' ],
+	qr/database "postresql" does not exist/,
+	'checking a non-existent primary database (superstring of existent database)');
+
+# And again, but testing the secondary database name rather than the primary
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, 'postgres', 'postgresql' ],
+	qr/pg_amcheck: no checkable database: "postgresql"/,
+	'checking a non-existent secondary database (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.
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-U=no_such_user', 'postgres' ],
+	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 still an
+# error when using --no-strict-names.
+command_fails_like(
+	[ 'pg_amcheck', '--no-strict-names', '-p', $port, '-U=no_such_user', 'postgres' ],
+	qr/role "=no_such_user" does not exist/,
+	'checking with a non-existent user');
+
+#########################################
+# Test checking databases without amcheck installed
+
+# Attempting to check a database by name where amcheck is not installed should
+# raise a warning.
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, 'template1' ],
+	0,
+	[],
+	[ qr/pg_amcheck: skipping database "template1": amcheck is not installed/ ],
+	'checking a database by name without amcheck installed');
+
+# Likewise, but by database pattern rather than by name.
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, '-d', '*', 'postgres' ],
+	0,
+	[],
+	[ qr/pg_amcheck: skipping database "template1": amcheck is not installed/ ],
+	'checking a database by dbname implication without amcheck installed');
+
+# And again, but by checking all databases.
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, '--all', 'postgres' ],
+	0,
+	[],
+	[ qr/pg_amcheck: skipping database "template1": amcheck is not installed/ ],
+	'checking a database by --all implication without amcheck installed');
+
+#########################################
+# Test unreasonable patterns
+
+# Check three-part unreasonable pattern that has zero-length names
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, 'postgres', '-t', '..' ],
+	1,
+	[ qr/^$/ ],
+	[ qr/pg_amcheck: no checkable database: "\.\."/ ],
+	'checking table pattern ".."');
+
+# Again, but with non-trivial schema and relation parts
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, 'postgres', '-t', '.foo.bar' ],
+	1,
+	[ qr/^$/ ],
+	[ qr/pg_amcheck: no checkable database: "\.foo\.bar"/ ],
+	'checking table pattern ".foo.bar"');
+
+# Check two-part unreasonable pattern that has zero-length names
+$node->command_checks_all(
+	[ 'pg_amcheck', '-p', $port, 'postgres', '-t', '.' ],
+	1,
+	[ qr/^$/ ],
+	[ qr/pg_amcheck: no tables to check for "\."/ ],
+	'checking table pattern "."');
+
+#########################################
+# Test checking non-existent schemas, tables, and indexes
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-s', 'no_such_schema' ],
+	qr/pg_amcheck: no relations to check in schemas for "no_such_schema"/,
+	'checking a non-existent schema');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-s', 'no_such_schema' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no relations to check in schemas for "no_such_schema"/ ],
+	'checking a non-existent schema with --no-strict-names -v');
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-t', 'no_such_table' ],
+	qr/pg_amcheck: no tables to check for "no_such_table"/,
+	'checking a non-existent table');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-t', 'no_such_table' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no tables to check for "no_such_table"/ ],
+	'checking a non-existent table with --no-strict-names -v');
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-i', 'no_such_index' ],
+	qr/pg_amcheck: no btree indexes to check for "no_such_index"/,
+	'checking a non-existent index');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-i', 'no_such_index' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no btree indexes to check for "no_such_index"/ ],
+	'checking a non-existent index with --no-strict-names -v');
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-s', 'no*such*schema*' ],
+	qr/pg_amcheck: no relations to check in schemas for "no\*such\*schema\*"/,
+	'no matching schemas');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-s', 'no*such*schema*' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no relations to check in schemas for "no\*such\*schema\*"/ ],
+	'no matching schemas with --no-strict-names -v');
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-t', 'no*such*table*' ],
+	qr/pg_amcheck: no tables to check for "no\*such\*table\*"/,
+	'no matching tables');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-t', 'no*such*table*' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no tables to check for "no\*such\*table\*"/ ],
+	'no matching tables with --no-strict-names -v');
+
+command_fails_like(
+	[ 'pg_amcheck', '-p', $port, '-i', 'no*such*index*' ],
+	qr/pg_amcheck: no btree indexes to check for "no\*such\*index\*"/,
+	'no matching indexes');
+
+$node->command_checks_all(
+	[ 'pg_amcheck', '--no-strict-names', '-v', '-p', $port, '-i', 'no*such*index*' ],
+	0,
+	[],
+	[ qr/pg_amcheck: in database "postgres": using amcheck version "\d+\.\d+" in schema ".+"/,
+	  qr/pg_amcheck: no btree indexes to check for "no\*such\*index\*"/ ],
+	'no matching indexes with --no-strict-names -v');
diff --git a/contrib/pg_amcheck/t/003_check.pl b/contrib/pg_amcheck/t/003_check.pl
new file mode 100644
index 0000000000..d100460ac9
--- /dev/null
+++ b/contrib/pg_amcheck/t/003_check.pl
@@ -0,0 +1,481 @@
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+use Test::More tests => 70;
+
+my ($node, $port);
+
+# Returns the filesystem path for the named relation.
+#
+# Assumes the test node is running
+sub relation_filepath($$)
+{
+	my ($dbname, $relname) = @_;
+
+	my $pgdata = $node->data_dir;
+	my $rel = $node->safe_psql($dbname,
+							   qq(SELECT pg_relation_filepath('$relname')));
+	die "path not found for relation $relname" unless defined $rel;
+	return "$pgdata/$rel";
+}
+
+# Returns the name of the toast relation associated with the named relation.
+#
+# Assumes the test node is running
+sub relation_toast($$)
+{
+	my ($dbname, $relname) = @_;
+
+	my $rel = $node->safe_psql($dbname, qq(
+		SELECT ct.relname
+			FROM pg_catalog.pg_class cr, pg_catalog.pg_class ct
+			WHERE cr.oid = '$relname'::regclass
+			  AND cr.reltoastrelid = ct.oid
+			));
+	return undef unless defined $rel;
+	return "pg_toast.$rel";
+}
+
+# Stops the test node, corrupts the first page of the named relation, and
+# restarts the node.
+#
+# Assumes the test node is running.
+sub corrupt_first_page($$)
+{
+	my ($dbname, $relname) = @_;
+	my $relpath = relation_filepath($dbname, $relname);
+
+	$node->stop;
+	my $fh;
+	open($fh, '+<', $relpath);
+	binmode $fh;
+	seek($fh, 32, 0);
+	syswrite($fh, '\x77\x77\x77\x77', 500);
+	close($fh);
+	$node->start;
+}
+
+# Stops the test node, unlinks the file from the filesystem that backs the
+# relation, and restarts the node.
+#
+# Assumes the test node is running
+sub remove_relation_file($$)
+{
+	my ($dbname, $relname) = @_;
+	my $relpath = relation_filepath($dbname, $relname);
+
+	$node->stop();
+	unlink($relpath);
+	$node->start;
+}
+
+# Stops the test node, unlinks the file from the filesystem that backs the
+# toast table (if any) corresponding to the given main table relation, and
+# restarts the node.
+#
+# Assumes the test node is running
+sub remove_toast_file($$)
+{
+	my ($dbname, $relname) = @_;
+	my $toastname = relation_toast($dbname, $relname);
+	remove_relation_file($dbname, $toastname) if ($toastname);
+}
+
+# Test set-up
+$node = get_new_node('test');
+$node->init;
+$node->start;
+$port = $node->port;
+
+for my $dbname (qw(db1 db2 db3))
+{
+	# Create the database
+	$node->safe_psql('postgres', qq(CREATE DATABASE $dbname));
+
+	# Load the amcheck extension, upon which pg_amcheck depends.  Put the
+	# extension in an unexpected location to test that pg_amcheck finds it
+	# correctly.  Create tables with names that look like pg_catalog names to
+	# check that pg_amcheck does not get confused by them.  Create functions in
+	# schema public that look like amcheck functions to check that pg_amcheck
+	# does not use them.
+	$node->safe_psql($dbname, q(
+		CREATE SCHEMA amcheck_schema;
+		CREATE EXTENSION amcheck WITH SCHEMA amcheck_schema;
+		CREATE TABLE amcheck_schema.pg_database (junk text);
+		CREATE TABLE amcheck_schema.pg_namespace (junk text);
+		CREATE TABLE amcheck_schema.pg_class (junk text);
+		CREATE TABLE amcheck_schema.pg_operator (junk text);
+		CREATE TABLE amcheck_schema.pg_proc (junk text);
+		CREATE TABLE amcheck_schema.pg_tablespace (junk text);
+
+		CREATE FUNCTION public.bt_index_check(index regclass,
+											  heapallindexed boolean default false)
+		RETURNS VOID AS $$
+		BEGIN
+			RAISE EXCEPTION 'Invoked wrong bt_index_check!';
+		END;
+		$$ LANGUAGE plpgsql;
+
+		CREATE FUNCTION public.bt_index_parent_check(index regclass,
+													 heapallindexed boolean default false,
+													 rootdescend boolean default false)
+		RETURNS VOID AS $$
+		BEGIN
+			RAISE EXCEPTION 'Invoked wrong bt_index_parent_check!';
+		END;
+		$$ LANGUAGE plpgsql;
+
+		CREATE FUNCTION public.verify_heapam(relation regclass,
+											 on_error_stop boolean default false,
+											 check_toast boolean default false,
+											 skip text default 'none',
+											 startblock bigint default null,
+											 endblock bigint default null,
+											 blkno OUT bigint,
+											 offnum OUT integer,
+											 attnum OUT integer,
+											 msg OUT text)
+		RETURNS SETOF record AS $$
+		BEGIN
+			RAISE EXCEPTION 'Invoked wrong verify_heapam!';
+		END;
+		$$ LANGUAGE plpgsql;
+	));
+
+	# Create schemas, tables and indexes in five separate
+	# schemas.  The schemas are all identical to start, but
+	# we will corrupt them differently later.
+	#
+	for my $schema (qw(s1 s2 s3 s4 s5))
+	{
+		$node->safe_psql($dbname, qq(
+			CREATE SCHEMA $schema;
+			CREATE SEQUENCE $schema.seq1;
+			CREATE SEQUENCE $schema.seq2;
+			CREATE TABLE $schema.t1 (
+				i INTEGER,
+				b BOX,
+				ia int4[],
+				ir int4range,
+				t TEXT
+			);
+			CREATE TABLE $schema.t2 (
+				i INTEGER,
+				b BOX,
+				ia int4[],
+				ir int4range,
+				t TEXT
+			);
+			CREATE VIEW $schema.t2_view AS (
+				SELECT i*2, t FROM $schema.t2
+			);
+			ALTER TABLE $schema.t2
+				ALTER COLUMN t
+				SET STORAGE EXTERNAL;
+
+			INSERT INTO $schema.t1 (i, b, ia, ir, t)
+				(SELECT gs::INTEGER AS i,
+						box(point(gs,gs+5),point(gs*2,gs*3)) AS b,
+						array[gs, gs + 1]::int4[] AS ia,
+						int4range(gs, gs+100) AS ir,
+						repeat('foo', gs) AS t
+					 FROM generate_series(1,10000,3000) AS gs);
+
+			INSERT INTO $schema.t2 (i, b, ia, ir, t)
+				(SELECT gs::INTEGER AS i,
+						box(point(gs,gs+5),point(gs*2,gs*3)) AS b,
+						array[gs, gs + 1]::int4[] AS ia,
+						int4range(gs, gs+100) AS ir,
+						repeat('foo', gs) AS t
+					 FROM generate_series(1,10000,3000) AS gs);
+
+			CREATE MATERIALIZED VIEW $schema.t1_mv AS SELECT * FROM $schema.t1;
+			CREATE MATERIALIZED VIEW $schema.t2_mv AS SELECT * FROM $schema.t2;
+
+			create table $schema.p1 (a int, b int) PARTITION BY list (a);
+			create table $schema.p2 (a int, b int) PARTITION BY list (a);
+
+			create table $schema.p1_1 partition of $schema.p1 for values in (1, 2, 3);
+			create table $schema.p1_2 partition of $schema.p1 for values in (4, 5, 6);
+			create table $schema.p2_1 partition of $schema.p2 for values in (1, 2, 3);
+			create table $schema.p2_2 partition of $schema.p2 for values in (4, 5, 6);
+
+			CREATE INDEX t1_btree ON $schema.t1 USING BTREE (i);
+			CREATE INDEX t2_btree ON $schema.t2 USING BTREE (i);
+
+			CREATE INDEX t1_hash ON $schema.t1 USING HASH (i);
+			CREATE INDEX t2_hash ON $schema.t2 USING HASH (i);
+
+			CREATE INDEX t1_brin ON $schema.t1 USING BRIN (i);
+			CREATE INDEX t2_brin ON $schema.t2 USING BRIN (i);
+
+			CREATE INDEX t1_gist ON $schema.t1 USING GIST (b);
+			CREATE INDEX t2_gist ON $schema.t2 USING GIST (b);
+
+			CREATE INDEX t1_gin ON $schema.t1 USING GIN (ia);
+			CREATE INDEX t2_gin ON $schema.t2 USING GIN (ia);
+
+			CREATE INDEX t1_spgist ON $schema.t1 USING SPGIST (ir);
+			CREATE INDEX t2_spgist ON $schema.t2 USING SPGIST (ir);
+		));
+	}
+}
+
+# Database 'db1' corruptions
+#
+
+# Corrupt indexes in schema "s1"
+remove_relation_file('db1', 's1.t1_btree');
+corrupt_first_page('db1', 's1.t2_btree');
+
+# Corrupt tables in schema "s2"
+remove_relation_file('db1', 's2.t1');
+corrupt_first_page('db1', 's2.t2');
+
+# Corrupt tables, partitions, matviews, and btrees in schema "s3"
+remove_relation_file('db1', 's3.t1');
+corrupt_first_page('db1', 's3.t2');
+
+remove_relation_file('db1', 's3.t1_mv');
+remove_relation_file('db1', 's3.p1_1');
+
+corrupt_first_page('db1', 's3.t2_mv');
+corrupt_first_page('db1', 's3.p2_1');
+
+remove_relation_file('db1', 's3.t1_btree');
+corrupt_first_page('db1', 's3.t2_btree');
+
+# Corrupt toast table, partitions, and materialized views in schema "s4"
+remove_toast_file('db1', 's4.t2');
+
+# Corrupt all other object types in schema "s5".  We don't have amcheck support
+# for these types, but we check that their corruption does not trigger any
+# errors in pg_amcheck
+remove_relation_file('db1', 's5.seq1');
+remove_relation_file('db1', 's5.t1_hash');
+remove_relation_file('db1', 's5.t1_gist');
+remove_relation_file('db1', 's5.t1_gin');
+remove_relation_file('db1', 's5.t1_brin');
+remove_relation_file('db1', 's5.t1_spgist');
+
+corrupt_first_page('db1', 's5.seq2');
+corrupt_first_page('db1', 's5.t2_hash');
+corrupt_first_page('db1', 's5.t2_gist');
+corrupt_first_page('db1', 's5.t2_gin');
+corrupt_first_page('db1', 's5.t2_brin');
+corrupt_first_page('db1', 's5.t2_spgist');
+
+
+# Database 'db2' corruptions
+#
+remove_relation_file('db2', 's1.t1');
+remove_relation_file('db2', 's1.t1_btree');
+
+
+# Leave 'db3' uncorrupted
+#
+
+
+# Standard first arguments to TestLib functions
+my @cmd = ('pg_amcheck', '--quiet', '-p', $port);
+
+# The pg_amcheck command itself should return a success exit status, even
+# though tables and indexes are corrupt.  An error code returned would mean the
+# pg_amcheck command itself failed, for example because a connection to the
+# database could not be established.
+#
+# For these checks, we're ignoring any corruption reported and focusing
+# exclusively on the exit code from pg_amcheck.
+#
+$node->command_ok(
+	[ @cmd,, 'db1' ],
+	'pg_amcheck all schemas, tables and indexes in database db1');
+
+$node->command_ok(
+	[ @cmd,, 'db1', 'db2', 'db3' ],
+	'pg_amcheck all schemas, tables and indexes in databases db1, db2 and db3');
+
+$node->command_ok(
+	[ @cmd, '--all' ],
+	'pg_amcheck all schemas, tables and indexes in all databases');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-s', 's1' ],
+	'pg_amcheck all objects in schema s1');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-r', 's*.t1' ],
+	'pg_amcheck all tables named t1 and their indexes');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-i', 's*.t*', '-i', 's*.*btree*' ],
+	'pg_amcheck all indexes with qualified names matching /s*.t*/ or /s*.*btree*/');
+
+$node->command_ok(
+	[ @cmd, '--no-dependents', 'db1', '-r', 's*.t1' ],
+	'pg_amcheck all relations with qualified names matching /s*.t1/');
+
+$node->command_ok(
+	[ @cmd, '--no-dependents', 'db1', '-t', 's*.t1' ],
+	'pg_amcheck all tables with qualified names matching /s*.t1/');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-T', 't1' ],
+	'pg_amcheck everything except tables named t1');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-S', 's1', '-R', 't1' ],
+	'pg_amcheck everything not named t1 nor in schema s1');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-t', '*.*.*' ],
+	'pg_amcheck all tables across all databases and schemas');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-t', '*.*.t1' ],
+	'pg_amcheck all tables named t1 across all databases and schemas');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-t', '*.s1.*' ],
+	'pg_amcheck all tables across all databases in schemas named s1');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-t', 'db2.*.*' ],
+	'pg_amcheck all tables across all schemas in database db2');
+
+$node->command_ok(
+	[ @cmd, 'db1', '-t', 'db2.*.*', '-t', 'db3.*.*' ],
+	'pg_amcheck all tables across all schemas in databases db2 and db3');
+
+# Scans of indexes in s1 should detect the specific corruption that we created
+# above.  For missing relation forks, we know what the error message looks
+# like.  For corrupted index pages, the error might vary depending on how the
+# page was formatted on disk, including variations due to alignment differences
+# between platforms, so we accept any non-empty error message.
+#
+
+$node->command_checks_all(
+	[ @cmd, '--all', '-s', 's1', '-i', 't1_btree' ],
+	0,
+	[ qr/index "t1_btree" lacks a main relation fork/ ],
+	[ qr/pg_amcheck: skipping database "postgres": amcheck is not installed/ ],
+	'pg_amcheck index s1.t1_btree reports missing main relation fork');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '-i', 't2_btree' ],
+	qr/.+/,			# Any non-empty error message is acceptable
+	'pg_amcheck index s1.s2 reports index corruption');
+
+# Checking db1.s1 should show no corruptions if indexes are excluded
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '--exclude-indexes' ],
+	qr/^$/,
+	'pg_amcheck of db1.s1 excluding indexes');
+
+# Checking db2.s1 should show table corruptions if indexes are excluded
+$node->command_checks_all(
+	[ @cmd, 'db2', '-s', 's1', '--exclude-indexes' ],
+	0,
+	[ qr/could not open file/ ],
+	[ qr/^$/ ],
+	'pg_amcheck of db2.s1 excluding indexes');
+
+# Checking db2.s1 should show table corruptions if indexes are excluded
+$node->command_checks_all(
+	[ @cmd, 'db3', 'db2', '-s', 's1', '--exclude-indexes' ],
+	0,
+	[ qr/could not open file/ ],
+	[ qr/^$/ ],
+	'pg_amcheck of db1.s1, db2.s1, and db3.s1, excluding indexes');
+
+# In schema s3, the tables and indexes are both corrupt.  We should see
+# corruption messages on stdout, nothing on stderr, and an exit
+# status of zero.
+#
+$node->command_checks_all(
+	[ @cmd, 'db1', '-s', 's3' ],
+	0,
+	[ qr/index "t1_btree" lacks a main relation fork/,
+	  qr/could not open file/ ],
+	[ qr/^$/ ],
+	'pg_amcheck schema s3 reports table and index errors');
+
+# In schema s2, only tables are corrupt.  Check that table corruption is
+# reported as expected.
+#
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's2', '-t', 't1' ],
+	qr/could not open file/,
+	'pg_amcheck in schema s2 reports table corruption');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's2', '-t', 't2' ],
+	qr/.+/,			# Any non-empty error message is acceptable
+	'pg_amcheck in schema s2 reports table corruption');
+
+# In schema s4, only toast tables are corrupt.  Check that under default
+# options the toast corruption is reported, but when excluding toast we get no
+# error reports.
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's4' ],
+	qr/could not open file/,
+	'pg_amcheck in schema s4 reports toast corruption');
+
+$node->command_like(
+	[ @cmd, '--exclude-toast', '--exclude-toast-pointers', 'db1', '-s', 's4' ],
+	qr/^$/,			# Empty
+	'pg_amcheck in schema s4 excluding toast reports no corruption');
+
+# Check that no corruption is reported in schema s5
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's5' ],
+	qr/^$/,			# Empty
+	'pg_amcheck over schema s5 reports no corruption');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '-I', 't1_btree', '-I', 't2_btree' ],
+	qr/^$/,			# Empty
+	'pg_amcheck over schema s1 with corrupt indexes excluded reports no corruption');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '--exclude-indexes' ],
+	qr/^$/,			# Empty
+	'pg_amcheck over schema s1 with all indexes excluded reports no corruption');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's2', '-T', 't1', '-T', 't2' ],
+	qr/^$/,			# Empty
+	'pg_amcheck over schema s2 with corrupt tables excluded reports no corruption');
+
+# Check errors about bad block range command line arguments.  We use schema s5
+# to avoid getting messages about corrupt tables or indexes.
+command_fails_like(
+	[ @cmd, 'db1', '-s', 's5', '--startblock', 'junk' ],
+	qr/relation starting block argument contains garbage characters/,
+	'pg_amcheck rejects garbage startblock');
+
+command_fails_like(
+	[ @cmd, 'db1', '-s', 's5', '--endblock', '1234junk' ],
+	qr/relation ending block argument contains garbage characters/,
+	'pg_amcheck rejects garbage endblock');
+
+command_fails_like(
+	[ @cmd, 'db1', '-s', 's5', '--startblock', '5', '--endblock', '4' ],
+	qr/relation ending block argument precedes starting block argument/,
+	'pg_amcheck rejects invalid block range');
+
+# Check bt_index_parent_check alternates.  We don't create any index corruption
+# that would behave differently under these modes, so just smoke test that the
+# arguments are handled sensibly.
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '-i', 't1_btree', '--parent-check' ],
+	qr/index "t1_btree" lacks a main relation fork/,
+	'pg_amcheck smoke test --parent-check');
+
+$node->command_like(
+	[ @cmd, 'db1', '-s', 's1', '-i', 't1_btree', '--heapallindexed', '--rootdescend' ],
+	qr/index "t1_btree" lacks a main relation fork/,
+	'pg_amcheck smoke test --heapallindexed --rootdescend');
diff --git a/contrib/pg_amcheck/t/004_verify_heapam.pl b/contrib/pg_amcheck/t/004_verify_heapam.pl
new file mode 100644
index 0000000000..a6d3bcfb78
--- /dev/null
+++ b/contrib/pg_amcheck/t/004_verify_heapam.pl
@@ -0,0 +1,496 @@
+use strict;
+use warnings;
+
+use PostgresNode;
+use TestLib;
+
+use Test::More tests => 22;
+
+# This regression test demonstrates that the pg_amcheck binary supplied with
+# the pg_amcheck contrib module correctly identifies specific kinds of
+# corruption within pages.  To test this, we need a mechanism to create corrupt
+# pages with predictable, repeatable corruption.  The postgres backend cannot
+# be expected to help us with this, as its design is not consistent with the
+# goal of intentionally corrupting pages.
+#
+# Instead, we create a table to corrupt, and with careful consideration of how
+# postgresql lays out heap pages, we seek to offsets within the page and
+# overwrite deliberately chosen bytes with specific values calculated to
+# corrupt the page in expected ways.  We then verify that pg_amcheck reports
+# the corruption, and that it runs without crashing.  Note that the backend
+# cannot simply be started to run queries against the corrupt table, as the
+# backend will crash, at least for some of the corruption types we generate.
+#
+# Autovacuum potentially touching the table in the background makes the exact
+# behavior of this test harder to reason about.  We turn it off to keep things
+# simpler.  We use a "belt and suspenders" approach, turning it off for the
+# system generally in postgresql.conf, and turning it off specifically for the
+# test table.
+#
+# This test depends on the table being written to the heap file exactly as we
+# expect it to be, so we take care to arrange the columns of the table, and
+# insert rows of the table, that give predictable sizes and locations within
+# the table page.
+#
+# The HeapTupleHeaderData has 23 bytes of fixed size fields before the variable
+# length t_bits[] array.  We have exactly 3 columns in the table, so natts = 3,
+# t_bits is 1 byte long, and t_hoff = MAXALIGN(23 + 1) = 24.
+#
+# We're not too fussy about which datatypes we use for the test, but we do care
+# about some specific properties.  We'd like to test both fixed size and
+# varlena types.  We'd like some varlena data inline and some toasted.  And
+# we'd like the layout of the table such that the datums land at predictable
+# offsets within the tuple.  We choose a structure without padding on all
+# supported architectures:
+#
+# 	a BIGINT
+#	b TEXT
+#	c TEXT
+#
+# We always insert a 7-ascii character string into field 'b', which with a
+# 1-byte varlena header gives an 8 byte inline value.  We always insert a long
+# text string in field 'c', long enough to force toast storage.
+#
+# We choose to read and write binary copies of our table's tuples, using perl's
+# pack() and unpack() functions.  Perl uses a packing code system in which:
+#
+#	L = "Unsigned 32-bit Long",
+#	S = "Unsigned 16-bit Short",
+#	C = "Unsigned 8-bit Octet",
+#	c = "signed 8-bit octet",
+#	q = "signed 64-bit quadword"
+#
+# Each tuple in our table has a layout as follows:
+#
+#    xx xx xx xx            t_xmin: xxxx		offset = 0		L
+#    xx xx xx xx            t_xmax: xxxx		offset = 4		L
+#    xx xx xx xx          t_field3: xxxx		offset = 8		L
+#    xx xx                   bi_hi: xx			offset = 12		S
+#    xx xx                   bi_lo: xx			offset = 14		S
+#    xx xx                ip_posid: xx			offset = 16		S
+#    xx xx             t_infomask2: xx			offset = 18		S
+#    xx xx              t_infomask: xx			offset = 20		S
+#    xx                     t_hoff: x			offset = 22		C
+#    xx                     t_bits: x			offset = 23		C
+#    xx xx xx xx xx xx xx xx   'a': xxxxxxxx	offset = 24		q
+#    xx xx xx xx xx xx xx xx   'b': xxxxxxxx	offset = 32		Cccccccc
+#    xx xx xx xx xx xx xx xx   'c': xxxxxxxx	offset = 40		SSSS
+#    xx xx xx xx xx xx xx xx      : xxxxxxxx	 ...continued	SSSS
+#    xx xx                        : xx      	 ...continued	S
+#
+# We could choose to read and write columns 'b' and 'c' in other ways, but
+# it is convenient enough to do it this way.  We define packing code
+# constants here, where they can be compared easily against the layout.
+
+use constant HEAPTUPLE_PACK_CODE => 'LLLSSSSSCCqCcccccccSSSSSSSSS';
+use constant HEAPTUPLE_PACK_LENGTH => 58;     # Total size
+
+# Read a tuple of our table from a heap page.
+#
+# Takes an open filehandle to the heap file, and the offset of the tuple.
+#
+# Rather than returning the binary data from the file, unpacks the data into a
+# perl hash with named fields.  These fields exactly match the ones understood
+# by write_tuple(), below.  Returns a reference to this hash.
+#
+sub read_tuple ($$)
+{
+	my ($fh, $offset) = @_;
+	my ($buffer, %tup);
+	seek($fh, $offset, 0);
+	sysread($fh, $buffer, HEAPTUPLE_PACK_LENGTH);
+
+	@_ = unpack(HEAPTUPLE_PACK_CODE, $buffer);
+	%tup = (t_xmin => shift,
+			t_xmax => shift,
+			t_field3 => shift,
+			bi_hi => shift,
+			bi_lo => shift,
+			ip_posid => shift,
+			t_infomask2 => shift,
+			t_infomask => shift,
+			t_hoff => shift,
+			t_bits => shift,
+			a => shift,
+			b_header => shift,
+			b_body1 => shift,
+			b_body2 => shift,
+			b_body3 => shift,
+			b_body4 => shift,
+			b_body5 => shift,
+			b_body6 => shift,
+			b_body7 => shift,
+			c1 => shift,
+			c2 => shift,
+			c3 => shift,
+			c4 => shift,
+			c5 => shift,
+			c6 => shift,
+			c7 => shift,
+			c8 => shift,
+			c9 => shift);
+	# Stitch together the text for column 'b'
+	$tup{b} = join('', map { chr($tup{"b_body$_"}) } (1..7));
+	return \%tup;
+}
+
+# Write a tuple of our table to a heap page.
+#
+# Takes an open filehandle to the heap file, the offset of the tuple, and a
+# reference to a hash with the tuple values, as returned by read_tuple().
+# Writes the tuple fields from the hash into the heap file.
+#
+# The purpose of this function is to write a tuple back to disk with some
+# subset of fields modified.  The function does no error checking.  Use
+# cautiously.
+#
+sub write_tuple($$$)
+{
+	my ($fh, $offset, $tup) = @_;
+	my $buffer = pack(HEAPTUPLE_PACK_CODE,
+					$tup->{t_xmin},
+					$tup->{t_xmax},
+					$tup->{t_field3},
+					$tup->{bi_hi},
+					$tup->{bi_lo},
+					$tup->{ip_posid},
+					$tup->{t_infomask2},
+					$tup->{t_infomask},
+					$tup->{t_hoff},
+					$tup->{t_bits},
+					$tup->{a},
+					$tup->{b_header},
+					$tup->{b_body1},
+					$tup->{b_body2},
+					$tup->{b_body3},
+					$tup->{b_body4},
+					$tup->{b_body5},
+					$tup->{b_body6},
+					$tup->{b_body7},
+					$tup->{c1},
+					$tup->{c2},
+					$tup->{c3},
+					$tup->{c4},
+					$tup->{c5},
+					$tup->{c6},
+					$tup->{c7},
+					$tup->{c8},
+					$tup->{c9});
+	seek($fh, $offset, 0);
+	syswrite($fh, $buffer, HEAPTUPLE_PACK_LENGTH);
+	return;
+}
+
+# Set umask so test directories and files are created with default permissions
+umask(0077);
+
+# Set up the node.  Once we create and corrupt the table,
+# autovacuum workers visiting the table could crash the backend.
+# Disable autovacuum so that won't happen.
+my $node = get_new_node('test');
+$node->init;
+$node->append_conf('postgresql.conf', 'autovacuum=off');
+
+# Start the node and load the extensions.  We depend on both
+# amcheck and pageinspect for this test.
+$node->start;
+my $port = $node->port;
+my $pgdata = $node->data_dir;
+$node->safe_psql('postgres', "CREATE EXTENSION amcheck");
+$node->safe_psql('postgres', "CREATE EXTENSION pageinspect");
+
+# Get a non-zero datfrozenxid
+$node->safe_psql('postgres', qq(VACUUM FREEZE));
+
+# Create the test table with precisely the schema that our corruption function
+# expects.
+$node->safe_psql(
+	'postgres', qq(
+		CREATE TABLE public.test (a BIGINT, b TEXT, c TEXT);
+		ALTER TABLE public.test SET (autovacuum_enabled=false);
+		ALTER TABLE public.test ALTER COLUMN c SET STORAGE EXTERNAL;
+		CREATE INDEX test_idx ON public.test(a, b);
+	));
+
+# We want (0 < datfrozenxid < test.relfrozenxid).  To achieve this, we freeze
+# an otherwise unused table, public.junk, prior to inserting data and freezing
+# public.test
+$node->safe_psql(
+	'postgres', qq(
+		CREATE TABLE public.junk AS SELECT 'junk'::TEXT AS junk_column;
+		ALTER TABLE public.junk SET (autovacuum_enabled=false);
+		VACUUM FREEZE public.junk
+	));
+
+my $rel = $node->safe_psql('postgres', qq(SELECT pg_relation_filepath('public.test')));
+my $relpath = "$pgdata/$rel";
+
+# Insert data and freeze public.test
+use constant ROWCOUNT => 16;
+$node->safe_psql('postgres', qq(
+	INSERT INTO public.test (a, b, c)
+		VALUES (
+			12345678,
+			'abcdefg',
+			repeat('w', 10000)
+		);
+	VACUUM FREEZE public.test
+	)) for (1..ROWCOUNT);
+
+my $relfrozenxid = $node->safe_psql('postgres',
+	q(select relfrozenxid from pg_class where relname = 'test'));
+my $datfrozenxid = $node->safe_psql('postgres',
+	q(select datfrozenxid from pg_database where datname = 'postgres'));
+
+# Find where each of the tuples is located on the page.
+my @lp_off;
+for my $tup (0..ROWCOUNT-1)
+{
+	push (@lp_off, $node->safe_psql('postgres', qq(
+select lp_off from heap_page_items(get_raw_page('test', 'main', 0))
+	offset $tup limit 1)));
+}
+
+# Check that pg_amcheck runs against the uncorrupted table without error.
+$node->command_ok(['pg_amcheck', '-p', $port, 'postgres'],
+				  'pg_amcheck test table, prior to corruption');
+
+# Check that pg_amcheck runs against the uncorrupted table and index without error.
+$node->command_ok(['pg_amcheck', '-p', $port, 'postgres'],
+				  'pg_amcheck test table and index, prior to corruption');
+
+$node->stop;
+
+# Sanity check that our 'test' table has a relfrozenxid newer than the
+# datfrozenxid for the database, and that the datfrozenxid is greater than the
+# first normal xid.  We rely on these invariants in some of our tests.
+if ($datfrozenxid <= 3 || $datfrozenxid >= $relfrozenxid)
+{
+	fail('Xid thresholds not as expected');
+	$node->clean_node;
+	exit;
+}
+
+# Some #define constants from access/htup_details.h for use while corrupting.
+use constant HEAP_HASNULL            => 0x0001;
+use constant HEAP_XMAX_LOCK_ONLY     => 0x0080;
+use constant HEAP_XMIN_COMMITTED     => 0x0100;
+use constant HEAP_XMIN_INVALID       => 0x0200;
+use constant HEAP_XMAX_COMMITTED     => 0x0400;
+use constant HEAP_XMAX_INVALID       => 0x0800;
+use constant HEAP_NATTS_MASK         => 0x07FF;
+use constant HEAP_XMAX_IS_MULTI      => 0x1000;
+use constant HEAP_KEYS_UPDATED       => 0x2000;
+
+# Helper function to generate a regular expression matching the header we
+# expect verify_heapam() to return given which fields we expect to be non-null.
+sub header
+{
+	my ($blkno, $offnum, $attnum) = @_;
+	return qr/relation postgres\.public\.test, block $blkno, offset $offnum, attribute $attnum\s+/ms
+		if (defined $attnum);
+	return qr/relation postgres\.public\.test, block $blkno, offset $offnum\s+/ms
+		if (defined $offnum);
+	return qr/relation postgres\.public\.test\s+/ms
+		if (defined $blkno);
+	return qr/relation postgres\.public\.test\s+/ms;
+}
+
+# Corrupt the tuples, one type of corruption per tuple.  Some types of
+# corruption cause verify_heapam to skip to the next tuple without
+# performing any remaining checks, so we can't exercise the system properly if
+# we focus all our corruption on a single tuple.
+#
+my @expected;
+my $file;
+open($file, '+<', $relpath);
+binmode $file;
+
+for (my $tupidx = 0; $tupidx < ROWCOUNT; $tupidx++)
+{
+	my $offnum = $tupidx + 1;  # offnum is 1-based, not zero-based
+	my $offset = $lp_off[$tupidx];
+	my $tup = read_tuple($file, $offset);
+
+	# Sanity-check that the data appears on the page where we expect.
+	if ($tup->{a} ne '12345678' || $tup->{b} ne 'abcdefg')
+	{
+		fail('Page layout differs from our expectations');
+		$node->clean_node;
+		exit;
+	}
+
+	my $header = header(0, $offnum, undef);
+	if ($offnum == 1)
+	{
+		# Corruptly set xmin < relfrozenxid
+		my $xmin = $relfrozenxid - 1;
+		$tup->{t_xmin} = $xmin;
+		$tup->{t_infomask} &= ~HEAP_XMIN_COMMITTED;
+		$tup->{t_infomask} &= ~HEAP_XMIN_INVALID;
+
+		# Expected corruption report
+		push @expected,
+			qr/${header}xmin $xmin precedes relation freeze threshold 0:\d+/;
+	}
+	if ($offnum == 2)
+	{
+		# Corruptly set xmin < datfrozenxid
+		my $xmin = 3;
+		$tup->{t_xmin} = $xmin;
+		$tup->{t_infomask} &= ~HEAP_XMIN_COMMITTED;
+		$tup->{t_infomask} &= ~HEAP_XMIN_INVALID;
+
+		push @expected,
+			qr/${$header}xmin $xmin precedes oldest valid transaction ID 0:\d+/;
+	}
+	elsif ($offnum == 3)
+	{
+		# Corruptly set xmin < datfrozenxid, further back, noting circularity
+		# of xid comparison.  For a new cluster with epoch = 0, the corrupt
+		# xmin will be interpreted as in the future
+		$tup->{t_xmin} = 4026531839;
+		$tup->{t_infomask} &= ~HEAP_XMIN_COMMITTED;
+		$tup->{t_infomask} &= ~HEAP_XMIN_INVALID;
+
+		push @expected,
+			qr/${$header}xmin 4026531839 equals or exceeds next valid transaction ID 0:\d+/;
+	}
+	elsif ($offnum == 4)
+	{
+		# Corruptly set xmax < relminmxid;
+		$tup->{t_xmax} = 4026531839;
+		$tup->{t_infomask} &= ~HEAP_XMAX_INVALID;
+
+		push @expected,
+			qr/${$header}xmax 4026531839 equals or exceeds next valid transaction ID 0:\d+/;
+	}
+	elsif ($offnum == 5)
+	{
+		# Corrupt the tuple t_hoff, but keep it aligned properly
+		$tup->{t_hoff} += 128;
+
+		push @expected,
+			qr/${$header}data begins at offset 152 beyond the tuple length 58/,
+			qr/${$header}tuple data should begin at byte 24, but actually begins at byte 152 \(3 attributes, no nulls\)/;
+	}
+	elsif ($offnum == 6)
+	{
+		# Corrupt the tuple t_hoff, wrong alignment
+		$tup->{t_hoff} += 3;
+
+		push @expected,
+			qr/${$header}tuple data should begin at byte 24, but actually begins at byte 27 \(3 attributes, no nulls\)/;
+	}
+	elsif ($offnum == 7)
+	{
+		# Corrupt the tuple t_hoff, underflow but correct alignment
+		$tup->{t_hoff} -= 8;
+
+		push @expected,
+			qr/${$header}tuple data should begin at byte 24, but actually begins at byte 16 \(3 attributes, no nulls\)/;
+	}
+	elsif ($offnum == 8)
+	{
+		# Corrupt the tuple t_hoff, underflow and wrong alignment
+		$tup->{t_hoff} -= 3;
+
+		push @expected,
+			qr/${$header}tuple data should begin at byte 24, but actually begins at byte 21 \(3 attributes, no nulls\)/;
+	}
+	elsif ($offnum == 9)
+	{
+		# Corrupt the tuple to look like it has lots of attributes, not just 3
+		$tup->{t_infomask2} |= HEAP_NATTS_MASK;
+
+		push @expected,
+			qr/${$header}number of attributes 2047 exceeds maximum expected for table 3/;
+	}
+	elsif ($offnum == 10)
+	{
+		# Corrupt the tuple to look like it has lots of attributes, some of
+		# them null.  This falsely creates the impression that the t_bits
+		# array is longer than just one byte, but t_hoff still says otherwise.
+		$tup->{t_infomask} |= HEAP_HASNULL;
+		$tup->{t_infomask2} |= HEAP_NATTS_MASK;
+		$tup->{t_bits} = 0xAA;
+
+		push @expected,
+			qr/${$header}tuple data should begin at byte 280, but actually begins at byte 24 \(2047 attributes, has nulls\)/;
+	}
+	elsif ($offnum == 11)
+	{
+		# Same as above, but this time t_hoff plays along
+		$tup->{t_infomask} |= HEAP_HASNULL;
+		$tup->{t_infomask2} |= (HEAP_NATTS_MASK & 0x40);
+		$tup->{t_bits} = 0xAA;
+		$tup->{t_hoff} = 32;
+
+		push @expected,
+			qr/${$header}number of attributes 67 exceeds maximum expected for table 3/;
+	}
+	elsif ($offnum == 12)
+	{
+		# Corrupt the bits in column 'b' 1-byte varlena header
+		$tup->{b_header} = 0x80;
+
+		$header = header(0, $offnum, 1);
+		push @expected,
+			qr/${header}attribute 1 with length 4294967295 ends at offset 416848000 beyond total tuple length 58/;
+	}
+	elsif ($offnum == 13)
+	{
+		# Corrupt the bits in column 'c' toast pointer
+		$tup->{c6} = 41;
+		$tup->{c7} = 41;
+
+		$header = header(0, $offnum, 2);
+		push @expected,
+			qr/${header}final toast chunk number 0 differs from expected value 6/,
+			qr/${header}toasted value for attribute 2 missing from toast table/;
+	}
+	elsif ($offnum == 14)
+	{
+		# Set both HEAP_XMAX_LOCK_ONLY and HEAP_KEYS_UPDATED
+		$tup->{t_infomask} |= HEAP_XMAX_LOCK_ONLY;
+		$tup->{t_infomask2} |= HEAP_KEYS_UPDATED;
+
+		push @expected,
+			qr/${header}tuple is marked as only locked, but also claims key columns were updated/;
+	}
+	elsif ($offnum == 15)
+	{
+		# Set both HEAP_XMAX_COMMITTED and HEAP_XMAX_IS_MULTI
+		$tup->{t_infomask} |= HEAP_XMAX_COMMITTED;
+		$tup->{t_infomask} |= HEAP_XMAX_IS_MULTI;
+		$tup->{t_xmax} = 4;
+
+		push @expected,
+			qr/${header}multitransaction ID 4 equals or exceeds next valid multitransaction ID 1/;
+	}
+	elsif ($offnum == 16)	# Last offnum must equal ROWCOUNT
+	{
+		# Set both HEAP_XMAX_COMMITTED and HEAP_XMAX_IS_MULTI
+		$tup->{t_infomask} |= HEAP_XMAX_COMMITTED;
+		$tup->{t_infomask} |= HEAP_XMAX_IS_MULTI;
+		$tup->{t_xmax} = 4000000000;
+
+		push @expected,
+			qr/${header}multitransaction ID 4000000000 precedes relation minimum multitransaction ID threshold 1/;
+	}
+	write_tuple($file, $offset, $tup);
+}
+close($file);
+$node->start;
+
+# Run pg_amcheck against the corrupt table with epoch=0, comparing actual
+# corruption messages against the expected messages
+$node->command_checks_all(
+	['pg_amcheck', '--exclude-indexes', '-p', $port, 'postgres'],
+	0,
+	[ @expected ],
+	[ qr/pg_amcheck: checking database "postgres"/ ],
+	'Expected corruption message output');
+
+$node->teardown_node;
+$node->clean_node;
diff --git a/contrib/pg_amcheck/t/005_opclass_damage.pl b/contrib/pg_amcheck/t/005_opclass_damage.pl
new file mode 100644
index 0000000000..0c39f2f638
--- /dev/null
+++ b/contrib/pg_amcheck/t/005_opclass_damage.pl
@@ -0,0 +1,54 @@
+# 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 => 6;
+
+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' ],
+	0,
+	[ qr/item order invariant violated for index "fickleidx"/ ],
+	[ qr/pg_amcheck: checking database "postgres"/ ],
+	'pg_amcheck all schemas, tables and indexes reports fickleidx corruption'
+);
diff --git a/doc/src/sgml/contrib.sgml b/doc/src/sgml/contrib.sgml
index d3ca4b6932..7e101f7c11 100644
--- a/doc/src/sgml/contrib.sgml
+++ b/doc/src/sgml/contrib.sgml
@@ -185,6 +185,7 @@ pages.
   </para>
 
  &oid2name;
+ &pgamcheck;
  &vacuumlo;
  </sect1>
 
diff --git a/doc/src/sgml/filelist.sgml b/doc/src/sgml/filelist.sgml
index db1d369743..5115cb03d0 100644
--- a/doc/src/sgml/filelist.sgml
+++ b/doc/src/sgml/filelist.sgml
@@ -133,6 +133,7 @@
 <!ENTITY oldsnapshot     SYSTEM "oldsnapshot.sgml">
 <!ENTITY pageinspect     SYSTEM "pageinspect.sgml">
 <!ENTITY passwordcheck   SYSTEM "passwordcheck.sgml">
+<!ENTITY pgamcheck       SYSTEM "pgamcheck.sgml">
 <!ENTITY pgbuffercache   SYSTEM "pgbuffercache.sgml">
 <!ENTITY pgcrypto        SYSTEM "pgcrypto.sgml">
 <!ENTITY pgfreespacemap  SYSTEM "pgfreespacemap.sgml">
diff --git a/doc/src/sgml/pgamcheck.sgml b/doc/src/sgml/pgamcheck.sgml
new file mode 100644
index 0000000000..e7bd066566
--- /dev/null
+++ b/doc/src/sgml/pgamcheck.sgml
@@ -0,0 +1,1029 @@
+<!-- doc/src/sgml/pgamcheck.sgml -->
+
+<refentry id="pgamcheck">
+ <indexterm zone="pgamcheck">
+  <primary>pg_amcheck</primary>
+ </indexterm>
+
+ <refmeta>
+  <refentrytitle><application>pg_amcheck</application></refentrytitle>
+  <manvolnum>1</manvolnum>
+  <refmiscinfo>Application</refmiscinfo>
+ </refmeta>
+
+ <refnamediv>
+  <refname>pg_amcheck</refname>
+  <refpurpose>checks for corruption in one or more
+  <productname>PostgreSQL</productname> databases</refpurpose>
+ </refnamediv>
+
+ <refsynopsisdiv>
+  <cmdsynopsis>
+   <command>pg_amcheck</command>
+   <arg rep="repeat"><replaceable>option</replaceable></arg>
+   <arg rep="repeat"><replaceable>dbname</replaceable></arg>
+  </cmdsynopsis>
+ </refsynopsisdiv>
+
+ <refsect1>
+  <title>Description</title>
+
+  <para>
+   <application>pg_amcheck</application> supports running
+   <xref linkend="amcheck"/>'s corruption checking functions against one or
+   more databases, with options to select which schemas, tables and indexes to
+   check, which kinds of checking to perform, and whether to perform the checks
+   in parallel, and if so, the number of parallel connections to establish and
+   use.
+  </para>
+
+  <para>
+   Only table relations and btree indexes are currently supported.  Other
+   relation types are silently skipped.
+  </para>
+
+ </refsect1>
+
+ <refsect1>
+  <title>Usage</title>
+
+  <refsect2>
+   <title>Parallelism Options</title>
+
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><literal>pg_amcheck --jobs=20 --all</literal></term>
+      <listitem>
+       <para>
+        Check relations in all connectable databases, using up to 20
+        simultaneous connections to check databases and relations in parallel.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --jobs=8 mydb yourdb</literal></term>
+      <listitem>
+       <para>
+        Check relations in databases <literal>mydb</literal> and
+        <literal>yourdb</literal>, using up to 8 simultaneous connections to
+        check relations in parallel.
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+  </refsect2>
+
+  <refsect2>
+   <title>Checking Option Specification</title>
+
+   <para>
+    If no checking options are specified, by default all table relation checks
+    and default level btree index checks are performed.  A variety of options
+    exist to change the set of checks performed on whichever relations are
+    being checked.  They are briefly mentioned here in the following examples,
+    but see their full descriptions below.
+   </para>
+
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><literal>pg_amcheck --parent-check --heapallindexed</literal></term>
+      <listitem>
+       <para>
+        For each btree index checked, performs more extensive checks.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --exclude-toast-pointers</literal></term>
+      <listitem>
+       <para>
+        For each table relation checked, do not check toast pointers against
+        the toast relation.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --on-error-stop</literal></term>
+      <listitem>
+       <para>
+        For each table relation checked, do not continue checking pages after
+        the first page where corruption is encountered.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --skip="all-frozen"</literal></term>
+      <listitem>
+       <para>
+        For each table relation checked, skips over blocks marked as all
+        frozen.  Note that "all-visible" may also be specified.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --startblock=3000 --endblock=4000</literal></term>
+      <listitem>
+       <para>
+        For each table relation checked, check only blocks in the given block
+        range.
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+  </refsect2>
+
+  <refsect2>
+   <title>Relation Specification</title>
+
+   <para>
+    If no relations are explicitly listed, by default all relations will be
+    checked, but there are options to specify which relations to check.
+   </para>
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><literal>pg_amcheck -r mytable -r yourtable</literal></term>
+      <listitem>
+       <para>
+        If one or more relations are explicitly given, they are interpreted as
+        an exhaustive list of all relations to be checked, with one caveat:
+        for all such relations, associated toast relations and indexes are by
+        default included in the list of relations to check.
+       </para>
+       <para>
+        Assuming <literal>mytable</literal> is an ordinary table, and that it
+        is indexed by <literal>mytable_idx</literal> and has an associated
+        toast table <literal>pg_toast_12345</literal>, checking will be
+        performed on <literal>mytable</literal>,
+        <literal>mytable_idx</literal>, and <literal>pg_toast_12345</literal>.
+       </para>
+       <para>
+        Likewise for <literal>yourtable</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -r mytable --no-dependents</literal></term>
+      <listitem>
+       <para>
+        This restricts the list of relations checked to just
+        <literal>mytable</literal>, without pulling in the corresponding
+        indexes or toast, but see also
+        <option>--exclude-toast-pointers</option>.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -t mytable -i myindex</literal></term>
+      <listitem>
+       <para>
+        The <option>-r</option> (<option>--relation</option>) will match any
+        relation, but <option>-t</option> (<option>--table</option>) and
+        <option>-i</option> (<option>--index</option>) may be used to avoid
+        matching objects of the other type.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -r="my*" -R="mytemp*"</literal></term>
+      <listitem>
+       <para>
+        Relations may be included (<option>-r</option>) or excluded
+        (<option>-R</option>) using shell-style patterns.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -r="my*" -I="myanmar"</literal></term>
+      <listitem>
+       <para>
+        Table and index inclusion and exclusion patterns may be used
+        equivalently with <option>-t</option>, <option>-T</option>,
+        <option>-i</option> and <option>-I</option>.  The above example checks
+        all tables and indexes starting with <literal>my</literal> except for
+        indexes named <literal>myanmar</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -R="india" -T="laos" -I="myanmar"</literal></term>
+      <listitem>
+       <para>
+        Unlike specifying one ore more <option>--relation</option> options,
+        which disables the default behavior of checking all relations,
+        specifying one or more of <option>-R</option>, <option>-T</option> or
+        <option>-I</option> does not.  The above command will check all
+        relations not named <literal>india</literal>, not a table named
+        <literal>laos</literal>, nor an index named <literal>myanmar</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+  </refsect2>
+
+  <refsect2>
+   <title>Schema Specification</title>
+
+   <para>
+    If no schemas are explicitly listed, by default all schemas will be
+    checked.
+   </para>
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><literal>pg_amcheck -s s1 -s s2</literal></term>
+      <listitem>
+       <para>
+        If one or more schemas are listed with <option>-s</option>, all
+        relations in the given schemas are selected.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -S s1 -S s2 -r mytable</literal></term>
+      <listitem>
+       <para>
+        As with relations, schemas may be excluded.  The above command will
+        check any table named <literal>mytable</literal> not in schemas
+        <literal>s1</literal> nor in <literal>s2</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -t s3.japan -T s3.korea</literal></term>
+      <listitem>
+       <para>
+        Relations may be included or excluded with a schema-qualified name.
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+  </refsect2>
+
+  <refsect2>
+   <title>Database Specification</title>
+
+   <para>
+    If no databases are explicitly listed, the database to check is obtained
+    from environment variables in the usual way.  Otherwise, when one or more
+    databases are explicitly given, they are interpreted as an exhaustive list
+    of all databases to be checked.  This list of databases to check may
+    contain patterns, but because any such patterns need to be reconciled
+    against a list of all databases to find the matching database names, at
+    least one database specified must be a literal database name and not merely
+    a pattern, and the one so specified must be in a location where
+    <application>pg_amcheck</application> expects to find it.
+   </para>
+   <para>
+    Databases will be checked in the order they are listed.  When performing
+    checking in parallel, the checks of relations in multiple databases may
+    overlap.
+   </para>
+   <para>
+    For example:
+   </para>
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><literal>pg_amcheck db1 db2 db3 --maintenance-db=foo</literal></term>
+      <listitem>
+       <para>
+        If the <option>--maintenance-db</option> option is given, it will be
+        used to look up the matching databases, though it will not by virtue of
+        having been listed as the <option>--maintenance-db</option> be added to
+        the list of databases for checking.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck foo bar baz</literal></term>
+      <listitem>
+       <para>
+        Otherwise, if one or more plain database name arguments not preceded by
+        <option>-d</option> or <option>--dbname</option> are given, the first
+        one will be used for this purpose, and it will also be included in the
+        list of databases to check.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck -d foo -d bar baz</literal></term>
+      <listitem>
+       <para>
+        If a mixture of plain database names and databases preceded with
+        <option>-d</option> or <option>--dbname</option> are given, the first
+        plain database name will be used for this purpose.  In the above
+        example, <literal>baz</literal> will be used.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --dbname=foo --dbname="bar*"</literal></term>
+      <listitem>
+       <para>
+        Otherwise, if one or more databases are given with the
+        <option>-d</option> or <option>--dbname</option> option, the first one
+        will be used and must be a literal database name.  In this example,
+        <literal>foo</literal> will be used.
+       </para>
+      </listitem>
+     </varlistentry>
+     <varlistentry>
+      <term><literal>pg_amcheck --relation="accounts_*.*.*"</literal></term>
+      <listitem>
+       <para>
+        Otherwise, the environment will be consulted for the database to be
+        used.  In the example above, the default database will be queried to
+        find all databases with names that begin with
+        <literal>accounts_</literal>.
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+
+   <para>
+    As discussed above for schema-qualified relations, a database-qualified
+    relation name or pattern may also be given.
+<programlisting>
+pg_amcheck mydb \
+           --schema="t*" \
+           --exclude-schema="tmp*" \
+           --relation=baz \
+           --relation=bar.baz \
+           --relation=foo.bar.baz \
+           --relation="f*.a.b" \
+           --exclude-relation=foo.a.b
+</programlisting>
+    will check relations in database <literal>mydb</literal> using the schema
+    resolution rules discussed above, but additionally will check all relations
+    named <literal>a.b</literal> in all databases with names starting with
+    <literal>f</literal> except database <literal>foo</literal>.
+   </para>
+
+  </refsect2>
+ </refsect1>
+
+ <refsect1>
+  <title>Options</title>
+
+  <para>
+   <application>pg_amcheck</application> accepts the following command-line
+   arguments:
+  </para>
+
+  <refsect2>
+   <title>Help and Version Information Options</title>
+
+   <variablelist>
+    <varlistentry>
+     <term><option>-?</option></term>
+     <term><option>--help</option></term>
+     <listitem>
+      <para>
+       Show help about <application>pg_amcheck</application> command line
+       arguments, and exit.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-V</option></term>
+     <term><option>--version</option></term>
+     <listitem>
+      <para>
+       Print the <application>pg_amcheck</application> version and exit.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-e</option></term>
+     <term><option>--echo</option></term>
+     <listitem>
+      <para>
+       Print to stdout all commands and queries being executed against the
+       server.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-q</option></term>
+     <term><option>--quiet</option></term>
+     <listitem>
+      <para>
+       Do not write additional messages beyond those about corruption.
+      </para>
+      <para>
+       This option does not quiet any output specifically due to the use of
+       the <option>-e</option> <option>--echo</option> option.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-v</option></term>
+     <term><option>--verbose</option></term>
+     <listitem>
+      <para>
+       Increases the log level verbosity.  This option may be given more than
+       once.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--progress</option></term>
+     <listitem>
+      <para>
+       Show progress information about how many relations have been checked.
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+  </refsect2>
+
+  <refsect2>
+   <title>Database Connection and Concurrent Connection Options</title>
+
+   <variablelist>
+    <varlistentry>
+     <term><option>-h</option></term>
+     <term><option>--host=HOSTNAME</option></term>
+     <listitem>
+      <para>
+       Specifies the host name of the machine on which the server is running.
+       If the value begins with a slash, it is used as the directory for the
+       Unix domain socket.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-p</option></term>
+     <term><option>--port=PORT</option></term>
+     <listitem>
+      <para>
+       Specifies the TCP port or local Unix domain socket file extension on
+       which the server is listening for connections.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-U</option></term>
+     <term><option>--username=USERNAME</option></term>
+     <listitem>
+      <para>
+       User name to connect as.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-w</option></term>
+     <term><option>--no-password</option></term>
+     <listitem>
+      <para>
+       Never issue a password prompt.  If the server requires password
+       authentication and a password is not available by other means such as
+       a <filename>.pgpass</filename> file, the connection attempt will fail.
+       This option can be useful in batch jobs and scripts where no user is
+       present to enter a password.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-W</option></term>
+     <term><option>--password</option></term>
+     <listitem>
+      <para>
+       Force <application>pg_amcheck</application> to prompt for a password
+       before connecting to a database.
+      </para>
+      <para>
+       This option is never essential, since
+       <application>pg_amcheck</application> will automatically prompt for a
+       password if the server demands password authentication.  However,
+       <application>pg_amcheck</application> will waste a connection attempt
+       finding out that the server wants a password.  In some cases it is
+       worth typing <option>-W</option> to avoid the extra connection attempt.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--maintenance-db=DBNAME</option></term>
+     <listitem>
+      <para>
+       Specifies the name of the database to connect to when querying the
+       list of all databases.  If not specified, the
+       <literal>postgres</literal> database will be used; if that does not
+       exist <literal>template1</literal> will be used.  This can be a
+       <link linkend="libpq-connstring">connection string</link>.  If so,
+       connection string parameters will override any conflicting command
+       line options.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-j</option></term>
+     <term><option>--jobs=NUM</option></term>
+     <listitem>
+      <para>
+       Use the specified number of concurrent connections to the server, or
+       one per object to be checked, whichever number is smaller.
+      </para>
+      <para>
+       The default is to use a single connection.
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+  </refsect2>
+
+  <refsect2>
+   <title>Options Controlling Index Checking Functions</title>
+
+   <variablelist>
+    <varlistentry>
+     <term><option>-P</option></term>
+     <term><option>--parent-check</option></term>
+     <listitem>
+      <para>
+       For each btree index checked, use <xref linkend="amcheck"/>'s
+       <function>bt_index_parent_check</function> function, which performs
+       additional checks of parent/child relationships during index checking.
+      </para>
+      <para>
+       The default is to use <application>amcheck</application>'s
+       <function>bt_index_check</function> function, but note that use of the
+       <option>--rootdescend</option> option implicitly selects
+       <function>bt_index_parent_check</function>.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>-H</option></term>
+     <term><option>--heapallindexed</option></term>
+     <listitem>
+      <para>
+       For each index checked, verify the presence of all heap tuples as index
+       tuples in the index using <application>amcheck</application>'s
+       <option>heapallindexed</option> option.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--rootdescend</option></term>
+     <listitem>
+      <para>
+       For each index checked, re-find tuples on the leaf level by performing a
+       new search from the root page for each tuple using
+       <xref linkend="amcheck"/>'s <option>rootdescend</option> option.
+      </para>
+      <para>
+       Use of this option implicitly also selects the <option>-P</option>
+       <option>--parent-check</option> option.
+      </para>
+      <para>
+       This form of verification was originally written to help in the
+       development of btree index features.  It may be of limited use or even
+       of no use in helping detect the kinds of corruption that occur in
+       practice.  It may also cause corruption checking to take considerably
+       longer and consume considerably more resources on the server.
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+  </refsect2>
+
+  <refsect2>
+   <title>Options Controlling Table Checking Functions</title>
+
+   <variablelist>
+    <varlistentry>
+     <term><option>--exclude-toast-pointers</option></term>
+     <listitem>
+      <para>
+       When checking main relations, do not look up entries in toast tables
+       corresponding to toast pointers in the main releation.
+      </para>
+      <para>
+       The default behavior checks each toast pointer encountered in the main
+       table to verify, as much as possible, that the pointer points at
+       something in the toast table that is reasonable.  Toast pointers which
+       point beyond the end of the toast table, or to the middle (rather than
+       the beginning) of a toast entry, are identified as corrupt.
+      </para>
+      <para>
+       The process by which <xref linkend="amcheck"/>'s
+       <function>verify_heapam</function> function checks each toast pointer is
+       slow and may be improved in a future release.  Some users may wish to
+       disable this check to save time.
+      </para>
+      <para>
+       Note that, despite their similar names, this option is unrelated to the
+       <option>--exclude-toast</option> option.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--on-error-stop</option></term>
+     <listitem>
+      <para>
+       After reporting all corruptions on the first page of a table where
+       corruptions are found, stop processing that table relation and move on
+       to the next table or index.
+      </para>
+      <para>
+       Note that index checking always stops after the first corrupt page.
+       This option only has meaning relative to table relations.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--skip=OPTION</option></term>
+     <listitem>
+      <para>
+       If <literal>"all-frozen"</literal> is given, table corruption checks
+       will skip over pages in all tables that are marked as all frozen.
+      </para>
+      <para>
+       If <literal>"all-visible"</literal> is given, table corruption checks
+       will skip over pages in all tables that are marked as all visible.
+      </para>
+      <para>
+       By default, no pages are skipped.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--startblock=BLOCK</option></term>
+     <listitem>
+      <para>
+       Skip (do not check) pages prior to the given starting block.
+      </para>
+      <para>
+       By default, no pages are skipped.  This option will be applied to all
+       table relations that are checked, including toast tables, but note
+       that unless <option>--exclude-toast-pointers</option> is given, toast
+       pointers found in the main table will be followed into the toast table
+       without regard for the location in the toast table.
+      </para>
+     </listitem>
+    </varlistentry>
+    <varlistentry>
+     <term><option>--endblock=BLOCK</option></term>
+     <listitem>
+      <para>
+       Skip (do not check) all pages after the given ending block.
+      </para>
+      <para>
+       By default, no pages are skipped.  This option will be applied to all
+       table relations that are checked, including toast tables, with the
+       same caveat about <option>--exclude-toast-pointers</option> as above.
+      </para>
+     </listitem>
+    </varlistentry>
+   </variablelist>
+  </refsect2>
+
+  <refsect2>
+   <title>Corruption Checking Target Options</title>
+
+   <para>
+    Objects to be checked may span schemas in more than one database.  Options
+    for restricting the list of databases, schemas, tables and indexes are
+    described below.  In each place where a name may be specified, a
+    <link linkend="app-psql-patterns"><replaceable class="parameter">pattern</replaceable></link>
+    may also be used.
+   </para>
+
+   <para>
+    <variablelist>
+     <varlistentry>
+      <term><option>--all</option></term>
+        <listitem>
+       <para>
+        Perform checking in all databases.
+       </para>
+       <para>
+        In the absence of any other options, selects all objects across all
+        schemas and databases.
+       </para>
+       <para>
+        Option <option>-D</option> <option>--exclude-db</option> takes
+        precedence over <option>--all</option>.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-d</option></term>
+      <term><option>--dbname</option></term>
+      <listitem>
+       <para>
+        Perform checking in the specified database.
+       </para>
+       <para>
+        This option may be specified multiple times to list more than one
+        database (or database pattern) for checking.  By default, all objects
+        in the matching database(s) will be checked.
+       </para>
+       <para>
+        If no <option>maintenance-db</option> argument is given nor is any
+        database name given as a command line argument, the first argument
+        specified with <option>-d</option> <option>--dbname</option> will be
+        used for the initial connection.  If that argument is not a literal
+        database name, the attempt to connect will fail.
+       </para>
+       <para>
+        If <option>--all</option> is also specified, <option>-d</option>
+        <option>--dbname</option> does not affect which databases are checked,
+        but may be used to specify the database for the initial connection.
+       </para>
+       <para>
+        Option <option>-D</option> <option>--exclude-db</option> takes
+        precedence over <option>-d</option> <option>--dbname</option>.
+       </para>
+       <para>
+        Examples:
+        <simplelist>
+         <member><literal>--dbname=africa</literal></member>
+         <member><literal>--dbname="a*"</literal></member>
+         <member><literal>--dbname="africa|asia|europe"</literal></member>
+        </simplelist>
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-D</option></term>
+      <term><option>--exclude-db</option></term>
+      <listitem>
+       <para>
+        Do not perform checking in the specified database.
+       </para>
+       <para>
+        This option may be specified multiple times to list more than one
+        database (or database pattern) for exclusion.
+       </para>
+       <para>
+        If a database which is included using <option>--all</option> or
+        <option>-d</option> <option>--dbname</option> is also excluded using
+        <option>-D</option> <option>--exclude-db</option>, the database will be
+        excluded.
+       </para>
+       <para>
+        Examples:
+        <simplelist>
+         <member><literal>--exclude-db=america</literal></member>
+         <member><literal>--exclude-db="*pacific*"</literal></member>
+        </simplelist>
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-s</option></term>
+      <term><option>--schema</option></term>
+      <listitem>
+       <para>
+        Perform checking in the specified schema(s).
+       </para>
+       <para>
+        This option may be specified multiple times to list more than one
+        schema (or schema pattern) for checking.  By default, all objects in
+        the matching schema(s) will be checked.
+       </para>
+       <para>
+        Option <option>-S</option> <option>--exclude-schema</option> takes
+        precedence over <option>-s</option> <option>--schema</option>.
+       </para>
+       <para>
+        Examples:
+        <simplelist>
+         <member><literal>--schema=corp</literal></member>
+         <member><literal>--schema="corp|llc|npo"</literal></member>
+        </simplelist>
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-S</option></term>
+      <term><option>--exclude-schema</option></term>
+      <listitem>
+       <para>
+        Do not perform checking in the specified schema.
+       </para>
+       <para>
+        This option may be specified multiple times to list more than one
+        schema (or schema pattern) for exclusion.
+       </para>
+       <para>
+        If a schema which is included using
+        <option>-s</option> <option>--schema</option> is also excluded using
+        <option>-S</option> <option>--exclude-schema</option>, the schema will
+        be excluded.
+       </para>
+       <para>
+        Examples:
+        <simplelist>
+         <member><literal>-S corp -S llc</literal></member>
+         <member><literal>--exclude-schema="*c*"</literal></member>
+        </simplelist>
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-r</option></term>
+      <term><option>--relation</option></term>
+      <listitem>
+       <para>
+        Perform checking on the specified relation(s).
+       </para>
+       <para>
+        This option may be specified multiple times to list more than one
+        relation (or relation pattern) for checking.
+       </para>
+       <para>
+        Option <option>-R</option> <option>--exclude-relation</option> takes
+        precedence over <option>-r</option> <option>--relation</option>.
+       </para>
+       <para>
+        If the relation is not schema qualified, it will apply to relations in
+        all schemas.
+       </para>
+       <para>
+        Examples:
+        <simplelist>
+         <member><literal>--relation=accounts_idx</literal></member>
+         <member><literal>--relation="llc.accounts_idx"</literal></member>
+         <member><literal>--relation="asia|africa.corp|llc.accounts_idx"</literal></member>
+        </simplelist>
+       </para>
+       <para>
+        The first example, <literal>--relation=accounts_idx</literal>, checks
+        relations named <literal>accounts_idx</literal> in all schemas across
+        all databases being checked.
+       </para>
+       <para>
+        The second example, <literal>--relation="llc.accounts_idx"</literal>,
+        checks relations named <literal>accounts_idx</literal> in schema
+        <literal>llc</literal> across all databases being checked.
+       </para>
+       <para>
+        The third example,
+        <literal>--relation="asia|africa.corp|llc.accounts_idx"</literal>,
+        checks relations named <literal>accounts_idx</literal> in schemas
+        <literal>corp</literal> and <literal>llc</literal> in databases
+        <literal>asia</literal> and <literal>africa</literal>, which is
+        equivalent to listing four separate relations: <literal>-r
+        "asia.corp.accounts_idx" -r "asia.llc.accounts_idx" -r
+        "africa.corp.accounts_idx" -r "africa.llc.accounts_idx"</literal>
+       </para>
+       <para>
+        Note that if a database is implicated in a relation pattern, such as
+        <literal>asia</literal> and <literal>africa</literal> in the third
+        example above, the database need not be otherwise given in the command
+        arguments for the relation to be checked.  As an extreme example of
+        this:
+        <simplelist>
+         <member><literal>pg_amcheck --relation="*.*.*" mydb</literal></member>
+        </simplelist>
+        will check all relations in all databases.  The <literal>mydb</literal>
+        argument only serves to tell <application>pg_amcheck</application> the
+        name of the database to use for querying the list of all databases.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-R</option></term>
+      <term><option>--exclude-relation</option></term>
+      <listitem>
+       <para>
+        Exclude checks on the specified relation(s).
+       </para>
+       <para>
+        Option <option>-R</option> <option>--exclude-relation</option> takes
+        precedence over <option>-r</option> <option>--relation</option>,
+        <option>-t</option> <option>--table</option> and <option>-i</option>
+        <option>--index</option>.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-t</option></term>
+      <term><option>--table</option></term>
+      <listitem>
+       <para>
+        Perform checks on the specified tables(s).  This is an alias for the
+        <option>-r</option> <option>--relation</option> option, except that it
+        applies only to tables, not indexes.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-T</option></term>
+      <term><option>--exclude-table</option></term>
+      <listitem>
+       <para>
+        Exclude checks on the specified tables(s).  This is an alias for the
+        <option>-R</option> <option>--exclude-relation</option> option, except
+        that it applies only to tables, not indexes.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-i</option></term>
+      <term><option>--index</option></term>
+      <listitem>
+       <para>
+        Perform checks on the specified index(es).  This is an alias for the
+        <option>-r</option> <option>--relation</option> option, except that it
+        applies only to indexes, not tables.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>-I</option></term>
+      <term><option>--exclude-index</option></term>
+      <listitem>
+       <para>
+        Exclude checks on the specified index(es).  This is an alias for the
+        <option>-R</option> <option>--exclude-relation</option> option, except
+        that it applies only to indexes, not tables.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--no-dependents</option></term>
+      <listitem>
+       <para>
+        When calculating the list of objects to be checked, do not
+        automatically expand the list to include associated indexes and toast
+        tables of elements otherwise in the list.
+       </para>
+       <para>
+        By default, for each main table relation checked, any associated toast
+        table and all associated indexes are also checked, unless explicitly
+        excluded.
+       </para>
+      </listitem>
+     </varlistentry>
+
+     <varlistentry>
+      <term><option>--no-strict-names</option></term>
+      <listitem>
+       <para>
+        When calculating the list of databases to check, and the objects within
+        those databases to be checked, do not raise an error for database,
+        schema, relation, table, nor index inclusion patterns which match no
+        corresponding objects.
+       </para>
+       <para>
+        Exclusion patterns are not required to match any objects, but by
+        default unmatched inclusion patterns raise an error, including when
+        they fail to match as a result of an exclusion pattern having
+        prohibited them matching an existent object, and when they fail to
+        match a database because it is unconnectable (datallowconn is false).
+       </para>
+      </listitem>
+     </varlistentry>
+    </variablelist>
+   </para>
+  </refsect2>
+ </refsect1>
+
+ <refsect1>
+  <title>Notes</title>
+
+  <para>
+   <application>pg_amcheck</application> is designed to work with
+   <productname>PostgreSQL</productname> 14.0 and later.
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>Author</title>
+
+  <para>
+   Mark Dilger <email>mark.dilger@enterprisedb.com</email>
+  </para>
+ </refsect1>
+
+ <refsect1>
+  <title>See Also</title>
+
+  <simplelist type="inline">
+   <member><xref linkend="amcheck"/></member>
+  </simplelist>
+ </refsect1>
+</refentry>
diff --git a/src/tools/msvc/Install.pm b/src/tools/msvc/Install.pm
index ea3af48777..49ad558b74 100644
--- a/src/tools/msvc/Install.pm
+++ b/src/tools/msvc/Install.pm
@@ -18,7 +18,7 @@ our (@ISA, @EXPORT_OK);
 @EXPORT_OK = qw(Install);
 
 my $insttype;
-my @client_contribs = ('oid2name', 'pgbench', 'vacuumlo');
+my @client_contribs = ('oid2name', 'pg_amcheck', 'pgbench', 'vacuumlo');
 my @client_program_files = (
 	'clusterdb',      'createdb',   'createuser',    'dropdb',
 	'dropuser',       'ecpg',       'libecpg',       'libecpg_compat',
diff --git a/src/tools/msvc/Mkvcbuild.pm b/src/tools/msvc/Mkvcbuild.pm
index 49614106dc..f680544e07 100644
--- a/src/tools/msvc/Mkvcbuild.pm
+++ b/src/tools/msvc/Mkvcbuild.pm
@@ -33,9 +33,9 @@ my @unlink_on_exit;
 
 # Set of variables for modules in contrib/ and src/test/modules/
 my $contrib_defines = { 'refint' => 'REFINT_VERBOSE' };
-my @contrib_uselibpq = ('dblink', 'oid2name', 'postgres_fdw', 'vacuumlo');
-my @contrib_uselibpgport   = ('oid2name', 'vacuumlo');
-my @contrib_uselibpgcommon = ('oid2name', 'vacuumlo');
+my @contrib_uselibpq = ('dblink', 'oid2name', 'pg_amcheck', 'postgres_fdw', 'vacuumlo');
+my @contrib_uselibpgport   = ('oid2name', 'pg_amcheck', 'vacuumlo');
+my @contrib_uselibpgcommon = ('oid2name', 'pg_amcheck', 'vacuumlo');
 my $contrib_extralibs      = undef;
 my $contrib_extraincludes = { 'dblink' => ['src/backend'] };
 my $contrib_extrasource = {
diff --git a/src/tools/pgindent/typedefs.list b/src/tools/pgindent/typedefs.list
index bab4f3adb3..531b9e2a00 100644
--- a/src/tools/pgindent/typedefs.list
+++ b/src/tools/pgindent/typedefs.list
@@ -403,6 +403,7 @@ ConfigData
 ConfigVariable
 ConnCacheEntry
 ConnCacheKey
+ConnParams
 ConnStatusType
 ConnType
 ConnectionStateEnum
@@ -498,6 +499,7 @@ DSA
 DWORD
 DataDumperPtr
 DataPageDeleteStack
+DatabaseInfo
 DateADT
 Datum
 DatumTupleFields
@@ -2082,6 +2084,7 @@ RelToCluster
 RelabelType
 Relation
 RelationData
+RelationInfo
 RelationPtr
 RelationSyncEntry
 RelcacheCallbackFunction
@@ -2846,6 +2849,7 @@ ambuildempty_function
 ambuildphasename_function
 ambulkdelete_function
 amcanreturn_function
+amcheckOptions
 amcostestimate_function
 amendscan_function
 amestimateparallelscan_function
-- 
2.21.1 (Apple Git-122.3)