Skip to content

Commit 566a3d8

Browse files
jackscodemonkeyyjhjstz
authored andcommitted
Analyzedb: Add materialized views to list of tables to be analyzed (#16410)
Previously, analyzedb did not include materialized views when preparing the list of tables to be analyzed. This caused refreshed materialized views to have poor performance until analyzed. Solution: Updated the SQL statements in analyzedb that collect the list of tables to also include relkind='m' from pg_class. This will add materialized views to the list of hash tables to be analyzed for each run.
1 parent 4ac70ed commit 566a3d8

File tree

3 files changed

+35
-6
lines changed

3 files changed

+35
-6
lines changed

gpMgmt/bin/analyzedb

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ NUM_REPORTS_TO_SAVE = 3
5252
GET_ALL_DATA_TABLES_SQL = """
5353
select n.nspname as schemaname, c.relname as tablename
5454
from pg_class c, pg_namespace n
55-
where c.relnamespace = n.oid and c.relkind='r'::char
55+
where c.relnamespace = n.oid and (c.relkind='r'::char or c.relkind = 'm'::char)
5656
and (c.relnamespace >= 16384 or n.nspname = 'public' or n.nspname = 'pg_catalog')
5757
and c.oid not in (select ftrelid from pg_foreign_table)
5858
"""
5959

6060
GET_VALID_DATA_TABLES_SQL = """
6161
select n.nspname as schemaname, c.relname as tablename from pg_class c, pg_namespace n where
62-
c.relnamespace = n.oid and c.oid in (%s) and c.relkind='r'::char and (c.relnamespace >= 16384 or n.nspname = 'public' or n.nspname = 'pg_catalog') and c.oid not in (select ftrelid from pg_foreign_table)
62+
c.relnamespace = n.oid and c.oid in (%s) and (c.relkind='r'::char or c.relkind = 'm'::char) and (c.relnamespace >= 16384 or n.nspname = 'public' or n.nspname = 'pg_catalog') and c.oid not in (select ftrelid from pg_foreign_table)
6363
"""
6464

6565
GET_REQUESTED_AO_DATA_TABLE_INFO_SQL = """
@@ -86,7 +86,7 @@ GET_REQUESTED_LAST_OP_INFO_SQL = """
8686
GET_ALL_DATA_TABLES_IN_SCHEMA_SQL = """
8787
select n.nspname as schemaname, c.relname as tablename
8888
from pg_class c, pg_namespace n
89-
where c.relnamespace = n.oid and c.relkind='r'::char
89+
where c.relnamespace = n.oid and (c.relkind='r'::char or c.relkind = 'm'::char)
9090
and (c.relnamespace >= 16384 or n.nspname = 'public' or n.nspname = 'pg_catalog')
9191
and c.oid not in (select ftrelid from pg_foreign_table)
9292
and n.nspname = '%s'
@@ -112,7 +112,7 @@ and c.relkind = 'p' and c.relispartition
112112
GET_REQUESTED_NON_AO_TABLES_SQL = """
113113
select n.nspname as schemaname, c.relname as tablename
114114
from pg_class c, pg_namespace n
115-
where c.relnamespace = n.oid and c.relkind='r'::char
115+
where c.relnamespace = n.oid and (c.relkind='r'::char or c.relkind = 'm'::char)
116116
and (c.relnamespace >= 16384 or n.nspname = 'public' or n.nspname = 'pg_catalog')
117117
and c.oid not in (select ftrelid from pg_foreign_table)
118118
and c.oid not in (select relid from pg_appendonly)
@@ -554,7 +554,7 @@ class AnalyzeDb(Operation):
554554
At the same time, parse the requested columns and populate the col_dict.
555555
If a requested table is partitioned, expand all the leaf partitions.
556556
"""
557-
logger.info("Getting and verifying input tables...")
557+
logger.info("Getting and verifying input tables and materialized views...")
558558
if self.single_table:
559559

560560
# Check that the table name given on the command line is schema-qualified.

gpMgmt/test/behave/mgmt_utils/analyzedb.feature

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,3 +1775,18 @@ Feature: Incrementally analyze the database
17751775
And the user runs "dropdb schema_with_temp_table"
17761776
And the user drops the named connection "default"
17771777

1778+
Scenario: analyzedb can handle the table name with special utf-8 characters.
1779+
Given database "special_encoding_db" is dropped and recreated
1780+
And the user connects to "special_encoding_db" with named connection "default"
1781+
And the user executes "CREATE TEMP TABLE spiegelungssätze (c1 int) DISTRIBUTED BY (c1)" with named connection "default"
1782+
When the user runs "analyzedb -a -d special_encoding_db"
1783+
Then analyzedb should return a return code of 0
1784+
1785+
Scenario: analyzedb finds materialized views
1786+
Given a materialized view "public.mv_test_view" exists on table "pg_class"
1787+
And the user runs "analyzedb -a -d incr_analyze"
1788+
Then analyzedb should print "-public.mv_test_view" to stdout
1789+
And the user runs "analyzedb -a -s public -d incr_analyze"
1790+
Then analyzedb should print "-public.mv_test_view" to stdout
1791+
And the user runs "analyzedb -a -t public.mv_test_view -d incr_analyze"
1792+
Then analyzedb should print "-public.mv_test_view" to stdout

gpMgmt/test/behave/mgmt_utils/steps/analyzedb_mgmt_utils.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
"""
3838

3939

40-
4140
@given('there is a regular "{storage_type}" table "{tablename}" with column name list "{col_name_list}" and column type list "{col_type_list}" in schema "{schemaname}"')
4241
def impl(context, storage_type, tablename, col_name_list, col_type_list, schemaname):
4342
schemaname_no_quote = schemaname
@@ -98,6 +97,13 @@ def impl(context, view_name, table_name):
9897
create_view_on_table(context.conn, view_name, table_name)
9998

10099

100+
@given('a materialized view "{view_name}" exists on table "{table_name}"')
101+
def impl(context, view_name, table_name):
102+
with closing(dbconn.connect(dbconn.DbURL(dbname=context.dbname))) as conn:
103+
create_materialized_view_on_table_in_schema(context.conn, viewname=view_name,
104+
tablename=table_name)
105+
106+
101107
@given('"{qualified_table}" appears in the latest state files')
102108
@then('"{qualified_table}" should appear in the latest state files')
103109
def impl(context, qualified_table):
@@ -462,3 +468,11 @@ def create_view_on_table(conn, viewname, tablename):
462468
" AS SELECT * FROM " + tablename
463469
dbconn.execSQL(conn, query)
464470
conn.commit()
471+
472+
473+
def create_materialized_view_on_table_in_schema(conn, tablename, viewname):
474+
query = "DROP MATERIALIZED VIEW IF EXISTS " + viewname + ";" \
475+
"CREATE MATERIALIZED VIEW " + viewname + \
476+
" AS SELECT * FROM " + tablename
477+
dbconn.execSQL(conn, query)
478+
conn.commit()

0 commit comments

Comments
 (0)