LCOV - differential code coverage report
Current view: top level - src/bin/pg_upgrade - check.c (source / functions) Coverage Total Hit UNC LBC UBC GBC GNC CBC DUB DCB
Current: c70b6db34ffeab48beef1fb4ce61bcad3772b8dd vs 06473f5a344df8c9594ead90a609b86f6724cff8 Lines: 54.1 % 677 366 9 21 281 3 27 336 6 11
Current Date: 2025-09-06 07:49:51 +0900 Functions: 66.7 % 39 26 2 11 6 20 1
Baseline: lcov-20250906-005545-baseline Branches: 45.7 % 324 148 13 3 160 3 27 118
Baseline Date: 2025-09-05 08:21:35 +0100 Line coverage date bins:
Legend: Lines:     hit not hit
Branches: + taken - not taken # not executed
(30,360] days: 40.2 % 343 138 9 21 175 1 27 110
(360..) days: 68.3 % 334 228 106 2 226
Function coverage date bins:
(30,360] days: 42.1 % 19 8 2 9 1 7
(360..) days: 90.0 % 20 18 2 5 13
Branch coverage date bins:
(30,360] days: 37.9 % 174 66 13 3 92 1 27 38
(360..) days: 54.7 % 150 82 68 2 80

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

Generated by: LCOV version 2.4-beta