@@ -385,7 +385,7 @@ CompareOpclassOptions(Datum *opts1, Datum *opts2, int natts)
385
385
* lazy VACUUMs, because they won't be fazed by missing index entries
386
386
* either. (Manual ANALYZEs, however, can't be excluded because they
387
387
* might be within transactions that are going to do arbitrary operations
388
- * later.) Processes running CREATE INDEX CONCURRENTLY
388
+ * later.) Processes running CREATE INDEX CONCURRENTLY or REINDEX CONCURRENTLY
389
389
* on indexes that are neither expressional nor partial are also safe to
390
390
* ignore, since we know that those processes won't examine any data
391
391
* outside the table they're indexing.
@@ -3066,6 +3066,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3066
3066
Oid indexId ;
3067
3067
Oid tableId ;
3068
3068
Oid amId ;
3069
+ bool safe ; /* for set_indexsafe_procflags */
3069
3070
} ReindexIndexInfo ;
3070
3071
List * heapRelationIds = NIL ;
3071
3072
List * indexIds = NIL ;
@@ -3377,6 +3378,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3377
3378
heapRel = table_open (indexRel -> rd_index -> indrelid ,
3378
3379
ShareUpdateExclusiveLock );
3379
3380
3381
+ /* determine safety of this index for set_indexsafe_procflags */
3382
+ idx -> safe = (indexRel -> rd_indexprs == NIL &&
3383
+ indexRel -> rd_indpred == NIL );
3380
3384
idx -> tableId = RelationGetRelid (heapRel );
3381
3385
idx -> amId = indexRel -> rd_rel -> relam ;
3382
3386
@@ -3418,6 +3422,7 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3418
3422
3419
3423
newidx = palloc (sizeof (ReindexIndexInfo ));
3420
3424
newidx -> indexId = newIndexId ;
3425
+ newidx -> safe = idx -> safe ;
3421
3426
newidx -> tableId = idx -> tableId ;
3422
3427
newidx -> amId = idx -> amId ;
3423
3428
@@ -3485,6 +3490,11 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3485
3490
CommitTransactionCommand ();
3486
3491
StartTransactionCommand ();
3487
3492
3493
+ /*
3494
+ * Because we don't take a snapshot in this transaction, there's no need
3495
+ * to set the PROC_IN_SAFE_IC flag here.
3496
+ */
3497
+
3488
3498
/*
3489
3499
* Phase 2 of REINDEX CONCURRENTLY
3490
3500
*
@@ -3514,6 +3524,10 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3514
3524
*/
3515
3525
CHECK_FOR_INTERRUPTS ();
3516
3526
3527
+ /* Tell concurrent indexing to ignore us, if index qualifies */
3528
+ if (newidx -> safe )
3529
+ set_indexsafe_procflags ();
3530
+
3517
3531
/* Set ActiveSnapshot since functions in the indexes may need it */
3518
3532
PushActiveSnapshot (GetTransactionSnapshot ());
3519
3533
@@ -3534,8 +3548,14 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3534
3548
PopActiveSnapshot ();
3535
3549
CommitTransactionCommand ();
3536
3550
}
3551
+
3537
3552
StartTransactionCommand ();
3538
3553
3554
+ /*
3555
+ * Because we don't take a snapshot or Xid in this transaction, there's no
3556
+ * need to set the PROC_IN_SAFE_IC flag here.
3557
+ */
3558
+
3539
3559
/*
3540
3560
* Phase 3 of REINDEX CONCURRENTLY
3541
3561
*
@@ -3564,6 +3584,10 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3564
3584
*/
3565
3585
CHECK_FOR_INTERRUPTS ();
3566
3586
3587
+ /* Tell concurrent indexing to ignore us, if index qualifies */
3588
+ if (newidx -> safe )
3589
+ set_indexsafe_procflags ();
3590
+
3567
3591
/*
3568
3592
* Take the "reference snapshot" that will be used by validate_index()
3569
3593
* to filter candidate tuples.
@@ -3607,6 +3631,9 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3607
3631
* interesting tuples. But since it might not contain tuples deleted
3608
3632
* just before the reference snap was taken, we have to wait out any
3609
3633
* transactions that might have older snapshots.
3634
+ *
3635
+ * Because we don't take a snapshot or Xid in this transaction,
3636
+ * there's no need to set the PROC_IN_SAFE_IC flag here.
3610
3637
*/
3611
3638
pgstat_progress_update_param (PROGRESS_CREATEIDX_PHASE ,
3612
3639
PROGRESS_CREATEIDX_PHASE_WAIT_3 );
@@ -3628,6 +3655,13 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3628
3655
3629
3656
StartTransactionCommand ();
3630
3657
3658
+ /*
3659
+ * Because this transaction only does catalog manipulations and doesn't do
3660
+ * any index operations, we can set the PROC_IN_SAFE_IC flag here
3661
+ * unconditionally.
3662
+ */
3663
+ set_indexsafe_procflags ();
3664
+
3631
3665
forboth (lc , indexIds , lc2 , newIndexIds )
3632
3666
{
3633
3667
ReindexIndexInfo * oldidx = lfirst (lc );
@@ -3675,6 +3709,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3675
3709
CommitTransactionCommand ();
3676
3710
StartTransactionCommand ();
3677
3711
3712
+ /*
3713
+ * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
3714
+ * real need for that, because we only acquire an Xid after the wait is
3715
+ * done, and that lasts for a very short period.
3716
+ */
3717
+
3678
3718
/*
3679
3719
* Phase 5 of REINDEX CONCURRENTLY
3680
3720
*
@@ -3705,6 +3745,12 @@ ReindexRelationConcurrently(Oid relationOid, int options)
3705
3745
CommitTransactionCommand ();
3706
3746
StartTransactionCommand ();
3707
3747
3748
+ /*
3749
+ * While we could set PROC_IN_SAFE_IC if all indexes qualified, there's no
3750
+ * real need for that, because we only acquire an Xid after the wait is
3751
+ * done, and that lasts for a very short period.
3752
+ */
3753
+
3708
3754
/*
3709
3755
* Phase 6 of REINDEX CONCURRENTLY
3710
3756
*
0 commit comments