@@ -46,7 +46,10 @@ static void reindex_one_database(ConnParams *cparams, ReindexType type,
4646static void reindex_all_databases (ConnParams * cparams ,
4747 const char * progname , bool echo ,
4848 bool quiet , bool verbose , bool concurrently ,
49- int concurrentCons , const char * tablespace );
49+ int concurrentCons , const char * tablespace ,
50+ bool syscatalog , SimpleStringList * schemas ,
51+ SimpleStringList * tables ,
52+ SimpleStringList * indexes );
5053static void run_reindex_command (PGconn * conn , ReindexType type ,
5154 const char * name , bool echo , bool verbose ,
5255 bool concurrently , bool async ,
@@ -203,62 +206,33 @@ main(int argc, char *argv[])
203206
204207 setup_cancel_handler (NULL );
205208
209+ if (concurrentCons > 1 )
210+ {
211+ /*
212+ * Index-level REINDEX is not supported with multiple jobs as we
213+ * cannot control the concurrent processing of multiple indexes
214+ * depending on the same relation.
215+ */
216+ if (indexes .head != NULL )
217+ pg_fatal ("cannot use multiple jobs to reindex indexes" );
218+
219+ if (syscatalog )
220+ pg_fatal ("cannot use multiple jobs to reindex system catalogs" );
221+ }
222+
206223 if (alldb )
207224 {
208225 if (dbname )
209226 pg_fatal ("cannot reindex all databases and a specific one at the same time" );
210- if (syscatalog )
211- pg_fatal ("cannot reindex all databases and system catalogs at the same time" );
212- if (schemas .head != NULL )
213- pg_fatal ("cannot reindex specific schema(s) in all databases" );
214- if (tables .head != NULL )
215- pg_fatal ("cannot reindex specific table(s) in all databases" );
216- if (indexes .head != NULL )
217- pg_fatal ("cannot reindex specific index(es) in all databases" );
218227
219228 cparams .dbname = maintenance_db ;
220229
221230 reindex_all_databases (& cparams , progname , echo , quiet , verbose ,
222- concurrently , concurrentCons , tablespace );
223- }
224- else if (syscatalog )
225- {
226- if (schemas .head != NULL )
227- pg_fatal ("cannot reindex specific schema(s) and system catalogs at the same time" );
228- if (tables .head != NULL )
229- pg_fatal ("cannot reindex specific table(s) and system catalogs at the same time" );
230- if (indexes .head != NULL )
231- pg_fatal ("cannot reindex specific index(es) and system catalogs at the same time" );
232-
233- if (concurrentCons > 1 )
234- pg_fatal ("cannot use multiple jobs to reindex system catalogs" );
235-
236- if (dbname == NULL )
237- {
238- if (getenv ("PGDATABASE" ))
239- dbname = getenv ("PGDATABASE" );
240- else if (getenv ("PGUSER" ))
241- dbname = getenv ("PGUSER" );
242- else
243- dbname = get_user_name_or_exit (progname );
244- }
245-
246- cparams .dbname = dbname ;
247-
248- reindex_one_database (& cparams , REINDEX_SYSTEM , NULL ,
249- progname , echo , verbose ,
250- concurrently , 1 , tablespace );
231+ concurrently , concurrentCons , tablespace ,
232+ syscatalog , & schemas , & tables , & indexes );
251233 }
252234 else
253235 {
254- /*
255- * Index-level REINDEX is not supported with multiple jobs as we
256- * cannot control the concurrent processing of multiple indexes
257- * depending on the same relation.
258- */
259- if (concurrentCons > 1 && indexes .head != NULL )
260- pg_fatal ("cannot use multiple jobs to reindex indexes" );
261-
262236 if (dbname == NULL )
263237 {
264238 if (getenv ("PGDATABASE" ))
@@ -271,6 +245,11 @@ main(int argc, char *argv[])
271245
272246 cparams .dbname = dbname ;
273247
248+ if (syscatalog )
249+ reindex_one_database (& cparams , REINDEX_SYSTEM , NULL ,
250+ progname , echo , verbose ,
251+ concurrently , 1 , tablespace );
252+
274253 if (schemas .head != NULL )
275254 reindex_one_database (& cparams , REINDEX_SCHEMA , & schemas ,
276255 progname , echo , verbose ,
@@ -287,10 +266,11 @@ main(int argc, char *argv[])
287266 concurrently , concurrentCons , tablespace );
288267
289268 /*
290- * reindex database only if neither index nor table nor schema is
291- * specified
269+ * reindex database only if neither index nor table nor schema nor
270+ * system catalogs is specified
292271 */
293- if (indexes .head == NULL && tables .head == NULL && schemas .head == NULL )
272+ if (!syscatalog && indexes .head == NULL &&
273+ tables .head == NULL && schemas .head == NULL )
294274 reindex_one_database (& cparams , REINDEX_DATABASE , NULL ,
295275 progname , echo , verbose ,
296276 concurrently , concurrentCons , tablespace );
@@ -711,7 +691,9 @@ static void
711691reindex_all_databases (ConnParams * cparams ,
712692 const char * progname , bool echo , bool quiet , bool verbose ,
713693 bool concurrently , int concurrentCons ,
714- const char * tablespace )
694+ const char * tablespace , bool syscatalog ,
695+ SimpleStringList * schemas , SimpleStringList * tables ,
696+ SimpleStringList * indexes )
715697{
716698 PGconn * conn ;
717699 PGresult * result ;
@@ -735,9 +717,35 @@ reindex_all_databases(ConnParams *cparams,
735717
736718 cparams -> override_dbname = dbname ;
737719
738- reindex_one_database (cparams , REINDEX_DATABASE , NULL ,
739- progname , echo , verbose , concurrently ,
740- concurrentCons , tablespace );
720+ if (syscatalog )
721+ reindex_one_database (cparams , REINDEX_SYSTEM , NULL ,
722+ progname , echo , verbose ,
723+ concurrently , 1 , tablespace );
724+
725+ if (schemas -> head != NULL )
726+ reindex_one_database (cparams , REINDEX_SCHEMA , schemas ,
727+ progname , echo , verbose ,
728+ concurrently , concurrentCons , tablespace );
729+
730+ if (indexes -> head != NULL )
731+ reindex_one_database (cparams , REINDEX_INDEX , indexes ,
732+ progname , echo , verbose ,
733+ concurrently , 1 , tablespace );
734+
735+ if (tables -> head != NULL )
736+ reindex_one_database (cparams , REINDEX_TABLE , tables ,
737+ progname , echo , verbose ,
738+ concurrently , concurrentCons , tablespace );
739+
740+ /*
741+ * reindex database only if neither index nor table nor schema nor
742+ * system catalogs is specified
743+ */
744+ if (!syscatalog && indexes -> head == NULL &&
745+ tables -> head == NULL && schemas -> head == NULL )
746+ reindex_one_database (cparams , REINDEX_DATABASE , NULL ,
747+ progname , echo , verbose ,
748+ concurrently , concurrentCons , tablespace );
741749 }
742750
743751 PQclear (result );
0 commit comments