@@ -40,14 +40,15 @@ static void reindex_one_database(const ConnParams *cparams, ReindexType type,
40
40
SimpleStringList * user_list ,
41
41
const char * progname ,
42
42
bool echo , bool verbose , bool concurrently ,
43
- int concurrentCons );
43
+ int concurrentCons , const char * tablespace );
44
44
static void reindex_all_databases (ConnParams * cparams ,
45
45
const char * progname , bool echo ,
46
46
bool quiet , bool verbose , bool concurrently ,
47
- int concurrentCons );
47
+ int concurrentCons , const char * tablespace );
48
48
static void run_reindex_command (PGconn * conn , ReindexType type ,
49
49
const char * name , bool echo , bool verbose ,
50
- bool concurrently , bool async );
50
+ bool concurrently , bool async ,
51
+ const char * tablespace );
51
52
52
53
static void help (const char * progname );
53
54
@@ -72,6 +73,7 @@ main(int argc, char *argv[])
72
73
{"verbose" , no_argument , NULL , 'v' },
73
74
{"concurrently" , no_argument , NULL , 1 },
74
75
{"maintenance-db" , required_argument , NULL , 2 },
76
+ {"tablespace" , required_argument , NULL , 3 },
75
77
{NULL , 0 , NULL , 0 }
76
78
};
77
79
@@ -84,6 +86,7 @@ main(int argc, char *argv[])
84
86
const char * host = NULL ;
85
87
const char * port = NULL ;
86
88
const char * username = NULL ;
89
+ const char * tablespace = NULL ;
87
90
enum trivalue prompt_password = TRI_DEFAULT ;
88
91
ConnParams cparams ;
89
92
bool syscatalog = false;
@@ -164,6 +167,9 @@ main(int argc, char *argv[])
164
167
case 2 :
165
168
maintenance_db = pg_strdup (optarg );
166
169
break ;
170
+ case 3 :
171
+ tablespace = pg_strdup (optarg );
172
+ break ;
167
173
default :
168
174
fprintf (stderr , _ ("Try \"%s --help\" for more information.\n" ), progname );
169
175
exit (1 );
@@ -228,7 +234,7 @@ main(int argc, char *argv[])
228
234
cparams .dbname = maintenance_db ;
229
235
230
236
reindex_all_databases (& cparams , progname , echo , quiet , verbose ,
231
- concurrently , concurrentCons );
237
+ concurrently , concurrentCons , tablespace );
232
238
}
233
239
else if (syscatalog )
234
240
{
@@ -268,7 +274,7 @@ main(int argc, char *argv[])
268
274
269
275
reindex_one_database (& cparams , REINDEX_SYSTEM , NULL ,
270
276
progname , echo , verbose ,
271
- concurrently , 1 );
277
+ concurrently , 1 , tablespace );
272
278
}
273
279
else
274
280
{
@@ -298,17 +304,17 @@ main(int argc, char *argv[])
298
304
if (schemas .head != NULL )
299
305
reindex_one_database (& cparams , REINDEX_SCHEMA , & schemas ,
300
306
progname , echo , verbose ,
301
- concurrently , concurrentCons );
307
+ concurrently , concurrentCons , tablespace );
302
308
303
309
if (indexes .head != NULL )
304
310
reindex_one_database (& cparams , REINDEX_INDEX , & indexes ,
305
311
progname , echo , verbose ,
306
- concurrently , 1 );
312
+ concurrently , 1 , tablespace );
307
313
308
314
if (tables .head != NULL )
309
315
reindex_one_database (& cparams , REINDEX_TABLE , & tables ,
310
316
progname , echo , verbose ,
311
- concurrently , concurrentCons );
317
+ concurrently , concurrentCons , tablespace );
312
318
313
319
/*
314
320
* reindex database only if neither index nor table nor schema is
@@ -317,7 +323,7 @@ main(int argc, char *argv[])
317
323
if (indexes .head == NULL && tables .head == NULL && schemas .head == NULL )
318
324
reindex_one_database (& cparams , REINDEX_DATABASE , NULL ,
319
325
progname , echo , verbose ,
320
- concurrently , concurrentCons );
326
+ concurrently , concurrentCons , tablespace );
321
327
}
322
328
323
329
exit (0 );
@@ -327,7 +333,8 @@ static void
327
333
reindex_one_database (const ConnParams * cparams , ReindexType type ,
328
334
SimpleStringList * user_list ,
329
335
const char * progname , bool echo ,
330
- bool verbose , bool concurrently , int concurrentCons )
336
+ bool verbose , bool concurrently , int concurrentCons ,
337
+ const char * tablespace )
331
338
{
332
339
PGconn * conn ;
333
340
SimpleStringListCell * cell ;
@@ -348,6 +355,14 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
348
355
exit (1 );
349
356
}
350
357
358
+ if (tablespace && PQserverVersion (conn ) < 140000 )
359
+ {
360
+ PQfinish (conn );
361
+ pg_log_error ("cannot use the \"%s\" option on server versions older than PostgreSQL %s" ,
362
+ "tablespace" , "14" );
363
+ exit (1 );
364
+ }
365
+
351
366
if (!parallel )
352
367
{
353
368
switch (process_type )
@@ -386,7 +401,8 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
386
401
pg_log_warning ("cannot reindex system catalogs concurrently, skipping all" );
387
402
else
388
403
run_reindex_command (conn , REINDEX_SYSTEM , PQdb (conn ), echo ,
389
- verbose , concurrently , false);
404
+ verbose , concurrently , false,
405
+ tablespace );
390
406
391
407
/* Build a list of relations from the database */
392
408
process_list = get_parallel_object_list (conn , process_type ,
@@ -468,7 +484,7 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
468
484
469
485
ParallelSlotSetHandler (free_slot , TableCommandResultHandler , NULL );
470
486
run_reindex_command (free_slot -> connection , process_type , objname ,
471
- echo , verbose , concurrently , true);
487
+ echo , verbose , concurrently , true, tablespace );
472
488
473
489
cell = cell -> next ;
474
490
} while (cell != NULL );
@@ -492,8 +508,12 @@ reindex_one_database(const ConnParams *cparams, ReindexType type,
492
508
493
509
static void
494
510
run_reindex_command (PGconn * conn , ReindexType type , const char * name ,
495
- bool echo , bool verbose , bool concurrently , bool async )
511
+ bool echo , bool verbose , bool concurrently , bool async ,
512
+ const char * tablespace )
496
513
{
514
+ const char * paren = "(" ;
515
+ const char * comma = ", " ;
516
+ const char * sep = paren ;
497
517
PQExpBufferData sql ;
498
518
bool status ;
499
519
@@ -505,7 +525,19 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
505
525
appendPQExpBufferStr (& sql , "REINDEX " );
506
526
507
527
if (verbose )
508
- appendPQExpBufferStr (& sql , "(VERBOSE) " );
528
+ {
529
+ appendPQExpBuffer (& sql , "%sVERBOSE" , sep );
530
+ sep = comma ;
531
+ }
532
+
533
+ if (tablespace )
534
+ {
535
+ appendPQExpBuffer (& sql , "%sTABLESPACE %s" , sep , fmtId (tablespace ));
536
+ sep = comma ;
537
+ }
538
+
539
+ if (sep != paren )
540
+ appendPQExpBufferStr (& sql , ") " );
509
541
510
542
/* object type */
511
543
switch (type )
@@ -527,6 +559,11 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
527
559
break ;
528
560
}
529
561
562
+ /*
563
+ * Parenthesized grammar is only supported for CONCURRENTLY since
564
+ * PostgreSQL 14. Since 12, CONCURRENTLY can be specified after the
565
+ * object type.
566
+ */
530
567
if (concurrently )
531
568
appendPQExpBufferStr (& sql , "CONCURRENTLY " );
532
569
@@ -716,7 +753,8 @@ get_parallel_object_list(PGconn *conn, ReindexType type,
716
753
static void
717
754
reindex_all_databases (ConnParams * cparams ,
718
755
const char * progname , bool echo , bool quiet , bool verbose ,
719
- bool concurrently , int concurrentCons )
756
+ bool concurrently , int concurrentCons ,
757
+ const char * tablespace )
720
758
{
721
759
PGconn * conn ;
722
760
PGresult * result ;
@@ -740,7 +778,7 @@ reindex_all_databases(ConnParams *cparams,
740
778
741
779
reindex_one_database (cparams , REINDEX_DATABASE , NULL ,
742
780
progname , echo , verbose , concurrently ,
743
- concurrentCons );
781
+ concurrentCons , tablespace );
744
782
}
745
783
746
784
PQclear (result );
@@ -753,26 +791,27 @@ help(const char *progname)
753
791
printf (_ ("Usage:\n" ));
754
792
printf (_ (" %s [OPTION]... [DBNAME]\n" ), progname );
755
793
printf (_ ("\nOptions:\n" ));
756
- printf (_ (" -a, --all reindex all databases\n" ));
757
- printf (_ (" --concurrently reindex concurrently\n" ));
758
- printf (_ (" -d, --dbname=DBNAME database to reindex\n" ));
759
- printf (_ (" -e, --echo show the commands being sent to the server\n" ));
760
- printf (_ (" -i, --index=INDEX recreate specific index(es) only\n" ));
761
- printf (_ (" -j, --jobs=NUM use this many concurrent connections to reindex\n" ));
762
- printf (_ (" -q, --quiet don't write any messages\n" ));
763
- printf (_ (" -s, --system reindex system catalogs\n" ));
764
- printf (_ (" -S, --schema=SCHEMA reindex specific schema(s) only\n" ));
765
- printf (_ (" -t, --table=TABLE reindex specific table(s) only\n" ));
766
- printf (_ (" -v, --verbose write a lot of output\n" ));
767
- printf (_ (" -V, --version output version information, then exit\n" ));
768
- printf (_ (" -?, --help show this help, then exit\n" ));
794
+ printf (_ (" -a, --all reindex all databases\n" ));
795
+ printf (_ (" --concurrently reindex concurrently\n" ));
796
+ printf (_ (" -d, --dbname=DBNAME database to reindex\n" ));
797
+ printf (_ (" -e, --echo show the commands being sent to the server\n" ));
798
+ printf (_ (" -i, --index=INDEX recreate specific index(es) only\n" ));
799
+ printf (_ (" -j, --jobs=NUM use this many concurrent connections to reindex\n" ));
800
+ printf (_ (" -q, --quiet don't write any messages\n" ));
801
+ printf (_ (" -s, --system reindex system catalogs\n" ));
802
+ printf (_ (" -S, --schema=SCHEMA reindex specific schema(s) only\n" ));
803
+ printf (_ (" -t, --table=TABLE reindex specific table(s) only\n" ));
804
+ printf (_ (" --tablespace=TABLESPACE tablespace where indexes are rebuilt\n" ));
805
+ printf (_ (" -v, --verbose write a lot of output\n" ));
806
+ printf (_ (" -V, --version output version information, then exit\n" ));
807
+ printf (_ (" -?, --help show this help, then exit\n" ));
769
808
printf (_ ("\nConnection options:\n" ));
770
- printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
771
- printf (_ (" -p, --port=PORT database server port\n" ));
772
- printf (_ (" -U, --username=USERNAME user name to connect as\n" ));
773
- printf (_ (" -w, --no-password never prompt for password\n" ));
774
- printf (_ (" -W, --password force password prompt\n" ));
775
- printf (_ (" --maintenance-db=DBNAME alternate maintenance database\n" ));
809
+ printf (_ (" -h, --host=HOSTNAME database server host or socket directory\n" ));
810
+ printf (_ (" -p, --port=PORT database server port\n" ));
811
+ printf (_ (" -U, --username=USERNAME user name to connect as\n" ));
812
+ printf (_ (" -w, --no-password never prompt for password\n" ));
813
+ printf (_ (" -W, --password force password prompt\n" ));
814
+ printf (_ (" --maintenance-db=DBNAME alternate maintenance database\n" ));
776
815
printf (_ ("\nRead the description of the SQL command REINDEX for details.\n" ));
777
816
printf (_ ("\nReport bugs to <%s>.\n" ), PACKAGE_BUGREPORT );
778
817
printf (_ ("%s home page: <%s>\n" ), PACKAGE_NAME , PACKAGE_URL );
0 commit comments