@@ -1445,6 +1445,9 @@ index_concurrently_build(Oid heapRelationId,
1445
1445
Oid indexRelationId )
1446
1446
{
1447
1447
Relation heapRel ;
1448
+ Oid save_userid ;
1449
+ int save_sec_context ;
1450
+ int save_nestlevel ;
1448
1451
Relation indexRelation ;
1449
1452
IndexInfo * indexInfo ;
1450
1453
@@ -1454,7 +1457,16 @@ index_concurrently_build(Oid heapRelationId,
1454
1457
/* Open and lock the parent heap relation */
1455
1458
heapRel = table_open (heapRelationId , ShareUpdateExclusiveLock );
1456
1459
1457
- /* And the target index relation */
1460
+ /*
1461
+ * Switch to the table owner's userid, so that any index functions are run
1462
+ * as that user. Also lock down security-restricted operations and
1463
+ * arrange to make GUC variable changes local to this command.
1464
+ */
1465
+ GetUserIdAndSecContext (& save_userid , & save_sec_context );
1466
+ SetUserIdAndSecContext (heapRel -> rd_rel -> relowner ,
1467
+ save_sec_context | SECURITY_RESTRICTED_OPERATION );
1468
+ save_nestlevel = NewGUCNestLevel ();
1469
+
1458
1470
indexRelation = index_open (indexRelationId , RowExclusiveLock );
1459
1471
1460
1472
/*
@@ -1470,6 +1482,12 @@ index_concurrently_build(Oid heapRelationId,
1470
1482
/* Now build the index */
1471
1483
index_build (heapRel , indexRelation , indexInfo , false, true);
1472
1484
1485
+ /* Roll back any GUC changes executed by index functions */
1486
+ AtEOXact_GUC (false, save_nestlevel );
1487
+
1488
+ /* Restore userid and security context */
1489
+ SetUserIdAndSecContext (save_userid , save_sec_context );
1490
+
1473
1491
/* Close both the relations, but keep the locks */
1474
1492
table_close (heapRel , NoLock );
1475
1493
index_close (indexRelation , NoLock );
@@ -3299,7 +3317,17 @@ validate_index(Oid heapId, Oid indexId, Snapshot snapshot)
3299
3317
3300
3318
/* Open and lock the parent heap relation */
3301
3319
heapRelation = table_open (heapId , ShareUpdateExclusiveLock );
3302
- /* And the target index relation */
3320
+
3321
+ /*
3322
+ * Switch to the table owner's userid, so that any index functions are run
3323
+ * as that user. Also lock down security-restricted operations and
3324
+ * arrange to make GUC variable changes local to this command.
3325
+ */
3326
+ GetUserIdAndSecContext (& save_userid , & save_sec_context );
3327
+ SetUserIdAndSecContext (heapRelation -> rd_rel -> relowner ,
3328
+ save_sec_context | SECURITY_RESTRICTED_OPERATION );
3329
+ save_nestlevel = NewGUCNestLevel ();
3330
+
3303
3331
indexRelation = index_open (indexId , RowExclusiveLock );
3304
3332
3305
3333
/*
@@ -3312,16 +3340,6 @@ validate_index(Oid heapId, Oid indexId, Snapshot snapshot)
3312
3340
/* mark build is concurrent just for consistency */
3313
3341
indexInfo -> ii_Concurrent = true;
3314
3342
3315
- /*
3316
- * Switch to the table owner's userid, so that any index functions are run
3317
- * as that user. Also lock down security-restricted operations and
3318
- * arrange to make GUC variable changes local to this command.
3319
- */
3320
- GetUserIdAndSecContext (& save_userid , & save_sec_context );
3321
- SetUserIdAndSecContext (heapRelation -> rd_rel -> relowner ,
3322
- save_sec_context | SECURITY_RESTRICTED_OPERATION );
3323
- save_nestlevel = NewGUCNestLevel ();
3324
-
3325
3343
/*
3326
3344
* Scan the index and gather up all the TIDs into a tuplesort object.
3327
3345
*/
@@ -3530,6 +3548,9 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3530
3548
Relation iRel ,
3531
3549
heapRelation ;
3532
3550
Oid heapId ;
3551
+ Oid save_userid ;
3552
+ int save_sec_context ;
3553
+ int save_nestlevel ;
3533
3554
IndexInfo * indexInfo ;
3534
3555
volatile bool skipped_constraint = false;
3535
3556
PGRUsage ru0 ;
@@ -3557,6 +3578,16 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3557
3578
if (!heapRelation )
3558
3579
return ;
3559
3580
3581
+ /*
3582
+ * Switch to the table owner's userid, so that any index functions are run
3583
+ * as that user. Also lock down security-restricted operations and
3584
+ * arrange to make GUC variable changes local to this command.
3585
+ */
3586
+ GetUserIdAndSecContext (& save_userid , & save_sec_context );
3587
+ SetUserIdAndSecContext (heapRelation -> rd_rel -> relowner ,
3588
+ save_sec_context | SECURITY_RESTRICTED_OPERATION );
3589
+ save_nestlevel = NewGUCNestLevel ();
3590
+
3560
3591
if (progress )
3561
3592
{
3562
3593
const int progress_cols [] = {
@@ -3775,12 +3806,18 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
3775
3806
errdetail_internal ("%s" ,
3776
3807
pg_rusage_show (& ru0 ))));
3777
3808
3778
- if (progress )
3779
- pgstat_progress_end_command ();
3809
+ /* Roll back any GUC changes executed by index functions */
3810
+ AtEOXact_GUC (false, save_nestlevel );
3811
+
3812
+ /* Restore userid and security context */
3813
+ SetUserIdAndSecContext (save_userid , save_sec_context );
3780
3814
3781
3815
/* Close rels, but keep locks */
3782
3816
index_close (iRel , NoLock );
3783
3817
table_close (heapRelation , NoLock );
3818
+
3819
+ if (progress )
3820
+ pgstat_progress_end_command ();
3784
3821
}
3785
3822
3786
3823
/*
0 commit comments