@@ -3891,15 +3891,25 @@ reindex_index(Oid indexId, bool skip_constraint_checks, char persistence,
38913891 * index rebuild.
38923892 */
38933893bool
3894- reindex_relation (Relation rel , int flags , ReindexParams * params )
3894+ reindex_relation (Oid relid , int flags , ReindexParams * params )
38953895{
3896+ Relation rel ;
38963897 Oid toast_relid ;
38973898 List * indexIds ;
38983899 char persistence ;
38993900 bool result ;
39003901 ListCell * indexId ;
39013902 int i ;
39023903
3904+ /*
3905+ * Open and lock the relation. ShareLock is sufficient since we only need
3906+ * to prevent schema and data changes in it. The lock level used here
3907+ * should match ReindexTable().
3908+ */
3909+ if ((params -> options & REINDEXOPT_MISSING_OK ) != 0 )
3910+ rel = try_table_open (relid , ShareLock );
3911+ else
3912+ rel = table_open (relid , ShareLock );
39033913
39043914 /* if relation is gone, leave */
39053915 if (!rel )
@@ -3991,6 +4001,11 @@ reindex_relation(Relation rel, int flags, ReindexParams *params)
39914001 i ++ ;
39924002 }
39934003
4004+ /*
4005+ * Close rel, but continue to hold the lock.
4006+ */
4007+ table_close (rel , NoLock );
4008+
39944009 result = (indexIds != NIL );
39954010
39964011 /*
@@ -4006,20 +4021,10 @@ reindex_relation(Relation rel, int flags, ReindexParams *params)
40064021 * This rule is enforced by setting tablespaceOid to InvalidOid.
40074022 */
40084023 ReindexParams newparams = * params ;
4009- /*
4010- * Open and lock the relation. ShareLock is sufficient since we only need
4011- * to prevent schema and data changes in it. The lock level used here
4012- * should match ReindexTable().
4013- */
4014- Relation toast_rel = table_open (toast_relid , ShareLock );
40154024
40164025 newparams .options &= ~(REINDEXOPT_MISSING_OK );
40174026 newparams .tablespaceOid = InvalidOid ;
4018- result |= reindex_relation (toast_rel , flags , & newparams );
4019- /*
4020- * Close rel, but continue to hold the lock.
4021- */
4022- table_close (toast_rel , NoLock );
4027+ result |= reindex_relation (toast_relid , flags , & newparams );
40234028 }
40244029
40254030 return result ;
0 commit comments