LCOV - code coverage report
Current view: top level - src/bin/pg_amcheck - pg_amcheck.c (source / functions) Coverage Total Hit
Test: Code coverage Lines: 0.0 % 900 0
Test Date: 2026-01-26 10:56:24 Functions: 0.0 % 22 0
Legend: Lines:     hit not hit

            Line data    Source code
       1              : /*-------------------------------------------------------------------------
       2              :  *
       3              :  * pg_amcheck.c
       4              :  *              Detects corruption within database relations.
       5              :  *
       6              :  * Copyright (c) 2017-2026, PostgreSQL Global Development Group
       7              :  *
       8              :  * IDENTIFICATION
       9              :  *        src/bin/pg_amcheck/pg_amcheck.c
      10              :  *
      11              :  *-------------------------------------------------------------------------
      12              :  */
      13              : #include "postgres_fe.h"
      14              : 
      15              : #include <limits.h>
      16              : #include <time.h>
      17              : 
      18              : #include "catalog/pg_am_d.h"
      19              : #include "catalog/pg_class_d.h"
      20              : #include "catalog/pg_namespace_d.h"
      21              : #include "common/logging.h"
      22              : #include "common/username.h"
      23              : #include "fe_utils/cancel.h"
      24              : #include "fe_utils/option_utils.h"
      25              : #include "fe_utils/parallel_slot.h"
      26              : #include "fe_utils/query_utils.h"
      27              : #include "fe_utils/simple_list.h"
      28              : #include "fe_utils/string_utils.h"
      29              : #include "getopt_long.h"
      30              : #include "pgtime.h"
      31              : #include "storage/block.h"
      32              : 
      33              : typedef struct PatternInfo
      34              : {
      35              :         const char *pattern;            /* Unaltered pattern from the command line */
      36              :         char       *db_regex;           /* Database regexp parsed from pattern, or
      37              :                                                                  * NULL */
      38              :         char       *nsp_regex;          /* Schema regexp parsed from pattern, or NULL */
      39              :         char       *rel_regex;          /* Relation regexp parsed from pattern, or
      40              :                                                                  * NULL */
      41              :         bool            heap_only;              /* true if rel_regex should only match heap
      42              :                                                                  * tables */
      43              :         bool            btree_only;             /* true if rel_regex should only match btree
      44              :                                                                  * indexes */
      45              :         bool            matched;                /* true if the pattern matched in any database */
      46              : } PatternInfo;
      47              : 
      48              : typedef struct PatternInfoArray
      49              : {
      50              :         PatternInfo *data;
      51              :         size_t          len;
      52              : } PatternInfoArray;
      53              : 
      54              : /* pg_amcheck command line options controlled by user flags */
      55              : typedef struct AmcheckOptions
      56              : {
      57              :         bool            dbpattern;
      58              :         bool            alldb;
      59              :         bool            echo;
      60              :         bool            verbose;
      61              :         bool            strict_names;
      62              :         bool            show_progress;
      63              :         int                     jobs;
      64              : 
      65              :         /*
      66              :          * Whether to install missing extensions, and optionally the name of the
      67              :          * schema in which to install the extension's objects.
      68              :          */
      69              :         bool            install_missing;
      70              :         char       *install_schema;
      71              : 
      72              :         /* Objects to check or not to check, as lists of PatternInfo structs. */
      73              :         PatternInfoArray include;
      74              :         PatternInfoArray exclude;
      75              : 
      76              :         /*
      77              :          * As an optimization, if any pattern in the exclude list applies to heap
      78              :          * tables, or similarly if any such pattern applies to btree indexes, or
      79              :          * to schemas, then these will be true, otherwise false.  These should
      80              :          * always agree with what you'd conclude by grep'ing through the exclude
      81              :          * list.
      82              :          */
      83              :         bool            excludetbl;
      84              :         bool            excludeidx;
      85              :         bool            excludensp;
      86              : 
      87              :         /*
      88              :          * If any inclusion pattern exists, then we should only be checking
      89              :          * matching relations rather than all relations, so this is true iff
      90              :          * include is empty.
      91              :          */
      92              :         bool            allrel;
      93              : 
      94              :         /* heap table checking options */
      95              :         bool            no_toast_expansion;
      96              :         bool            reconcile_toast;
      97              :         bool            on_error_stop;
      98              :         int64           startblock;
      99              :         int64           endblock;
     100              :         const char *skip;
     101              : 
     102              :         /* btree index checking options */
     103              :         bool            parent_check;
     104              :         bool            rootdescend;
     105              :         bool            heapallindexed;
     106              :         bool            checkunique;
     107              : 
     108              :         /* heap and btree hybrid option */
     109              :         bool            no_btree_expansion;
     110              : } AmcheckOptions;
     111              : 
     112              : static AmcheckOptions opts = {
     113              :         .dbpattern = false,
     114              :         .alldb = false,
     115              :         .echo = false,
     116              :         .verbose = false,
     117              :         .strict_names = true,
     118              :         .show_progress = false,
     119              :         .jobs = 1,
     120              :         .install_missing = false,
     121              :         .install_schema = "pg_catalog",
     122              :         .include = {NULL, 0},
     123              :         .exclude = {NULL, 0},
     124              :         .excludetbl = false,
     125              :         .excludeidx = false,
     126              :         .excludensp = false,
     127              :         .allrel = true,
     128              :         .no_toast_expansion = false,
     129              :         .reconcile_toast = true,
     130              :         .on_error_stop = false,
     131              :         .startblock = -1,
     132              :         .endblock = -1,
     133              :         .skip = "none",
     134              :         .parent_check = false,
     135              :         .rootdescend = false,
     136              :         .heapallindexed = false,
     137              :         .checkunique = false,
     138              :         .no_btree_expansion = false
     139              : };
     140              : 
     141              : static const char *progname = NULL;
     142              : 
     143              : /* Whether all relations have so far passed their corruption checks */
     144              : static bool all_checks_pass = true;
     145              : 
     146              : /* Time last progress report was displayed */
     147              : static pg_time_t last_progress_report = 0;
     148              : static bool progress_since_last_stderr = false;
     149              : 
     150              : typedef struct DatabaseInfo
     151              : {
     152              :         char       *datname;
     153              :         char       *amcheck_schema; /* escaped, quoted literal */
     154              :         bool            is_checkunique;
     155              : } DatabaseInfo;
     156              : 
     157              : typedef struct RelationInfo
     158              : {
     159              :         const DatabaseInfo *datinfo;    /* shared by other relinfos */
     160              :         Oid                     reloid;
     161              :         bool            is_heap;                /* true if heap, false if btree */
     162              :         char       *nspname;
     163              :         char       *relname;
     164              :         int                     relpages;
     165              :         int                     blocks_to_check;
     166              :         char       *sql;                        /* set during query run, pg_free'd after */
     167              : } RelationInfo;
     168              : 
     169              : /*
     170              :  * Query for determining if contrib's amcheck is installed.  If so, selects the
     171              :  * namespace name where amcheck's functions can be found.
     172              :  */
     173              : static const char *const amcheck_sql =
     174              : "SELECT n.nspname, x.extversion FROM pg_catalog.pg_extension x"
     175              : "\nJOIN pg_catalog.pg_namespace n ON x.extnamespace = n.oid"
     176              : "\nWHERE x.extname = 'amcheck'";
     177              : 
     178              : static void prepare_heap_command(PQExpBuffer sql, RelationInfo *rel,
     179              :                                                                  PGconn *conn);
     180              : static void prepare_btree_command(PQExpBuffer sql, RelationInfo *rel,
     181              :                                                                   PGconn *conn);
     182              : static void run_command(ParallelSlot *slot, const char *sql);
     183              : static bool verify_heap_slot_handler(PGresult *res, PGconn *conn,
     184              :                                                                          void *context);
     185              : static bool verify_btree_slot_handler(PGresult *res, PGconn *conn, void *context);
     186              : static void help(const char *progname);
     187              : static void progress_report(uint64 relations_total, uint64 relations_checked,
     188              :                                                         uint64 relpages_total, uint64 relpages_checked,
     189              :                                                         const char *datname, bool force, bool finished);
     190              : 
     191              : static void append_database_pattern(PatternInfoArray *pia, const char *pattern,
     192              :                                                                         int encoding);
     193              : static void append_schema_pattern(PatternInfoArray *pia, const char *pattern,
     194              :                                                                   int encoding);
     195              : static void append_relation_pattern(PatternInfoArray *pia, const char *pattern,
     196              :                                                                         int encoding);
     197              : static void append_heap_pattern(PatternInfoArray *pia, const char *pattern,
     198              :                                                                 int encoding);
     199              : static void append_btree_pattern(PatternInfoArray *pia, const char *pattern,
     200              :                                                                  int encoding);
     201              : static void compile_database_list(PGconn *conn, SimplePtrList *databases,
     202              :                                                                   const char *initial_dbname);
     203              : static void compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
     204              :                                                                                  const DatabaseInfo *dat,
     205              :                                                                                  uint64 *pagecount);
     206              : 
     207              : #define log_no_match(...) do { \
     208              :                 if (opts.strict_names) \
     209              :                         pg_log_error(__VA_ARGS__); \
     210              :                 else \
     211              :                         pg_log_warning(__VA_ARGS__); \
     212              :         } while(0)
     213              : 
     214              : #define FREE_AND_SET_NULL(x) do { \
     215              :         pg_free(x); \
     216              :         (x) = NULL; \
     217              :         } while (0)
     218              : 
     219              : int
     220            0 : main(int argc, char *argv[])
     221              : {
     222            0 :         PGconn     *conn = NULL;
     223            0 :         SimplePtrListCell *cell;
     224            0 :         SimplePtrList databases = {NULL, NULL};
     225            0 :         SimplePtrList relations = {NULL, NULL};
     226            0 :         bool            failed = false;
     227            0 :         const char *latest_datname;
     228            0 :         int                     parallel_workers;
     229            0 :         ParallelSlotArray *sa;
     230            0 :         PQExpBufferData sql;
     231            0 :         uint64          reltotal = 0;
     232            0 :         uint64          pageschecked = 0;
     233            0 :         uint64          pagestotal = 0;
     234            0 :         uint64          relprogress = 0;
     235            0 :         int                     pattern_id;
     236              : 
     237              :         static struct option long_options[] = {
     238              :                 /* Connection options */
     239              :                 {"host", required_argument, NULL, 'h'},
     240              :                 {"port", required_argument, NULL, 'p'},
     241              :                 {"username", required_argument, NULL, 'U'},
     242              :                 {"no-password", no_argument, NULL, 'w'},
     243              :                 {"password", no_argument, NULL, 'W'},
     244              :                 {"maintenance-db", required_argument, NULL, 1},
     245              : 
     246              :                 /* check options */
     247              :                 {"all", no_argument, NULL, 'a'},
     248              :                 {"database", required_argument, NULL, 'd'},
     249              :                 {"exclude-database", required_argument, NULL, 'D'},
     250              :                 {"echo", no_argument, NULL, 'e'},
     251              :                 {"index", required_argument, NULL, 'i'},
     252              :                 {"exclude-index", required_argument, NULL, 'I'},
     253              :                 {"jobs", required_argument, NULL, 'j'},
     254              :                 {"progress", no_argument, NULL, 'P'},
     255              :                 {"relation", required_argument, NULL, 'r'},
     256              :                 {"exclude-relation", required_argument, NULL, 'R'},
     257              :                 {"schema", required_argument, NULL, 's'},
     258              :                 {"exclude-schema", required_argument, NULL, 'S'},
     259              :                 {"table", required_argument, NULL, 't'},
     260              :                 {"exclude-table", required_argument, NULL, 'T'},
     261              :                 {"verbose", no_argument, NULL, 'v'},
     262              :                 {"no-dependent-indexes", no_argument, NULL, 2},
     263              :                 {"no-dependent-toast", no_argument, NULL, 3},
     264              :                 {"exclude-toast-pointers", no_argument, NULL, 4},
     265              :                 {"on-error-stop", no_argument, NULL, 5},
     266              :                 {"skip", required_argument, NULL, 6},
     267              :                 {"startblock", required_argument, NULL, 7},
     268              :                 {"endblock", required_argument, NULL, 8},
     269              :                 {"rootdescend", no_argument, NULL, 9},
     270              :                 {"no-strict-names", no_argument, NULL, 10},
     271              :                 {"heapallindexed", no_argument, NULL, 11},
     272              :                 {"parent-check", no_argument, NULL, 12},
     273              :                 {"install-missing", optional_argument, NULL, 13},
     274              :                 {"checkunique", no_argument, NULL, 14},
     275              : 
     276              :                 {NULL, 0, NULL, 0}
     277              :         };
     278              : 
     279            0 :         int                     optindex;
     280            0 :         int                     c;
     281              : 
     282            0 :         const char *db = NULL;
     283            0 :         const char *maintenance_db = NULL;
     284              : 
     285            0 :         const char *host = NULL;
     286            0 :         const char *port = NULL;
     287            0 :         const char *username = NULL;
     288            0 :         enum trivalue prompt_password = TRI_DEFAULT;
     289            0 :         int                     encoding = pg_get_encoding_from_locale(NULL, false);
     290            0 :         ConnParams      cparams;
     291              : 
     292            0 :         pg_logging_init(argv[0]);
     293            0 :         progname = get_progname(argv[0]);
     294            0 :         set_pglocale_pgservice(argv[0], PG_TEXTDOMAIN("pg_amcheck"));
     295              : 
     296            0 :         handle_help_version_opts(argc, argv, progname, help);
     297              : 
     298              :         /* process command-line options */
     299            0 :         while ((c = getopt_long(argc, argv, "ad:D:eh:Hi:I:j:p:Pr:R:s:S:t:T:U:vwW",
     300            0 :                                                         long_options, &optindex)) != -1)
     301              :         {
     302            0 :                 char       *endptr;
     303            0 :                 unsigned long optval;
     304              : 
     305            0 :                 switch (c)
     306              :                 {
     307              :                         case 'a':
     308            0 :                                 opts.alldb = true;
     309            0 :                                 break;
     310              :                         case 'd':
     311            0 :                                 opts.dbpattern = true;
     312            0 :                                 append_database_pattern(&opts.include, optarg, encoding);
     313            0 :                                 break;
     314              :                         case 'D':
     315            0 :                                 opts.dbpattern = true;
     316            0 :                                 append_database_pattern(&opts.exclude, optarg, encoding);
     317            0 :                                 break;
     318              :                         case 'e':
     319            0 :                                 opts.echo = true;
     320            0 :                                 break;
     321              :                         case 'h':
     322            0 :                                 host = pg_strdup(optarg);
     323            0 :                                 break;
     324              :                         case 'i':
     325            0 :                                 opts.allrel = false;
     326            0 :                                 append_btree_pattern(&opts.include, optarg, encoding);
     327            0 :                                 break;
     328              :                         case 'I':
     329            0 :                                 opts.excludeidx = true;
     330            0 :                                 append_btree_pattern(&opts.exclude, optarg, encoding);
     331            0 :                                 break;
     332              :                         case 'j':
     333            0 :                                 if (!option_parse_int(optarg, "-j/--jobs", 1, INT_MAX,
     334              :                                                                           &opts.jobs))
     335            0 :                                         exit(1);
     336            0 :                                 break;
     337              :                         case 'p':
     338            0 :                                 port = pg_strdup(optarg);
     339            0 :                                 break;
     340              :                         case 'P':
     341            0 :                                 opts.show_progress = true;
     342            0 :                                 break;
     343              :                         case 'r':
     344            0 :                                 opts.allrel = false;
     345            0 :                                 append_relation_pattern(&opts.include, optarg, encoding);
     346            0 :                                 break;
     347              :                         case 'R':
     348            0 :                                 opts.excludeidx = true;
     349            0 :                                 opts.excludetbl = true;
     350            0 :                                 append_relation_pattern(&opts.exclude, optarg, encoding);
     351            0 :                                 break;
     352              :                         case 's':
     353            0 :                                 opts.allrel = false;
     354            0 :                                 append_schema_pattern(&opts.include, optarg, encoding);
     355            0 :                                 break;
     356              :                         case 'S':
     357            0 :                                 opts.excludensp = true;
     358            0 :                                 append_schema_pattern(&opts.exclude, optarg, encoding);
     359            0 :                                 break;
     360              :                         case 't':
     361            0 :                                 opts.allrel = false;
     362            0 :                                 append_heap_pattern(&opts.include, optarg, encoding);
     363            0 :                                 break;
     364              :                         case 'T':
     365            0 :                                 opts.excludetbl = true;
     366            0 :                                 append_heap_pattern(&opts.exclude, optarg, encoding);
     367            0 :                                 break;
     368              :                         case 'U':
     369            0 :                                 username = pg_strdup(optarg);
     370            0 :                                 break;
     371              :                         case 'v':
     372            0 :                                 opts.verbose = true;
     373            0 :                                 pg_logging_increase_verbosity();
     374            0 :                                 break;
     375              :                         case 'w':
     376            0 :                                 prompt_password = TRI_NO;
     377            0 :                                 break;
     378              :                         case 'W':
     379            0 :                                 prompt_password = TRI_YES;
     380            0 :                                 break;
     381              :                         case 1:
     382            0 :                                 maintenance_db = pg_strdup(optarg);
     383            0 :                                 break;
     384              :                         case 2:
     385            0 :                                 opts.no_btree_expansion = true;
     386            0 :                                 break;
     387              :                         case 3:
     388            0 :                                 opts.no_toast_expansion = true;
     389            0 :                                 break;
     390              :                         case 4:
     391            0 :                                 opts.reconcile_toast = false;
     392            0 :                                 break;
     393              :                         case 5:
     394            0 :                                 opts.on_error_stop = true;
     395            0 :                                 break;
     396              :                         case 6:
     397            0 :                                 if (pg_strcasecmp(optarg, "all-visible") == 0)
     398            0 :                                         opts.skip = "all-visible";
     399            0 :                                 else if (pg_strcasecmp(optarg, "all-frozen") == 0)
     400            0 :                                         opts.skip = "all-frozen";
     401            0 :                                 else if (pg_strcasecmp(optarg, "none") == 0)
     402            0 :                                         opts.skip = "none";
     403              :                                 else
     404            0 :                                         pg_fatal("invalid argument for option %s", "--skip");
     405            0 :                                 break;
     406              :                         case 7:
     407            0 :                                 errno = 0;
     408            0 :                                 optval = strtoul(optarg, &endptr, 10);
     409            0 :                                 if (endptr == optarg || *endptr != '\0' || errno != 0)
     410            0 :                                         pg_fatal("invalid start block");
     411            0 :                                 if (optval > MaxBlockNumber)
     412            0 :                                         pg_fatal("start block out of bounds");
     413            0 :                                 opts.startblock = optval;
     414            0 :                                 break;
     415              :                         case 8:
     416            0 :                                 errno = 0;
     417            0 :                                 optval = strtoul(optarg, &endptr, 10);
     418            0 :                                 if (endptr == optarg || *endptr != '\0' || errno != 0)
     419            0 :                                         pg_fatal("invalid end block");
     420            0 :                                 if (optval > MaxBlockNumber)
     421            0 :                                         pg_fatal("end block out of bounds");
     422            0 :                                 opts.endblock = optval;
     423            0 :                                 break;
     424              :                         case 9:
     425            0 :                                 opts.rootdescend = true;
     426            0 :                                 opts.parent_check = true;
     427            0 :                                 break;
     428              :                         case 10:
     429            0 :                                 opts.strict_names = false;
     430            0 :                                 break;
     431              :                         case 11:
     432            0 :                                 opts.heapallindexed = true;
     433            0 :                                 break;
     434              :                         case 12:
     435            0 :                                 opts.parent_check = true;
     436            0 :                                 break;
     437              :                         case 13:
     438            0 :                                 opts.install_missing = true;
     439            0 :                                 if (optarg)
     440            0 :                                         opts.install_schema = pg_strdup(optarg);
     441            0 :                                 break;
     442              :                         case 14:
     443            0 :                                 opts.checkunique = true;
     444            0 :                                 break;
     445              :                         default:
     446              :                                 /* getopt_long already emitted a complaint */
     447            0 :                                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     448            0 :                                 exit(1);
     449              :                 }
     450            0 :         }
     451              : 
     452            0 :         if (opts.endblock >= 0 && opts.endblock < opts.startblock)
     453            0 :                 pg_fatal("end block precedes start block");
     454              : 
     455              :         /*
     456              :          * A single non-option arguments specifies a database name or connection
     457              :          * string.
     458              :          */
     459            0 :         if (optind < argc)
     460              :         {
     461            0 :                 db = argv[optind];
     462            0 :                 optind++;
     463            0 :         }
     464              : 
     465            0 :         if (optind < argc)
     466              :         {
     467            0 :                 pg_log_error("too many command-line arguments (first is \"%s\")",
     468              :                                          argv[optind]);
     469            0 :                 pg_log_error_hint("Try \"%s --help\" for more information.", progname);
     470            0 :                 exit(1);
     471              :         }
     472              : 
     473              :         /* fill cparams except for dbname, which is set below */
     474            0 :         cparams.pghost = host;
     475            0 :         cparams.pgport = port;
     476            0 :         cparams.pguser = username;
     477            0 :         cparams.prompt_password = prompt_password;
     478            0 :         cparams.dbname = NULL;
     479            0 :         cparams.override_dbname = NULL;
     480              : 
     481            0 :         setup_cancel_handler(NULL);
     482              : 
     483              :         /* choose the database for our initial connection */
     484            0 :         if (opts.alldb)
     485              :         {
     486            0 :                 if (db != NULL)
     487            0 :                         pg_fatal("cannot specify a database name with --all");
     488            0 :                 cparams.dbname = maintenance_db;
     489            0 :         }
     490            0 :         else if (db != NULL)
     491              :         {
     492            0 :                 if (opts.dbpattern)
     493            0 :                         pg_fatal("cannot specify both a database name and database patterns");
     494            0 :                 cparams.dbname = db;
     495            0 :         }
     496              : 
     497            0 :         if (opts.alldb || opts.dbpattern)
     498              :         {
     499            0 :                 conn = connectMaintenanceDatabase(&cparams, progname, opts.echo);
     500            0 :                 compile_database_list(conn, &databases, NULL);
     501            0 :         }
     502              :         else
     503              :         {
     504            0 :                 if (cparams.dbname == NULL)
     505              :                 {
     506            0 :                         if (getenv("PGDATABASE"))
     507            0 :                                 cparams.dbname = getenv("PGDATABASE");
     508            0 :                         else if (getenv("PGUSER"))
     509            0 :                                 cparams.dbname = getenv("PGUSER");
     510              :                         else
     511            0 :                                 cparams.dbname = get_user_name_or_exit(progname);
     512            0 :                 }
     513            0 :                 conn = connectDatabase(&cparams, progname, opts.echo, false, true);
     514            0 :                 compile_database_list(conn, &databases, PQdb(conn));
     515              :         }
     516              : 
     517            0 :         if (databases.head == NULL)
     518              :         {
     519            0 :                 if (conn != NULL)
     520            0 :                         disconnectDatabase(conn);
     521            0 :                 pg_log_warning("no databases to check");
     522            0 :                 exit(0);
     523              :         }
     524              : 
     525              :         /*
     526              :          * Compile a list of all relations spanning all databases to be checked.
     527              :          */
     528            0 :         for (cell = databases.head; cell; cell = cell->next)
     529              :         {
     530            0 :                 PGresult   *result;
     531            0 :                 int                     ntups;
     532            0 :                 const char *amcheck_schema = NULL;
     533            0 :                 DatabaseInfo *dat = (DatabaseInfo *) cell->ptr;
     534              : 
     535            0 :                 cparams.override_dbname = dat->datname;
     536            0 :                 if (conn == NULL || strcmp(PQdb(conn), dat->datname) != 0)
     537              :                 {
     538            0 :                         if (conn != NULL)
     539            0 :                                 disconnectDatabase(conn);
     540            0 :                         conn = connectDatabase(&cparams, progname, opts.echo, false, true);
     541            0 :                 }
     542              : 
     543              :                 /*
     544              :                  * Optionally install amcheck if not already installed in this
     545              :                  * database.
     546              :                  */
     547            0 :                 if (opts.install_missing)
     548              :                 {
     549            0 :                         char       *schema;
     550            0 :                         char       *install_sql;
     551              : 
     552              :                         /*
     553              :                          * Must re-escape the schema name for each database, as the
     554              :                          * escaping rules may change.
     555              :                          */
     556            0 :                         schema = PQescapeIdentifier(conn, opts.install_schema,
     557            0 :                                                                                 strlen(opts.install_schema));
     558            0 :                         install_sql = psprintf("CREATE EXTENSION IF NOT EXISTS amcheck WITH SCHEMA %s",
     559            0 :                                                                    schema);
     560              : 
     561            0 :                         executeCommand(conn, install_sql, opts.echo);
     562            0 :                         pfree(install_sql);
     563            0 :                         PQfreemem(schema);
     564            0 :                 }
     565              : 
     566              :                 /*
     567              :                  * Verify that amcheck is installed for this next database.  User
     568              :                  * error could result in a database not having amcheck that should
     569              :                  * have it, but we also could be iterating over multiple databases
     570              :                  * where not all of them have amcheck installed (for example,
     571              :                  * 'template1').
     572              :                  */
     573            0 :                 result = executeQuery(conn, amcheck_sql, opts.echo);
     574            0 :                 if (PQresultStatus(result) != PGRES_TUPLES_OK)
     575              :                 {
     576              :                         /* Querying the catalog failed. */
     577            0 :                         pg_log_error("database \"%s\": %s",
     578              :                                                  PQdb(conn), PQerrorMessage(conn));
     579            0 :                         pg_log_error_detail("Query was: %s", amcheck_sql);
     580            0 :                         PQclear(result);
     581            0 :                         disconnectDatabase(conn);
     582            0 :                         exit(1);
     583              :                 }
     584            0 :                 ntups = PQntuples(result);
     585            0 :                 if (ntups == 0)
     586              :                 {
     587              :                         /* Querying the catalog succeeded, but amcheck is missing. */
     588            0 :                         pg_log_warning("skipping database \"%s\": amcheck is not installed",
     589              :                                                    PQdb(conn));
     590            0 :                         PQclear(result);
     591            0 :                         disconnectDatabase(conn);
     592            0 :                         conn = NULL;
     593            0 :                         continue;
     594              :                 }
     595            0 :                 amcheck_schema = PQgetvalue(result, 0, 0);
     596            0 :                 if (opts.verbose)
     597            0 :                         pg_log_info("in database \"%s\": using amcheck version \"%s\" in schema \"%s\"",
     598              :                                                 PQdb(conn), PQgetvalue(result, 0, 1), amcheck_schema);
     599            0 :                 dat->amcheck_schema = PQescapeIdentifier(conn, amcheck_schema,
     600            0 :                                                                                                  strlen(amcheck_schema));
     601              : 
     602              :                 /*
     603              :                  * Check the version of amcheck extension. Skip requested unique
     604              :                  * constraint check with warning if it is not yet supported by
     605              :                  * amcheck.
     606              :                  */
     607            0 :                 if (opts.checkunique == true)
     608              :                 {
     609              :                         /*
     610              :                          * Now amcheck has only major and minor versions in the string but
     611              :                          * we also support revision just in case. Now it is expected to be
     612              :                          * zero.
     613              :                          */
     614            0 :                         int                     vmaj = 0,
     615            0 :                                                 vmin = 0,
     616            0 :                                                 vrev = 0;
     617            0 :                         const char *amcheck_version = PQgetvalue(result, 0, 1);
     618              : 
     619            0 :                         sscanf(amcheck_version, "%d.%d.%d", &vmaj, &vmin, &vrev);
     620              : 
     621              :                         /*
     622              :                          * checkunique option is supported in amcheck since version 1.4
     623              :                          */
     624            0 :                         if ((vmaj == 1 && vmin < 4) || vmaj == 0)
     625              :                         {
     626            0 :                                 pg_log_warning("option %s is not supported by amcheck version %s",
     627              :                                                            "--checkunique", amcheck_version);
     628            0 :                                 dat->is_checkunique = false;
     629            0 :                         }
     630              :                         else
     631            0 :                                 dat->is_checkunique = true;
     632            0 :                 }
     633              : 
     634            0 :                 PQclear(result);
     635              : 
     636            0 :                 compile_relation_list_one_db(conn, &relations, dat, &pagestotal);
     637            0 :         }
     638              : 
     639              :         /*
     640              :          * Check that all inclusion patterns matched at least one schema or
     641              :          * relation that we can check.
     642              :          */
     643            0 :         for (pattern_id = 0; pattern_id < opts.include.len; pattern_id++)
     644              :         {
     645            0 :                 PatternInfo *pat = &opts.include.data[pattern_id];
     646              : 
     647            0 :                 if (!pat->matched && (pat->nsp_regex != NULL || pat->rel_regex != NULL))
     648              :                 {
     649            0 :                         failed = opts.strict_names;
     650              : 
     651            0 :                         if (pat->heap_only)
     652            0 :                                 log_no_match("no heap tables to check matching \"%s\"",
     653              :                                                          pat->pattern);
     654            0 :                         else if (pat->btree_only)
     655            0 :                                 log_no_match("no btree indexes to check matching \"%s\"",
     656              :                                                          pat->pattern);
     657            0 :                         else if (pat->rel_regex == NULL)
     658            0 :                                 log_no_match("no relations to check in schemas matching \"%s\"",
     659              :                                                          pat->pattern);
     660              :                         else
     661            0 :                                 log_no_match("no relations to check matching \"%s\"",
     662              :                                                          pat->pattern);
     663            0 :                 }
     664            0 :         }
     665              : 
     666            0 :         if (failed)
     667              :         {
     668            0 :                 if (conn != NULL)
     669            0 :                         disconnectDatabase(conn);
     670            0 :                 exit(1);
     671              :         }
     672              : 
     673              :         /*
     674              :          * Set parallel_workers to the lesser of opts.jobs and the number of
     675              :          * relations.
     676              :          */
     677            0 :         parallel_workers = 0;
     678            0 :         for (cell = relations.head; cell; cell = cell->next)
     679              :         {
     680            0 :                 reltotal++;
     681            0 :                 if (parallel_workers < opts.jobs)
     682            0 :                         parallel_workers++;
     683            0 :         }
     684              : 
     685            0 :         if (reltotal == 0)
     686              :         {
     687            0 :                 if (conn != NULL)
     688            0 :                         disconnectDatabase(conn);
     689            0 :                 pg_fatal("no relations to check");
     690            0 :         }
     691            0 :         progress_report(reltotal, relprogress, pagestotal, pageschecked,
     692              :                                         NULL, true, false);
     693              : 
     694              :         /*
     695              :          * Main event loop.
     696              :          *
     697              :          * We use server-side parallelism to check up to parallel_workers
     698              :          * relations in parallel.  The list of relations was computed in database
     699              :          * order, which minimizes the number of connects and disconnects as we
     700              :          * process the list.
     701              :          */
     702            0 :         latest_datname = NULL;
     703            0 :         sa = ParallelSlotsSetup(parallel_workers, &cparams, progname, opts.echo,
     704              :                                                         NULL);
     705            0 :         if (conn != NULL)
     706              :         {
     707            0 :                 ParallelSlotsAdoptConn(sa, conn);
     708            0 :                 conn = NULL;
     709            0 :         }
     710              : 
     711            0 :         initPQExpBuffer(&sql);
     712            0 :         for (relprogress = 0, cell = relations.head; cell; cell = cell->next)
     713              :         {
     714            0 :                 ParallelSlot *free_slot;
     715            0 :                 RelationInfo *rel;
     716              : 
     717            0 :                 rel = (RelationInfo *) cell->ptr;
     718              : 
     719            0 :                 if (CancelRequested)
     720              :                 {
     721            0 :                         failed = true;
     722            0 :                         break;
     723              :                 }
     724              : 
     725              :                 /*
     726              :                  * The list of relations is in database sorted order.  If this next
     727              :                  * relation is in a different database than the last one seen, we are
     728              :                  * about to start checking this database.  Note that other slots may
     729              :                  * still be working on relations from prior databases.
     730              :                  */
     731            0 :                 latest_datname = rel->datinfo->datname;
     732              : 
     733            0 :                 progress_report(reltotal, relprogress, pagestotal, pageschecked,
     734            0 :                                                 latest_datname, false, false);
     735              : 
     736            0 :                 relprogress++;
     737            0 :                 pageschecked += rel->blocks_to_check;
     738              : 
     739              :                 /*
     740              :                  * Get a parallel slot for the next amcheck command, blocking if
     741              :                  * necessary until one is available, or until a previously issued slot
     742              :                  * command fails, indicating that we should abort checking the
     743              :                  * remaining objects.
     744              :                  */
     745            0 :                 free_slot = ParallelSlotsGetIdle(sa, rel->datinfo->datname);
     746            0 :                 if (!free_slot)
     747              :                 {
     748              :                         /*
     749              :                          * Something failed.  We don't need to know what it was, because
     750              :                          * the handler should already have emitted the necessary error
     751              :                          * messages.
     752              :                          */
     753            0 :                         failed = true;
     754            0 :                         break;
     755              :                 }
     756              : 
     757            0 :                 if (opts.verbose)
     758            0 :                         PQsetErrorVerbosity(free_slot->connection, PQERRORS_VERBOSE);
     759              : 
     760              :                 /*
     761              :                  * Execute the appropriate amcheck command for this relation using our
     762              :                  * slot's database connection.  We do not wait for the command to
     763              :                  * complete, nor do we perform any error checking, as that is done by
     764              :                  * the parallel slots and our handler callback functions.
     765              :                  */
     766            0 :                 if (rel->is_heap)
     767              :                 {
     768            0 :                         if (opts.verbose)
     769              :                         {
     770            0 :                                 if (opts.show_progress && progress_since_last_stderr)
     771            0 :                                         fprintf(stderr, "\n");
     772            0 :                                 pg_log_info("checking heap table \"%s.%s.%s\"",
     773              :                                                         rel->datinfo->datname, rel->nspname, rel->relname);
     774            0 :                                 progress_since_last_stderr = false;
     775            0 :                         }
     776            0 :                         prepare_heap_command(&sql, rel, free_slot->connection);
     777            0 :                         rel->sql = pstrdup(sql.data);        /* pg_free'd after command */
     778            0 :                         ParallelSlotSetHandler(free_slot, verify_heap_slot_handler, rel);
     779            0 :                         run_command(free_slot, rel->sql);
     780            0 :                 }
     781              :                 else
     782              :                 {
     783            0 :                         if (opts.verbose)
     784              :                         {
     785            0 :                                 if (opts.show_progress && progress_since_last_stderr)
     786            0 :                                         fprintf(stderr, "\n");
     787              : 
     788            0 :                                 pg_log_info("checking btree index \"%s.%s.%s\"",
     789              :                                                         rel->datinfo->datname, rel->nspname, rel->relname);
     790            0 :                                 progress_since_last_stderr = false;
     791            0 :                         }
     792            0 :                         prepare_btree_command(&sql, rel, free_slot->connection);
     793            0 :                         rel->sql = pstrdup(sql.data);        /* pg_free'd after command */
     794            0 :                         ParallelSlotSetHandler(free_slot, verify_btree_slot_handler, rel);
     795            0 :                         run_command(free_slot, rel->sql);
     796              :                 }
     797            0 :         }
     798            0 :         termPQExpBuffer(&sql);
     799              : 
     800            0 :         if (!failed)
     801              :         {
     802              : 
     803              :                 /*
     804              :                  * Wait for all slots to complete, or for one to indicate that an
     805              :                  * error occurred.  Like above, we rely on the handler emitting the
     806              :                  * necessary error messages.
     807              :                  */
     808            0 :                 if (sa && !ParallelSlotsWaitCompletion(sa))
     809            0 :                         failed = true;
     810              : 
     811            0 :                 progress_report(reltotal, relprogress, pagestotal, pageschecked, NULL, true, true);
     812            0 :         }
     813              : 
     814            0 :         if (sa)
     815              :         {
     816            0 :                 ParallelSlotsTerminate(sa);
     817            0 :                 FREE_AND_SET_NULL(sa);
     818            0 :         }
     819              : 
     820            0 :         if (failed)
     821            0 :                 exit(1);
     822              : 
     823            0 :         if (!all_checks_pass)
     824            0 :                 exit(2);
     825            0 : }
     826              : 
     827              : /*
     828              :  * prepare_heap_command
     829              :  *
     830              :  * Creates a SQL command for running amcheck checking on the given heap
     831              :  * relation.  The command is phrased as a SQL query, with column order and
     832              :  * names matching the expectations of verify_heap_slot_handler, which will
     833              :  * receive and handle each row returned from the verify_heapam() function.
     834              :  *
     835              :  * The constructed SQL command will silently skip temporary tables, as checking
     836              :  * them would needlessly draw errors from the underlying amcheck function.
     837              :  *
     838              :  * sql: buffer into which the heap table checking command will be written
     839              :  * rel: relation information for the heap table to be checked
     840              :  * conn: the connection to be used, for string escaping purposes
     841              :  */
     842              : static void
     843            0 : prepare_heap_command(PQExpBuffer sql, RelationInfo *rel, PGconn *conn)
     844              : {
     845            0 :         resetPQExpBuffer(sql);
     846            0 :         appendPQExpBuffer(sql,
     847              :                                           "SELECT v.blkno, v.offnum, v.attnum, v.msg "
     848              :                                           "FROM pg_catalog.pg_class c, %s.verify_heapam("
     849              :                                           "\nrelation := c.oid, on_error_stop := %s, check_toast := %s, skip := '%s'",
     850            0 :                                           rel->datinfo->amcheck_schema,
     851            0 :                                           opts.on_error_stop ? "true" : "false",
     852            0 :                                           opts.reconcile_toast ? "true" : "false",
     853            0 :                                           opts.skip);
     854              : 
     855            0 :         if (opts.startblock >= 0)
     856            0 :                 appendPQExpBuffer(sql, ", startblock := " INT64_FORMAT, opts.startblock);
     857            0 :         if (opts.endblock >= 0)
     858            0 :                 appendPQExpBuffer(sql, ", endblock := " INT64_FORMAT, opts.endblock);
     859              : 
     860            0 :         appendPQExpBuffer(sql,
     861              :                                           "\n) v WHERE c.oid = %u "
     862              :                                           "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP),
     863            0 :                                           rel->reloid);
     864            0 : }
     865              : 
     866              : /*
     867              :  * prepare_btree_command
     868              :  *
     869              :  * Creates a SQL command for running amcheck checking on the given btree index
     870              :  * relation.  The command does not select any columns, as btree checking
     871              :  * functions do not return any, but rather return corruption information by
     872              :  * raising errors, which verify_btree_slot_handler expects.
     873              :  *
     874              :  * The constructed SQL command will silently skip temporary indexes, and
     875              :  * indexes being reindexed concurrently, as checking them would needlessly draw
     876              :  * errors from the underlying amcheck functions.
     877              :  *
     878              :  * sql: buffer into which the heap table checking command will be written
     879              :  * rel: relation information for the index to be checked
     880              :  * conn: the connection to be used, for string escaping purposes
     881              :  */
     882              : static void
     883            0 : prepare_btree_command(PQExpBuffer sql, RelationInfo *rel, PGconn *conn)
     884              : {
     885            0 :         resetPQExpBuffer(sql);
     886              : 
     887            0 :         if (opts.parent_check)
     888            0 :                 appendPQExpBuffer(sql,
     889              :                                                   "SELECT %s.bt_index_parent_check("
     890              :                                                   "index := c.oid, heapallindexed := %s, rootdescend := %s "
     891              :                                                   "%s)"
     892              :                                                   "\nFROM pg_catalog.pg_class c, pg_catalog.pg_index i "
     893              :                                                   "WHERE c.oid = %u "
     894              :                                                   "AND c.oid = i.indexrelid "
     895              :                                                   "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP) " "
     896              :                                                   "AND i.indisready AND i.indisvalid AND i.indislive",
     897            0 :                                                   rel->datinfo->amcheck_schema,
     898            0 :                                                   (opts.heapallindexed ? "true" : "false"),
     899            0 :                                                   (opts.rootdescend ? "true" : "false"),
     900            0 :                                                   (rel->datinfo->is_checkunique ? ", checkunique := true" : ""),
     901            0 :                                                   rel->reloid);
     902              :         else
     903            0 :                 appendPQExpBuffer(sql,
     904              :                                                   "SELECT %s.bt_index_check("
     905              :                                                   "index := c.oid, heapallindexed := %s "
     906              :                                                   "%s)"
     907              :                                                   "\nFROM pg_catalog.pg_class c, pg_catalog.pg_index i "
     908              :                                                   "WHERE c.oid = %u "
     909              :                                                   "AND c.oid = i.indexrelid "
     910              :                                                   "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP) " "
     911              :                                                   "AND i.indisready AND i.indisvalid AND i.indislive",
     912            0 :                                                   rel->datinfo->amcheck_schema,
     913            0 :                                                   (opts.heapallindexed ? "true" : "false"),
     914            0 :                                                   (rel->datinfo->is_checkunique ? ", checkunique := true" : ""),
     915            0 :                                                   rel->reloid);
     916            0 : }
     917              : 
     918              : /*
     919              :  * run_command
     920              :  *
     921              :  * Sends a command to the server without waiting for the command to complete.
     922              :  * Logs an error if the command cannot be sent, but otherwise any errors are
     923              :  * expected to be handled by a ParallelSlotHandler.
     924              :  *
     925              :  * If reconnecting to the database is necessary, the cparams argument may be
     926              :  * modified.
     927              :  *
     928              :  * slot: slot with connection to the server we should use for the command
     929              :  * sql: query to send
     930              :  */
     931              : static void
     932            0 : run_command(ParallelSlot *slot, const char *sql)
     933              : {
     934            0 :         if (opts.echo)
     935            0 :                 printf("%s\n", sql);
     936              : 
     937            0 :         if (PQsendQuery(slot->connection, sql) == 0)
     938              :         {
     939            0 :                 pg_log_error("error sending command to database \"%s\": %s",
     940              :                                          PQdb(slot->connection),
     941              :                                          PQerrorMessage(slot->connection));
     942            0 :                 pg_log_error_detail("Command was: %s", sql);
     943            0 :                 exit(1);
     944              :         }
     945            0 : }
     946              : 
     947              : /*
     948              :  * should_processing_continue
     949              :  *
     950              :  * Checks a query result returned from a query (presumably issued on a slot's
     951              :  * connection) to determine if parallel slots should continue issuing further
     952              :  * commands.
     953              :  *
     954              :  * Note: Heap relation corruption is reported by verify_heapam() via the result
     955              :  * set, rather than an ERROR, but running verify_heapam() on a corrupted heap
     956              :  * table may still result in an error being returned from the server due to
     957              :  * missing relation files, bad checksums, etc.  The btree corruption checking
     958              :  * functions always use errors to communicate corruption messages.  We can't
     959              :  * just abort processing because we got a mere ERROR.
     960              :  *
     961              :  * res: result from an executed sql query
     962              :  */
     963              : static bool
     964            0 : should_processing_continue(PGresult *res)
     965              : {
     966            0 :         const char *severity;
     967              : 
     968            0 :         switch (PQresultStatus(res))
     969              :         {
     970              :                         /* These are expected and ok */
     971              :                 case PGRES_COMMAND_OK:
     972              :                 case PGRES_TUPLES_OK:
     973              :                 case PGRES_NONFATAL_ERROR:
     974            0 :                         break;
     975              : 
     976              :                         /* This is expected but requires closer scrutiny */
     977              :                 case PGRES_FATAL_ERROR:
     978            0 :                         severity = PQresultErrorField(res, PG_DIAG_SEVERITY_NONLOCALIZED);
     979            0 :                         if (severity == NULL)
     980            0 :                                 return false;   /* libpq failure, probably lost connection */
     981            0 :                         if (strcmp(severity, "FATAL") == 0)
     982            0 :                                 return false;
     983            0 :                         if (strcmp(severity, "PANIC") == 0)
     984            0 :                                 return false;
     985            0 :                         break;
     986              : 
     987              :                         /* These are unexpected */
     988              :                 case PGRES_BAD_RESPONSE:
     989              :                 case PGRES_EMPTY_QUERY:
     990              :                 case PGRES_COPY_OUT:
     991              :                 case PGRES_COPY_IN:
     992              :                 case PGRES_COPY_BOTH:
     993              :                 case PGRES_SINGLE_TUPLE:
     994              :                 case PGRES_PIPELINE_SYNC:
     995              :                 case PGRES_PIPELINE_ABORTED:
     996              :                 case PGRES_TUPLES_CHUNK:
     997            0 :                         return false;
     998              :         }
     999            0 :         return true;
    1000            0 : }
    1001              : 
    1002              : /*
    1003              :  * Returns a copy of the argument string with all lines indented four spaces.
    1004              :  *
    1005              :  * The caller should pg_free the result when finished with it.
    1006              :  */
    1007              : static char *
    1008            0 : indent_lines(const char *str)
    1009              : {
    1010            0 :         PQExpBufferData buf;
    1011            0 :         const char *c;
    1012            0 :         char       *result;
    1013              : 
    1014            0 :         initPQExpBuffer(&buf);
    1015            0 :         appendPQExpBufferStr(&buf, "    ");
    1016            0 :         for (c = str; *c; c++)
    1017              :         {
    1018            0 :                 appendPQExpBufferChar(&buf, *c);
    1019            0 :                 if (c[0] == '\n' && c[1] != '\0')
    1020            0 :                         appendPQExpBufferStr(&buf, "    ");
    1021            0 :         }
    1022            0 :         result = pstrdup(buf.data);
    1023            0 :         termPQExpBuffer(&buf);
    1024              : 
    1025            0 :         return result;
    1026            0 : }
    1027              : 
    1028              : /*
    1029              :  * verify_heap_slot_handler
    1030              :  *
    1031              :  * ParallelSlotHandler that receives results from a heap table checking command
    1032              :  * created by prepare_heap_command and outputs the results for the user.
    1033              :  *
    1034              :  * res: result from an executed sql query
    1035              :  * conn: connection on which the sql query was executed
    1036              :  * context: the sql query being handled, as a cstring
    1037              :  */
    1038              : static bool
    1039            0 : verify_heap_slot_handler(PGresult *res, PGconn *conn, void *context)
    1040              : {
    1041            0 :         RelationInfo *rel = (RelationInfo *) context;
    1042              : 
    1043            0 :         if (PQresultStatus(res) == PGRES_TUPLES_OK)
    1044              :         {
    1045            0 :                 int                     i;
    1046            0 :                 int                     ntups = PQntuples(res);
    1047              : 
    1048            0 :                 if (ntups > 0)
    1049            0 :                         all_checks_pass = false;
    1050              : 
    1051            0 :                 for (i = 0; i < ntups; i++)
    1052              :                 {
    1053            0 :                         const char *msg;
    1054              : 
    1055              :                         /* The message string should never be null, but check */
    1056            0 :                         if (PQgetisnull(res, i, 3))
    1057            0 :                                 msg = "NO MESSAGE";
    1058              :                         else
    1059            0 :                                 msg = PQgetvalue(res, i, 3);
    1060              : 
    1061            0 :                         if (!PQgetisnull(res, i, 2))
    1062            0 :                                 printf(_("heap table \"%s.%s.%s\", block %s, offset %s, attribute %s:\n"),
    1063              :                                            rel->datinfo->datname, rel->nspname, rel->relname,
    1064              :                                            PQgetvalue(res, i, 0),       /* blkno */
    1065              :                                            PQgetvalue(res, i, 1),       /* offnum */
    1066              :                                            PQgetvalue(res, i, 2));      /* attnum */
    1067              : 
    1068            0 :                         else if (!PQgetisnull(res, i, 1))
    1069            0 :                                 printf(_("heap table \"%s.%s.%s\", block %s, offset %s:\n"),
    1070              :                                            rel->datinfo->datname, rel->nspname, rel->relname,
    1071              :                                            PQgetvalue(res, i, 0),       /* blkno */
    1072              :                                            PQgetvalue(res, i, 1));      /* offnum */
    1073              : 
    1074            0 :                         else if (!PQgetisnull(res, i, 0))
    1075            0 :                                 printf(_("heap table \"%s.%s.%s\", block %s:\n"),
    1076              :                                            rel->datinfo->datname, rel->nspname, rel->relname,
    1077              :                                            PQgetvalue(res, i, 0));      /* blkno */
    1078              : 
    1079              :                         else
    1080            0 :                                 printf(_("heap table \"%s.%s.%s\":\n"),
    1081              :                                            rel->datinfo->datname, rel->nspname, rel->relname);
    1082              : 
    1083            0 :                         printf("    %s\n", msg);
    1084            0 :                 }
    1085            0 :         }
    1086            0 :         else if (PQresultStatus(res) != PGRES_TUPLES_OK)
    1087              :         {
    1088            0 :                 char       *msg = indent_lines(PQerrorMessage(conn));
    1089              : 
    1090            0 :                 all_checks_pass = false;
    1091            0 :                 printf(_("heap table \"%s.%s.%s\":\n"),
    1092              :                            rel->datinfo->datname, rel->nspname, rel->relname);
    1093            0 :                 printf("%s", msg);
    1094            0 :                 if (opts.verbose)
    1095            0 :                         printf(_("query was: %s\n"), rel->sql);
    1096            0 :                 FREE_AND_SET_NULL(msg);
    1097            0 :         }
    1098              : 
    1099            0 :         FREE_AND_SET_NULL(rel->sql);
    1100            0 :         FREE_AND_SET_NULL(rel->nspname);
    1101            0 :         FREE_AND_SET_NULL(rel->relname);
    1102              : 
    1103            0 :         return should_processing_continue(res);
    1104            0 : }
    1105              : 
    1106              : /*
    1107              :  * verify_btree_slot_handler
    1108              :  *
    1109              :  * ParallelSlotHandler that receives results from a btree checking command
    1110              :  * created by prepare_btree_command and outputs them for the user.  The results
    1111              :  * from the btree checking command is assumed to be empty, but when the results
    1112              :  * are an error code, the useful information about the corruption is expected
    1113              :  * in the connection's error message.
    1114              :  *
    1115              :  * res: result from an executed sql query
    1116              :  * conn: connection on which the sql query was executed
    1117              :  * context: unused
    1118              :  */
    1119              : static bool
    1120            0 : verify_btree_slot_handler(PGresult *res, PGconn *conn, void *context)
    1121              : {
    1122            0 :         RelationInfo *rel = (RelationInfo *) context;
    1123              : 
    1124            0 :         if (PQresultStatus(res) == PGRES_TUPLES_OK)
    1125              :         {
    1126            0 :                 int                     ntups = PQntuples(res);
    1127              : 
    1128            0 :                 if (ntups > 1)
    1129              :                 {
    1130              :                         /*
    1131              :                          * We expect the btree checking functions to return one void row
    1132              :                          * each, or zero rows if the check was skipped due to the object
    1133              :                          * being in the wrong state to be checked, so we should output
    1134              :                          * some sort of warning if we get anything more, not because it
    1135              :                          * indicates corruption, but because it suggests a mismatch
    1136              :                          * between amcheck and pg_amcheck versions.
    1137              :                          *
    1138              :                          * In conjunction with --progress, anything written to stderr at
    1139              :                          * this time would present strangely to the user without an extra
    1140              :                          * newline, so we print one.  If we were multithreaded, we'd have
    1141              :                          * to avoid splitting this across multiple calls, but we're in an
    1142              :                          * event loop, so it doesn't matter.
    1143              :                          */
    1144            0 :                         if (opts.show_progress && progress_since_last_stderr)
    1145            0 :                                 fprintf(stderr, "\n");
    1146            0 :                         pg_log_warning("btree index \"%s.%s.%s\": btree checking function returned unexpected number of rows: %d",
    1147              :                                                    rel->datinfo->datname, rel->nspname, rel->relname, ntups);
    1148            0 :                         if (opts.verbose)
    1149            0 :                                 pg_log_warning_detail("Query was: %s", rel->sql);
    1150            0 :                         pg_log_warning_hint("Are %s's and amcheck's versions compatible?",
    1151              :                                                                 progname);
    1152            0 :                         progress_since_last_stderr = false;
    1153            0 :                 }
    1154            0 :         }
    1155              :         else
    1156              :         {
    1157            0 :                 char       *msg = indent_lines(PQerrorMessage(conn));
    1158              : 
    1159            0 :                 all_checks_pass = false;
    1160            0 :                 printf(_("btree index \"%s.%s.%s\":\n"),
    1161              :                            rel->datinfo->datname, rel->nspname, rel->relname);
    1162            0 :                 printf("%s", msg);
    1163            0 :                 if (opts.verbose)
    1164            0 :                         printf(_("query was: %s\n"), rel->sql);
    1165            0 :                 FREE_AND_SET_NULL(msg);
    1166            0 :         }
    1167              : 
    1168            0 :         FREE_AND_SET_NULL(rel->sql);
    1169            0 :         FREE_AND_SET_NULL(rel->nspname);
    1170            0 :         FREE_AND_SET_NULL(rel->relname);
    1171              : 
    1172            0 :         return should_processing_continue(res);
    1173            0 : }
    1174              : 
    1175              : /*
    1176              :  * help
    1177              :  *
    1178              :  * Prints help page for the program
    1179              :  *
    1180              :  * progname: the name of the executed program, such as "pg_amcheck"
    1181              :  */
    1182              : static void
    1183            0 : help(const char *progname)
    1184              : {
    1185            0 :         printf(_("%s checks objects in a PostgreSQL database for corruption.\n\n"), progname);
    1186            0 :         printf(_("Usage:\n"));
    1187            0 :         printf(_("  %s [OPTION]... [DBNAME]\n"), progname);
    1188            0 :         printf(_("\nTarget options:\n"));
    1189            0 :         printf(_("  -a, --all                       check all databases\n"));
    1190            0 :         printf(_("  -d, --database=PATTERN          check matching database(s)\n"));
    1191            0 :         printf(_("  -D, --exclude-database=PATTERN  do NOT check matching database(s)\n"));
    1192            0 :         printf(_("  -i, --index=PATTERN             check matching index(es)\n"));
    1193            0 :         printf(_("  -I, --exclude-index=PATTERN     do NOT check matching index(es)\n"));
    1194            0 :         printf(_("  -r, --relation=PATTERN          check matching relation(s)\n"));
    1195            0 :         printf(_("  -R, --exclude-relation=PATTERN  do NOT check matching relation(s)\n"));
    1196            0 :         printf(_("  -s, --schema=PATTERN            check matching schema(s)\n"));
    1197            0 :         printf(_("  -S, --exclude-schema=PATTERN    do NOT check matching schema(s)\n"));
    1198            0 :         printf(_("  -t, --table=PATTERN             check matching table(s)\n"));
    1199            0 :         printf(_("  -T, --exclude-table=PATTERN     do NOT check matching table(s)\n"));
    1200            0 :         printf(_("      --no-dependent-indexes      do NOT expand list of relations to include indexes\n"));
    1201            0 :         printf(_("      --no-dependent-toast        do NOT expand list of relations to include TOAST tables\n"));
    1202            0 :         printf(_("      --no-strict-names           do NOT require patterns to match objects\n"));
    1203            0 :         printf(_("\nTable checking options:\n"));
    1204            0 :         printf(_("      --exclude-toast-pointers    do NOT follow relation TOAST pointers\n"));
    1205            0 :         printf(_("      --on-error-stop             stop checking at end of first corrupt page\n"));
    1206            0 :         printf(_("      --skip=OPTION               do NOT check \"all-frozen\" or \"all-visible\" blocks\n"));
    1207            0 :         printf(_("      --startblock=BLOCK          begin checking table(s) at the given block number\n"));
    1208            0 :         printf(_("      --endblock=BLOCK            check table(s) only up to the given block number\n"));
    1209            0 :         printf(_("\nB-tree index checking options:\n"));
    1210            0 :         printf(_("      --checkunique               check unique constraint if index is unique\n"));
    1211            0 :         printf(_("      --heapallindexed            check that all heap tuples are found within indexes\n"));
    1212            0 :         printf(_("      --parent-check              check index parent/child relationships\n"));
    1213            0 :         printf(_("      --rootdescend               search from root page to refind tuples\n"));
    1214            0 :         printf(_("\nConnection options:\n"));
    1215            0 :         printf(_("  -h, --host=HOSTNAME             database server host or socket directory\n"));
    1216            0 :         printf(_("  -p, --port=PORT                 database server port\n"));
    1217            0 :         printf(_("  -U, --username=USERNAME         user name to connect as\n"));
    1218            0 :         printf(_("  -w, --no-password               never prompt for password\n"));
    1219            0 :         printf(_("  -W, --password                  force password prompt\n"));
    1220            0 :         printf(_("      --maintenance-db=DBNAME     alternate maintenance database\n"));
    1221            0 :         printf(_("\nOther options:\n"));
    1222            0 :         printf(_("  -e, --echo                      show the commands being sent to the server\n"));
    1223            0 :         printf(_("  -j, --jobs=NUM                  use this many concurrent connections to the server\n"));
    1224            0 :         printf(_("  -P, --progress                  show progress information\n"));
    1225            0 :         printf(_("  -v, --verbose                   write a lot of output\n"));
    1226            0 :         printf(_("  -V, --version                   output version information, then exit\n"));
    1227            0 :         printf(_("      --install-missing           install missing extensions\n"));
    1228            0 :         printf(_("  -?, --help                      show this help, then exit\n"));
    1229              : 
    1230            0 :         printf(_("\nReport bugs to <%s>.\n"), PACKAGE_BUGREPORT);
    1231            0 :         printf(_("%s home page: <%s>\n"), PACKAGE_NAME, PACKAGE_URL);
    1232            0 : }
    1233              : 
    1234              : /*
    1235              :  * Print a progress report based on the global variables.
    1236              :  *
    1237              :  * Progress report is written at maximum once per second, unless the force
    1238              :  * parameter is set to true.
    1239              :  *
    1240              :  * If finished is set to true, this is the last progress report. The cursor
    1241              :  * is moved to the next line.
    1242              :  */
    1243              : static void
    1244            0 : progress_report(uint64 relations_total, uint64 relations_checked,
    1245              :                                 uint64 relpages_total, uint64 relpages_checked,
    1246              :                                 const char *datname, bool force, bool finished)
    1247              : {
    1248            0 :         int                     percent_rel = 0;
    1249            0 :         int                     percent_pages = 0;
    1250            0 :         char            checked_rel[32];
    1251            0 :         char            total_rel[32];
    1252            0 :         char            checked_pages[32];
    1253            0 :         char            total_pages[32];
    1254            0 :         pg_time_t       now;
    1255              : 
    1256            0 :         if (!opts.show_progress)
    1257            0 :                 return;
    1258              : 
    1259            0 :         now = time(NULL);
    1260            0 :         if (now == last_progress_report && !force && !finished)
    1261            0 :                 return;                                 /* Max once per second */
    1262              : 
    1263            0 :         last_progress_report = now;
    1264            0 :         if (relations_total)
    1265            0 :                 percent_rel = (int) (relations_checked * 100 / relations_total);
    1266            0 :         if (relpages_total)
    1267            0 :                 percent_pages = (int) (relpages_checked * 100 / relpages_total);
    1268              : 
    1269            0 :         snprintf(checked_rel, sizeof(checked_rel), UINT64_FORMAT, relations_checked);
    1270            0 :         snprintf(total_rel, sizeof(total_rel), UINT64_FORMAT, relations_total);
    1271            0 :         snprintf(checked_pages, sizeof(checked_pages), UINT64_FORMAT, relpages_checked);
    1272            0 :         snprintf(total_pages, sizeof(total_pages), UINT64_FORMAT, relpages_total);
    1273              : 
    1274              : #define VERBOSE_DATNAME_LENGTH 35
    1275            0 :         if (opts.verbose)
    1276              :         {
    1277            0 :                 if (!datname)
    1278              : 
    1279              :                         /*
    1280              :                          * No datname given, so clear the status line (used for first and
    1281              :                          * last call)
    1282              :                          */
    1283            0 :                         fprintf(stderr,
    1284            0 :                                         _("%*s/%s relations (%d%%), %*s/%s pages (%d%%) %*s"),
    1285            0 :                                         (int) strlen(total_rel),
    1286            0 :                                         checked_rel, total_rel, percent_rel,
    1287            0 :                                         (int) strlen(total_pages),
    1288            0 :                                         checked_pages, total_pages, percent_pages,
    1289              :                                         VERBOSE_DATNAME_LENGTH + 2, "");
    1290              :                 else
    1291              :                 {
    1292            0 :                         bool            truncate = (strlen(datname) > VERBOSE_DATNAME_LENGTH);
    1293              : 
    1294            0 :                         fprintf(stderr,
    1295            0 :                                         _("%*s/%s relations (%d%%), %*s/%s pages (%d%%) (%s%-*.*s)"),
    1296            0 :                                         (int) strlen(total_rel),
    1297            0 :                                         checked_rel, total_rel, percent_rel,
    1298            0 :                                         (int) strlen(total_pages),
    1299            0 :                                         checked_pages, total_pages, percent_pages,
    1300              :                         /* Prefix with "..." if we do leading truncation */
    1301            0 :                                         truncate ? "..." : "",
    1302            0 :                                         truncate ? VERBOSE_DATNAME_LENGTH - 3 : VERBOSE_DATNAME_LENGTH,
    1303            0 :                                         truncate ? VERBOSE_DATNAME_LENGTH - 3 : VERBOSE_DATNAME_LENGTH,
    1304              :                         /* Truncate datname at beginning if it's too long */
    1305            0 :                                         truncate ? datname + strlen(datname) - VERBOSE_DATNAME_LENGTH + 3 : datname);
    1306            0 :                 }
    1307            0 :         }
    1308              :         else
    1309            0 :                 fprintf(stderr,
    1310            0 :                                 _("%*s/%s relations (%d%%), %*s/%s pages (%d%%)"),
    1311            0 :                                 (int) strlen(total_rel),
    1312            0 :                                 checked_rel, total_rel, percent_rel,
    1313            0 :                                 (int) strlen(total_pages),
    1314            0 :                                 checked_pages, total_pages, percent_pages);
    1315              : 
    1316              :         /*
    1317              :          * Stay on the same line if reporting to a terminal and we're not done
    1318              :          * yet.
    1319              :          */
    1320            0 :         if (!finished && isatty(fileno(stderr)))
    1321              :         {
    1322            0 :                 fputc('\r', stderr);
    1323            0 :                 progress_since_last_stderr = true;
    1324            0 :         }
    1325              :         else
    1326            0 :                 fputc('\n', stderr);
    1327            0 : }
    1328              : 
    1329              : /*
    1330              :  * Extend the pattern info array to hold one additional initialized pattern
    1331              :  * info entry.
    1332              :  *
    1333              :  * Returns a pointer to the new entry.
    1334              :  */
    1335              : static PatternInfo *
    1336            0 : extend_pattern_info_array(PatternInfoArray *pia)
    1337              : {
    1338            0 :         PatternInfo *result;
    1339              : 
    1340            0 :         pia->len++;
    1341            0 :         pia->data = (PatternInfo *) pg_realloc(pia->data, pia->len * sizeof(PatternInfo));
    1342            0 :         result = &pia->data[pia->len - 1];
    1343            0 :         memset(result, 0, sizeof(*result));
    1344              : 
    1345            0 :         return result;
    1346            0 : }
    1347              : 
    1348              : /*
    1349              :  * append_database_pattern
    1350              :  *
    1351              :  * Adds the given pattern interpreted as a database name pattern.
    1352              :  *
    1353              :  * pia: the pattern info array to be appended
    1354              :  * pattern: the database name pattern
    1355              :  * encoding: client encoding for parsing the pattern
    1356              :  */
    1357              : static void
    1358            0 : append_database_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
    1359              : {
    1360            0 :         PQExpBufferData buf;
    1361            0 :         int                     dotcnt;
    1362            0 :         PatternInfo *info = extend_pattern_info_array(pia);
    1363              : 
    1364            0 :         initPQExpBuffer(&buf);
    1365            0 :         patternToSQLRegex(encoding, NULL, NULL, &buf, pattern, false, false,
    1366              :                                           &dotcnt);
    1367            0 :         if (dotcnt > 0)
    1368              :         {
    1369            0 :                 pg_log_error("improper qualified name (too many dotted names): %s", pattern);
    1370            0 :                 exit(2);
    1371              :         }
    1372            0 :         info->pattern = pattern;
    1373            0 :         info->db_regex = pstrdup(buf.data);
    1374              : 
    1375            0 :         termPQExpBuffer(&buf);
    1376            0 : }
    1377              : 
    1378              : /*
    1379              :  * append_schema_pattern
    1380              :  *
    1381              :  * Adds the given pattern interpreted as a schema name pattern.
    1382              :  *
    1383              :  * pia: the pattern info array to be appended
    1384              :  * pattern: the schema name pattern
    1385              :  * encoding: client encoding for parsing the pattern
    1386              :  */
    1387              : static void
    1388            0 : append_schema_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
    1389              : {
    1390            0 :         PQExpBufferData dbbuf;
    1391            0 :         PQExpBufferData nspbuf;
    1392            0 :         int                     dotcnt;
    1393            0 :         PatternInfo *info = extend_pattern_info_array(pia);
    1394              : 
    1395            0 :         initPQExpBuffer(&dbbuf);
    1396            0 :         initPQExpBuffer(&nspbuf);
    1397              : 
    1398            0 :         patternToSQLRegex(encoding, NULL, &dbbuf, &nspbuf, pattern, false, false,
    1399              :                                           &dotcnt);
    1400            0 :         if (dotcnt > 1)
    1401              :         {
    1402            0 :                 pg_log_error("improper qualified name (too many dotted names): %s", pattern);
    1403            0 :                 exit(2);
    1404              :         }
    1405            0 :         info->pattern = pattern;
    1406            0 :         if (dbbuf.data[0])
    1407              :         {
    1408            0 :                 opts.dbpattern = true;
    1409            0 :                 info->db_regex = pstrdup(dbbuf.data);
    1410            0 :         }
    1411            0 :         if (nspbuf.data[0])
    1412            0 :                 info->nsp_regex = pstrdup(nspbuf.data);
    1413              : 
    1414            0 :         termPQExpBuffer(&dbbuf);
    1415            0 :         termPQExpBuffer(&nspbuf);
    1416            0 : }
    1417              : 
    1418              : /*
    1419              :  * append_relation_pattern_helper
    1420              :  *
    1421              :  * Adds to a list the given pattern interpreted as a relation pattern.
    1422              :  *
    1423              :  * pia: the pattern info array to be appended
    1424              :  * pattern: the relation name pattern
    1425              :  * encoding: client encoding for parsing the pattern
    1426              :  * heap_only: whether the pattern should only be matched against heap tables
    1427              :  * btree_only: whether the pattern should only be matched against btree indexes
    1428              :  */
    1429              : static void
    1430            0 : append_relation_pattern_helper(PatternInfoArray *pia, const char *pattern,
    1431              :                                                            int encoding, bool heap_only, bool btree_only)
    1432              : {
    1433            0 :         PQExpBufferData dbbuf;
    1434            0 :         PQExpBufferData nspbuf;
    1435            0 :         PQExpBufferData relbuf;
    1436            0 :         int                     dotcnt;
    1437            0 :         PatternInfo *info = extend_pattern_info_array(pia);
    1438              : 
    1439            0 :         initPQExpBuffer(&dbbuf);
    1440            0 :         initPQExpBuffer(&nspbuf);
    1441            0 :         initPQExpBuffer(&relbuf);
    1442              : 
    1443            0 :         patternToSQLRegex(encoding, &dbbuf, &nspbuf, &relbuf, pattern, false,
    1444              :                                           false, &dotcnt);
    1445            0 :         if (dotcnt > 2)
    1446              :         {
    1447            0 :                 pg_log_error("improper relation name (too many dotted names): %s", pattern);
    1448            0 :                 exit(2);
    1449              :         }
    1450            0 :         info->pattern = pattern;
    1451            0 :         if (dbbuf.data[0])
    1452              :         {
    1453            0 :                 opts.dbpattern = true;
    1454            0 :                 info->db_regex = pstrdup(dbbuf.data);
    1455            0 :         }
    1456            0 :         if (nspbuf.data[0])
    1457            0 :                 info->nsp_regex = pstrdup(nspbuf.data);
    1458            0 :         if (relbuf.data[0])
    1459            0 :                 info->rel_regex = pstrdup(relbuf.data);
    1460              : 
    1461            0 :         termPQExpBuffer(&dbbuf);
    1462            0 :         termPQExpBuffer(&nspbuf);
    1463            0 :         termPQExpBuffer(&relbuf);
    1464              : 
    1465            0 :         info->heap_only = heap_only;
    1466            0 :         info->btree_only = btree_only;
    1467            0 : }
    1468              : 
    1469              : /*
    1470              :  * append_relation_pattern
    1471              :  *
    1472              :  * Adds the given pattern interpreted as a relation pattern, to be matched
    1473              :  * against both heap tables and btree indexes.
    1474              :  *
    1475              :  * pia: the pattern info array to be appended
    1476              :  * pattern: the relation name pattern
    1477              :  * encoding: client encoding for parsing the pattern
    1478              :  */
    1479              : static void
    1480            0 : append_relation_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
    1481              : {
    1482            0 :         append_relation_pattern_helper(pia, pattern, encoding, false, false);
    1483            0 : }
    1484              : 
    1485              : /*
    1486              :  * append_heap_pattern
    1487              :  *
    1488              :  * Adds the given pattern interpreted as a relation pattern, to be matched only
    1489              :  * against heap tables.
    1490              :  *
    1491              :  * pia: the pattern info array to be appended
    1492              :  * pattern: the relation name pattern
    1493              :  * encoding: client encoding for parsing the pattern
    1494              :  */
    1495              : static void
    1496            0 : append_heap_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
    1497              : {
    1498            0 :         append_relation_pattern_helper(pia, pattern, encoding, true, false);
    1499            0 : }
    1500              : 
    1501              : /*
    1502              :  * append_btree_pattern
    1503              :  *
    1504              :  * Adds the given pattern interpreted as a relation pattern, to be matched only
    1505              :  * against btree indexes.
    1506              :  *
    1507              :  * pia: the pattern info array to be appended
    1508              :  * pattern: the relation name pattern
    1509              :  * encoding: client encoding for parsing the pattern
    1510              :  */
    1511              : static void
    1512            0 : append_btree_pattern(PatternInfoArray *pia, const char *pattern, int encoding)
    1513              : {
    1514            0 :         append_relation_pattern_helper(pia, pattern, encoding, false, true);
    1515            0 : }
    1516              : 
    1517              : /*
    1518              :  * append_db_pattern_cte
    1519              :  *
    1520              :  * Appends to the buffer the body of a Common Table Expression (CTE) containing
    1521              :  * the database portions filtered from the list of patterns expressed as two
    1522              :  * columns:
    1523              :  *
    1524              :  *     pattern_id: the index of this pattern in pia->data[]
    1525              :  *     rgx: the database regular expression parsed from the pattern
    1526              :  *
    1527              :  * Patterns without a database portion are skipped.  Patterns with more than
    1528              :  * just a database portion are optionally skipped, depending on argument
    1529              :  * 'inclusive'.
    1530              :  *
    1531              :  * buf: the buffer to be appended
    1532              :  * pia: the array of patterns to be inserted into the CTE
    1533              :  * conn: the database connection
    1534              :  * inclusive: whether to include patterns with schema and/or relation parts
    1535              :  *
    1536              :  * Returns whether any database patterns were appended.
    1537              :  */
    1538              : static bool
    1539            0 : append_db_pattern_cte(PQExpBuffer buf, const PatternInfoArray *pia,
    1540              :                                           PGconn *conn, bool inclusive)
    1541              : {
    1542            0 :         int                     pattern_id;
    1543            0 :         const char *comma;
    1544            0 :         bool            have_values;
    1545              : 
    1546            0 :         comma = "";
    1547            0 :         have_values = false;
    1548            0 :         for (pattern_id = 0; pattern_id < pia->len; pattern_id++)
    1549              :         {
    1550            0 :                 PatternInfo *info = &pia->data[pattern_id];
    1551              : 
    1552            0 :                 if (info->db_regex != NULL &&
    1553            0 :                         (inclusive || (info->nsp_regex == NULL && info->rel_regex == NULL)))
    1554              :                 {
    1555            0 :                         if (!have_values)
    1556            0 :                                 appendPQExpBufferStr(buf, "\nVALUES");
    1557            0 :                         have_values = true;
    1558            0 :                         appendPQExpBuffer(buf, "%s\n(%d, ", comma, pattern_id);
    1559            0 :                         appendStringLiteralConn(buf, info->db_regex, conn);
    1560            0 :                         appendPQExpBufferChar(buf, ')');
    1561            0 :                         comma = ",";
    1562            0 :                 }
    1563            0 :         }
    1564              : 
    1565            0 :         if (!have_values)
    1566            0 :                 appendPQExpBufferStr(buf, "\nSELECT NULL, NULL, NULL WHERE false");
    1567              : 
    1568            0 :         return have_values;
    1569            0 : }
    1570              : 
    1571              : /*
    1572              :  * compile_database_list
    1573              :  *
    1574              :  * If any database patterns exist, or if --all was given, compiles a distinct
    1575              :  * list of databases to check using a SQL query based on the patterns plus the
    1576              :  * literal initial database name, if given.  If no database patterns exist and
    1577              :  * --all was not given, the query is not necessary, and only the initial
    1578              :  * database name (if any) is added to the list.
    1579              :  *
    1580              :  * conn: connection to the initial database
    1581              :  * databases: the list onto which databases should be appended
    1582              :  * initial_dbname: an optional extra database name to include in the list
    1583              :  */
    1584              : static void
    1585            0 : compile_database_list(PGconn *conn, SimplePtrList *databases,
    1586              :                                           const char *initial_dbname)
    1587              : {
    1588            0 :         PGresult   *res;
    1589            0 :         PQExpBufferData sql;
    1590            0 :         int                     ntups;
    1591            0 :         int                     i;
    1592            0 :         bool            fatal;
    1593              : 
    1594            0 :         if (initial_dbname)
    1595              :         {
    1596            0 :                 DatabaseInfo *dat = (DatabaseInfo *) pg_malloc0(sizeof(DatabaseInfo));
    1597              : 
    1598              :                 /* This database is included.  Add to list */
    1599            0 :                 if (opts.verbose)
    1600            0 :                         pg_log_info("including database \"%s\"", initial_dbname);
    1601              : 
    1602            0 :                 dat->datname = pstrdup(initial_dbname);
    1603            0 :                 simple_ptr_list_append(databases, dat);
    1604            0 :         }
    1605              : 
    1606            0 :         initPQExpBuffer(&sql);
    1607              : 
    1608              :         /* Append the include patterns CTE. */
    1609            0 :         appendPQExpBufferStr(&sql, "WITH include_raw (pattern_id, rgx) AS (");
    1610            0 :         if (!append_db_pattern_cte(&sql, &opts.include, conn, true) &&
    1611            0 :                 !opts.alldb)
    1612              :         {
    1613              :                 /*
    1614              :                  * None of the inclusion patterns (if any) contain database portions,
    1615              :                  * so there is no need to query the database to resolve database
    1616              :                  * patterns.
    1617              :                  *
    1618              :                  * Since we're also not operating under --all, we don't need to query
    1619              :                  * the exhaustive list of connectable databases, either.
    1620              :                  */
    1621            0 :                 termPQExpBuffer(&sql);
    1622            0 :                 return;
    1623              :         }
    1624              : 
    1625              :         /* Append the exclude patterns CTE. */
    1626            0 :         appendPQExpBufferStr(&sql, "),\nexclude_raw (pattern_id, rgx) AS (");
    1627            0 :         append_db_pattern_cte(&sql, &opts.exclude, conn, false);
    1628            0 :         appendPQExpBufferStr(&sql, "),");
    1629              : 
    1630              :         /*
    1631              :          * Append the database CTE, which includes whether each database is
    1632              :          * connectable and also joins against exclude_raw to determine whether
    1633              :          * each database is excluded.
    1634              :          */
    1635            0 :         appendPQExpBufferStr(&sql,
    1636              :                                                  "\ndatabase (datname) AS ("
    1637              :                                                  "\nSELECT d.datname "
    1638              :                                                  "FROM pg_catalog.pg_database d "
    1639              :                                                  "LEFT OUTER JOIN exclude_raw e "
    1640              :                                                  "ON d.datname ~ e.rgx "
    1641              :                                                  "\nWHERE d.datallowconn AND datconnlimit != -2 "
    1642              :                                                  "AND e.pattern_id IS NULL"
    1643              :                                                  "),"
    1644              : 
    1645              :         /*
    1646              :          * Append the include_pat CTE, which joins the include_raw CTE against the
    1647              :          * databases CTE to determine if all the inclusion patterns had matches,
    1648              :          * and whether each matched pattern had the misfortune of only matching
    1649              :          * excluded or unconnectable databases.
    1650              :          */
    1651              :                                                  "\ninclude_pat (pattern_id, checkable) AS ("
    1652              :                                                  "\nSELECT i.pattern_id, "
    1653              :                                                  "COUNT(*) FILTER ("
    1654              :                                                  "WHERE d IS NOT NULL"
    1655              :                                                  ") AS checkable"
    1656              :                                                  "\nFROM include_raw i "
    1657              :                                                  "LEFT OUTER JOIN database d "
    1658              :                                                  "ON d.datname ~ i.rgx"
    1659              :                                                  "\nGROUP BY i.pattern_id"
    1660              :                                                  "),"
    1661              : 
    1662              :         /*
    1663              :          * Append the filtered_databases CTE, which selects from the database CTE
    1664              :          * optionally joined against the include_raw CTE to only select databases
    1665              :          * that match an inclusion pattern.  This appears to duplicate what the
    1666              :          * include_pat CTE already did above, but here we want only databases, and
    1667              :          * there we wanted patterns.
    1668              :          */
    1669              :                                                  "\nfiltered_databases (datname) AS ("
    1670              :                                                  "\nSELECT DISTINCT d.datname "
    1671              :                                                  "FROM database d");
    1672            0 :         if (!opts.alldb)
    1673            0 :                 appendPQExpBufferStr(&sql,
    1674              :                                                          " INNER JOIN include_raw i "
    1675              :                                                          "ON d.datname ~ i.rgx");
    1676            0 :         appendPQExpBufferStr(&sql,
    1677              :                                                  ")"
    1678              : 
    1679              :         /*
    1680              :          * Select the checkable databases and the unmatched inclusion patterns.
    1681              :          */
    1682              :                                                  "\nSELECT pattern_id, datname FROM ("
    1683              :                                                  "\nSELECT pattern_id, NULL::TEXT AS datname "
    1684              :                                                  "FROM include_pat "
    1685              :                                                  "WHERE checkable = 0 "
    1686              :                                                  "UNION ALL"
    1687              :                                                  "\nSELECT NULL, datname "
    1688              :                                                  "FROM filtered_databases"
    1689              :                                                  ") AS combined_records"
    1690              :                                                  "\nORDER BY pattern_id NULLS LAST, datname");
    1691              : 
    1692            0 :         res = executeQuery(conn, sql.data, opts.echo);
    1693            0 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    1694              :         {
    1695            0 :                 pg_log_error("query failed: %s", PQerrorMessage(conn));
    1696            0 :                 pg_log_error_detail("Query was: %s", sql.data);
    1697            0 :                 disconnectDatabase(conn);
    1698            0 :                 exit(1);
    1699              :         }
    1700            0 :         termPQExpBuffer(&sql);
    1701              : 
    1702            0 :         ntups = PQntuples(res);
    1703            0 :         for (fatal = false, i = 0; i < ntups; i++)
    1704              :         {
    1705            0 :                 int                     pattern_id = -1;
    1706            0 :                 const char *datname = NULL;
    1707              : 
    1708            0 :                 if (!PQgetisnull(res, i, 0))
    1709            0 :                         pattern_id = atoi(PQgetvalue(res, i, 0));
    1710            0 :                 if (!PQgetisnull(res, i, 1))
    1711            0 :                         datname = PQgetvalue(res, i, 1);
    1712              : 
    1713            0 :                 if (pattern_id >= 0)
    1714              :                 {
    1715              :                         /*
    1716              :                          * Current record pertains to an inclusion pattern that matched no
    1717              :                          * checkable databases.
    1718              :                          */
    1719            0 :                         fatal = opts.strict_names;
    1720            0 :                         if (pattern_id >= opts.include.len)
    1721            0 :                                 pg_fatal("internal error: received unexpected database pattern_id %d",
    1722              :                                                  pattern_id);
    1723            0 :                         log_no_match("no connectable databases to check matching \"%s\"",
    1724              :                                                  opts.include.data[pattern_id].pattern);
    1725            0 :                 }
    1726              :                 else
    1727              :                 {
    1728            0 :                         DatabaseInfo *dat;
    1729              : 
    1730              :                         /* Current record pertains to a database */
    1731            0 :                         Assert(datname != NULL);
    1732              : 
    1733              :                         /* Avoid entering a duplicate entry matching the initial_dbname */
    1734            0 :                         if (initial_dbname != NULL && strcmp(initial_dbname, datname) == 0)
    1735            0 :                                 continue;
    1736              : 
    1737              :                         /* This database is included.  Add to list */
    1738            0 :                         if (opts.verbose)
    1739            0 :                                 pg_log_info("including database \"%s\"", datname);
    1740              : 
    1741            0 :                         dat = (DatabaseInfo *) pg_malloc0(sizeof(DatabaseInfo));
    1742            0 :                         dat->datname = pstrdup(datname);
    1743            0 :                         simple_ptr_list_append(databases, dat);
    1744            0 :                 }
    1745            0 :         }
    1746            0 :         PQclear(res);
    1747              : 
    1748            0 :         if (fatal)
    1749              :         {
    1750            0 :                 if (conn != NULL)
    1751            0 :                         disconnectDatabase(conn);
    1752            0 :                 exit(1);
    1753              :         }
    1754            0 : }
    1755              : 
    1756              : /*
    1757              :  * append_rel_pattern_raw_cte
    1758              :  *
    1759              :  * Appends to the buffer the body of a Common Table Expression (CTE) containing
    1760              :  * the given patterns as six columns:
    1761              :  *
    1762              :  *     pattern_id: the index of this pattern in pia->data[]
    1763              :  *     db_regex: the database regexp parsed from the pattern, or NULL if the
    1764              :  *               pattern had no database part
    1765              :  *     nsp_regex: the namespace regexp parsed from the pattern, or NULL if the
    1766              :  *                pattern had no namespace part
    1767              :  *     rel_regex: the relname regexp parsed from the pattern, or NULL if the
    1768              :  *                pattern had no relname part
    1769              :  *     heap_only: true if the pattern applies only to heap tables (not indexes)
    1770              :  *     btree_only: true if the pattern applies only to btree indexes (not tables)
    1771              :  *
    1772              :  * buf: the buffer to be appended
    1773              :  * patterns: the array of patterns to be inserted into the CTE
    1774              :  * conn: the database connection
    1775              :  */
    1776              : static void
    1777            0 : append_rel_pattern_raw_cte(PQExpBuffer buf, const PatternInfoArray *pia,
    1778              :                                                    PGconn *conn)
    1779              : {
    1780            0 :         int                     pattern_id;
    1781            0 :         const char *comma;
    1782            0 :         bool            have_values;
    1783              : 
    1784            0 :         comma = "";
    1785            0 :         have_values = false;
    1786            0 :         for (pattern_id = 0; pattern_id < pia->len; pattern_id++)
    1787              :         {
    1788            0 :                 PatternInfo *info = &pia->data[pattern_id];
    1789              : 
    1790            0 :                 if (!have_values)
    1791            0 :                         appendPQExpBufferStr(buf, "\nVALUES");
    1792            0 :                 have_values = true;
    1793            0 :                 appendPQExpBuffer(buf, "%s\n(%d::INTEGER, ", comma, pattern_id);
    1794            0 :                 if (info->db_regex == NULL)
    1795            0 :                         appendPQExpBufferStr(buf, "NULL");
    1796              :                 else
    1797            0 :                         appendStringLiteralConn(buf, info->db_regex, conn);
    1798            0 :                 appendPQExpBufferStr(buf, "::TEXT, ");
    1799            0 :                 if (info->nsp_regex == NULL)
    1800            0 :                         appendPQExpBufferStr(buf, "NULL");
    1801              :                 else
    1802            0 :                         appendStringLiteralConn(buf, info->nsp_regex, conn);
    1803            0 :                 appendPQExpBufferStr(buf, "::TEXT, ");
    1804            0 :                 if (info->rel_regex == NULL)
    1805            0 :                         appendPQExpBufferStr(buf, "NULL");
    1806              :                 else
    1807            0 :                         appendStringLiteralConn(buf, info->rel_regex, conn);
    1808            0 :                 if (info->heap_only)
    1809            0 :                         appendPQExpBufferStr(buf, "::TEXT, true::BOOLEAN");
    1810              :                 else
    1811            0 :                         appendPQExpBufferStr(buf, "::TEXT, false::BOOLEAN");
    1812            0 :                 if (info->btree_only)
    1813            0 :                         appendPQExpBufferStr(buf, ", true::BOOLEAN");
    1814              :                 else
    1815            0 :                         appendPQExpBufferStr(buf, ", false::BOOLEAN");
    1816            0 :                 appendPQExpBufferChar(buf, ')');
    1817            0 :                 comma = ",";
    1818            0 :         }
    1819              : 
    1820            0 :         if (!have_values)
    1821            0 :                 appendPQExpBufferStr(buf,
    1822              :                                                          "\nSELECT NULL::INTEGER, NULL::TEXT, NULL::TEXT, "
    1823              :                                                          "NULL::TEXT, NULL::BOOLEAN, NULL::BOOLEAN "
    1824              :                                                          "WHERE false");
    1825            0 : }
    1826              : 
    1827              : /*
    1828              :  * append_rel_pattern_filtered_cte
    1829              :  *
    1830              :  * Appends to the buffer a Common Table Expression (CTE) which selects
    1831              :  * all patterns from the named raw CTE, filtered by database.  All patterns
    1832              :  * which have no database portion or whose database portion matches our
    1833              :  * connection's database name are selected, with other patterns excluded.
    1834              :  *
    1835              :  * The basic idea here is that if we're connected to database "foo" and we have
    1836              :  * patterns "foo.bar.baz", "alpha.beta" and "one.two.three", we only want to
    1837              :  * use the first two while processing relations in this database, as the third
    1838              :  * one is not relevant.
    1839              :  *
    1840              :  * buf: the buffer to be appended
    1841              :  * raw: the name of the CTE to select from
    1842              :  * filtered: the name of the CTE to create
    1843              :  * conn: the database connection
    1844              :  */
    1845              : static void
    1846            0 : append_rel_pattern_filtered_cte(PQExpBuffer buf, const char *raw,
    1847              :                                                                 const char *filtered, PGconn *conn)
    1848              : {
    1849            0 :         appendPQExpBuffer(buf,
    1850              :                                           "\n%s (pattern_id, nsp_regex, rel_regex, heap_only, btree_only) AS ("
    1851              :                                           "\nSELECT pattern_id, nsp_regex, rel_regex, heap_only, btree_only "
    1852              :                                           "FROM %s r"
    1853              :                                           "\nWHERE (r.db_regex IS NULL "
    1854              :                                           "OR ",
    1855            0 :                                           filtered, raw);
    1856            0 :         appendStringLiteralConn(buf, PQdb(conn), conn);
    1857            0 :         appendPQExpBufferStr(buf, " ~ r.db_regex)");
    1858            0 :         appendPQExpBufferStr(buf,
    1859              :                                                  " AND (r.nsp_regex IS NOT NULL"
    1860              :                                                  " OR r.rel_regex IS NOT NULL)"
    1861              :                                                  "),");
    1862            0 : }
    1863              : 
    1864              : /*
    1865              :  * compile_relation_list_one_db
    1866              :  *
    1867              :  * Compiles a list of relations to check within the currently connected
    1868              :  * database based on the user supplied options, sorted by descending size,
    1869              :  * and appends them to the given list of relations.
    1870              :  *
    1871              :  * The cells of the constructed list contain all information about the relation
    1872              :  * necessary to connect to the database and check the object, including which
    1873              :  * database to connect to, where contrib/amcheck is installed, and the Oid and
    1874              :  * type of object (heap table vs. btree index).  Rather than duplicating the
    1875              :  * database details per relation, the relation structs use references to the
    1876              :  * same database object, provided by the caller.
    1877              :  *
    1878              :  * conn: connection to this next database, which should be the same as in 'dat'
    1879              :  * relations: list onto which the relations information should be appended
    1880              :  * dat: the database info struct for use by each relation
    1881              :  * pagecount: gets incremented by the number of blocks to check in all
    1882              :  * relations added
    1883              :  */
    1884              : static void
    1885            0 : compile_relation_list_one_db(PGconn *conn, SimplePtrList *relations,
    1886              :                                                          const DatabaseInfo *dat,
    1887              :                                                          uint64 *pagecount)
    1888              : {
    1889            0 :         PGresult   *res;
    1890            0 :         PQExpBufferData sql;
    1891            0 :         int                     ntups;
    1892            0 :         int                     i;
    1893              : 
    1894            0 :         initPQExpBuffer(&sql);
    1895            0 :         appendPQExpBufferStr(&sql, "WITH");
    1896              : 
    1897              :         /* Append CTEs for the relation inclusion patterns, if any */
    1898            0 :         if (!opts.allrel)
    1899              :         {
    1900            0 :                 appendPQExpBufferStr(&sql,
    1901              :                                                          " include_raw (pattern_id, db_regex, nsp_regex, rel_regex, heap_only, btree_only) AS (");
    1902            0 :                 append_rel_pattern_raw_cte(&sql, &opts.include, conn);
    1903            0 :                 appendPQExpBufferStr(&sql, "\n),");
    1904            0 :                 append_rel_pattern_filtered_cte(&sql, "include_raw", "include_pat", conn);
    1905            0 :         }
    1906              : 
    1907              :         /* Append CTEs for the relation exclusion patterns, if any */
    1908            0 :         if (opts.excludetbl || opts.excludeidx || opts.excludensp)
    1909              :         {
    1910            0 :                 appendPQExpBufferStr(&sql,
    1911              :                                                          " exclude_raw (pattern_id, db_regex, nsp_regex, rel_regex, heap_only, btree_only) AS (");
    1912            0 :                 append_rel_pattern_raw_cte(&sql, &opts.exclude, conn);
    1913            0 :                 appendPQExpBufferStr(&sql, "\n),");
    1914            0 :                 append_rel_pattern_filtered_cte(&sql, "exclude_raw", "exclude_pat", conn);
    1915            0 :         }
    1916              : 
    1917              :         /* Append the relation CTE. */
    1918            0 :         appendPQExpBufferStr(&sql,
    1919              :                                                  " relation (pattern_id, oid, nspname, relname, reltoastrelid, relpages, is_heap, is_btree) AS ("
    1920              :                                                  "\nSELECT DISTINCT ON (c.oid");
    1921            0 :         if (!opts.allrel)
    1922            0 :                 appendPQExpBufferStr(&sql, ", ip.pattern_id) ip.pattern_id,");
    1923              :         else
    1924            0 :                 appendPQExpBufferStr(&sql, ") NULL::INTEGER AS pattern_id,");
    1925            0 :         appendPQExpBuffer(&sql,
    1926              :                                           "\nc.oid, n.nspname, c.relname, c.reltoastrelid, c.relpages, "
    1927              :                                           "c.relam = %u AS is_heap, "
    1928              :                                           "c.relam = %u AS is_btree"
    1929              :                                           "\nFROM pg_catalog.pg_class c "
    1930              :                                           "INNER JOIN pg_catalog.pg_namespace n "
    1931              :                                           "ON c.relnamespace = n.oid",
    1932              :                                           HEAP_TABLE_AM_OID, BTREE_AM_OID);
    1933            0 :         if (!opts.allrel)
    1934            0 :                 appendPQExpBuffer(&sql,
    1935              :                                                   "\nINNER JOIN include_pat ip"
    1936              :                                                   "\nON (n.nspname ~ ip.nsp_regex OR ip.nsp_regex IS NULL)"
    1937              :                                                   "\nAND (c.relname ~ ip.rel_regex OR ip.rel_regex IS NULL)"
    1938              :                                                   "\nAND (c.relam = %u OR NOT ip.heap_only)"
    1939              :                                                   "\nAND (c.relam = %u OR NOT ip.btree_only)",
    1940              :                                                   HEAP_TABLE_AM_OID, BTREE_AM_OID);
    1941            0 :         if (opts.excludetbl || opts.excludeidx || opts.excludensp)
    1942            0 :                 appendPQExpBuffer(&sql,
    1943              :                                                   "\nLEFT OUTER JOIN exclude_pat ep"
    1944              :                                                   "\nON (n.nspname ~ ep.nsp_regex OR ep.nsp_regex IS NULL)"
    1945              :                                                   "\nAND (c.relname ~ ep.rel_regex OR ep.rel_regex IS NULL)"
    1946              :                                                   "\nAND (c.relam = %u OR NOT ep.heap_only OR ep.rel_regex IS NULL)"
    1947              :                                                   "\nAND (c.relam = %u OR NOT ep.btree_only OR ep.rel_regex IS NULL)",
    1948              :                                                   HEAP_TABLE_AM_OID, BTREE_AM_OID);
    1949              : 
    1950              :         /*
    1951              :          * Exclude temporary tables and indexes, which must necessarily belong to
    1952              :          * other sessions.  (We don't create any ourselves.)  We must ultimately
    1953              :          * exclude indexes marked invalid or not ready, but we delay that decision
    1954              :          * until firing off the amcheck command, as the state of an index may
    1955              :          * change by then.
    1956              :          */
    1957            0 :         appendPQExpBufferStr(&sql, "\nWHERE c.relpersistence != "
    1958              :                                                  CppAsString2(RELPERSISTENCE_TEMP));
    1959            0 :         if (opts.excludetbl || opts.excludeidx || opts.excludensp)
    1960            0 :                 appendPQExpBufferStr(&sql, "\nAND ep.pattern_id IS NULL");
    1961              : 
    1962              :         /*
    1963              :          * We need to be careful not to break the --no-dependent-toast and
    1964              :          * --no-dependent-indexes options.  By default, the btree indexes, toast
    1965              :          * tables, and toast table btree indexes associated with primary heap
    1966              :          * tables are included, using their own CTEs below.  We implement the
    1967              :          * --exclude-* options by not creating those CTEs, but that's no use if
    1968              :          * we've already selected the toast and indexes here.  On the other hand,
    1969              :          * we want inclusion patterns that match indexes or toast tables to be
    1970              :          * honored.  So, if inclusion patterns were given, we want to select all
    1971              :          * tables, toast tables, or indexes that match the patterns.  But if no
    1972              :          * inclusion patterns were given, and we're simply matching all relations,
    1973              :          * then we only want to match the primary tables here.
    1974              :          */
    1975            0 :         if (opts.allrel)
    1976            0 :                 appendPQExpBuffer(&sql,
    1977              :                                                   " AND c.relam = %u "
    1978              :                                                   "AND c.relkind IN ("
    1979              :                                                   CppAsString2(RELKIND_RELATION) ", "
    1980              :                                                   CppAsString2(RELKIND_SEQUENCE) ", "
    1981              :                                                   CppAsString2(RELKIND_MATVIEW) ", "
    1982              :                                                   CppAsString2(RELKIND_TOASTVALUE) ") "
    1983              :                                                   "AND c.relnamespace != %u",
    1984              :                                                   HEAP_TABLE_AM_OID, PG_TOAST_NAMESPACE);
    1985              :         else
    1986            0 :                 appendPQExpBuffer(&sql,
    1987              :                                                   " AND c.relam IN (%u, %u)"
    1988              :                                                   "AND c.relkind IN ("
    1989              :                                                   CppAsString2(RELKIND_RELATION) ", "
    1990              :                                                   CppAsString2(RELKIND_SEQUENCE) ", "
    1991              :                                                   CppAsString2(RELKIND_MATVIEW) ", "
    1992              :                                                   CppAsString2(RELKIND_TOASTVALUE) ", "
    1993              :                                                   CppAsString2(RELKIND_INDEX) ") "
    1994              :                                                   "AND ((c.relam = %u AND c.relkind IN ("
    1995              :                                                   CppAsString2(RELKIND_RELATION) ", "
    1996              :                                                   CppAsString2(RELKIND_SEQUENCE) ", "
    1997              :                                                   CppAsString2(RELKIND_MATVIEW) ", "
    1998              :                                                   CppAsString2(RELKIND_TOASTVALUE) ")) OR "
    1999              :                                                   "(c.relam = %u AND c.relkind = "
    2000              :                                                   CppAsString2(RELKIND_INDEX) "))",
    2001              :                                                   HEAP_TABLE_AM_OID, BTREE_AM_OID,
    2002              :                                                   HEAP_TABLE_AM_OID, BTREE_AM_OID);
    2003              : 
    2004            0 :         appendPQExpBufferStr(&sql,
    2005              :                                                  "\nORDER BY c.oid)");
    2006              : 
    2007            0 :         if (!opts.no_toast_expansion)
    2008              :         {
    2009              :                 /*
    2010              :                  * Include a CTE for toast tables associated with primary heap tables
    2011              :                  * selected above, filtering by exclusion patterns (if any) that match
    2012              :                  * toast table names.
    2013              :                  */
    2014            0 :                 appendPQExpBufferStr(&sql,
    2015              :                                                          ", toast (oid, nspname, relname, relpages) AS ("
    2016              :                                                          "\nSELECT t.oid, 'pg_toast', t.relname, t.relpages"
    2017              :                                                          "\nFROM pg_catalog.pg_class t "
    2018              :                                                          "INNER JOIN relation r "
    2019              :                                                          "ON r.reltoastrelid = t.oid");
    2020            0 :                 if (opts.excludetbl || opts.excludensp)
    2021            0 :                         appendPQExpBufferStr(&sql,
    2022              :                                                                  "\nLEFT OUTER JOIN exclude_pat ep"
    2023              :                                                                  "\nON ('pg_toast' ~ ep.nsp_regex OR ep.nsp_regex IS NULL)"
    2024              :                                                                  "\nAND (t.relname ~ ep.rel_regex OR ep.rel_regex IS NULL)"
    2025              :                                                                  "\nAND ep.heap_only"
    2026              :                                                                  "\nWHERE ep.pattern_id IS NULL"
    2027              :                                                                  "\nAND t.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
    2028            0 :                 appendPQExpBufferStr(&sql,
    2029              :                                                          "\n)");
    2030            0 :         }
    2031            0 :         if (!opts.no_btree_expansion)
    2032              :         {
    2033              :                 /*
    2034              :                  * Include a CTE for btree indexes associated with primary heap tables
    2035              :                  * selected above, filtering by exclusion patterns (if any) that match
    2036              :                  * btree index names.
    2037              :                  */
    2038            0 :                 appendPQExpBufferStr(&sql,
    2039              :                                                          ", index (oid, nspname, relname, relpages) AS ("
    2040              :                                                          "\nSELECT c.oid, r.nspname, c.relname, c.relpages "
    2041              :                                                          "FROM relation r"
    2042              :                                                          "\nINNER JOIN pg_catalog.pg_index i "
    2043              :                                                          "ON r.oid = i.indrelid "
    2044              :                                                          "INNER JOIN pg_catalog.pg_class c "
    2045              :                                                          "ON i.indexrelid = c.oid "
    2046              :                                                          "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
    2047            0 :                 if (opts.excludeidx || opts.excludensp)
    2048            0 :                         appendPQExpBufferStr(&sql,
    2049              :                                                                  "\nINNER JOIN pg_catalog.pg_namespace n "
    2050              :                                                                  "ON c.relnamespace = n.oid"
    2051              :                                                                  "\nLEFT OUTER JOIN exclude_pat ep "
    2052              :                                                                  "ON (n.nspname ~ ep.nsp_regex OR ep.nsp_regex IS NULL) "
    2053              :                                                                  "AND (c.relname ~ ep.rel_regex OR ep.rel_regex IS NULL) "
    2054              :                                                                  "AND ep.btree_only"
    2055              :                                                                  "\nWHERE ep.pattern_id IS NULL");
    2056              :                 else
    2057            0 :                         appendPQExpBufferStr(&sql,
    2058              :                                                                  "\nWHERE true");
    2059            0 :                 appendPQExpBuffer(&sql,
    2060              :                                                   " AND c.relam = %u "
    2061              :                                                   "AND c.relkind = " CppAsString2(RELKIND_INDEX),
    2062              :                                                   BTREE_AM_OID);
    2063            0 :                 if (opts.no_toast_expansion)
    2064            0 :                         appendPQExpBuffer(&sql,
    2065              :                                                           " AND c.relnamespace != %u",
    2066              :                                                           PG_TOAST_NAMESPACE);
    2067            0 :                 appendPQExpBufferStr(&sql, "\n)");
    2068            0 :         }
    2069              : 
    2070            0 :         if (!opts.no_toast_expansion && !opts.no_btree_expansion)
    2071              :         {
    2072              :                 /*
    2073              :                  * Include a CTE for btree indexes associated with toast tables of
    2074              :                  * primary heap tables selected above, filtering by exclusion patterns
    2075              :                  * (if any) that match the toast index names.
    2076              :                  */
    2077            0 :                 appendPQExpBufferStr(&sql,
    2078              :                                                          ", toast_index (oid, nspname, relname, relpages) AS ("
    2079              :                                                          "\nSELECT c.oid, 'pg_toast', c.relname, c.relpages "
    2080              :                                                          "FROM toast t "
    2081              :                                                          "INNER JOIN pg_catalog.pg_index i "
    2082              :                                                          "ON t.oid = i.indrelid"
    2083              :                                                          "\nINNER JOIN pg_catalog.pg_class c "
    2084              :                                                          "ON i.indexrelid = c.oid "
    2085              :                                                          "AND c.relpersistence != " CppAsString2(RELPERSISTENCE_TEMP));
    2086            0 :                 if (opts.excludeidx)
    2087            0 :                         appendPQExpBufferStr(&sql,
    2088              :                                                                  "\nLEFT OUTER JOIN exclude_pat ep "
    2089              :                                                                  "ON ('pg_toast' ~ ep.nsp_regex OR ep.nsp_regex IS NULL) "
    2090              :                                                                  "AND (c.relname ~ ep.rel_regex OR ep.rel_regex IS NULL) "
    2091              :                                                                  "AND ep.btree_only "
    2092              :                                                                  "WHERE ep.pattern_id IS NULL");
    2093              :                 else
    2094            0 :                         appendPQExpBufferStr(&sql,
    2095              :                                                                  "\nWHERE true");
    2096            0 :                 appendPQExpBuffer(&sql,
    2097              :                                                   " AND c.relam = %u"
    2098              :                                                   " AND c.relkind = " CppAsString2(RELKIND_INDEX) ")",
    2099              :                                                   BTREE_AM_OID);
    2100            0 :         }
    2101              : 
    2102              :         /*
    2103              :          * Roll-up distinct rows from CTEs.
    2104              :          *
    2105              :          * Relations that match more than one pattern may occur more than once in
    2106              :          * the list, and indexes and toast for primary relations may also have
    2107              :          * matched in their own right, so we rely on UNION to deduplicate the
    2108              :          * list.
    2109              :          */
    2110            0 :         appendPQExpBufferStr(&sql,
    2111              :                                                  "\nSELECT pattern_id, is_heap, is_btree, oid, nspname, relname, relpages "
    2112              :                                                  "FROM (");
    2113            0 :         appendPQExpBufferStr(&sql,
    2114              :         /* Inclusion patterns that failed to match */
    2115              :                                                  "\nSELECT pattern_id, is_heap, is_btree, "
    2116              :                                                  "NULL::OID AS oid, "
    2117              :                                                  "NULL::TEXT AS nspname, "
    2118              :                                                  "NULL::TEXT AS relname, "
    2119              :                                                  "NULL::INTEGER AS relpages"
    2120              :                                                  "\nFROM relation "
    2121              :                                                  "WHERE pattern_id IS NOT NULL "
    2122              :                                                  "UNION"
    2123              :         /* Primary relations */
    2124              :                                                  "\nSELECT NULL::INTEGER AS pattern_id, "
    2125              :                                                  "is_heap, is_btree, oid, nspname, relname, relpages "
    2126              :                                                  "FROM relation");
    2127            0 :         if (!opts.no_toast_expansion)
    2128            0 :                 appendPQExpBufferStr(&sql,
    2129              :                                                          " UNION"
    2130              :                 /* Toast tables for primary relations */
    2131              :                                                          "\nSELECT NULL::INTEGER AS pattern_id, TRUE AS is_heap, "
    2132              :                                                          "FALSE AS is_btree, oid, nspname, relname, relpages "
    2133              :                                                          "FROM toast");
    2134            0 :         if (!opts.no_btree_expansion)
    2135            0 :                 appendPQExpBufferStr(&sql,
    2136              :                                                          " UNION"
    2137              :                 /* Indexes for primary relations */
    2138              :                                                          "\nSELECT NULL::INTEGER AS pattern_id, FALSE AS is_heap, "
    2139              :                                                          "TRUE AS is_btree, oid, nspname, relname, relpages "
    2140              :                                                          "FROM index");
    2141            0 :         if (!opts.no_toast_expansion && !opts.no_btree_expansion)
    2142            0 :                 appendPQExpBufferStr(&sql,
    2143              :                                                          " UNION"
    2144              :                 /* Indexes for toast relations */
    2145              :                                                          "\nSELECT NULL::INTEGER AS pattern_id, FALSE AS is_heap, "
    2146              :                                                          "TRUE AS is_btree, oid, nspname, relname, relpages "
    2147              :                                                          "FROM toast_index");
    2148            0 :         appendPQExpBufferStr(&sql,
    2149              :                                                  "\n) AS combined_records "
    2150              :                                                  "ORDER BY relpages DESC NULLS FIRST, oid");
    2151              : 
    2152            0 :         res = executeQuery(conn, sql.data, opts.echo);
    2153            0 :         if (PQresultStatus(res) != PGRES_TUPLES_OK)
    2154              :         {
    2155            0 :                 pg_log_error("query failed: %s", PQerrorMessage(conn));
    2156            0 :                 pg_log_error_detail("Query was: %s", sql.data);
    2157            0 :                 disconnectDatabase(conn);
    2158            0 :                 exit(1);
    2159              :         }
    2160            0 :         termPQExpBuffer(&sql);
    2161              : 
    2162            0 :         ntups = PQntuples(res);
    2163            0 :         for (i = 0; i < ntups; i++)
    2164              :         {
    2165            0 :                 int                     pattern_id = -1;
    2166            0 :                 bool            is_heap = false;
    2167            0 :                 bool            is_btree PG_USED_FOR_ASSERTS_ONLY = false;
    2168            0 :                 Oid                     oid = InvalidOid;
    2169            0 :                 const char *nspname = NULL;
    2170            0 :                 const char *relname = NULL;
    2171            0 :                 int                     relpages = 0;
    2172              : 
    2173            0 :                 if (!PQgetisnull(res, i, 0))
    2174            0 :                         pattern_id = atoi(PQgetvalue(res, i, 0));
    2175            0 :                 if (!PQgetisnull(res, i, 1))
    2176            0 :                         is_heap = (PQgetvalue(res, i, 1)[0] == 't');
    2177            0 :                 if (!PQgetisnull(res, i, 2))
    2178            0 :                         is_btree = (PQgetvalue(res, i, 2)[0] == 't');
    2179            0 :                 if (!PQgetisnull(res, i, 3))
    2180            0 :                         oid = atooid(PQgetvalue(res, i, 3));
    2181            0 :                 if (!PQgetisnull(res, i, 4))
    2182            0 :                         nspname = PQgetvalue(res, i, 4);
    2183            0 :                 if (!PQgetisnull(res, i, 5))
    2184            0 :                         relname = PQgetvalue(res, i, 5);
    2185            0 :                 if (!PQgetisnull(res, i, 6))
    2186            0 :                         relpages = atoi(PQgetvalue(res, i, 6));
    2187              : 
    2188            0 :                 if (pattern_id >= 0)
    2189              :                 {
    2190              :                         /*
    2191              :                          * Current record pertains to an inclusion pattern.  Record that
    2192              :                          * it matched.
    2193              :                          */
    2194              : 
    2195            0 :                         if (pattern_id >= opts.include.len)
    2196            0 :                                 pg_fatal("internal error: received unexpected relation pattern_id %d",
    2197              :                                                  pattern_id);
    2198              : 
    2199            0 :                         opts.include.data[pattern_id].matched = true;
    2200            0 :                 }
    2201              :                 else
    2202              :                 {
    2203              :                         /* Current record pertains to a relation */
    2204              : 
    2205            0 :                         RelationInfo *rel = (RelationInfo *) pg_malloc0(sizeof(RelationInfo));
    2206              : 
    2207            0 :                         Assert(OidIsValid(oid));
    2208            0 :                         Assert((is_heap && !is_btree) || (is_btree && !is_heap));
    2209              : 
    2210            0 :                         rel->datinfo = dat;
    2211            0 :                         rel->reloid = oid;
    2212            0 :                         rel->is_heap = is_heap;
    2213            0 :                         rel->nspname = pstrdup(nspname);
    2214            0 :                         rel->relname = pstrdup(relname);
    2215            0 :                         rel->relpages = relpages;
    2216            0 :                         rel->blocks_to_check = relpages;
    2217            0 :                         if (is_heap && (opts.startblock >= 0 || opts.endblock >= 0))
    2218              :                         {
    2219              :                                 /*
    2220              :                                  * We apply --startblock and --endblock to heap tables, but
    2221              :                                  * not btree indexes, and for progress purposes we need to
    2222              :                                  * track how many blocks we expect to check.
    2223              :                                  */
    2224            0 :                                 if (opts.endblock >= 0 && rel->blocks_to_check > opts.endblock)
    2225            0 :                                         rel->blocks_to_check = opts.endblock + 1;
    2226            0 :                                 if (opts.startblock >= 0)
    2227              :                                 {
    2228            0 :                                         if (rel->blocks_to_check > opts.startblock)
    2229            0 :                                                 rel->blocks_to_check -= opts.startblock;
    2230              :                                         else
    2231            0 :                                                 rel->blocks_to_check = 0;
    2232            0 :                                 }
    2233            0 :                         }
    2234            0 :                         *pagecount += rel->blocks_to_check;
    2235              : 
    2236            0 :                         simple_ptr_list_append(relations, rel);
    2237            0 :                 }
    2238            0 :         }
    2239            0 :         PQclear(res);
    2240            0 : }
        

Generated by: LCOV version 2.3.2-1