This is an automated email from the ASF dual-hosted git repository.

reshke pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/cloudberry.git

commit 8e490bc213d0230d6bd32234b2b09789c7da27db
Author: Brent Doil <[email protected]>
AuthorDate: Mon Apr 1 14:53:10 2024 -0400

    Update check_for_appendonly_materialized_view_with_relfrozenxid
    
    Move function to check_gp.c and refactor it to match the other
    checks.
---
 src/bin/pg_upgrade/check.c              | 119 --------------------
 src/bin/pg_upgrade/greenplum/check_gp.c | 185 ++++++++++++++++++++++++++++++++
 2 files changed, 185 insertions(+), 119 deletions(-)

diff --git a/src/bin/pg_upgrade/check.c b/src/bin/pg_upgrade/check.c
index 9745f8e0aa..1cf10dace1 100644
--- a/src/bin/pg_upgrade/check.c
+++ b/src/bin/pg_upgrade/check.c
@@ -38,7 +38,6 @@ static void check_for_cluster_key_failure(ClusterInfo 
*cluster);
 static void check_for_new_tablespace_dir(ClusterInfo *new_cluster);
 static void check_for_user_defined_encoding_conversions(ClusterInfo *cluster);
 static char *get_canonical_locale_name(int category, const char *locale);
-static void 
check_for_appendonly_materialized_view_with_relfrozenxid(ClusterInfo *cluster);
 
 /*
  * fix_path_separator
@@ -200,10 +199,6 @@ check_and_dump_old_cluster(bool live_check, char 
**sequence_script_file_name)
                old_9_3_check_for_line_data_type_usage(&old_cluster);
 #endif
 
-       /* For now, the issue exists only for Greenplum 6.x/PostgreSQL 9.4 */
-       if (GET_MAJOR_VERSION(old_cluster.major_version) == 904)
-               
check_for_appendonly_materialized_view_with_relfrozenxid(&old_cluster);
-
        teardown_GPDB6_data_type_checks(&old_cluster);
 
 dump_old_cluster:
@@ -1655,117 +1650,3 @@ get_canonical_locale_name(int category, const char 
*locale)
 
        return res;
 }
