15
15
#include "fe_utils/simple_list.h"
16
16
#include "fe_utils/string_utils.h"
17
17
18
+ typedef enum ReindexType
19
+ {
20
+ REINDEX_DATABASE ,
21
+ REINDEX_INDEX ,
22
+ REINDEX_SCHEMA ,
23
+ REINDEX_SYSTEM ,
24
+ REINDEX_TABLE
25
+ }ReindexType ;
26
+
18
27
19
28
static void reindex_one_database (const char * name ,const char * dbname ,
20
- const char * type ,const char * host ,
29
+ ReindexType type ,const char * host ,
21
30
const char * port ,const char * username ,
22
31
enum trivalue prompt_password ,const char * progname ,
23
32
bool echo ,bool verbose ,bool concurrently );
@@ -26,11 +35,6 @@ static void reindex_all_databases(const char *maintenance_db,
26
35
const char * username ,enum trivalue prompt_password ,
27
36
const char * progname ,bool echo ,
28
37
bool 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 );
34
38
static void help (const char * progname );
35
39
36
40
int
@@ -220,8 +224,9 @@ main(int argc, char *argv[])
220
224
dbname = get_user_name_or_exit (progname );
221
225
}
222
226
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 );
225
230
}
226
231
else
227
232
{
@@ -241,8 +246,9 @@ main(int argc, char *argv[])
241
246
242
247
for (cell = schemas .head ;cell ;cell = cell -> next )
243
248
{
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 );
246
252
}
247
253
}
248
254
@@ -252,8 +258,9 @@ main(int argc, char *argv[])
252
258
253
259
for (cell = indexes .head ;cell ;cell = cell -> next )
254
260
{
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 );
257
264
}
258
265
}
259
266
if (tables .head != NULL )
@@ -262,8 +269,9 @@ main(int argc, char *argv[])
262
269
263
270
for (cell = tables .head ;cell ;cell = cell -> next )
264
271
{
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 );
267
275
}
268
276
}
269
277
@@ -272,21 +280,21 @@ main(int argc, char *argv[])
272
280
* specified
273
281
*/
274
282
if (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 );
277
286
}
278
287
279
288
exit (0 );
280
289
}
281
290
282
291
static 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 ,
284
293
const char * host ,const char * port ,const char * username ,
285
294
enum trivalue prompt_password ,const char * progname ,bool echo ,
286
295
bool verbose ,bool concurrently )
287
296
{
288
297
PQExpBufferData sql ;
289
-
290
298
PGconn * conn ;
291
299
292
300
conn = connectDatabase (dbname ,host ,port ,username ,prompt_password ,
@@ -300,40 +308,81 @@ reindex_one_database(const char *name, const char *dbname, const char *type,
300
308
exit (1 );
301
309
}
302
310
311
+ /* build the REINDEX query */
303
312
initPQExpBuffer (& sql );
304
313
305
314
appendPQExpBufferStr (& sql ,"REINDEX " );
306
315
307
316
if (verbose )
308
317
appendPQExpBufferStr (& sql ,"(VERBOSE) " );
309
318
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
+
312
339
if (concurrently )
313
340
appendPQExpBufferStr (& 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 */
321
359
appendPQExpBufferChar (& sql ,';' );
322
360
323
361
if (!executeMaintenanceCommand (conn ,sql .data ,echo ))
324
362
{
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
+ }
337
386
PQfinish (conn );
338
387
exit (1 );
339
388
}
@@ -374,7 +423,7 @@ reindex_all_databases(const char *maintenance_db,
374
423
appendPQExpBuffer (& connstr ,"dbname=" );
375
424
appendConnStrVal (& connstr ,dbname );
376
425
377
- reindex_one_database (NULL ,connstr .data ,"DATABASE" ,host ,
426
+ reindex_one_database (NULL ,connstr .data ,REINDEX_DATABASE ,host ,
378
427
port ,username ,prompt_password ,
379
428
progname ,echo ,verbose ,concurrently );
380
429
}
@@ -383,41 +432,6 @@ reindex_all_databases(const char *maintenance_db,
383
432
PQclear (result );
384
433
}
385
434
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
-
421
435
static void
422
436
help (const char * progname )
423
437
{