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