@@ -1980,7 +1980,7 @@ index_build(Relation heapRelation,
19801980 * created it, or truncated twice in a subsequent transaction, the
19811981 * relfilenode won't change, and nothing needs to be done here.
19821982 */
1983- if (heapRelation -> rd_rel -> relpersistence == RELPERSISTENCE_UNLOGGED &&
1983+ if (indexRelation -> rd_rel -> relpersistence == RELPERSISTENCE_UNLOGGED &&
19841984!smgrexists (indexRelation -> rd_smgr ,INIT_FORKNUM ))
19851985{
19861986RegProcedure ambuildempty = indexRelation -> rd_am -> ambuildempty ;
@@ -3130,7 +3130,7 @@ IndexGetRelation(Oid indexId, bool missing_ok)
31303130 * reindex_index - This routine is used to recreate a single index
31313131 */
31323132void
3133- reindex_index (Oid indexId ,bool skip_constraint_checks )
3133+ reindex_index (Oid indexId ,bool skip_constraint_checks , char persistence )
31343134{
31353135Relation iRel ,
31363136heapRelation ;
@@ -3191,6 +3191,9 @@ reindex_index(Oid indexId, bool skip_constraint_checks)
31913191indexInfo -> ii_ExclusionStrats = NULL ;
31923192}
31933193
3194+ /* Set the relpersistence of the new index */
3195+ iRel -> rd_rel -> relpersistence = persistence ;
3196+
31943197/* We'll build a new physical relation for the index */
31953198RelationSetNewRelfilenode (iRel ,InvalidTransactionId ,
31963199InvalidMultiXactId );
@@ -3310,6 +3313,12 @@ reindex_index(Oid indexId, bool skip_constraint_checks)
33103313 * performance, other callers should include the flag only after transforming
33113314 * the data in a manner that risks a change in constraint validity.
33123315 *
3316+ * REINDEX_REL_FORCE_INDEXES_UNLOGGED: if true, set the persistence of the
3317+ * rebuilt indexes to unlogged.
3318+ *
3319+ * REINDEX_REL_FORCE_INDEXES_LOGGED: if true, set the persistence of the
3320+ * rebuilt indexes to permanent.
3321+ *
33133322 * Returns true if any indexes were rebuilt (including toast table's index
33143323 * when relevant). Note that a CommandCounterIncrement will occur after each
33153324 * index rebuild.
@@ -3371,6 +3380,7 @@ reindex_relation(Oid relid, int flags)
33713380{
33723381List * doneIndexes ;
33733382ListCell * indexId ;
3383+ char persistence ;
33743384
33753385if (flags & REINDEX_REL_SUPPRESS_INDEX_USE )
33763386{
@@ -3384,6 +3394,17 @@ reindex_relation(Oid relid, int flags)
33843394CommandCounterIncrement ();
33853395}
33863396
3397+ /*
3398+ * Compute persistence of indexes: same as that of owning rel, unless
3399+ * caller specified otherwise.
3400+ */
3401+ if (flags & REINDEX_REL_FORCE_INDEXES_UNLOGGED )
3402+ persistence = RELPERSISTENCE_UNLOGGED ;
3403+ else if (flags & REINDEX_REL_FORCE_INDEXES_PERMANENT )
3404+ persistence = RELPERSISTENCE_PERMANENT ;
3405+ else
3406+ persistence = rel -> rd_rel -> relpersistence ;
3407+
33873408/* Reindex all the indexes. */
33883409doneIndexes = NIL ;
33893410foreach (indexId ,indexIds )
@@ -3393,7 +3414,8 @@ reindex_relation(Oid relid, int flags)
33933414if (is_pg_class )
33943415RelationSetIndexList (rel ,doneIndexes ,InvalidOid );
33953416
3396- reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ));
3417+ reindex_index (indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
3418+ persistence );
33973419
33983420CommandCounterIncrement ();
33993421