LCOV - code coverage report
Current view: top level - src/bin/pg_upgrade - check.c (source / functions) Hit Total Coverage
Test: PostgreSQL 18devel Lines: 326 581 56.1 %
Date: 2025-04-02 11:15:58 Functions: 24 34 70.6 %
Legend: Lines: hit not hit

          Line data    Source code
       1             : /*
       2             :  *  check.c
       3             :  *
       4             :  *  server checks and output routines
       5             :  *
       6             :  *  Copyright (c) 2010-2025, PostgreSQL Global Development Group
       7             :  *  src/bin/pg_upgrade/check.c
       8             :  */
       9             : 
      10             : #include "postgres_fe.h"
      11             : 
      12             : #include "catalog/pg_authid_d.h"
      13             : #include "catalog/pg_class_d.h"
      14             : #include "fe_utils/string_utils.h"
      15             : #include "pg_upgrade.h"
      16             : 
      17             : static void check_new_cluster_is_empty(void);
      18             : static void check_is_install_user(ClusterInfo *cluster);
      19             : static void check_for_connection_status(ClusterInfo *cluster);
      20             : static void check_for_prepared_transactions(ClusterInfo *cluster);
      21             : static void check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster);
      22             : static void check_for_user_defined_postfix_ops(ClusterInfo *cluster);
      23             : static void check_for_incompatible_polymorphics(ClusterInfo *cluster);
      24             : static void check_for_tables_with_oids(ClusterInfo *cluster);
      25             : static void check_for_pg_role_prefix(ClusterInfo *cluster);
      26             : static void check_for_new_tablespace_dir(void);
      27             : static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster);
      28             : static void check_new_cluster_logical_replication_slots(void);
      29             : static void check_new_cluster_subscription_configuration(void);
      30             : static void check_old_cluster_for_valid_slots(void);
      31             : static void check_old_cluster_subscription_state(void);
      32             : 
      33             : /*
      34             :  * DataTypesUsageChecks - definitions of data type checks for the old cluster
      35             :  * in order to determine if an upgrade can be performed.  See the comment on
      36             :  * data_types_usage_checks below for a more detailed description.
      37             :  */
      38             : typedef struct
      39             : {
      40             :     /* Status line to print to the user */
      41             :     const char *status;
      42             :     /* Filename to store report to */
      43             :     const char *report_filename;
      44             :     /* Query to extract the oid of the datatype */
      45             :     const char *base_query;
      46             :     /* Text to store to report in case of error */
      47             :     const char *report_text;
      48             :     /* The latest version where the check applies */
      49             :     int         threshold_version;
      50             :     /* A function pointer for determining if the check applies */
      51             :     DataTypesUsageVersionCheck version_hook;
      52             : } DataTypesUsageChecks;
      53             : 
      54             : /*
      55             :  * Special values for threshold_version for indicating that a check applies to
      56             :  * all versions, or that a custom function needs to be invoked to determine
      57             :  * if the check applies.
      58             :  */
      59             : #define MANUAL_CHECK 1
      60             : #define ALL_VERSIONS -1
      61             : 
      62             : /*--
      63             :  * Data type usage checks. Each check for problematic data type usage is
      64             :  * defined in this array with metadata, SQL query for finding the data type
      65             :  * and functionality for deciding if the check is applicable to the version
      66             :  * of the old cluster. The struct members are described in detail below:
      67             :  *
      68             :  * status               A oneline string which can be printed to the user to
      69             :  *                      inform about progress. Should not end with newline.
      70             :  * report_filename      The filename in which the list of problems detected by
      71             :  *                      the check will be printed.
      72             :  * base_query           A query which extracts the Oid of the datatype checked
      73             :  *                      for.
      74             :  * report_text          The text which will be printed to the user to explain
      75             :  *                      what the check did, and why it failed. The text should
      76             :  *                      end with a newline, and does not need to refer to the
      77             :  *                      report_filename as that is automatically appended to
      78             :  *                      the report with the path to the log folder.
      79             :  * threshold_version    The major version of PostgreSQL for which to run the
      80             :  *                      check. Iff the old cluster is less than, or equal to,
      81             :  *                      the threshold version then the check will be executed.
      82             :  *                      If the old version is greater than the threshold then
      83             :  *                      the check is skipped. If the threshold_version is set
      84             :  *                      to ALL_VERSIONS then it will be run unconditionally,
      85             :  *                      if set to MANUAL_CHECK then the version_hook function
      86             :  *                      will be executed in order to determine whether or not
      87             :  *                      to run.
      88             :  * version_hook         A function pointer to a version check function of type
      89             :  *                      DataTypesUsageVersionCheck which is used to determine
      90             :  *                      if the check is applicable to the old cluster. If the
      91             :  *                      version_hook returns true then the check will be run,
      92             :  *                      else it will be skipped. The function will only be
      93             :  *                      executed iff threshold_version is set to MANUAL_CHECK.
      94             :  */
      95             : static DataTypesUsageChecks data_types_usage_checks[] =
      96             : {
      97             :     /*
      98             :      * Look for composite types that were made during initdb *or* belong to
      99             :      * information_schema; that's important in case information_schema was
     100             :      * dropped and reloaded.
     101             :      *
     102             :      * The cutoff OID here should match the source cluster's value of
     103             :      * FirstNormalObjectId.  We hardcode it rather than using that C #define
     104             :      * because, if that #define is ever changed, our own version's value is
     105             :      * NOT what to use.  Eventually we may need a test on the source cluster's
     106             :      * version to select the correct value.
     107             :      */
     108             :     {
     109             :         .status = gettext_noop("Checking for system-defined composite types in user tables"),
     110             :         .report_filename = "tables_using_composite.txt",
     111             :         .base_query =
     112             :         "SELECT t.oid FROM pg_catalog.pg_type t "
     113             :         "LEFT JOIN pg_catalog.pg_namespace n ON t.typnamespace = n.oid "
     114             :         " WHERE typtype = 'c' AND (t.oid < 16384 OR nspname = 'information_schema')",
     115             :         .report_text =
     116             :         gettext_noop("Your installation contains system-defined composite types in user tables.\n"
     117             :                      "These type OIDs are not stable across PostgreSQL versions,\n"
     118             :                      "so this cluster cannot currently be upgraded.  You can drop the\n"
     119             :                      "problem columns and restart the upgrade.\n"),
     120             :         .threshold_version = ALL_VERSIONS
     121             :     },
     122             : 
     123             :     /*
     124             :      * 9.3 -> 9.4 Fully implement the 'line' data type in 9.4, which
     125             :      * previously returned "not enabled" by default and was only functionally
     126             :      * enabled with a compile-time switch; as of 9.4 "line" has a different
     127             :      * on-disk representation format.
     128             :      */
     129             :     {
     130             :         .status = gettext_noop("Checking for incompatible \"line\" data type"),
     131             :         .report_filename = "tables_using_line.txt",
     132             :         .base_query =
     133             :         "SELECT 'pg_catalog.line'::pg_catalog.regtype AS oid",
     134             :         .report_text =
     135             :         gettext_noop("Your installation contains the \"line\" data type in user tables.\n"
     136             :                      "This data type changed its internal and input/output format\n"
     137             :                      "between your old and new versions so this\n"
     138             :                      "cluster cannot currently be upgraded.  You can\n"
     139             :                      "drop the problem columns and restart the upgrade.\n"),
     140             :         .threshold_version = 903
     141             :     },
     142             : 
     143             :     /*
     144             :      * pg_upgrade only preserves these system values: pg_class.oid pg_type.oid
     145             :      * pg_enum.oid
     146             :      *
     147             :      * Many of the reg* data types reference system catalog info that is not
     148             :      * preserved, and hence these data types cannot be used in user tables
     149             :      * upgraded by pg_upgrade.
     150             :      */
     151             :     {
     152             :         .status = gettext_noop("Checking for reg* data types in user tables"),
     153             :         .report_filename = "tables_using_reg.txt",
     154             : 
     155             :         /*
     156             :          * Note: older servers will not have all of these reg* types, so we
     157             :          * have to write the query like this rather than depending on casts to
     158             :          * regtype.
     159             :          */
     160             :         .base_query =
     161             :         "SELECT oid FROM pg_catalog.pg_type t "
     162             :         "WHERE t.typnamespace = "
     163             :         "        (SELECT oid FROM pg_catalog.pg_namespace "
     164             :         "         WHERE nspname = 'pg_catalog') "
     165             :         "  AND t.typname IN ( "
     166             :         /* pg_class.oid is preserved, so 'regclass' is OK */
     167             :         "           'regcollation', "
     168             :         "           'regconfig', "
     169             :         "           'regdictionary', "
     170             :         "           'regnamespace', "
     171             :         "           'regoper', "
     172             :         "           'regoperator', "
     173             :         "           'regproc', "
     174             :         "           'regprocedure' "
     175             :         /* pg_authid.oid is preserved, so 'regrole' is OK */
     176             :         /* pg_type.oid is (mostly) preserved, so 'regtype' is OK */
     177             :         "         )",
     178             :         .report_text =
     179             :         gettext_noop("Your installation contains one of the reg* data types in user tables.\n"
     180             :                      "These data types reference system OIDs that are not preserved by\n"
     181             :                      "pg_upgrade, so this cluster cannot currently be upgraded.  You can\n"
     182             :                      "drop the problem columns and restart the upgrade.\n"),
     183             :         .threshold_version = ALL_VERSIONS
     184             :     },
     185             : 
     186             :     /*
     187             :      * PG 16 increased the size of the 'aclitem' type, which breaks the
     188             :      * on-disk format for existing data.
     189             :      */
     190             :     {
     191             :         .status = gettext_noop("Checking for incompatible \"aclitem\" data type"),
     192             :         .report_filename = "tables_using_aclitem.txt",
     193             :         .base_query =
     194             :         "SELECT 'pg_catalog.aclitem'::pg_catalog.regtype AS oid",
     195             :         .report_text =
     196             :         gettext_noop("Your installation contains the \"aclitem\" data type in user tables.\n"
     197             :                      "The internal format of \"aclitem\" changed in PostgreSQL version 16\n"
     198             :                      "so this cluster cannot currently be upgraded.  You can drop the\n"
     199             :                      "problem columns and restart the upgrade.\n"),
     200             :         .threshold_version = 1500
     201             :     },
     202             : 
     203             :     /*
     204             :      * It's no longer allowed to create tables or views with "unknown"-type
     205             :      * columns.  We do not complain about views with such columns, because
     206             :      * they should get silently converted to "text" columns during the DDL
     207             :      * dump and reload; it seems unlikely to be worth making users do that by
     208             :      * hand.  However, if there's a table with such a column, the DDL reload
     209             :      * will fail, so we should pre-detect that rather than failing
     210             :      * mid-upgrade.  Worse, if there's a matview with such a column, the DDL
     211             :      * reload will silently change it to "text" which won't match the on-disk
     212             :      * storage (which is like "cstring").  So we *must* reject that.
     213             :      */
     214             :     {
     215             :         .status = gettext_noop("Checking for invalid \"unknown\" user columns"),
     216             :         .report_filename = "tables_using_unknown.txt",
     217             :         .base_query =
     218             :         "SELECT 'pg_catalog.unknown'::pg_catalog.regtype AS oid",
     219             :         .report_text =
     220             :         gettext_noop("Your installation contains the \"unknown\" data type in user tables.\n"
     221             :                      "This data type is no longer allowed in tables, so this cluster\n"
     222             :                      "cannot currently be upgraded.  You can drop the problem columns\n"
     223             :                      "and restart the upgrade.\n"),
     224             :         .threshold_version = 906
     225             :     },
     226             : 
     227             :     /*
     228             :      * PG 12 changed the 'sql_identifier' type storage to be based on name,
     229             :      * not varchar, which breaks on-disk format for existing data. So we need
     230             :      * to prevent upgrade when used in user objects (tables, indexes, ...). In
     231             :      * 12, the sql_identifier data type was switched from name to varchar,
     232             :      * which does affect the storage (name is by-ref, but not varlena). This
     233             :      * means user tables using sql_identifier for columns are broken because
     234             :      * the on-disk format is different.
     235             :      */
     236             :     {
     237             :         .status = gettext_noop("Checking for invalid \"sql_identifier\" user columns"),
     238             :         .report_filename = "tables_using_sql_identifier.txt",
     239             :         .base_query =
     240             :         "SELECT 'information_schema.sql_identifier'::pg_catalog.regtype AS oid",
     241             :         .report_text =
     242             :         gettext_noop("Your installation contains the \"sql_identifier\" data type in user tables.\n"
     243             :                      "The on-disk format for this data type has changed, so this\n"
     244             :                      "cluster cannot currently be upgraded.  You can drop the problem\n"
     245             :                      "columns and restart the upgrade.\n"),
     246             :         .threshold_version = 1100
     247             :     },
     248             : 
     249             :     /*
     250             :      * JSONB changed its storage format during 9.4 beta, so check for it.
     251             :      */
     252             :     {
     253             :         .status = gettext_noop("Checking for incompatible \"jsonb\" data type in user tables"),
     254             :         .report_filename = "tables_using_jsonb.txt",
     255             :         .base_query =
     256             :         "SELECT 'pg_catalog.jsonb'::pg_catalog.regtype AS oid",
     257             :         .report_text =
     258             :         gettext_noop("Your installation contains the \"jsonb\" data type in user tables.\n"
     259             :                      "The internal format of \"jsonb\" changed during 9.4 beta so this\n"
     260             :                      "cluster cannot currently be upgraded.  You can drop the problem \n"
     261             :                      "columns and restart the upgrade.\n"),
     262             :         .threshold_version = MANUAL_CHECK,
     263             :         .version_hook = jsonb_9_4_check_applicable
     264             :     },
     265             : 
     266             :     /*
     267             :      * PG 12 removed types abstime, reltime, tinterval.
     268             :      */
     269             :     {
     270             :         .status = gettext_noop("Checking for removed \"abstime\" data type in user tables"),
     271             :         .report_filename = "tables_using_abstime.txt",
     272             :         .base_query =
     273             :         "SELECT 'pg_catalog.abstime'::pg_catalog.regtype AS oid",
     274             :         .report_text =
     275             :         gettext_noop("Your installation contains the \"abstime\" data type in user tables.\n"
     276             :                      "The \"abstime\" type has been removed in PostgreSQL version 12,\n"
     277             :                      "so this cluster cannot currently be upgraded.  You can drop the\n"
     278             :                      "problem columns, or change them to another data type, and restart\n"
     279             :                      "the upgrade.\n"),
     280             :         .threshold_version = 1100
     281             :     },
     282             :     {
     283             :         .status = gettext_noop("Checking for removed \"reltime\" data type in user tables"),
     284             :         .report_filename = "tables_using_reltime.txt",
     285             :         .base_query =
     286             :         "SELECT 'pg_catalog.reltime'::pg_catalog.regtype AS oid",
     287             :         .report_text =
     288             :         gettext_noop("Your installation contains the \"reltime\" data type in user tables.\n"
     289             :                      "The \"reltime\" type has been removed in PostgreSQL version 12,\n"
     290             :                      "so this cluster cannot currently be upgraded.  You can drop the\n"
     291             :                      "problem columns, or change them to another data type, and restart\n"
     292             :                      "the upgrade.\n"),
     293             :         .threshold_version = 1100
     294             :     },
     295             :     {
     296             :         .status = gettext_noop("Checking for removed \"tinterval\" data type in user tables"),
     297             :         .report_filename = "tables_using_tinterval.txt",
     298             :         .base_query =
     299             :         "SELECT 'pg_catalog.tinterval'::pg_catalog.regtype AS oid",
     300             :         .report_text =
     301             :         gettext_noop("Your installation contains the \"tinterval\" data type in user tables.\n"
     302             :                      "The \"tinterval\" type has been removed in PostgreSQL version 12,\n"
     303             :                      "so this cluster cannot currently be upgraded.  You can drop the\n"
     304             :                      "problem columns, or change them to another data type, and restart\n"
     305             :                      "the upgrade.\n"),
     306             :         .threshold_version = 1100
     307             :     },
     308             : 
     309             :     /* End of checks marker, must remain last */
     310             :     {
     311             :         NULL, NULL, NULL, NULL, 0, NULL
     312             :     }
     313             : };
     314             : 
     315             : /*
     316             :  * Private state for check_for_data_types_usage()'s UpgradeTask.
     317             :  */
     318             : struct data_type_check_state
     319             : {
     320             :     DataTypesUsageChecks *check;    /* the check for this step */
     321             :     bool        result;         /* true if check failed for any database */
     322             :     PQExpBuffer *report;        /* buffer for report on failed checks */
     323             : };
     324             : 
     325             : /*
     326             :  * Returns a palloc'd query string for the data type check, for use by
     327             :  * check_for_data_types_usage()'s UpgradeTask.
     328             :  */
     329             : static char *
     330          48 : data_type_check_query(int checknum)
     331             : {
     332          48 :     DataTypesUsageChecks *check = &data_types_usage_checks[checknum];
     333             : 
     334          48 :     return psprintf("WITH RECURSIVE oids AS ( "
     335             :     /* start with the type(s) returned by base_query */
     336             :                     "  %s "
     337             :                     "  UNION ALL "
     338             :                     "  SELECT * FROM ( "
     339             :     /* inner WITH because we can only reference the CTE once */
     340             :                     "      WITH x AS (SELECT oid FROM oids) "
     341             :     /* domains on any type selected so far */
     342             :                     "          SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typbasetype = x.oid AND typtype = 'd' "
     343             :                     "          UNION ALL "
     344             :     /* arrays over any type selected so far */
     345             :                     "          SELECT t.oid FROM pg_catalog.pg_type t, x WHERE typelem = x.oid AND typtype = 'b' "
     346             :                     "          UNION ALL "
     347             :     /* composite types containing any type selected so far */
     348             :                     "          SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_class c, pg_catalog.pg_attribute a, x "
     349             :                     "          WHERE t.typtype = 'c' AND "
     350             :                     "                t.oid = c.reltype AND "
     351             :                     "                c.oid = a.attrelid AND "
     352             :                     "                NOT a.attisdropped AND "
     353             :                     "                a.atttypid = x.oid "
     354             :                     "          UNION ALL "
     355             :     /* ranges containing any type selected so far */
     356             :                     "          SELECT t.oid FROM pg_catalog.pg_type t, pg_catalog.pg_range r, x "
     357             :                     "          WHERE t.typtype = 'r' AND r.rngtypid = t.oid AND r.rngsubtype = x.oid"
     358             :                     "  ) foo "
     359             :                     ") "
     360             :     /* now look for stored columns of any such type */
     361             :                     "SELECT n.nspname, c.relname, a.attname "
     362             :                     "FROM  pg_catalog.pg_class c, "
     363             :                     "      pg_catalog.pg_namespace n, "
     364             :                     "      pg_catalog.pg_attribute a "
     365             :                     "WHERE c.oid = a.attrelid AND "
     366             :                     "      NOT a.attisdropped AND "
     367             :                     "      a.atttypid IN (SELECT oid FROM oids) AND "
     368             :                     "      c.relkind IN ("
     369             :                     CppAsString2(RELKIND_RELATION) ", "
     370             :                     CppAsString2(RELKIND_MATVIEW) ", "
     371             :                     CppAsString2(RELKIND_INDEX) ") AND "
     372             :                     "      c.relnamespace = n.oid AND "
     373             :     /* exclude possible orphaned temp tables */
     374             :                     "      n.nspname !~ '^pg_temp_' AND "
     375             :                     "      n.nspname !~ '^pg_toast_temp_' AND "
     376             :     /* exclude system catalogs, too */
     377             :                     "      n.nspname NOT IN ('pg_catalog', 'information_schema')",
     378             :                     check->base_query);
     379             : }
     380             : 
     381             : /*
     382             :  * Callback function for processing results of queries for
     383             :  * check_for_data_types_usage()'s UpgradeTask.  If the query returned any rows
     384             :  * (i.e., the check failed), write the details to the report file.
     385             :  */
     386             : static void
     387         148 : process_data_type_check(DbInfo *dbinfo, PGresult *res, void *arg)
     388             : {
     389         148 :     struct data_type_check_state *state = (struct data_type_check_state *) arg;
     390         148 :     int         ntups = PQntuples(res);
     391             :     char        output_path[MAXPGPATH];
     392         148 :     int         i_nspname = PQfnumber(res, "nspname");
     393         148 :     int         i_relname = PQfnumber(res, "relname");
     394         148 :     int         i_attname = PQfnumber(res, "attname");
     395         148 :     FILE       *script = NULL;
     396             : 
     397             :     AssertVariableIsOfType(&process_data_type_check, UpgradeTaskProcessCB);
     398             : 
     399         148 :     if (ntups == 0)
     400         148 :         return;
     401             : 
     402           0 :     snprintf(output_path, sizeof(output_path), "%s/%s",
     403             :              log_opts.basedir,
     404           0 :              state->check->report_filename);
     405             : 
     406             :     /*
     407             :      * Make sure we have a buffer to save reports to now that we found a first
     408             :      * failing check.
     409             :      */
     410           0 :     if (*state->report == NULL)
     411           0 :         *state->report = createPQExpBuffer();
     412             : 
     413             :     /*
     414             :      * If this is the first time we see an error for the check in question
     415             :      * then print a status message of the failure.
     416             :      */
     417           0 :     if (!state->result)
     418             :     {
     419           0 :         pg_log(PG_REPORT, "failed check: %s", _(state->check->status));
     420           0 :         appendPQExpBuffer(*state->report, "\n%s\n%s    %s\n",
     421           0 :                           _(state->check->report_text),
     422             :                           _("A list of the problem columns is in the file:"),
     423             :                           output_path);
     424             :     }
     425           0 :     state->result = true;
     426             : 
     427           0 :     if ((script = fopen_priv(output_path, "a")) == NULL)
     428           0 :         pg_fatal("could not open file \"%s\": %m", output_path);
     429             : 
     430           0 :     fprintf(script, "In database: %s\n", dbinfo->db_name);
     431             : 
     432           0 :     for (int rowno = 0; rowno < ntups; rowno++)
     433           0 :         fprintf(script, "  %s.%s.%s\n",
     434             :                 PQgetvalue(res, rowno, i_nspname),
     435             :                 PQgetvalue(res, rowno, i_relname),
     436             :                 PQgetvalue(res, rowno, i_attname));
     437             : 
     438           0 :     fclose(script);
     439             : }
     440             : 
     441             : /*
     442             :  * check_for_data_types_usage()
     443             :  *  Detect whether there are any stored columns depending on given type(s)
     444             :  *
     445             :  * If so, write a report to the given file name and signal a failure to the
     446             :  * user.
     447             :  *
     448             :  * The checks to run are defined in a DataTypesUsageChecks structure where
     449             :  * each check has a metadata for explaining errors to the user, a base_query,
     450             :  * a report filename and a function pointer hook for validating if the check
     451             :  * should be executed given the cluster at hand.
     452             :  *
     453             :  * base_query should be a SELECT yielding a single column named "oid",
     454             :  * containing the pg_type OIDs of one or more types that are known to have
     455             :  * inconsistent on-disk representations across server versions.
     456             :  *
     457             :  * We check for the type(s) in tables, matviews, and indexes, but not views;
     458             :  * there's no storage involved in a view.
     459             :  */
     460             : static void
     461          24 : check_for_data_types_usage(ClusterInfo *cluster)
     462             : {
     463          24 :     PQExpBuffer report = NULL;
     464          24 :     DataTypesUsageChecks *tmp = data_types_usage_checks;
     465          24 :     int         n_data_types_usage_checks = 0;
     466          24 :     UpgradeTask *task = upgrade_task_create();
     467          24 :     char      **queries = NULL;
     468             :     struct data_type_check_state *states;
     469             : 
     470          24 :     prep_status("Checking data type usage");
     471             : 
     472             :     /* Gather number of checks to perform */
     473         264 :     while (tmp->status != NULL)
     474             :     {
     475         240 :         n_data_types_usage_checks++;
     476         240 :         tmp++;
     477             :     }
     478             : 
     479             :     /* Allocate memory for queries and for task states */
     480          24 :     queries = pg_malloc0(sizeof(char *) * n_data_types_usage_checks);
     481          24 :     states = pg_malloc0(sizeof(struct data_type_check_state) * n_data_types_usage_checks);
     482             : 
     483         264 :     for (int i = 0; i < n_data_types_usage_checks; i++)
     484             :     {
     485         240 :         DataTypesUsageChecks *check = &data_types_usage_checks[i];
     486             : 
     487         240 :         if (check->threshold_version == MANUAL_CHECK)
     488             :         {
     489             :             Assert(check->version_hook);
     490             : 
     491             :             /*
     492             :              * Make sure that the check applies to the current cluster version
     493             :              * and skip it if not.
     494             :              */
     495          24 :             if (!check->version_hook(cluster))
     496          24 :                 continue;
     497             :         }
     498         216 :         else if (check->threshold_version != ALL_VERSIONS)
     499             :         {
     500         168 :             if (GET_MAJOR_VERSION(cluster->major_version) > check->threshold_version)
     501         168 :                 continue;
     502             :         }
     503             :         else
     504             :             Assert(check->threshold_version == ALL_VERSIONS);
     505             : 
     506          48 :         queries[i] = data_type_check_query(i);
     507             : 
     508          48 :         states[i].check = check;
     509          48 :         states[i].report = &report;
     510             : 
     511          48 :         upgrade_task_add_step(task, queries[i], process_data_type_check,
     512          48 :                               true, &states[i]);
     513             :     }
     514             : 
     515             :     /*
     516             :      * Connect to each database in the cluster and run all defined checks
     517             :      * against that database before trying the next one.
     518             :      */
     519          24 :     upgrade_task_run(task, cluster);
     520          24 :     upgrade_task_free(task);
     521             : 
     522          24 :     if (report)
     523             :     {
     524           0 :         pg_fatal("Data type checks failed: %s", report->data);
     525             :         destroyPQExpBuffer(report);
     526             :     }
     527             : 
     528         264 :     for (int i = 0; i < n_data_types_usage_checks; i++)
     529             :     {
     530         240 :         if (queries[i])
     531          48 :             pg_free(queries[i]);
     532             :     }
     533          24 :     pg_free(queries);
     534          24 :     pg_free(states);
     535             : 
     536          24 :     check_ok();
     537          24 : }
     538             : 
     539             : /*
     540             :  * fix_path_separator
     541             :  * For non-Windows, just return the argument.
     542             :  * For Windows convert any forward slash to a backslash
     543             :  * such as is suitable for arguments to builtin commands
     544             :  * like RMDIR and DEL.
     545             :  */
     546             : static char *
     547          32 : fix_path_separator(char *path)
     548             : {
     549             : #ifdef WIN32
     550             : 
     551             :     char       *result;
     552             :     char       *c;
     553             : 
     554             :     result = pg_strdup(path);
     555             : 
     556             :     for (c = result; *c != '\0'; c++)
     557             :         if (*c == '/')
     558             :             *c = '\\';
     559             : 
     560             :     return result;
     561             : #else
     562             : 
     563          32 :     return path;
     564             : #endif
     565             : }
     566             : 
     567             : void
     568          32 : output_check_banner(void)
     569             : {
     570          32 :     if (user_opts.live_check)
     571             :     {
     572           0 :         pg_log(PG_REPORT,
     573             :                "Performing Consistency Checks on Old Live Server\n"
     574             :                "------------------------------------------------");
     575             :     }
     576             :     else
     577             :     {
     578          32 :         pg_log(PG_REPORT,
     579             :                "Performing Consistency Checks\n"
     580             :                "-----------------------------");
     581             :     }
     582          32 : }
     583             : 
     584             : 
     585             : void
     586          30 : check_and_dump_old_cluster(void)
     587             : {
     588             :     /* -- OLD -- */
     589             : 
     590          30 :     if (!user_opts.live_check)
     591          30 :         start_postmaster(&old_cluster, true);
     592             : 
     593             :     /*
     594             :      * First check that all databases allow connections since we'll otherwise
     595             :      * fail in later stages.
     596             :      */
     597          30 :     check_for_connection_status(&old_cluster);
     598             : 
     599             :     /*
     600             :      * Extract a list of databases, tables, and logical replication slots from
     601             :      * the old cluster.
     602             :      */
     603          28 :     get_db_rel_and_slot_infos(&old_cluster);
     604             : 
     605          28 :     init_tablespaces();
     606             : 
     607          28 :     get_loadable_libraries();
     608             : 
     609             : 
     610             :     /*
     611             :      * Check for various failure cases
     612             :      */
     613          28 :     check_is_install_user(&old_cluster);
     614          28 :     check_for_prepared_transactions(&old_cluster);
     615          28 :     check_for_isn_and_int8_passing_mismatch(&old_cluster);
     616             : 
     617          28 :     if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1700)
     618             :     {
     619             :         /*
     620             :          * Logical replication slots can be migrated since PG17. See comments
     621             :          * atop get_old_cluster_logical_slot_infos().
     622             :          */
     623          28 :         check_old_cluster_for_valid_slots();
     624             : 
     625             :         /*
     626             :          * Subscriptions and their dependencies can be migrated since PG17.
     627             :          * Before that the logical slots are not upgraded, so we will not be
     628             :          * able to upgrade the logical replication clusters completely.
     629             :          */
     630          26 :         get_subscription_count(&old_cluster);
     631          26 :         check_old_cluster_subscription_state();
     632             :     }
     633             : 
     634          24 :     check_for_data_types_usage(&old_cluster);
     635             : 
     636             :     /*
     637             :      * PG 14 changed the function signature of encoding conversion functions.
     638             :      * Conversions from older versions cannot be upgraded automatically
     639             :      * because the user-defined functions used by the encoding conversions
     640             :      * need to be changed to match the new signature.
     641             :      */
     642          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
     643           0 :         check_for_user_defined_encoding_conversions(&old_cluster);
     644             : 
     645             :     /*
     646             :      * Pre-PG 14 allowed user defined postfix operators, which are not
     647             :      * supported anymore.  Verify there are none, iff applicable.
     648             :      */
     649          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
     650           0 :         check_for_user_defined_postfix_ops(&old_cluster);
     651             : 
     652             :     /*
     653             :      * PG 14 changed polymorphic functions from anyarray to
     654             :      * anycompatiblearray.
     655             :      */
     656          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1300)
     657           0 :         check_for_incompatible_polymorphics(&old_cluster);
     658             : 
     659             :     /*
     660             :      * Pre-PG 12 allowed tables to be declared WITH OIDS, which is not
     661             :      * supported anymore. Verify there are none, iff applicable.
     662             :      */
     663          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1100)
     664           0 :         check_for_tables_with_oids(&old_cluster);
     665             : 
     666             :     /*
     667             :      * Pre-PG 10 allowed tables with 'unknown' type columns and non WAL logged
     668             :      * hash indexes
     669             :      */
     670          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
     671             :     {
     672           0 :         if (user_opts.check)
     673           0 :             old_9_6_invalidate_hash_indexes(&old_cluster, true);
     674             :     }
     675             : 
     676             :     /* 9.5 and below should not have roles starting with pg_ */
     677          24 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 905)
     678           0 :         check_for_pg_role_prefix(&old_cluster);
     679             : 
     680             :     /*
     681             :      * While not a check option, we do this now because this is the only time
     682             :      * the old server is running.
     683             :      */
     684          24 :     if (!user_opts.check)
     685          20 :         generate_old_dump();
     686             : 
     687          24 :     if (!user_opts.live_check)
     688          24 :         stop_postmaster(false);
     689          24 : }
     690             : 
     691             : 
     692             : void
     693          24 : check_new_cluster(void)
     694             : {
     695          24 :     get_db_rel_and_slot_infos(&new_cluster);
     696             : 
     697          24 :     check_new_cluster_is_empty();
     698             : 
     699          24 :     check_loadable_libraries();
     700             : 
     701          24 :     switch (user_opts.transfer_mode)
     702             :     {
     703           2 :         case TRANSFER_MODE_CLONE:
     704           2 :             check_file_clone();
     705           0 :             break;
     706          16 :         case TRANSFER_MODE_COPY:
     707          16 :             break;
     708           2 :         case TRANSFER_MODE_COPY_FILE_RANGE:
     709           2 :             check_copy_file_range();
     710           2 :             break;
     711           2 :         case TRANSFER_MODE_LINK:
     712           2 :             check_hard_link(TRANSFER_MODE_LINK);
     713           2 :             break;
     714           2 :         case TRANSFER_MODE_SWAP:
     715             : 
     716             :             /*
     717             :              * We do the hard link check for --swap, too, since it's an easy
     718             :              * way to verify the clusters are in the same file system.  This
     719             :              * allows us to take some shortcuts in the file synchronization
     720             :              * step.  With some more effort, we could probably support the
     721             :              * separate-file-system use case, but this mode is unlikely to
     722             :              * offer much benefit if we have to copy the files across file
     723             :              * system boundaries.
     724             :              */
     725           2 :             check_hard_link(TRANSFER_MODE_SWAP);
     726             : 
     727             :             /*
     728             :              * There are a few known issues with using --swap to upgrade from
     729             :              * versions older than 10.  For example, the sequence tuple format
     730             :              * changed in v10, and the visibility map format changed in 9.6.
     731             :              * While such problems are not insurmountable (and we may have to
     732             :              * deal with similar problems in the future, anyway), it doesn't
     733             :              * seem worth the effort to support swap mode for upgrades from
     734             :              * long-unsupported versions.
     735             :              */
     736           2 :             if (GET_MAJOR_VERSION(old_cluster.major_version) < 1000)
     737           0 :                 pg_fatal("Swap mode can only upgrade clusters from PostgreSQL version %s and later.",
     738             :                          "10");
     739             : 
     740           2 :             break;
     741             :     }
     742             : 
     743          22 :     check_is_install_user(&new_cluster);
     744             : 
     745          22 :     check_for_prepared_transactions(&new_cluster);
     746             : 
     747          22 :     check_for_new_tablespace_dir();
     748             : 
     749          22 :     check_new_cluster_logical_replication_slots();
     750             : 
     751          20 :     check_new_cluster_subscription_configuration();
     752          18 : }
     753             : 
     754             : 
     755             : void
     756          18 : report_clusters_compatible(void)
     757             : {
     758          18 :     if (user_opts.check)
     759             :     {
     760           2 :         pg_log(PG_REPORT, "\n*Clusters are compatible*");
     761             :         /* stops new cluster */
     762           2 :         stop_postmaster(false);
     763             : 
     764           2 :         cleanup_output_dirs();
     765           2 :         exit(0);
     766             :     }
     767             : 
     768          16 :     pg_log(PG_REPORT, "\n"
     769             :            "If pg_upgrade fails after this point, you must re-initdb the\n"
     770             :            "new cluster before continuing.");
     771          16 : }
     772             : 
     773             : 
     774             : void
     775          16 : issue_warnings_and_set_wal_level(void)
     776             : {
     777             :     /*
     778             :      * We unconditionally start/stop the new server because pg_resetwal -o set
     779             :      * wal_level to 'minimum'.  If the user is upgrading standby servers using
     780             :      * the rsync instructions, they will need pg_upgrade to write its final
     781             :      * WAL record showing wal_level as 'replica'.
     782             :      */
     783          16 :     start_postmaster(&new_cluster, true);
     784             : 
     785             :     /* Reindex hash indexes for old < 10.0 */
     786          16 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 906)
     787           0 :         old_9_6_invalidate_hash_indexes(&new_cluster, false);
     788             : 
     789          16 :     report_extension_updates(&new_cluster);
     790             : 
     791          16 :     stop_postmaster(false);
     792          16 : }
     793             : 
     794             : 
     795             : void
     796          16 : output_completion_banner(char *deletion_script_file_name)
     797             : {
     798             :     PQExpBufferData user_specification;
     799             : 
     800          16 :     initPQExpBuffer(&user_specification);
     801          16 :     if (os_info.user_specified)
     802             :     {
     803           0 :         appendPQExpBufferStr(&user_specification, "-U ");
     804           0 :         appendShellString(&user_specification, os_info.user);
     805           0 :         appendPQExpBufferChar(&user_specification, ' ');
     806             :     }
     807             : 
     808          16 :     pg_log(PG_REPORT,
     809             :            "Some optimizer statistics may not have been transferred by pg_upgrade.\n"
     810             :            "Once you start the new server, consider running:\n"
     811             :            "    %s/vacuumdb %s--all --analyze-in-stages --missing-stats-only", new_cluster.bindir, user_specification.data);
     812             : 
     813          16 :     if (deletion_script_file_name)
     814          16 :         pg_log(PG_REPORT,
     815             :                "Running this script will delete the old cluster's data files:\n"
     816             :                "    %s",
     817             :                deletion_script_file_name);
     818             :     else
     819           0 :         pg_log(PG_REPORT,
     820             :                "Could not create a script to delete the old cluster's data files\n"
     821             :                "because user-defined tablespaces or the new cluster's data directory\n"
     822             :                "exist in the old cluster directory.  The old cluster's contents must\n"
     823             :                "be deleted manually.");
     824             : 
     825          16 :     termPQExpBuffer(&user_specification);
     826          16 : }
     827             : 
     828             : 
     829             : void
     830          32 : check_cluster_versions(void)
     831             : {
     832          32 :     prep_status("Checking cluster versions");
     833             : 
     834             :     /* cluster versions should already have been obtained */
     835             :     Assert(old_cluster.major_version != 0);
     836             :     Assert(new_cluster.major_version != 0);
     837             : 
     838             :     /*
     839             :      * We allow upgrades from/to the same major version for alpha/beta
     840             :      * upgrades
     841             :      */
     842             : 
     843          32 :     if (GET_MAJOR_VERSION(old_cluster.major_version) < 902)
     844           0 :         pg_fatal("This utility can only upgrade from PostgreSQL version %s and later.",
     845             :                  "9.2");
     846             : 
     847             :     /* Only current PG version is supported as a target */
     848          32 :     if (GET_MAJOR_VERSION(new_cluster.major_version) != GET_MAJOR_VERSION(PG_VERSION_NUM))
     849           0 :         pg_fatal("This utility can only upgrade to PostgreSQL version %s.",
     850             :                  PG_MAJORVERSION);
     851             : 
     852             :     /*
     853             :      * We can't allow downgrading because we use the target pg_dump, and
     854             :      * pg_dump cannot operate on newer database versions, only current and
     855             :      * older versions.
     856             :      */
     857          32 :     if (old_cluster.major_version > new_cluster.major_version)
     858           0 :         pg_fatal("This utility cannot be used to downgrade to older major PostgreSQL versions.");
     859             : 
     860             :     /* Ensure binaries match the designated data directories */
     861          32 :     if (GET_MAJOR_VERSION(old_cluster.major_version) !=
     862          32 :         GET_MAJOR_VERSION(old_cluster.bin_version))
     863           0 :         pg_fatal("Old cluster data and binary directories are from different major versions.");
     864          32 :     if (GET_MAJOR_VERSION(new_cluster.major_version) !=
     865          32 :         GET_MAJOR_VERSION(new_cluster.bin_version))
     866           0 :         pg_fatal("New cluster data and binary directories are from different major versions.");
     867             : 
     868             :     /*
     869             :      * Since from version 18, newly created database clusters always have
     870             :      * 'signed' default char-signedness, it makes less sense to use
     871             :      * --set-char-signedness option for upgrading from version 18 or later.
     872             :      * Users who want to change the default char signedness of the new
     873             :      * cluster, they can use pg_resetwal manually before the upgrade.
     874             :      */
     875          32 :     if (GET_MAJOR_VERSION(old_cluster.major_version) >= 1800 &&
     876          32 :         user_opts.char_signedness != -1)
     877           2 :         pg_fatal("%s option cannot be used to upgrade from PostgreSQL %s and later.",
     878             :                  "--set-char-signedness", "18");
     879             : 
     880          30 :     check_ok();
     881          30 : }
     882             : 
     883             : 
     884             : void
     885          30 : check_cluster_compatibility(void)
     886             : {
     887             :     /* get/check pg_control data of servers */
     888          30 :     get_control_data(&old_cluster);
     889          30 :     get_control_data(&new_cluster);
     890          30 :     check_control_data(&old_cluster.controldata, &new_cluster.controldata);
     891             : 
     892          30 :     if (user_opts.live_check && old_cluster.port == new_cluster.port)
     893           0 :         pg_fatal("When checking a live server, "
     894             :                  "the old and new port numbers must be different.");
     895          30 : }
     896             : 
     897             : 
     898             : static void
     899          24 : check_new_cluster_is_empty(void)
     900             : {
     901             :     int         dbnum;
     902             : 
     903          72 :     for (dbnum = 0; dbnum < new_cluster.dbarr.ndbs; dbnum++)
     904             :     {
     905             :         int         relnum;
     906          48 :         RelInfoArr *rel_arr = &new_cluster.dbarr.dbs[dbnum].rel_arr;
     907             : 
     908         144 :         for (relnum = 0; relnum < rel_arr->nrels;
     909          96 :              relnum++)
     910             :         {
     911             :             /* pg_largeobject and its index should be skipped */
     912          96 :             if (strcmp(rel_arr->rels[relnum].nspname, "pg_catalog") != 0)
     913           0 :                 pg_fatal("New cluster database \"%s\" is not empty: found relation \"%s.%s\"",
     914           0 :                          new_cluster.dbarr.dbs[dbnum].db_name,
     915           0 :                          rel_arr->rels[relnum].nspname,
     916           0 :                          rel_arr->rels[relnum].relname);
     917             :         }
     918             :     }
     919          24 : }
     920             : 
     921             : /*
     922             :  * A previous run of pg_upgrade might have failed and the new cluster
     923             :  * directory recreated, but they might have forgotten to remove
     924             :  * the new cluster's tablespace directories.  Therefore, check that
     925             :  * new cluster tablespace directories do not already exist.  If
     926             :  * they do, it would cause an error while restoring global objects.
     927             :  * This allows the failure to be detected at check time, rather than
     928             :  * during schema restore.
     929             :  */
     930             : static void
     931          22 : check_for_new_tablespace_dir(void)
     932             : {
     933             :     int         tblnum;
     934             :     char        new_tablespace_dir[MAXPGPATH];
     935             : 
     936          22 :     prep_status("Checking for new cluster tablespace directories");
     937             : 
     938          22 :     for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
     939             :     {
     940             :         struct stat statbuf;
     941             : 
     942           0 :         snprintf(new_tablespace_dir, MAXPGPATH, "%s%s",
     943           0 :                  os_info.old_tablespaces[tblnum],
     944             :                  new_cluster.tablespace_suffix);
     945             : 
     946           0 :         if (stat(new_tablespace_dir, &statbuf) == 0 || errno != ENOENT)
     947           0 :             pg_fatal("new cluster tablespace directory already exists: \"%s\"",
     948             :                      new_tablespace_dir);
     949             :     }
     950             : 
     951          22 :     check_ok();
     952          22 : }
     953             : 
     954             : /*
     955             :  * create_script_for_old_cluster_deletion()
     956             :  *
     957             :  *  This is particularly useful for tablespace deletion.
     958             :  */
     959             : void
     960          16 : create_script_for_old_cluster_deletion(char **deletion_script_file_name)
     961             : {
     962          16 :     FILE       *script = NULL;
     963             :     int         tblnum;
     964             :     char        old_cluster_pgdata[MAXPGPATH],
     965             :                 new_cluster_pgdata[MAXPGPATH];
     966             :     char       *old_tblspc_suffix;
     967             : 
     968          16 :     *deletion_script_file_name = psprintf("%sdelete_old_cluster.%s",
     969             :                                           SCRIPT_PREFIX, SCRIPT_EXT);
     970             : 
     971          16 :     strlcpy(old_cluster_pgdata, old_cluster.pgdata, MAXPGPATH);
     972          16 :     canonicalize_path(old_cluster_pgdata);
     973             : 
     974          16 :     strlcpy(new_cluster_pgdata, new_cluster.pgdata, MAXPGPATH);
     975          16 :     canonicalize_path(new_cluster_pgdata);
     976             : 
     977             :     /* Some people put the new data directory inside the old one. */
     978          16 :     if (path_is_prefix_of_path(old_cluster_pgdata, new_cluster_pgdata))
     979             :     {
     980           0 :         pg_log(PG_WARNING,
     981             :                "\nWARNING:  new data directory should not be inside the old data directory, i.e. %s", old_cluster_pgdata);
     982             : 
     983             :         /* Unlink file in case it is left over from a previous run. */
     984           0 :         unlink(*deletion_script_file_name);
     985           0 :         pg_free(*deletion_script_file_name);
     986           0 :         *deletion_script_file_name = NULL;
     987           0 :         return;
     988             :     }
     989             : 
     990             :     /*
     991             :      * Some users (oddly) create tablespaces inside the cluster data
     992             :      * directory.  We can't create a proper old cluster delete script in that
     993             :      * case.
     994             :      */
     995          16 :     for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
     996             :     {
     997             :         char        old_tablespace_dir[MAXPGPATH];
     998             : 
     999           0 :         strlcpy(old_tablespace_dir, os_info.old_tablespaces[tblnum], MAXPGPATH);
    1000           0 :         canonicalize_path(old_tablespace_dir);
    1001           0 :         if (path_is_prefix_of_path(old_cluster_pgdata, old_tablespace_dir))
    1002             :         {
    1003             :             /* reproduce warning from CREATE TABLESPACE that is in the log */
    1004           0 :             pg_log(PG_WARNING,
    1005             :                    "\nWARNING:  user-defined tablespace locations should not be inside the data directory, i.e. %s", old_tablespace_dir);
    1006             : 
    1007             :             /* Unlink file in case it is left over from a previous run. */
    1008           0 :             unlink(*deletion_script_file_name);
    1009           0 :             pg_free(*deletion_script_file_name);
    1010           0 :             *deletion_script_file_name = NULL;
    1011           0 :             return;
    1012             :         }
    1013             :     }
    1014             : 
    1015          16 :     prep_status("Creating script to delete old cluster");
    1016             : 
    1017          16 :     if ((script = fopen_priv(*deletion_script_file_name, "w")) == NULL)
    1018           0 :         pg_fatal("could not open file \"%s\": %m",
    1019             :                  *deletion_script_file_name);
    1020             : 
    1021             : #ifndef WIN32
    1022             :     /* add shebang header */
    1023          16 :     fprintf(script, "#!/bin/sh\n\n");
    1024             : #endif
    1025             : 
    1026             :     /* delete old cluster's default tablespace */
    1027          16 :     fprintf(script, RMDIR_CMD " %c%s%c\n", PATH_QUOTE,
    1028             :             fix_path_separator(old_cluster.pgdata), PATH_QUOTE);
    1029             : 
    1030             :     /* delete old cluster's alternate tablespaces */
    1031          16 :     old_tblspc_suffix = pg_strdup(old_cluster.tablespace_suffix);
    1032          16 :     fix_path_separator(old_tblspc_suffix);
    1033          16 :     for (tblnum = 0; tblnum < os_info.num_old_tablespaces; tblnum++)
    1034           0 :         fprintf(script, RMDIR_CMD " %c%s%s%c\n", PATH_QUOTE,
    1035           0 :                 fix_path_separator(os_info.old_tablespaces[tblnum]),
    1036             :                 old_tblspc_suffix, PATH_QUOTE);
    1037          16 :     pfree(old_tblspc_suffix);
    1038             : 
    1039          16 :     fclose(script);
    1040             : 
    1041             : #ifndef WIN32
    1042          16 :     if (chmod(*deletion_script_file_name, S_IRWXU) != 0)
    1043           0 :         pg_fatal("could not add execute permission to file \"%s\": %m",
    1044             :                  *deletion_script_file_name);
    1045             : #endif
    1046             : 
    1047          16 :     check_ok();
    1048             : }
    1049             : 
    1050             : 
    1051             : /*
    1052             :  *  check_is_install_user()
    1053             :  *
    1054             :  *  Check we are the install user, and that the new cluster
    1055             :  *  has no other users.
    1056             :  */
    1057             : static void
    1058          50 : check_is_install_user(ClusterInfo *cluster)
    1059             : {
    1060             :     PGresult   *res;
    1061          50 :     PGconn     *conn = connectToServer(cluster, "template1");
    1062             : 
    1063          50 :     prep_status("Checking database user is the install user");
    1064             : 
    1065             :     /* Can't use pg_authid because only superusers can view it. */
    1066          50 :     res = executeQueryOrDie(conn,
    1067             :                             "SELECT rolsuper, oid "
    1068             :                             "FROM pg_catalog.pg_roles "
    1069             :                             "WHERE rolname = current_user "
    1070             :                             "AND rolname !~ '^pg_'");
    1071             : 
    1072             :     /*
    1073             :      * We only allow the install user in the new cluster (see comment below)
    1074             :      * and we preserve pg_authid.oid, so this must be the install user in the
    1075             :      * old cluster too.
    1076             :      */
    1077          50 :     if (PQntuples(res) != 1 ||
    1078          50 :         atooid(PQgetvalue(res, 0, 1)) != BOOTSTRAP_SUPERUSERID)
    1079           0 :         pg_fatal("database user \"%s\" is not the install user",
    1080             :                  os_info.user);
    1081             : 
    1082          50 :     PQclear(res);
    1083             : 
    1084          50 :     res = executeQueryOrDie(conn,
    1085             :                             "SELECT COUNT(*) "
    1086             :                             "FROM pg_catalog.pg_roles "
    1087             :                             "WHERE rolname !~ '^pg_'");
    1088             : 
    1089          50 :     if (PQntuples(res) != 1)
    1090           0 :         pg_fatal("could not determine the number of users");
    1091             : 
    1092             :     /*
    1093             :      * We only allow the install user in the new cluster because other defined
    1094             :      * users might match users defined in the old cluster and generate an
    1095             :      * error during pg_dump restore.
    1096             :      */
    1097          50 :     if (cluster == &new_cluster && strcmp(PQgetvalue(res, 0, 0), "1") != 0)
    1098           0 :         pg_fatal("Only the install user can be defined in the new cluster.");
    1099             : 
    1100          50 :     PQclear(res);
    1101             : 
    1102          50 :     PQfinish(conn);
    1103             : 
    1104          50 :     check_ok();
    1105          50 : }
    1106             : 
    1107             : 
    1108             : /*
    1109             :  *  check_for_connection_status
    1110             :  *
    1111             :  *  Ensure that all non-template0 databases allow connections since they
    1112             :  *  otherwise won't be restored; and that template0 explicitly doesn't allow
    1113             :  *  connections since it would make pg_dumpall --globals restore fail.
    1114             :  */
    1115             : static void
    1116          30 : check_for_connection_status(ClusterInfo *cluster)
    1117             : {
    1118             :     int         dbnum;
    1119             :     PGconn     *conn_template1;
    1120             :     PGresult   *dbres;
    1121             :     int         ntups;
    1122             :     int         i_datname;
    1123             :     int         i_datallowconn;
    1124             :     int         i_datconnlimit;
    1125          30 :     FILE       *script = NULL;
    1126             :     char        output_path[MAXPGPATH];
    1127             : 
    1128          30 :     prep_status("Checking database connection settings");
    1129             : 
    1130          30 :     snprintf(output_path, sizeof(output_path), "%s/%s",
    1131             :              log_opts.basedir,
    1132             :              "databases_cannot_connect_to.txt");
    1133             : 
    1134          30 :     conn_template1 = connectToServer(cluster, "template1");
    1135             : 
    1136             :     /* get database names */
    1137          30 :     dbres = executeQueryOrDie(conn_template1,
    1138             :                               "SELECT  datname, datallowconn, datconnlimit "
    1139             :                               "FROM    pg_catalog.pg_database");
    1140             : 
    1141          30 :     i_datname = PQfnumber(dbres, "datname");
    1142          30 :     i_datallowconn = PQfnumber(dbres, "datallowconn");
    1143          30 :     i_datconnlimit = PQfnumber(dbres, "datconnlimit");
    1144             : 
    1145          30 :     ntups = PQntuples(dbres);
    1146         156 :     for (dbnum = 0; dbnum < ntups; dbnum++)
    1147             :     {
    1148         126 :         char       *datname = PQgetvalue(dbres, dbnum, i_datname);
    1149         126 :         char       *datallowconn = PQgetvalue(dbres, dbnum, i_datallowconn);
    1150         126 :         char       *datconnlimit = PQgetvalue(dbres, dbnum, i_datconnlimit);
    1151             : 
    1152         126 :         if (strcmp(datname, "template0") == 0)
    1153             :         {
    1154             :             /* avoid restore failure when pg_dumpall tries to create template0 */
    1155          30 :             if (strcmp(datallowconn, "t") == 0)
    1156           0 :                 pg_fatal("template0 must not allow connections, "
    1157             :                          "i.e. its pg_database.datallowconn must be false");
    1158             :         }
    1159             :         else
    1160             :         {
    1161             :             /*
    1162             :              * Avoid datallowconn == false databases from being skipped on
    1163             :              * restore, and ensure that no databases are marked invalid with
    1164             :              * datconnlimit == -2.
    1165             :              */
    1166          96 :             if ((strcmp(datallowconn, "f") == 0) || strcmp(datconnlimit, "-2") == 0)
    1167             :             {
    1168           2 :                 if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
    1169           0 :                     pg_fatal("could not open file \"%s\": %m", output_path);
    1170             : 
    1171           2 :                 fprintf(script, "%s\n", datname);
    1172             :             }
    1173             :         }
    1174             :     }
    1175             : 
    1176          30 :     PQclear(dbres);
    1177             : 
    1178          30 :     PQfinish(conn_template1);
    1179             : 
    1180          30 :     if (script)
    1181             :     {
    1182           2 :         fclose(script);
    1183           2 :         pg_log(PG_REPORT, "fatal");
    1184           2 :         pg_fatal("All non-template0 databases must allow connections, i.e. their\n"
    1185             :                  "pg_database.datallowconn must be true and pg_database.datconnlimit\n"
    1186             :                  "must not be -2.  Your installation contains non-template0 databases\n"
    1187             :                  "which cannot be connected to.  Consider allowing connection for all\n"
    1188             :                  "non-template0 databases or drop the databases which do not allow\n"
    1189             :                  "connections.  A list of databases with the problem is in the file:\n"
    1190             :                  "    %s", output_path);
    1191             :     }
    1192             :     else
    1193          28 :         check_ok();
    1194          28 : }
    1195             : 
    1196             : 
    1197             : /*
    1198             :  *  check_for_prepared_transactions()
    1199             :  *
    1200             :  *  Make sure there are no prepared transactions because the storage format
    1201             :  *  might have changed.
    1202             :  */
    1203             : static void
    1204          50 : check_for_prepared_transactions(ClusterInfo *cluster)
    1205             : {
    1206             :     PGresult   *res;
    1207          50 :     PGconn     *conn = connectToServer(cluster, "template1");
    1208             : 
    1209          50 :     prep_status("Checking for prepared transactions");
    1210             : 
    1211          50 :     res = executeQueryOrDie(conn,
    1212             :                             "SELECT * "
    1213             :                             "FROM pg_catalog.pg_prepared_xacts");
    1214             : 
    1215          50 :     if (PQntuples(res) != 0)
    1216             :     {
    1217           0 :         if (cluster == &old_cluster)
    1218           0 :             pg_fatal("The source cluster contains prepared transactions");
    1219             :         else
    1220           0 :             pg_fatal("The target cluster contains prepared transactions");
    1221             :     }
    1222             : 
    1223          50 :     PQclear(res);
    1224             : 
    1225          50 :     PQfinish(conn);
    1226             : 
    1227          50 :     check_ok();
    1228          50 : }
    1229             : 
    1230             : /*
    1231             :  * Callback function for processing result of query for
    1232             :  * check_for_isn_and_int8_passing_mismatch()'s UpgradeTask.  If the query
    1233             :  * returned any rows (i.e., the check failed), write the details to the report
    1234             :  * file.
    1235             :  */
    1236             : static void
    1237           0 : process_isn_and_int8_passing_mismatch(DbInfo *dbinfo, PGresult *res, void *arg)
    1238             : {
    1239           0 :     int         ntups = PQntuples(res);
    1240           0 :     int         i_nspname = PQfnumber(res, "nspname");
    1241           0 :     int         i_proname = PQfnumber(res, "proname");
    1242           0 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1243             : 
    1244             :     AssertVariableIsOfType(&process_isn_and_int8_passing_mismatch,
    1245             :                            UpgradeTaskProcessCB);
    1246             : 
    1247           0 :     if (ntups == 0)
    1248           0 :         return;
    1249             : 
    1250           0 :     if (report->file == NULL &&
    1251           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1252           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1253             : 
    1254           0 :     fprintf(report->file, "In database: %s\n", dbinfo->db_name);
    1255             : 
    1256           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1257           0 :         fprintf(report->file, "  %s.%s\n",
    1258             :                 PQgetvalue(res, rowno, i_nspname),
    1259             :                 PQgetvalue(res, rowno, i_proname));
    1260             : }
    1261             : 
    1262             : /*
    1263             :  *  check_for_isn_and_int8_passing_mismatch()
    1264             :  *
    1265             :  *  contrib/isn relies on data type int8, and in 8.4 int8 can now be passed
    1266             :  *  by value.  The schema dumps the CREATE TYPE PASSEDBYVALUE setting so
    1267             :  *  it must match for the old and new servers.
    1268             :  */
    1269             : static void
    1270          28 : check_for_isn_and_int8_passing_mismatch(ClusterInfo *cluster)
    1271             : {
    1272             :     UpgradeTask *task;
    1273             :     UpgradeTaskReport report;
    1274          28 :     const char *query = "SELECT n.nspname, p.proname "
    1275             :         "FROM   pg_catalog.pg_proc p, "
    1276             :         "       pg_catalog.pg_namespace n "
    1277             :         "WHERE  p.pronamespace = n.oid AND "
    1278             :         "       p.probin = '$libdir/isn'";
    1279             : 
    1280          28 :     prep_status("Checking for contrib/isn with bigint-passing mismatch");
    1281             : 
    1282          28 :     if (old_cluster.controldata.float8_pass_by_value ==
    1283          28 :         new_cluster.controldata.float8_pass_by_value)
    1284             :     {
    1285             :         /* no mismatch */
    1286          28 :         check_ok();
    1287          28 :         return;
    1288             :     }
    1289             : 
    1290           0 :     report.file = NULL;
    1291           0 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    1292             :              log_opts.basedir,
    1293             :              "contrib_isn_and_int8_pass_by_value.txt");
    1294             : 
    1295           0 :     task = upgrade_task_create();
    1296           0 :     upgrade_task_add_step(task, query, process_isn_and_int8_passing_mismatch,
    1297             :                           true, &report);
    1298           0 :     upgrade_task_run(task, cluster);
    1299           0 :     upgrade_task_free(task);
    1300             : 
    1301           0 :     if (report.file)
    1302             :     {
    1303           0 :         fclose(report.file);
    1304           0 :         pg_log(PG_REPORT, "fatal");
    1305           0 :         pg_fatal("Your installation contains \"contrib/isn\" functions which rely on the\n"
    1306             :                  "bigint data type.  Your old and new clusters pass bigint values\n"
    1307             :                  "differently so this cluster cannot currently be upgraded.  You can\n"
    1308             :                  "manually dump databases in the old cluster that use \"contrib/isn\"\n"
    1309             :                  "facilities, drop them, perform the upgrade, and then restore them.  A\n"
    1310             :                  "list of the problem functions is in the file:\n"
    1311             :                  "    %s", report.path);
    1312             :     }
    1313             :     else
    1314           0 :         check_ok();
    1315             : }
    1316             : 
    1317             : /*
    1318             :  * Callback function for processing result of query for
    1319             :  * check_for_user_defined_postfix_ops()'s UpgradeTask.  If the query returned
    1320             :  * any rows (i.e., the check failed), write the details to the report file.
    1321             :  */
    1322             : static void
    1323           0 : process_user_defined_postfix_ops(DbInfo *dbinfo, PGresult *res, void *arg)
    1324             : {
    1325           0 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1326           0 :     int         ntups = PQntuples(res);
    1327           0 :     int         i_oproid = PQfnumber(res, "oproid");
    1328           0 :     int         i_oprnsp = PQfnumber(res, "oprnsp");
    1329           0 :     int         i_oprname = PQfnumber(res, "oprname");
    1330           0 :     int         i_typnsp = PQfnumber(res, "typnsp");
    1331           0 :     int         i_typname = PQfnumber(res, "typname");
    1332             : 
    1333             :     AssertVariableIsOfType(&process_user_defined_postfix_ops,
    1334             :                            UpgradeTaskProcessCB);
    1335             : 
    1336           0 :     if (ntups == 0)
    1337           0 :         return;
    1338             : 
    1339           0 :     if (report->file == NULL &&
    1340           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1341           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1342             : 
    1343           0 :     fprintf(report->file, "In database: %s\n", dbinfo->db_name);
    1344             : 
    1345           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1346           0 :         fprintf(report->file, "  (oid=%s) %s.%s (%s.%s, NONE)\n",
    1347             :                 PQgetvalue(res, rowno, i_oproid),
    1348             :                 PQgetvalue(res, rowno, i_oprnsp),
    1349             :                 PQgetvalue(res, rowno, i_oprname),
    1350             :                 PQgetvalue(res, rowno, i_typnsp),
    1351             :                 PQgetvalue(res, rowno, i_typname));
    1352             : }
    1353             : 
    1354             : /*
    1355             :  * Verify that no user defined postfix operators exist.
    1356             :  */
    1357             : static void
    1358           0 : check_for_user_defined_postfix_ops(ClusterInfo *cluster)
    1359             : {
    1360             :     UpgradeTaskReport report;
    1361           0 :     UpgradeTask *task = upgrade_task_create();
    1362             :     const char *query;
    1363             : 
    1364             :     /*
    1365             :      * The query below hardcodes FirstNormalObjectId as 16384 rather than
    1366             :      * interpolating that C #define into the query because, if that #define is
    1367             :      * ever changed, the cutoff we want to use is the value used by
    1368             :      * pre-version 14 servers, not that of some future version.
    1369             :      */
    1370           0 :     query = "SELECT o.oid AS oproid, "
    1371             :         "       n.nspname AS oprnsp, "
    1372             :         "       o.oprname, "
    1373             :         "       tn.nspname AS typnsp, "
    1374             :         "       t.typname "
    1375             :         "FROM pg_catalog.pg_operator o, "
    1376             :         "     pg_catalog.pg_namespace n, "
    1377             :         "     pg_catalog.pg_type t, "
    1378             :         "     pg_catalog.pg_namespace tn "
    1379             :         "WHERE o.oprnamespace = n.oid AND "
    1380             :         "      o.oprleft = t.oid AND "
    1381             :         "      t.typnamespace = tn.oid AND "
    1382             :         "      o.oprright = 0 AND "
    1383             :         "      o.oid >= 16384";
    1384             : 
    1385           0 :     prep_status("Checking for user-defined postfix operators");
    1386             : 
    1387           0 :     report.file = NULL;
    1388           0 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    1389             :              log_opts.basedir,
    1390             :              "postfix_ops.txt");
    1391             : 
    1392           0 :     upgrade_task_add_step(task, query, process_user_defined_postfix_ops,
    1393             :                           true, &report);
    1394           0 :     upgrade_task_run(task, cluster);
    1395           0 :     upgrade_task_free(task);
    1396             : 
    1397           0 :     if (report.file)
    1398             :     {
    1399           0 :         fclose(report.file);
    1400           0 :         pg_log(PG_REPORT, "fatal");
    1401           0 :         pg_fatal("Your installation contains user-defined postfix operators, which are not\n"
    1402             :                  "supported anymore.  Consider dropping the postfix operators and replacing\n"
    1403             :                  "them with prefix operators or function calls.\n"
    1404             :                  "A list of user-defined postfix operators is in the file:\n"
    1405             :                  "    %s", report.path);
    1406             :     }
    1407             :     else
    1408           0 :         check_ok();
    1409           0 : }
    1410             : 
    1411             : /*
    1412             :  * Callback function for processing results of query for
    1413             :  * check_for_incompatible_polymorphics()'s UpgradeTask.  If the query returned
    1414             :  * any rows (i.e., the check failed), write the details to the report file.
    1415             :  */
    1416             : static void
    1417           0 : process_incompat_polymorphics(DbInfo *dbinfo, PGresult *res, void *arg)
    1418             : {
    1419           0 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1420           0 :     int         ntups = PQntuples(res);
    1421           0 :     int         i_objkind = PQfnumber(res, "objkind");
    1422           0 :     int         i_objname = PQfnumber(res, "objname");
    1423             : 
    1424             :     AssertVariableIsOfType(&process_incompat_polymorphics,
    1425             :                            UpgradeTaskProcessCB);
    1426             : 
    1427           0 :     if (ntups == 0)
    1428           0 :         return;
    1429             : 
    1430           0 :     if (report->file == NULL &&
    1431           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1432           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1433             : 
    1434           0 :     fprintf(report->file, "In database: %s\n", dbinfo->db_name);
    1435             : 
    1436           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1437           0 :         fprintf(report->file, "  %s: %s\n",
    1438             :                 PQgetvalue(res, rowno, i_objkind),
    1439             :                 PQgetvalue(res, rowno, i_objname));
    1440             : }
    1441             : 
    1442             : /*
    1443             :  *  check_for_incompatible_polymorphics()
    1444             :  *
    1445             :  *  Make sure nothing is using old polymorphic functions with
    1446             :  *  anyarray/anyelement rather than the new anycompatible variants.
    1447             :  */
    1448             : static void
    1449           0 : check_for_incompatible_polymorphics(ClusterInfo *cluster)
    1450             : {
    1451             :     PQExpBufferData old_polymorphics;
    1452           0 :     UpgradeTask *task = upgrade_task_create();
    1453             :     UpgradeTaskReport report;
    1454             :     char       *query;
    1455             : 
    1456           0 :     prep_status("Checking for incompatible polymorphic functions");
    1457             : 
    1458           0 :     report.file = NULL;
    1459           0 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    1460             :              log_opts.basedir,
    1461             :              "incompatible_polymorphics.txt");
    1462             : 
    1463             :     /* The set of problematic functions varies a bit in different versions */
    1464           0 :     initPQExpBuffer(&old_polymorphics);
    1465             : 
    1466           0 :     appendPQExpBufferStr(&old_polymorphics,
    1467             :                          "'array_append(anyarray,anyelement)'"
    1468             :                          ", 'array_cat(anyarray,anyarray)'"
    1469             :                          ", 'array_prepend(anyelement,anyarray)'");
    1470             : 
    1471           0 :     if (GET_MAJOR_VERSION(cluster->major_version) >= 903)
    1472           0 :         appendPQExpBufferStr(&old_polymorphics,
    1473             :                              ", 'array_remove(anyarray,anyelement)'"
    1474             :                              ", 'array_replace(anyarray,anyelement,anyelement)'");
    1475             : 
    1476           0 :     if (GET_MAJOR_VERSION(cluster->major_version) >= 905)
    1477           0 :         appendPQExpBufferStr(&old_polymorphics,
    1478             :                              ", 'array_position(anyarray,anyelement)'"
    1479             :                              ", 'array_position(anyarray,anyelement,integer)'"
    1480             :                              ", 'array_positions(anyarray,anyelement)'"
    1481             :                              ", 'width_bucket(anyelement,anyarray)'");
    1482             : 
    1483             :     /*
    1484             :      * The query below hardcodes FirstNormalObjectId as 16384 rather than
    1485             :      * interpolating that C #define into the query because, if that #define is
    1486             :      * ever changed, the cutoff we want to use is the value used by
    1487             :      * pre-version 14 servers, not that of some future version.
    1488             :      */
    1489             : 
    1490             :     /* Aggregate transition functions */
    1491           0 :     query = psprintf("SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
    1492             :                      "FROM pg_proc AS p "
    1493             :                      "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
    1494             :                      "JOIN pg_proc AS transfn ON transfn.oid=a.aggtransfn "
    1495             :                      "WHERE p.oid >= 16384 "
    1496             :                      "AND a.aggtransfn = ANY(ARRAY[%s]::regprocedure[]) "
    1497             :                      "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
    1498             : 
    1499             :     /* Aggregate final functions */
    1500             :                      "UNION ALL "
    1501             :                      "SELECT 'aggregate' AS objkind, p.oid::regprocedure::text AS objname "
    1502             :                      "FROM pg_proc AS p "
    1503             :                      "JOIN pg_aggregate AS a ON a.aggfnoid=p.oid "
    1504             :                      "JOIN pg_proc AS finalfn ON finalfn.oid=a.aggfinalfn "
    1505             :                      "WHERE p.oid >= 16384 "
    1506             :                      "AND a.aggfinalfn = ANY(ARRAY[%s]::regprocedure[]) "
    1507             :                      "AND a.aggtranstype = ANY(ARRAY['anyarray', 'anyelement']::regtype[]) "
    1508             : 
    1509             :     /* Operators */
    1510             :                      "UNION ALL "
    1511             :                      "SELECT 'operator' AS objkind, op.oid::regoperator::text AS objname "
    1512             :                      "FROM pg_operator AS op "
    1513             :                      "WHERE op.oid >= 16384 "
    1514             :                      "AND oprcode = ANY(ARRAY[%s]::regprocedure[]) "
    1515             :                      "AND oprleft = ANY(ARRAY['anyarray', 'anyelement']::regtype[])",
    1516             :                      old_polymorphics.data,
    1517             :                      old_polymorphics.data,
    1518             :                      old_polymorphics.data);
    1519             : 
    1520           0 :     upgrade_task_add_step(task, query, process_incompat_polymorphics,
    1521             :                           true, &report);
    1522           0 :     upgrade_task_run(task, cluster);
    1523           0 :     upgrade_task_free(task);
    1524             : 
    1525           0 :     if (report.file)
    1526             :     {
    1527           0 :         fclose(report.file);
    1528           0 :         pg_log(PG_REPORT, "fatal");
    1529           0 :         pg_fatal("Your installation contains user-defined objects that refer to internal\n"
    1530             :                  "polymorphic functions with arguments of type \"anyarray\" or \"anyelement\".\n"
    1531             :                  "These user-defined objects must be dropped before upgrading and restored\n"
    1532             :                  "afterwards, changing them to refer to the new corresponding functions with\n"
    1533             :                  "arguments of type \"anycompatiblearray\" and \"anycompatible\".\n"
    1534             :                  "A list of the problematic objects is in the file:\n"
    1535             :                  "    %s", report.path);
    1536             :     }
    1537             :     else
    1538           0 :         check_ok();
    1539             : 
    1540           0 :     termPQExpBuffer(&old_polymorphics);
    1541           0 :     pg_free(query);
    1542           0 : }
    1543             : 
    1544             : /*
    1545             :  * Callback function for processing results of query for
    1546             :  * check_for_tables_with_oids()'s UpgradeTask.  If the query returned any rows
    1547             :  * (i.e., the check failed), write the details to the report file.
    1548             :  */
    1549             : static void
    1550           0 : process_with_oids_check(DbInfo *dbinfo, PGresult *res, void *arg)
    1551             : {
    1552           0 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1553           0 :     int         ntups = PQntuples(res);
    1554           0 :     int         i_nspname = PQfnumber(res, "nspname");
    1555           0 :     int         i_relname = PQfnumber(res, "relname");
    1556             : 
    1557             :     AssertVariableIsOfType(&process_with_oids_check, UpgradeTaskProcessCB);
    1558             : 
    1559           0 :     if (ntups == 0)
    1560           0 :         return;
    1561             : 
    1562           0 :     if (report->file == NULL &&
    1563           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1564           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1565             : 
    1566           0 :     fprintf(report->file, "In database: %s\n", dbinfo->db_name);
    1567             : 
    1568           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1569           0 :         fprintf(report->file, "  %s.%s\n",
    1570             :                 PQgetvalue(res, rowno, i_nspname),
    1571             :                 PQgetvalue(res, rowno, i_relname));
    1572             : }
    1573             : 
    1574             : /*
    1575             :  * Verify that no tables are declared WITH OIDS.
    1576             :  */
    1577             : static void
    1578           0 : check_for_tables_with_oids(ClusterInfo *cluster)
    1579             : {
    1580             :     UpgradeTaskReport report;
    1581           0 :     UpgradeTask *task = upgrade_task_create();
    1582           0 :     const char *query = "SELECT n.nspname, c.relname "
    1583             :         "FROM   pg_catalog.pg_class c, "
    1584             :         "       pg_catalog.pg_namespace n "
    1585             :         "WHERE  c.relnamespace = n.oid AND "
    1586             :         "       c.relhasoids AND"
    1587             :         "       n.nspname NOT IN ('pg_catalog')";
    1588             : 
    1589           0 :     prep_status("Checking for tables WITH OIDS");
    1590             : 
    1591           0 :     report.file = NULL;
    1592           0 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    1593             :              log_opts.basedir,
    1594             :              "tables_with_oids.txt");
    1595             : 
    1596           0 :     upgrade_task_add_step(task, query, process_with_oids_check,
    1597             :                           true, &report);
    1598           0 :     upgrade_task_run(task, cluster);
    1599           0 :     upgrade_task_free(task);
    1600             : 
    1601           0 :     if (report.file)
    1602             :     {
    1603           0 :         fclose(report.file);
    1604           0 :         pg_log(PG_REPORT, "fatal");
    1605           0 :         pg_fatal("Your installation contains tables declared WITH OIDS, which is not\n"
    1606             :                  "supported anymore.  Consider removing the oid column using\n"
    1607             :                  "    ALTER TABLE ... SET WITHOUT OIDS;\n"
    1608             :                  "A list of tables with the problem is in the file:\n"
    1609             :                  "    %s", report.path);
    1610             :     }
    1611             :     else
    1612           0 :         check_ok();
    1613           0 : }
    1614             : 
    1615             : 
    1616             : /*
    1617             :  * check_for_pg_role_prefix()
    1618             :  *
    1619             :  *  Versions older than 9.6 should not have any pg_* roles
    1620             :  */
    1621             : static void
    1622           0 : check_for_pg_role_prefix(ClusterInfo *cluster)
    1623             : {
    1624             :     PGresult   *res;
    1625           0 :     PGconn     *conn = connectToServer(cluster, "template1");
    1626             :     int         ntups;
    1627             :     int         i_roloid;
    1628             :     int         i_rolname;
    1629           0 :     FILE       *script = NULL;
    1630             :     char        output_path[MAXPGPATH];
    1631             : 
    1632           0 :     prep_status("Checking for roles starting with \"pg_\"");
    1633             : 
    1634           0 :     snprintf(output_path, sizeof(output_path), "%s/%s",
    1635             :              log_opts.basedir,
    1636             :              "pg_role_prefix.txt");
    1637             : 
    1638           0 :     res = executeQueryOrDie(conn,
    1639             :                             "SELECT oid AS roloid, rolname "
    1640             :                             "FROM pg_catalog.pg_roles "
    1641             :                             "WHERE rolname ~ '^pg_'");
    1642             : 
    1643           0 :     ntups = PQntuples(res);
    1644           0 :     i_roloid = PQfnumber(res, "roloid");
    1645           0 :     i_rolname = PQfnumber(res, "rolname");
    1646           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1647             :     {
    1648           0 :         if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
    1649           0 :             pg_fatal("could not open file \"%s\": %m", output_path);
    1650           0 :         fprintf(script, "%s (oid=%s)\n",
    1651             :                 PQgetvalue(res, rowno, i_rolname),
    1652             :                 PQgetvalue(res, rowno, i_roloid));
    1653             :     }
    1654             : 
    1655           0 :     PQclear(res);
    1656             : 
    1657           0 :     PQfinish(conn);
    1658             : 
    1659           0 :     if (script)
    1660             :     {
    1661           0 :         fclose(script);
    1662           0 :         pg_log(PG_REPORT, "fatal");
    1663           0 :         pg_fatal("Your installation contains roles starting with \"pg_\".\n"
    1664             :                  "\"pg_\" is a reserved prefix for system roles.  The cluster\n"
    1665             :                  "cannot be upgraded until these roles are renamed.\n"
    1666             :                  "A list of roles starting with \"pg_\" is in the file:\n"
    1667             :                  "    %s", output_path);
    1668             :     }
    1669             :     else
    1670           0 :         check_ok();
    1671           0 : }
    1672             : 
    1673             : /*
    1674             :  * Callback function for processing results of query for
    1675             :  * check_for_user_defined_encoding_conversions()'s UpgradeTask.  If the query
    1676             :  * returned any rows (i.e., the check failed), write the details to the report
    1677             :  * file.
    1678             :  */
    1679             : static void
    1680           0 : process_user_defined_encoding_conversions(DbInfo *dbinfo, PGresult *res, void *arg)
    1681             : {
    1682           0 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1683           0 :     int         ntups = PQntuples(res);
    1684           0 :     int         i_conoid = PQfnumber(res, "conoid");
    1685           0 :     int         i_conname = PQfnumber(res, "conname");
    1686           0 :     int         i_nspname = PQfnumber(res, "nspname");
    1687             : 
    1688             :     AssertVariableIsOfType(&process_user_defined_encoding_conversions,
    1689             :                            UpgradeTaskProcessCB);
    1690             : 
    1691           0 :     if (ntups == 0)
    1692           0 :         return;
    1693             : 
    1694           0 :     if (report->file == NULL &&
    1695           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1696           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1697             : 
    1698           0 :     fprintf(report->file, "In database: %s\n", dbinfo->db_name);
    1699             : 
    1700           0 :     for (int rowno = 0; rowno < ntups; rowno++)
    1701           0 :         fprintf(report->file, "  (oid=%s) %s.%s\n",
    1702             :                 PQgetvalue(res, rowno, i_conoid),
    1703             :                 PQgetvalue(res, rowno, i_nspname),
    1704             :                 PQgetvalue(res, rowno, i_conname));
    1705             : }
    1706             : 
    1707             : /*
    1708             :  * Verify that no user-defined encoding conversions exist.
    1709             :  */
    1710             : static void
    1711           0 : check_for_user_defined_encoding_conversions(ClusterInfo *cluster)
    1712             : {
    1713             :     UpgradeTaskReport report;
    1714           0 :     UpgradeTask *task = upgrade_task_create();
    1715             :     const char *query;
    1716             : 
    1717           0 :     prep_status("Checking for user-defined encoding conversions");
    1718             : 
    1719           0 :     report.file = NULL;
    1720           0 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    1721             :              log_opts.basedir,
    1722             :              "encoding_conversions.txt");
    1723             : 
    1724             :     /*
    1725             :      * The query below hardcodes FirstNormalObjectId as 16384 rather than
    1726             :      * interpolating that C #define into the query because, if that #define is
    1727             :      * ever changed, the cutoff we want to use is the value used by
    1728             :      * pre-version 14 servers, not that of some future version.
    1729             :      */
    1730           0 :     query = "SELECT c.oid as conoid, c.conname, n.nspname "
    1731             :         "FROM pg_catalog.pg_conversion c, "
    1732             :         "     pg_catalog.pg_namespace n "
    1733             :         "WHERE c.connamespace = n.oid AND "
    1734             :         "      c.oid >= 16384";
    1735             : 
    1736           0 :     upgrade_task_add_step(task, query,
    1737             :                           process_user_defined_encoding_conversions,
    1738             :                           true, &report);
    1739           0 :     upgrade_task_run(task, cluster);
    1740           0 :     upgrade_task_free(task);
    1741             : 
    1742           0 :     if (report.file)
    1743             :     {
    1744           0 :         fclose(report.file);
    1745           0 :         pg_log(PG_REPORT, "fatal");
    1746           0 :         pg_fatal("Your installation contains user-defined encoding conversions.\n"
    1747             :                  "The conversion function parameters changed in PostgreSQL version 14\n"
    1748             :                  "so this cluster cannot currently be upgraded.  You can remove the\n"
    1749             :                  "encoding conversions in the old cluster and restart the upgrade.\n"
    1750             :                  "A list of user-defined encoding conversions is in the file:\n"
    1751             :                  "    %s", report.path);
    1752             :     }
    1753             :     else
    1754           0 :         check_ok();
    1755           0 : }
    1756             : 
    1757             : /*
    1758             :  * check_new_cluster_logical_replication_slots()
    1759             :  *
    1760             :  * Verify that there are no logical replication slots on the new cluster and
    1761             :  * that the parameter settings necessary for creating slots are sufficient.
    1762             :  */
    1763             : static void
    1764          22 : check_new_cluster_logical_replication_slots(void)
    1765             : {
    1766             :     PGresult   *res;
    1767             :     PGconn     *conn;
    1768             :     int         nslots_on_old;
    1769             :     int         nslots_on_new;
    1770             :     int         max_replication_slots;
    1771             :     char       *wal_level;
    1772             : 
    1773             :     /* Logical slots can be migrated since PG17. */
    1774          22 :     if (GET_MAJOR_VERSION(old_cluster.major_version) <= 1600)
    1775           0 :         return;
    1776             : 
    1777          22 :     nslots_on_old = count_old_cluster_logical_slots();
    1778             : 
    1779             :     /* Quick return if there are no logical slots to be migrated. */
    1780          22 :     if (nslots_on_old == 0)
    1781          18 :         return;
    1782             : 
    1783           4 :     conn = connectToServer(&new_cluster, "template1");
    1784             : 
    1785           4 :     prep_status("Checking for new cluster logical replication slots");
    1786             : 
    1787           4 :     res = executeQueryOrDie(conn, "SELECT count(*) "
    1788             :                             "FROM pg_catalog.pg_replication_slots "
    1789             :                             "WHERE slot_type = 'logical' AND "
    1790             :                             "temporary IS FALSE;");
    1791             : 
    1792           4 :     if (PQntuples(res) != 1)
    1793           0 :         pg_fatal("could not count the number of logical replication slots");
    1794             : 
    1795           4 :     nslots_on_new = atoi(PQgetvalue(res, 0, 0));
    1796             : 
    1797           4 :     if (nslots_on_new)
    1798           0 :         pg_fatal("expected 0 logical replication slots but found %d",
    1799             :                  nslots_on_new);
    1800             : 
    1801           4 :     PQclear(res);
    1802             : 
    1803           4 :     res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
    1804             :                             "WHERE name IN ('wal_level', 'max_replication_slots') "
    1805             :                             "ORDER BY name DESC;");
    1806             : 
    1807           4 :     if (PQntuples(res) != 2)
    1808           0 :         pg_fatal("could not determine parameter settings on new cluster");
    1809             : 
    1810           4 :     wal_level = PQgetvalue(res, 0, 0);
    1811             : 
    1812           4 :     if (strcmp(wal_level, "logical") != 0)
    1813           0 :         pg_fatal("\"wal_level\" must be \"logical\" but is set to \"%s\"",
    1814             :                  wal_level);
    1815             : 
    1816           4 :     max_replication_slots = atoi(PQgetvalue(res, 1, 0));
    1817             : 
    1818           4 :     if (nslots_on_old > max_replication_slots)
    1819           2 :         pg_fatal("\"max_replication_slots\" (%d) must be greater than or equal to the number of "
    1820             :                  "logical replication slots (%d) on the old cluster",
    1821             :                  max_replication_slots, nslots_on_old);
    1822             : 
    1823           2 :     PQclear(res);
    1824           2 :     PQfinish(conn);
    1825             : 
    1826           2 :     check_ok();
    1827             : }
    1828             : 
    1829             : /*
    1830             :  * check_new_cluster_subscription_configuration()
    1831             :  *
    1832             :  * Verify that the max_active_replication_origins configuration specified is
    1833             :  * enough for creating the subscriptions. This is required to create the
    1834             :  * replication origin for each subscription.
    1835             :  */
    1836             : static void
    1837          20 : check_new_cluster_subscription_configuration(void)
    1838             : {
    1839             :     PGresult   *res;
    1840             :     PGconn     *conn;
    1841             :     int         max_active_replication_origins;
    1842             : 
    1843             :     /* Subscriptions and their dependencies can be migrated since PG17. */
    1844          20 :     if (GET_MAJOR_VERSION(old_cluster.major_version) < 1700)
    1845           0 :         return;
    1846             : 
    1847             :     /* Quick return if there are no subscriptions to be migrated. */
    1848          20 :     if (old_cluster.nsubs == 0)
    1849          16 :         return;
    1850             : 
    1851           4 :     prep_status("Checking for new cluster configuration for subscriptions");
    1852             : 
    1853           4 :     conn = connectToServer(&new_cluster, "template1");
    1854             : 
    1855           4 :     res = executeQueryOrDie(conn, "SELECT setting FROM pg_settings "
    1856             :                             "WHERE name = 'max_active_replication_origins';");
    1857             : 
    1858           4 :     if (PQntuples(res) != 1)
    1859           0 :         pg_fatal("could not determine parameter settings on new cluster");
    1860             : 
    1861           4 :     max_active_replication_origins = atoi(PQgetvalue(res, 0, 0));
    1862           4 :     if (old_cluster.nsubs > max_active_replication_origins)
    1863           2 :         pg_fatal("\"max_active_replication_origins\" (%d) must be greater than or equal to the number of "
    1864             :                  "subscriptions (%d) on the old cluster",
    1865             :                  max_active_replication_origins, old_cluster.nsubs);
    1866             : 
    1867           2 :     PQclear(res);
    1868           2 :     PQfinish(conn);
    1869             : 
    1870           2 :     check_ok();
    1871             : }
    1872             : 
    1873             : /*
    1874             :  * check_old_cluster_for_valid_slots()
    1875             :  *
    1876             :  * Verify that all the logical slots are valid and have consumed all the WAL
    1877             :  * before shutdown.
    1878             :  */
    1879             : static void
    1880          28 : check_old_cluster_for_valid_slots(void)
    1881             : {
    1882             :     char        output_path[MAXPGPATH];
    1883          28 :     FILE       *script = NULL;
    1884             : 
    1885          28 :     prep_status("Checking for valid logical replication slots");
    1886             : 
    1887          28 :     snprintf(output_path, sizeof(output_path), "%s/%s",
    1888             :              log_opts.basedir,
    1889             :              "invalid_logical_slots.txt");
    1890             : 
    1891         110 :     for (int dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
    1892             :     {
    1893          82 :         LogicalSlotInfoArr *slot_arr = &old_cluster.dbarr.dbs[dbnum].slot_arr;
    1894             : 
    1895          92 :         for (int slotnum = 0; slotnum < slot_arr->nslots; slotnum++)
    1896             :         {
    1897          10 :             LogicalSlotInfo *slot = &slot_arr->slots[slotnum];
    1898             : 
    1899             :             /* Is the slot usable? */
    1900          10 :             if (slot->invalid)
    1901             :             {
    1902           0 :                 if (script == NULL &&
    1903           0 :                     (script = fopen_priv(output_path, "w")) == NULL)
    1904           0 :                     pg_fatal("could not open file \"%s\": %m", output_path);
    1905             : 
    1906           0 :                 fprintf(script, "The slot \"%s\" is invalid\n",
    1907             :                         slot->slotname);
    1908             : 
    1909           0 :                 continue;
    1910             :             }
    1911             : 
    1912             :             /*
    1913             :              * Do additional check to ensure that all logical replication
    1914             :              * slots have consumed all the WAL before shutdown.
    1915             :              *
    1916             :              * Note: This can be satisfied only when the old cluster has been
    1917             :              * shut down, so we skip this for live checks.
    1918             :              */
    1919          10 :             if (!user_opts.live_check && !slot->caught_up)
    1920             :             {
    1921           6 :                 if (script == NULL &&
    1922           2 :                     (script = fopen_priv(output_path, "w")) == NULL)
    1923           0 :                     pg_fatal("could not open file \"%s\": %m", output_path);
    1924             : 
    1925           4 :                 fprintf(script,
    1926             :                         "The slot \"%s\" has not consumed the WAL yet\n",
    1927             :                         slot->slotname);
    1928             :             }
    1929             :         }
    1930             :     }
    1931             : 
    1932          28 :     if (script)
    1933             :     {
    1934           2 :         fclose(script);
    1935             : 
    1936           2 :         pg_log(PG_REPORT, "fatal");
    1937           2 :         pg_fatal("Your installation contains logical replication slots that cannot be upgraded.\n"
    1938             :                  "You can remove invalid slots and/or consume the pending WAL for other slots,\n"
    1939             :                  "and then restart the upgrade.\n"
    1940             :                  "A list of the problematic slots is in the file:\n"
    1941             :                  "    %s", output_path);
    1942             :     }
    1943             : 
    1944          26 :     check_ok();
    1945          26 : }
    1946             : 
    1947             : /*
    1948             :  * Callback function for processing results of query for
    1949             :  * check_old_cluster_subscription_state()'s UpgradeTask.  If the query returned
    1950             :  * any rows (i.e., the check failed), write the details to the report file.
    1951             :  */
    1952             : static void
    1953          78 : process_old_sub_state_check(DbInfo *dbinfo, PGresult *res, void *arg)
    1954             : {
    1955          78 :     UpgradeTaskReport *report = (UpgradeTaskReport *) arg;
    1956          78 :     int         ntups = PQntuples(res);
    1957          78 :     int         i_srsubstate = PQfnumber(res, "srsubstate");
    1958          78 :     int         i_subname = PQfnumber(res, "subname");
    1959          78 :     int         i_nspname = PQfnumber(res, "nspname");
    1960          78 :     int         i_relname = PQfnumber(res, "relname");
    1961             : 
    1962             :     AssertVariableIsOfType(&process_old_sub_state_check, UpgradeTaskProcessCB);
    1963             : 
    1964          78 :     if (ntups == 0)
    1965          76 :         return;
    1966             : 
    1967           2 :     if (report->file == NULL &&
    1968           0 :         (report->file = fopen_priv(report->path, "w")) == NULL)
    1969           0 :         pg_fatal("could not open file \"%s\": %m", report->path);
    1970             : 
    1971           4 :     for (int i = 0; i < ntups; i++)
    1972           2 :         fprintf(report->file, "The table sync state \"%s\" is not allowed for database:\"%s\" subscription:\"%s\" schema:\"%s\" relation:\"%s\"\n",
    1973             :                 PQgetvalue(res, i, i_srsubstate),
    1974             :                 dbinfo->db_name,
    1975             :                 PQgetvalue(res, i, i_subname),
    1976             :                 PQgetvalue(res, i, i_nspname),
    1977             :                 PQgetvalue(res, i, i_relname));
    1978             : }
    1979             : 
    1980             : /*
    1981             :  * check_old_cluster_subscription_state()
    1982             :  *
    1983             :  * Verify that the replication origin corresponding to each of the
    1984             :  * subscriptions are present and each of the subscribed tables is in
    1985             :  * 'i' (initialize) or 'r' (ready) state.
    1986             :  */
    1987             : static void
    1988          26 : check_old_cluster_subscription_state(void)
    1989             : {
    1990          26 :     UpgradeTask *task = upgrade_task_create();
    1991             :     UpgradeTaskReport report;
    1992             :     const char *query;
    1993             :     PGresult   *res;
    1994             :     PGconn     *conn;
    1995             :     int         ntup;
    1996             : 
    1997          26 :     prep_status("Checking for subscription state");
    1998             : 
    1999          26 :     report.file = NULL;
    2000          26 :     snprintf(report.path, sizeof(report.path), "%s/%s",
    2001             :              log_opts.basedir,
    2002             :              "subs_invalid.txt");
    2003             : 
    2004             :     /*
    2005             :      * Check that all the subscriptions have their respective replication
    2006             :      * origin.  This check only needs to run once.
    2007             :      */
    2008          26 :     conn = connectToServer(&old_cluster, old_cluster.dbarr.dbs[0].db_name);
    2009          26 :     res = executeQueryOrDie(conn,
    2010             :                             "SELECT d.datname, s.subname "
    2011             :                             "FROM pg_catalog.pg_subscription s "
    2012             :                             "LEFT OUTER JOIN pg_catalog.pg_replication_origin o "
    2013             :                             "  ON o.roname = 'pg_' || s.oid "
    2014             :                             "INNER JOIN pg_catalog.pg_database d "
    2015             :                             "  ON d.oid = s.subdbid "
    2016             :                             "WHERE o.roname IS NULL;");
    2017          26 :     ntup = PQntuples(res);
    2018          28 :     for (int i = 0; i < ntup; i++)
    2019             :     {
    2020           2 :         if (report.file == NULL &&
    2021           2 :             (report.file = fopen_priv(report.path, "w")) == NULL)
    2022           0 :             pg_fatal("could not open file \"%s\": %m", report.path);
    2023           2 :         fprintf(report.file, "The replication origin is missing for database:\"%s\" subscription:\"%s\"\n",
    2024             :                 PQgetvalue(res, i, 0),
    2025             :                 PQgetvalue(res, i, 1));
    2026             :     }
    2027          26 :     PQclear(res);
    2028          26 :     PQfinish(conn);
    2029             : 
    2030             :     /*
    2031             :      * We don't allow upgrade if there is a risk of dangling slot or origin
    2032             :      * corresponding to initial sync after upgrade.
    2033             :      *
    2034             :      * A slot/origin not created yet refers to the 'i' (initialize) state,
    2035             :      * while 'r' (ready) state refers to a slot/origin created previously but
    2036             :      * already dropped. These states are supported for pg_upgrade. The other
    2037             :      * states listed below are not supported:
    2038             :      *
    2039             :      * a) SUBREL_STATE_DATASYNC: A relation upgraded while in this state would
    2040             :      * retain a replication slot, which could not be dropped by the sync
    2041             :      * worker spawned after the upgrade because the subscription ID used for
    2042             :      * the slot name won't match anymore.
    2043             :      *
    2044             :      * b) SUBREL_STATE_SYNCDONE: A relation upgraded while in this state would
    2045             :      * retain the replication origin when there is a failure in tablesync
    2046             :      * worker immediately after dropping the replication slot in the
    2047             :      * publisher.
    2048             :      *
    2049             :      * c) SUBREL_STATE_FINISHEDCOPY: A tablesync worker spawned to work on a
    2050             :      * relation upgraded while in this state would expect an origin ID with
    2051             :      * the OID of the subscription used before the upgrade, causing it to
    2052             :      * fail.
    2053             :      *
    2054             :      * d) SUBREL_STATE_SYNCWAIT, SUBREL_STATE_CATCHUP and
    2055             :      * SUBREL_STATE_UNKNOWN: These states are not stored in the catalog, so we
    2056             :      * need not allow these states.
    2057             :      */
    2058          26 :     query = "SELECT r.srsubstate, s.subname, n.nspname, c.relname "
    2059             :         "FROM pg_catalog.pg_subscription_rel r "
    2060             :         "LEFT JOIN pg_catalog.pg_subscription s"
    2061             :         "   ON r.srsubid = s.oid "
    2062             :         "LEFT JOIN pg_catalog.pg_class c"
    2063             :         "   ON r.srrelid = c.oid "
    2064             :         "LEFT JOIN pg_catalog.pg_namespace n"
    2065             :         "   ON c.relnamespace = n.oid "
    2066             :         "WHERE r.srsubstate NOT IN ('i', 'r') "
    2067             :         "ORDER BY s.subname";
    2068             : 
    2069          26 :     upgrade_task_add_step(task, query, process_old_sub_state_check,
    2070             :                           true, &report);
    2071             : 
    2072          26 :     upgrade_task_run(task, &old_cluster);
    2073          26 :     upgrade_task_free(task);
    2074             : 
    2075          26 :     if (report.file)
    2076             :     {
    2077           2 :         fclose(report.file);
    2078           2 :         pg_log(PG_REPORT, "fatal");
    2079           2 :         pg_fatal("Your installation contains subscriptions without origin or having relations not in i (initialize) or r (ready) state.\n"
    2080             :                  "You can allow the initial sync to finish for all relations and then restart the upgrade.\n"
    2081             :                  "A list of the problematic subscriptions is in the file:\n"
    2082             :                  "    %s", report.path);
    2083             :     }
    2084             :     else
    2085          24 :         check_ok();
    2086          24 : }

Generated by: LCOV version 1.14