-
-/* Check for any materialized view of append only mode with relfrozenxid != 0
- *
- * A materialized view of append only mode must have invalid relfrozenxid (0).
- * However, some views might have valid relfrozenxid due to a known code issue.
- * We need to check the problematical view before upgrading.
- * The problem can be fixed by issuing "REFRESH MATERIALIZED VIEW <viewname>"
- * with latest code.
- * See the PR for details:
- *
- * https://github.com/greenplum-db/gpdb/pull/11662/
- */
-static void
-check_for_appendonly_materialized_view_with_relfrozenxid(ClusterInfo *cluster)
-{
-       FILE            *script = NULL;
-       char            output_path[MAXPGPATH];
-       bool            found = false;
-
-       prep_status("Checking for appendonly materialized view with 
relfrozenxid");
-
-       snprintf(output_path,
-                               sizeof(output_path),
-                               
"appendonly_materialized_view_with_relfrozenxid.txt");
-       if (script == NULL && (script = fopen_priv(output_path, "w")) == NULL)
-       {
-               pg_fatal("could not open file \"%s\": %s\n",
-                                       output_path,
-                                       strerror(errno));
-       }
-
-       for (int dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
-       {
-               DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
-               PGconn     *conn = connectToServer(cluster, active_db->db_name);
-               PGresult   *res;
-               int                     ntups = 0, i_relname = 0, i_relfxid = 
0, i_nspname = 0;
-
-               fprintf(script, "Checking database: %s\n", active_db->db_name);
-               if (conn == NULL)
-               {
-                       pg_fatal("Failed to connect to database %s\n", 
active_db->db_name);
-               }
-
-               // Detect any materialized view of append only mode (relstorage 
is
-               // RELSTORAGE_AOROWS or RELSTORAGE_AOCOLS) with relfrozenxid != 0
-               res = executeQueryOrDie(conn,
-                                                               "select 
tb.relname, tb.relfrozenxid, tbsp.nspname "
-                                                               " from 
pg_catalog.pg_class tb "
-                                                               " left join 
pg_catalog.pg_namespace tbsp "
-                                                               " on 
tb.relnamespace = tbsp.oid "
-                                                               " where 
tb.relkind = 'm' "
-                                                               " and 
(tb.relstorage = 'a' or tb.relstorage = 'c') "
-                                                               " and 
tb.relfrozenxid::text <> '0';");
-               if (res == 0)
-               {
-                       pg_fatal("Failed to query pg_catalog.pg_class on 
database \"%s\"\n",
-                                        active_db->db_name);
-               }
-
-               ntups = PQntuples(res);
-               if (ntups == 0)
-               {
-                       fprintf(script, "No problematical view detected.\n\n");
-               }
-               else
-               {
-                       found = true;
-                       i_relname = PQfnumber(res, "relname");
-                       i_relfxid = PQfnumber(res, "relfrozenxid");
-                       i_nspname = PQfnumber(res, "nspname");
-                       for (int rowno = 0; rowno < ntups; rowno++)
-                       {
-                               fprintf(script,
-                                               "Detected view: %s, 
relfrozenxid: %s\n"
-                                               "Try to fix it by issuing 
\"REFRESH MATERIALIZED VIEW %s.%s\"\n",
-                                               PQgetvalue(res, rowno, 
i_relname),
-                                               PQgetvalue(res, rowno, 
i_relfxid),
-                                               PQgetvalue(res, rowno, 
i_nspname),
-                                               PQgetvalue(res, rowno, 
i_relname));
-                       }
-                       fprintf(script, "%d problematical view(s) 
detected.\n\n", ntups);
-               }
-               PQclear(res);
-
-               PQfinish(conn);
-       }
-
-       if(found)
-       {
-               fprintf(script,
-                               "Note: A materialized view of append only mode 
must have invalid\n"
-                               "relfrozenxid (0). However, view(s) with valid 
relfrozenxid was\n"
-                               "detected.\n"
-                               "Try to fix it by issuing \"REFRESH 
MATERIALIZED VIEW "
-                               "<schemaname>.<viewname>\"\n"
-                               "with latest GPDB 6 release and run the 
upgrading again.\n");
-       }
-
-       if (script)
-               fclose(script);
-
-       if(found)
-       {
-               gp_fatal_log(
-                               "| Detected appendonly materialized view with 
incorrect relfrozenxid.\n"
-                               "| A list of the problem materialized views are 
in the file:\n"
-                               "| %s\n\n", output_path);
-       }
-       else
-       {
-               check_ok();
-       }
-}
diff --git a/src/bin/pg_upgrade/greenplum/check_gp.c 
b/src/bin/pg_upgrade/greenplum/check_gp.c
index b724051a23..7b95841c09 100644
--- a/src/bin/pg_upgrade/greenplum/check_gp.c
+++ b/src/bin/pg_upgrade/greenplum/check_gp.c
@@ -31,6 +31,8 @@ static void check_views_with_removed_operators(void);
 static void check_views_with_removed_functions(void);
 static void check_views_with_removed_types(void);
 static void check_for_disallowed_pg_operator(void);
+static void check_for_ao_matview_with_relfrozenxid(ClusterInfo *cluster);
+static void check_views_with_changed_function_signatures(void);
 
 /*
  *     check_greenplum
@@ -55,6 +57,8 @@ check_greenplum(void)
        check_views_with_removed_functions();
        check_views_with_removed_types();
        check_for_disallowed_pg_operator();
+       check_views_with_changed_function_signatures();
+       check_for_ao_matview_with_relfrozenxid(&old_cluster);
 }
 
 /*
@@ -1157,3 +1161,184 @@ check_for_disallowed_pg_operator(void)
        else
                check_ok();
 }
+
+/* Check for any AO materialized views with relfrozenxid != 0
+ *
+ * An AO materialized view must have invalid relfrozenxid (0).
+ * However, some views might have valid relfrozenxid due to a known code issue.
+ * We need to check for problematic views before upgrading.
+ * The problem can be fixed by issuing "REFRESH MATERIALIZED VIEW <viewname>"
+ * with latest code.
+ * See the PR for details:
+ *
+ * https://github.com/greenplum-db/gpdb/pull/11662/
+ */
+static void
+check_for_ao_matview_with_relfrozenxid(ClusterInfo *cluster)
+{
+       char  output_path[MAXPGPATH];
+       FILE *script = NULL;
+       int   dbnum;
+
+       /* For now, the issue exists only for Greenplum 6.x/PostgreSQL 9.4 */
+       if (GET_MAJOR_VERSION(old_cluster.major_version) != 904)
+               return;
+
+       prep_status("Checking for AO materialized views with relfrozenxid");
+
+       snprintf(output_path, sizeof(output_path), "%s/%s",
+                        log_opts.basedir,
+                               "ao_materialized_view_with_relfrozenxid.txt");
+
+       for (dbnum = 0; dbnum < cluster->dbarr.ndbs; dbnum++)
+       {
+               PGresult   *res;
+               bool            db_used = false;
+               int                     ntups;
+               int                     rowno;
+               int                     i_nspname,
+                                       i_relname;
+               DbInfo     *active_db = &cluster->dbarr.dbs[dbnum];
+               PGconn     *conn = connectToServer(cluster, active_db->db_name);
+
+               /*
+                * Detect any materialized view where relstorage is
+                * RELSTORAGE_AOROWS or RELSTORAGE_AOCOLS with relfrozenxid != 0
+                */
+               res = executeQueryOrDie(conn,
+                                                               "select 
tb.relname, tb.relfrozenxid, tbsp.nspname "
+                                                               " from 
pg_catalog.pg_class tb "
+                                                               " left join 
pg_catalog.pg_namespace tbsp "
+                                                               " on 
tb.relnamespace = tbsp.oid "
+                                                               " where 
tb.relkind = 'm' "
+                                                               " and 
(tb.relstorage = 'a' or tb.relstorage = 'c') "
+                                                               " and 
tb.relfrozenxid::text <> '0';");
+
+               ntups = PQntuples(res);
+               i_nspname = PQfnumber(res, "nspname");
+               i_relname = PQfnumber(res, "relname");
+               for (rowno = 0; rowno < ntups; rowno++)
+               {
+                       if (script == NULL && (script = fopen_priv(output_path, 
"w")) == NULL)
+                               pg_fatal("could not open file \"%s\": %s\n",
+                                                       output_path, 
strerror(errno));
+                       if (!db_used)
+                       {
+                               fprintf(script, "In database: %s\n", 
active_db->db_name);
+                               db_used = true;
+                       }
+
+                       fprintf(script, "  %s.%s\n",
+                                       PQgetvalue(res, rowno, i_nspname),
+                                       PQgetvalue(res, rowno, i_relname));
+               }
+
+               PQclear(res);
+
+               PQfinish(conn);
+       }
+
+       if(script)
+       {
+               fclose(script);
+               gp_fatal_log(
+                               "| Detected AO materialized views with 
incorrect relfrozenxid.\n"
+                               "| Issue \"REFRESH MATERIALIZED VIEW 
<schemaname>.<viewname> on the problem\n"
+                               "| views to resolve the issue.\n"
+                               "| A list of the problem materialized views are 
in the file:\n"
+                               "| %s\n\n", output_path);
+       }
+       else
+               check_ok();
+}
+
+static void
+check_views_with_changed_function_signatures()
+{
+       if (GET_MAJOR_VERSION(old_cluster.major_version) > 904)
+               return;
+
+       char  output_path[MAXPGPATH];
+       FILE *script = NULL;
+       bool  found = false;
+       int   dbnum;
+       int   i_viewname;
+
+       prep_status("Checking for views with functions having changed 
signatures");
+
+       snprintf(output_path, sizeof(output_path), "%s/%s",
+                        log_opts.basedir, 
"views_with_changed_function_signatures.txt");
+
+       for (dbnum = 0; dbnum < old_cluster.dbarr.ndbs; dbnum++)
+       {
+               PGresult   *res;
+               int                     ntups;
+               int                     rowno;
+               DbInfo     *active_db = &old_cluster.dbarr.dbs[dbnum];
+               PGconn     *conn;
+               bool            db_used = false;
+
+               conn = connectToServer(&old_cluster, active_db->db_name);
+               PQclear(executeQueryOrDie(conn, "SET search_path TO 
'public';"));
+
+               /*
+                * Disabling track_counts results in a large performance 
improvement of
+                * several orders of magnitude when walking the views. This is 
because
+                * calling try_relation_open to get a handle of the view calls
+                * pgstat_initstats which has been profiled to be very 
expensive. For
+                * our purposes, this is not needed and disabled for 
performance.
+                */
+               PQclear(executeQueryOrDie(conn, "SET track_counts TO off;"));
+
+               /* Install check support function */
+               PQclear(executeQueryOrDie(conn,
+                                                                 "CREATE OR 
REPLACE FUNCTION "
+                                                                 
"view_has_changed_function_signatures(OID) "
+                                                                 "RETURNS BOOL 
"
+                                                                 "AS 
'$libdir/pg_upgrade_support' "
+                                                                 "LANGUAGE C 
STRICT;"));
+               res = executeQueryOrDie(conn,
+                                                               "SELECT 
quote_ident(n.nspname) || '.' || quote_ident(c.relname) AS badviewname "
+                                                               "FROM pg_class 
c JOIN pg_namespace n on c.relnamespace=n.oid "
+                                                               "WHERE 
c.relkind = 'v' "
+                                                               "AND c.oid >= 
16384 "
+                                                               "AND 
view_has_changed_function_signatures(c.oid) = TRUE;");
+
+               PQclear(executeQueryOrDie(conn, "DROP FUNCTION 
view_has_changed_function_signatures(OID);"));
+               PQclear(executeQueryOrDie(conn, "SET search_path to 
'pg_catalog';"));
+               PQclear(executeQueryOrDie(conn, "RESET track_counts;"));
+
+               ntups = PQntuples(res);
+               i_viewname = PQfnumber(res, "badviewname");
+               for (rowno = 0; rowno < ntups; rowno++)
+               {
+                       found = true;
+                       if (script == NULL && (script = fopen(output_path, 
"w")) == NULL)
+                               pg_fatal("Could not create necessary file:  
%s\n", output_path);
+                       if (!db_used)
+                       {
+                               fprintf(script, "Database: %s\n", 
active_db->db_name);
+                               db_used = true;
+                       }
+                       fprintf(script, "  %s\n", PQgetvalue(res, rowno, 
i_viewname));
+               }
+
+               PQclear(res);
+               PQfinish(conn);
+       }
+
+       if (found)
+       {
+               pg_log(PG_REPORT, "fatal\n");
+               gp_fatal_log(
+                       "| Your installation contains views using "
+                       "| functions with changed signatures.\n"
+                       "| These functions are present on the target version 
but with "
+                       "| different arguments and/or return values.\n"
+                       "| These views must be updated to use functions 
supported in the\n"
+                       "| target version or removed before upgrade can 
continue. A list\n"
+                       "| of the problem views is in the file:\n\t%s\n\n", 
output_path);
+       }
+       else
+               check_ok();
+}


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to