@@ -50,10 +50,13 @@ static void reindex_all_databases(ConnParams *cparams,
50
50
bool syscatalog , SimpleStringList * schemas ,
51
51
SimpleStringList * tables ,
52
52
SimpleStringList * indexes );
53
- static void run_reindex_command (PGconn * conn , ReindexType type ,
53
+ static void gen_reindex_command (PGconn * conn , ReindexType type ,
54
54
const char * name , bool echo , bool verbose ,
55
- bool concurrently , bool async ,
56
- const char * tablespace );
55
+ bool concurrently , const char * tablespace ,
56
+ PQExpBufferData * sql );
57
+ static void run_reindex_command (PGconn * conn , ReindexType type ,
58
+ const char * name , bool echo ,
59
+ PQExpBufferData * sq );
57
60
58
61
static void help (const char * progname );
59
62
@@ -285,7 +288,6 @@ reindex_one_database(ConnParams *cparams, ReindexType type,
285
288
ParallelSlotArray * sa ;
286
289
bool failed = false;
287
290
int items_count = 0 ;
288
- char * prev_index_table_name = NULL ;
289
291
ParallelSlot * free_slot = NULL ;
290
292
291
293
conn = connectDatabase (cparams , progname , echo , false, true);
@@ -421,44 +423,54 @@ reindex_one_database(ConnParams *cparams, ReindexType type,
421
423
cell = process_list -> head ;
422
424
do
423
425
{
426
+ PQExpBufferData sql ;
424
427
const char * objname = cell -> val ;
425
- bool need_new_slot = true;
426
428
427
429
if (CancelRequested )
428
430
{
429
431
failed = true;
430
432
goto finish ;
431
433
}
432
434
433
- /*
434
- * For parallel index-level REINDEX, the indices of the same table are
435
- * ordered together and they are to be processed by the same job. So,
436
- * we don't switch the job as soon as the index belongs to the same
437
- * table as the previous one.
438
- */
439
- if (parallel && process_type == REINDEX_INDEX )
435
+ free_slot = ParallelSlotsGetIdle (sa , NULL );
436
+ if (!free_slot )
440
437
{
441
- if (prev_index_table_name != NULL &&
442
- strcmp (prev_index_table_name , indices_tables_cell -> val ) == 0 )
443
- need_new_slot = false;
444
- prev_index_table_name = indices_tables_cell -> val ;
445
- indices_tables_cell = indices_tables_cell -> next ;
438
+ failed = true;
439
+ goto finish ;
446
440
}
447
441
448
- if (need_new_slot )
442
+ ParallelSlotSetHandler (free_slot , TableCommandResultHandler , NULL );
443
+ initPQExpBuffer (& sql );
444
+ if (parallel && process_type == REINDEX_INDEX )
449
445
{
450
- free_slot = ParallelSlotsGetIdle (sa , NULL );
451
- if (!free_slot )
446
+ /*
447
+ * For parallel index-level REINDEX, the indices of the same table
448
+ * are ordered together and they are to be processed by the same
449
+ * job. So, we put all the relevant REINDEX commands into the
450
+ * same SQL query to be processed by this job at once.
451
+ */
452
+ gen_reindex_command (free_slot -> connection , process_type , objname ,
453
+ echo , verbose , concurrently , tablespace , & sql );
454
+ while (indices_tables_cell -> next &&
455
+ strcmp (indices_tables_cell -> val , indices_tables_cell -> next -> val ) == 0 )
452
456
{
453
- failed = true;
454
- goto finish ;
457
+ indices_tables_cell = indices_tables_cell -> next ;
458
+ cell = cell -> next ;
459
+ objname = cell -> val ;
460
+ appendPQExpBufferChar (& sql , '\n' );
461
+ gen_reindex_command (free_slot -> connection , process_type , objname ,
462
+ echo , verbose , concurrently , tablespace , & sql );
455
463
}
456
-
457
- ParallelSlotSetHandler (free_slot , TableCommandResultHandler , NULL );
464
+ indices_tables_cell = indices_tables_cell -> next ;
465
+ }
466
+ else
467
+ {
468
+ gen_reindex_command (free_slot -> connection , process_type , objname ,
469
+ echo , verbose , concurrently , tablespace , & sql );
458
470
}
459
-
460
471
run_reindex_command (free_slot -> connection , process_type , objname ,
461
- echo , verbose , concurrently , true, tablespace );
472
+ echo , & sql );
473
+ termPQExpBuffer (& sql );
462
474
463
475
cell = cell -> next ;
464
476
} while (cell != NULL );
@@ -486,57 +498,57 @@ reindex_one_database(ConnParams *cparams, ReindexType type,
486
498
exit (1 );
487
499
}
488
500
501
+ /*
502
+ * Append a SQL command required to reindex a given database object to the
503
+ * '*sql' string.
504
+ */
489
505
static void
490
- run_reindex_command (PGconn * conn , ReindexType type , const char * name ,
491
- bool echo , bool verbose , bool concurrently , bool async ,
492
- const char * tablespace )
506
+ gen_reindex_command (PGconn * conn , ReindexType type , const char * name ,
507
+ bool echo , bool verbose , bool concurrently ,
508
+ const char * tablespace , PQExpBufferData * sql )
493
509
{
494
510
const char * paren = "(" ;
495
511
const char * comma = ", " ;
496
512
const char * sep = paren ;
497
- PQExpBufferData sql ;
498
- bool status ;
499
513
500
514
Assert (name );
501
515
502
516
/* build the REINDEX query */
503
- initPQExpBuffer (& sql );
504
-
505
- appendPQExpBufferStr (& sql , "REINDEX " );
517
+ appendPQExpBufferStr (sql , "REINDEX " );
506
518
507
519
if (verbose )
508
520
{
509
- appendPQExpBuffer (& sql , "%sVERBOSE" , sep );
521
+ appendPQExpBuffer (sql , "%sVERBOSE" , sep );
510
522
sep = comma ;
511
523
}
512
524
513
525
if (tablespace )
514
526
{
515
- appendPQExpBuffer (& sql , "%sTABLESPACE %s" , sep ,
527
+ appendPQExpBuffer (sql , "%sTABLESPACE %s" , sep ,
516
528
fmtIdEnc (tablespace , PQclientEncoding (conn )));
517
529
sep = comma ;
518
530
}
519
531
520
532
if (sep != paren )
521
- appendPQExpBufferStr (& sql , ") " );
533
+ appendPQExpBufferStr (sql , ") " );
522
534
523
535
/* object type */
524
536
switch (type )
525
537
{
526
538
case REINDEX_DATABASE :
527
- appendPQExpBufferStr (& sql , "DATABASE " );
539
+ appendPQExpBufferStr (sql , "DATABASE " );
528
540
break ;
529
541
case REINDEX_INDEX :
530
- appendPQExpBufferStr (& sql , "INDEX " );
542
+ appendPQExpBufferStr (sql , "INDEX " );
531
543
break ;
532
544
case REINDEX_SCHEMA :
533
- appendPQExpBufferStr (& sql , "SCHEMA " );
545
+ appendPQExpBufferStr (sql , "SCHEMA " );
534
546
break ;
535
547
case REINDEX_SYSTEM :
536
- appendPQExpBufferStr (& sql , "SYSTEM " );
548
+ appendPQExpBufferStr (sql , "SYSTEM " );
537
549
break ;
538
550
case REINDEX_TABLE :
539
- appendPQExpBufferStr (& sql , "TABLE " );
551
+ appendPQExpBufferStr (sql , "TABLE " );
540
552
break ;
541
553
}
542
554
@@ -546,37 +558,43 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
546
558
* object type.
547
559
*/
548
560
if (concurrently )
549
- appendPQExpBufferStr (& sql , "CONCURRENTLY " );
561
+ appendPQExpBufferStr (sql , "CONCURRENTLY " );
550
562
551
563
/* object name */
552
564
switch (type )
553
565
{
554
566
case REINDEX_DATABASE :
555
567
case REINDEX_SYSTEM :
556
- appendPQExpBufferStr (& sql ,
568
+ appendPQExpBufferStr (sql ,
557
569
fmtIdEnc (name , PQclientEncoding (conn )));
558
570
break ;
559
571
case REINDEX_INDEX :
560
572
case REINDEX_TABLE :
561
- appendQualifiedRelation (& sql , name , conn , echo );
573
+ appendQualifiedRelation (sql , name , conn , echo );
562
574
break ;
563
575
case REINDEX_SCHEMA :
564
- appendPQExpBufferStr (& sql , name );
576
+ appendPQExpBufferStr (sql , name );
565
577
break ;
566
578
}
567
579
568
580
/* finish the query */
569
- appendPQExpBufferChar (& sql , ';' );
581
+ appendPQExpBufferChar (sql , ';' );
582
+ }
570
583
571
- if (async )
572
- {
573
- if (echo )
574
- printf ("%s\n" , sql .data );
584
+ /*
585
+ * Run one or more reindex commands accumulated in the '*sql' string against
586
+ * a given database connection.
587
+ */
588
+ static void
589
+ run_reindex_command (PGconn * conn , ReindexType type , const char * name ,
590
+ bool echo , PQExpBufferData * sql )
591
+ {
592
+ bool status ;
575
593
576
- status = PQsendQuery ( conn , sql . data ) == 1 ;
577
- }
578
- else
579
- status = executeMaintenanceCommand (conn , sql . data , echo ) ;
594
+ if ( echo )
595
+ printf ( "%s\n" , sql -> data );
596
+
597
+ status = PQsendQuery (conn , sql -> data ) == 1 ;
580
598
581
599
if (!status )
582
600
{
@@ -603,14 +621,7 @@ run_reindex_command(PGconn *conn, ReindexType type, const char *name,
603
621
name , PQdb (conn ), PQerrorMessage (conn ));
604
622
break ;
605
623
}
606
- if (!async )
607
- {
608
- PQfinish (conn );
609
- exit (1 );
610
- }
611
624
}
612
-
613
- termPQExpBuffer (& sql );
614
625
}
615
626
616
627
/*
0 commit comments