From 5cd6f020d05f85b10426247b29a1b93e0e9ed1cf Mon Sep 17 00:00:00 2001
From: Michael Paquier <michael@paquier.xyz>
Date: Fri, 26 Jul 2019 12:23:11 +0900
Subject: [PATCH 2/2] Forbid --index with --jobs

---
 doc/src/sgml/ref/reindexdb.sgml    |  6 ++----
 src/bin/scripts/reindexdb.c        | 21 ++++++++++++---------
 src/bin/scripts/t/090_reindexdb.pl |  5 ++++-
 3 files changed, 18 insertions(+), 14 deletions(-)

diff --git a/doc/src/sgml/ref/reindexdb.sgml b/doc/src/sgml/ref/reindexdb.sgml
index 477b77c6e9..5e21fbcc4e 100644
--- a/doc/src/sgml/ref/reindexdb.sgml
+++ b/doc/src/sgml/ref/reindexdb.sgml
@@ -183,10 +183,8 @@ PostgreSQL documentation
         setting is high enough to accommodate all connections.
        </para>
        <para>
-        Note that this option is ignored with the <option>--index</option>
-        option to prevent deadlocks when processing multiple indexes from
-        the same relation, and incompatible with the <option>--system</option>
-        option.
+        Note that this option is incompatible with the <option>--index</option>
+        and <option>--system</option> options.
        </para>
       </listitem>
      </varlistentry>
diff --git a/src/bin/scripts/reindexdb.c b/src/bin/scripts/reindexdb.c
index a877689eec..ecd4490ecf 100644
--- a/src/bin/scripts/reindexdb.c
+++ b/src/bin/scripts/reindexdb.c
@@ -267,6 +267,17 @@ main(int argc, char *argv[])
 	}
 	else
 	{
+		/*
+		 * Index-level REINDEX is not supported with multiple jobs as we
+		 * cannot control the concurrent processing of multiple indexes
+		 * depending on the same relation.
+		 */
+		if (concurrentCons > 1 && indexes.head != NULL)
+		{
+			pg_log_error("cannot use multiple jobs to reindex indexes");
+			exit(1);
+		}
+
 		if (dbname == NULL)
 		{
 			if (getenv("PGDATABASE"))
@@ -283,17 +294,9 @@ main(int argc, char *argv[])
 								 echo, verbose, concurrently, concurrentCons);
 
 		if (indexes.head != NULL)
-		{
-			/*
-			 * An index list cannot be processed by multiple connections, as
-			 * it could cause conflicts if reindexing multiple indexes from
-			 * the same table.  We simply ignore the passed number of jobs if
-			 * any.
-			 */
 			reindex_one_database(dbname, REINDEX_INDEX, &indexes, host,
 								 port, username, prompt_password, progname,
 								 echo, verbose, concurrently, 1);
-		}
 
 		if (tables.head != NULL)
 			reindex_one_database(dbname, REINDEX_TABLE, &tables, host,
@@ -409,12 +412,12 @@ reindex_one_database(const char *dbname, ReindexType type,
 				break;
 
 			case REINDEX_SYSTEM:
+			case REINDEX_INDEX:
 				/* not supported */
 				Assert(false);
 				break;
 
 			case REINDEX_TABLE:
-			case REINDEX_INDEX:
 
 				/*
 				 * Fall through.  The list of items for indexes and tables is
diff --git a/src/bin/scripts/t/090_reindexdb.pl b/src/bin/scripts/t/090_reindexdb.pl
index bcd334eb8b..50144e9764 100644
--- a/src/bin/scripts/t/090_reindexdb.pl
+++ b/src/bin/scripts/t/090_reindexdb.pl
@@ -3,7 +3,7 @@ use warnings;
 
 use PostgresNode;
 use TestLib;
-use Test::More tests => 42;
+use Test::More tests => 43;
 
 program_help_ok('reindexdb');
 program_version_ok('reindexdb');
@@ -92,6 +92,9 @@ $node->safe_psql(
 $node->command_fails(
 	[ 'reindexdb', '-j', '2', '-s', 'postgres' ],
 	'reindexdb cannot process system catalogs in parallel');
+$node->command_fails(
+	[ 'reindexdb', '-j', '2', '-i', 'i1', 'postgres' ],
+	'reindexdb cannot process indexes in parallel');
 $node->issues_sql_like(
 	[ 'reindexdb', '-j', '2', 'postgres' ],
 	qr/statement:\ REINDEX SYSTEM postgres;
-- 
2.22.0

