1515#include "fe_utils/simple_list.h"
1616#include "fe_utils/string_utils.h"
1717
18+ typedef enum ReindexType
19+ {
20+ REINDEX_DATABASE ,
21+ REINDEX_INDEX ,
22+ REINDEX_SCHEMA ,
23+ REINDEX_SYSTEM ,
24+ REINDEX_TABLE
25+ }ReindexType ;
26+
1827
1928static void reindex_one_database (const char * name ,const char * dbname ,
20- const char * type ,const char * host ,
29+ ReindexType type ,const char * host ,
2130const char * port ,const char * username ,
2231enum trivalue prompt_password ,const char * progname ,
2332bool echo ,bool verbose ,bool concurrently );
@@ -26,11 +35,6 @@ static void reindex_all_databases(const char *maintenance_db,
2635const char * username ,enum trivalue prompt_password ,
2736const char * progname ,bool echo ,
2837bool quiet ,bool verbose ,bool concurrently );
29- static void reindex_system_catalogs (const char * dbname ,
30- const char * host ,const char * port ,
31- const char * username ,enum trivalue prompt_password ,
32- const char * progname ,bool echo ,bool verbose ,
33- bool concurrently );
3438static void help (const char * progname );
3539
3640int
@@ -220,8 +224,9 @@ main(int argc, char *argv[])
220224dbname = get_user_name_or_exit (progname );
221225}
222226
223- reindex_system_catalogs (dbname ,host ,port ,username ,prompt_password ,
224- progname ,echo ,verbose ,concurrently );
227+ reindex_one_database (NULL ,dbname ,REINDEX_SYSTEM ,host ,
228+ port ,username ,prompt_password ,progname ,
229+ echo ,verbose ,concurrently );
225230}
226231else
227232{
@@ -241,8 +246,9 @@ main(int argc, char *argv[])
241246
242247for (cell = schemas .head ;cell ;cell = cell -> next )
243248{
244- reindex_one_database (cell -> val ,dbname ,"SCHEMA" ,host ,port ,
245- username ,prompt_password ,progname ,echo ,verbose ,concurrently );
249+ reindex_one_database (cell -> val ,dbname ,REINDEX_SCHEMA ,host ,
250+ port ,username ,prompt_password ,progname ,
251+ echo ,verbose ,concurrently );
246252}
247253}
248254
@@ -252,8 +258,9 @@ main(int argc, char *argv[])
252258
253259for (cell = indexes .head ;cell ;cell = cell -> next )
254260{
255- reindex_one_database (cell -> val ,dbname ,"INDEX" ,host ,port ,
256- username ,prompt_password ,progname ,echo ,verbose ,concurrently );
261+ reindex_one_database (cell -> val ,dbname ,REINDEX_INDEX ,host ,
262+ port ,username ,prompt_password ,progname ,
263+ echo ,verbose ,concurrently );
257264}
258265}
259266if (tables .head != NULL )
@@ -262,8 +269,9 @@ main(int argc, char *argv[])
262269
263270for (cell = tables .head ;cell ;cell = cell -> next )
264271{
265- reindex_one_database (cell -> val ,dbname ,"TABLE" ,host ,port ,
266- username ,prompt_password ,progname ,echo ,verbose ,concurrently );
272+ reindex_one_database (cell -> val ,dbname ,REINDEX_TABLE ,host ,
273+ port ,username ,prompt_password ,progname ,
274+ echo ,verbose ,concurrently );
267275}
268276}
269277
@@ -272,21 +280,21 @@ main(int argc, char *argv[])
272280 * specified
273281 */
274282if (indexes .head == NULL && tables .head == NULL && schemas .head == NULL )
275- reindex_one_database (NULL ,dbname ,"DATABASE" ,host ,port ,
276- username ,prompt_password ,progname ,echo ,verbose ,concurrently );
283+ reindex_one_database (NULL ,dbname ,REINDEX_DATABASE ,host ,
284+ port ,username ,prompt_password ,progname ,
285+ echo ,verbose ,concurrently );
277286}
278287
279288exit (0 );
280289}
281290
282291static void
283- reindex_one_database (const char * name ,const char * dbname ,const char * type ,
292+ reindex_one_database (const char * name ,const char * dbname ,ReindexType type ,
284293const char * host ,const char * port ,const char * username ,
285294enum trivalue prompt_password ,const char * progname ,bool echo ,
286295bool verbose ,bool concurrently )
287296{
288297PQExpBufferData sql ;
289-
290298PGconn * conn ;
291299
292300conn = connectDatabase (dbname ,host ,port ,username ,prompt_password ,
@@ -300,40 +308,81 @@ reindex_one_database(const char *name, const char *dbname, const char *type,
300308exit (1 );
301309}
302310
311+ /* build the REINDEX query */
303312initPQExpBuffer (& sql );
304313
305314appendPQExpBufferStr (& sql ,"REINDEX " );
306315
307316if (verbose )
308317appendPQExpBufferStr (& sql ,"(VERBOSE) " );
309318
310- appendPQExpBufferStr (& sql ,type );
311- appendPQExpBufferChar (& sql ,' ' );
319+ /* object type */
320+ switch (type )
321+ {
322+ case REINDEX_DATABASE :
323+ appendPQExpBufferStr (& sql ,"DATABASE " );
324+ break ;
325+ case REINDEX_INDEX :
326+ appendPQExpBufferStr (& sql ,"INDEX " );
327+ break ;
328+ case REINDEX_SCHEMA :
329+ appendPQExpBufferStr (& sql ,"SCHEMA " );
330+ break ;
331+ case REINDEX_SYSTEM :
332+ appendPQExpBufferStr (& sql ,"SYSTEM " );
333+ break ;
334+ case REINDEX_TABLE :
335+ appendPQExpBufferStr (& sql ,"TABLE " );
336+ break ;
337+ }
338+
312339if (concurrently )
313340appendPQExpBufferStr (& sql ,"CONCURRENTLY " );
314- if (strcmp (type ,"TABLE" )== 0 ||
315- strcmp (type ,"INDEX" )== 0 )
316- appendQualifiedRelation (& sql ,name ,conn ,progname ,echo );
317- else if (strcmp (type ,"SCHEMA" )== 0 )
318- appendPQExpBufferStr (& sql ,name );
319- else if (strcmp (type ,"DATABASE" )== 0 )
320- appendPQExpBufferStr (& sql ,fmtId (PQdb (conn )));
341+
342+ /* object name */
343+ switch (type )
344+ {
345+ case REINDEX_DATABASE :
346+ case REINDEX_SYSTEM :
347+ appendPQExpBufferStr (& sql ,fmtId (PQdb (conn )));
348+ break ;
349+ case REINDEX_INDEX :
350+ case REINDEX_TABLE :
351+ appendQualifiedRelation (& sql ,name ,conn ,progname ,echo );
352+ break ;
353+ case REINDEX_SCHEMA :
354+ appendPQExpBufferStr (& sql ,name );
355+ break ;
356+ }
357+
358+ /* finish the query */
321359appendPQExpBufferChar (& sql ,';' );
322360
323361if (!executeMaintenanceCommand (conn ,sql .data ,echo ))
324362{
325- if (strcmp (type ,"TABLE" )== 0 )
326- pg_log_error ("reindexing of table \"%s\" in database \"%s\" failed: %s" ,
327- name ,PQdb (conn ),PQerrorMessage (conn ));
328- else if (strcmp (type ,"INDEX" )== 0 )
329- pg_log_error ("reindexing of index \"%s\" in database \"%s\" failed: %s" ,
330- name ,PQdb (conn ),PQerrorMessage (conn ));
331- else if (strcmp (type ,"SCHEMA" )== 0 )
332- pg_log_error ("reindexing of schema \"%s\" in database \"%s\" failed: %s" ,
333- name ,PQdb (conn ),PQerrorMessage (conn ));
334- else
335- pg_log_error ("reindexing of database \"%s\" failed: %s" ,
336- PQdb (conn ),PQerrorMessage (conn ));
363+ switch (type )
364+ {
365+ case REINDEX_DATABASE :
366+ pg_log_error ("reindexing of database \"%s\" failed: %s" ,
367+ PQdb (conn ),PQerrorMessage (conn ));
368+ break ;
369+ case REINDEX_INDEX :
370+ pg_log_error ("reindexing of index \"%s\" in database \"%s\" failed: %s" ,
371+ name ,PQdb (conn ),PQerrorMessage (conn ));
372+ break ;
373+ case REINDEX_SCHEMA :
374+ pg_log_error ("reindexing of schema \"%s\" in database \"%s\" failed: %s" ,
375+ name ,PQdb (conn ),PQerrorMessage (conn ));
376+ break ;
377+ case REINDEX_SYSTEM :
378+ pg_log_error ("reindexing of system catalogs on database \"%s\" failed: %s" ,
379+ PQdb (conn ),PQerrorMessage (conn ));
380+ break ;
381+ case REINDEX_TABLE :
382+ pg_log_error ("reindexing of table \"%s\" in database \"%s\" failed: %s" ,
383+ name ,PQdb (conn ),PQerrorMessage (conn ));
384+ break ;
385+ }
337386PQfinish (conn );
338387exit (1 );
339388}
@@ -374,7 +423,7 @@ reindex_all_databases(const char *maintenance_db,
374423appendPQExpBuffer (& connstr ,"dbname=" );
375424appendConnStrVal (& connstr ,dbname );
376425
377- reindex_one_database (NULL ,connstr .data ,"DATABASE" ,host ,
426+ reindex_one_database (NULL ,connstr .data ,REINDEX_DATABASE ,host ,
378427port ,username ,prompt_password ,
379428progname ,echo ,verbose ,concurrently );
380429}
@@ -383,41 +432,6 @@ reindex_all_databases(const char *maintenance_db,
383432PQclear (result );
384433}
385434
386- static void
387- reindex_system_catalogs (const char * dbname ,const char * host ,const char * port ,
388- const char * username ,enum trivalue prompt_password ,
389- const char * progname ,bool echo ,bool verbose ,bool concurrently )
390- {
391- PGconn * conn ;
392- PQExpBufferData sql ;
393-
394- conn = connectDatabase (dbname ,host ,port ,username ,prompt_password ,
395- progname ,echo , false, false);
396-
397- initPQExpBuffer (& sql );
398-
399- appendPQExpBuffer (& sql ,"REINDEX" );
400-
401- if (verbose )
402- appendPQExpBuffer (& sql ," (VERBOSE)" );
403-
404- appendPQExpBufferStr (& sql ," SYSTEM " );
405- if (concurrently )
406- appendPQExpBuffer (& sql ,"CONCURRENTLY " );
407- appendPQExpBufferStr (& sql ,fmtId (PQdb (conn )));
408- appendPQExpBufferChar (& sql ,';' );
409-
410- if (!executeMaintenanceCommand (conn ,sql .data ,echo ))
411- {
412- pg_log_error ("reindexing of system catalogs failed: %s" ,
413- PQerrorMessage (conn ));
414- PQfinish (conn );
415- exit (1 );
416- }
417- PQfinish (conn );
418- termPQExpBuffer (& sql );
419- }
420-
421435static void
422436help (const char * progname )
423437{