From ae2b1b8c426698bb7142f9f02e4cf08295e9dd73 Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@otacoo.com>
Date: Tue, 9 Dec 2014 16:40:39 +0900
Subject: [PATCH] Fix REINDEX SCHEMA ignoring matviews

The key scan used was using a filter on relation relkind, but that's not
actually necessary as a filter is applied when building the list of OIDs
reindexed.
---
 src/backend/commands/indexcmds.c           |  8 ++------
 src/test/regress/expected/create_index.out | 18 +++++++++---------
 src/test/regress/sql/create_index.sql      |  6 +++---
 3 files changed, 14 insertions(+), 18 deletions(-)

diff --git a/src/backend/commands/indexcmds.c b/src/backend/commands/indexcmds.c
index a3e8a15..9b07216 100644
--- a/src/backend/commands/indexcmds.c
+++ b/src/backend/commands/indexcmds.c
@@ -1867,16 +1867,12 @@ ReindexObject(const char *objectName, ReindexObjectType objectKind)
 	 */
 	if (objectKind == REINDEX_OBJECT_SCHEMA)
 	{
-		scan_keys = palloc(sizeof(ScanKeyData) * 2);
+		scan_keys = palloc(sizeof(ScanKeyData));
 		ScanKeyInit(&scan_keys[0],
 					Anum_pg_class_relnamespace,
 					BTEqualStrategyNumber, F_OIDEQ,
 					ObjectIdGetDatum(objectOid));
-		ScanKeyInit(&scan_keys[1],
-					Anum_pg_class_relkind,
-					BTEqualStrategyNumber, F_CHAREQ,
-					'r');
-		num_keys = 2;
+		num_keys = 1;
 	}
 	else
 		num_keys = 0;
diff --git a/src/test/regress/expected/create_index.out b/src/test/regress/expected/create_index.out
index ebac939..abffe65 100644
--- a/src/test/regress/expected/create_index.out
+++ b/src/test/regress/expected/create_index.out
@@ -2837,12 +2837,12 @@ explain (costs off)
 REINDEX SCHEMA schema_to_reindex; -- failure, schema does not exist
 ERROR:  schema "schema_to_reindex" does not exist
 CREATE SCHEMA schema_to_reindex;
-CREATE TABLE schema_to_reindex.table1(col1 SERIAL PRIMARY KEY);
-CREATE TABLE schema_to_reindex.table2(col1 SERIAL PRIMARY KEY, col2 VARCHAR(100) NOT NULL);
-CREATE INDEX ON schema_to_reindex.table2(col2);
+CREATE TABLE schema_to_reindex.table(col1 SERIAL PRIMARY KEY);
+CREATE MATERIALIZED VIEW schema_to_reindex.matview AS SELECT col1 FROM schema_to_reindex.table;
+CREATE INDEX ON schema_to_reindex.matview(col1);
 REINDEX SCHEMA schema_to_reindex;
-NOTICE:  table "schema_to_reindex.table1" was reindexed
-NOTICE:  table "schema_to_reindex.table2" was reindexed
+NOTICE:  table "schema_to_reindex.table" was reindexed
+NOTICE:  table "schema_to_reindex.matview" was reindexed
 BEGIN;
 REINDEX SCHEMA schema_to_reindex; -- failure, cannot run in a transaction
 ERROR:  REINDEX SCHEMA cannot run inside a transaction block
@@ -2852,13 +2852,13 @@ CREATE ROLE reindexuser login;
 SET SESSION ROLE user_reindex;
 ERROR:  role "user_reindex" does not exist
 REINDEX SCHEMA schema_to_reindex;
-NOTICE:  table "schema_to_reindex.table1" was reindexed
-NOTICE:  table "schema_to_reindex.table2" was reindexed
+NOTICE:  table "schema_to_reindex.table" was reindexed
+NOTICE:  table "schema_to_reindex.matview" was reindexed
 -- Clean up
 RESET ROLE;
 DROP ROLE user_reindex;
 ERROR:  role "user_reindex" does not exist
 DROP SCHEMA schema_to_reindex CASCADE;
 NOTICE:  drop cascades to 2 other objects
-DETAIL:  drop cascades to table schema_to_reindex.table1
-drop cascades to table schema_to_reindex.table2
+DETAIL:  drop cascades to table schema_to_reindex."table"
+drop cascades to materialized view schema_to_reindex.matview
diff --git a/src/test/regress/sql/create_index.sql b/src/test/regress/sql/create_index.sql
index 1cd57da..a5b3403 100644
--- a/src/test/regress/sql/create_index.sql
+++ b/src/test/regress/sql/create_index.sql
@@ -970,9 +970,9 @@ explain (costs off)
 --
 REINDEX SCHEMA schema_to_reindex; -- failure, schema does not exist
 CREATE SCHEMA schema_to_reindex;
-CREATE TABLE schema_to_reindex.table1(col1 SERIAL PRIMARY KEY);
-CREATE TABLE schema_to_reindex.table2(col1 SERIAL PRIMARY KEY, col2 VARCHAR(100) NOT NULL);
-CREATE INDEX ON schema_to_reindex.table2(col2);
+CREATE TABLE schema_to_reindex.table(col1 SERIAL PRIMARY KEY);
+CREATE MATERIALIZED VIEW schema_to_reindex.matview AS SELECT col1 FROM schema_to_reindex.table;
+CREATE INDEX ON schema_to_reindex.matview(col1);
 REINDEX SCHEMA schema_to_reindex;
 BEGIN;
 REINDEX SCHEMA schema_to_reindex; -- failure, cannot run in a transaction
-- 
2.2.0

