@@ -776,6 +776,8 @@ index_create(Relation heapRelation,
776776 ((flags & INDEX_CREATE_ADD_CONSTRAINT ) != 0 ));
777777 /* partitioned indexes must never be "built" by themselves */
778778 Assert (!partitioned || (flags & INDEX_CREATE_SKIP_BUILD ));
779+ /* ii_AuxiliaryForIndexId and INDEX_CREATE_AUXILIARY are required both or neither */
780+ Assert (OidIsValid (indexInfo -> ii_AuxiliaryForIndexId ) == auxiliary );
779781
780782 relkind = partitioned ? RELKIND_PARTITIONED_INDEX : RELKIND_INDEX ;
781783 is_exclusion = (indexInfo -> ii_ExclusionOps != NULL );
@@ -1181,6 +1183,15 @@ index_create(Relation heapRelation,
11811183 recordDependencyOn (& myself , & referenced , DEPENDENCY_PARTITION_SEC );
11821184 }
11831185
1186+ /*
1187+ * Record dependency on the main index in case of auxiliary index.
1188+ */
1189+ if (OidIsValid (indexInfo -> ii_AuxiliaryForIndexId ))
1190+ {
1191+ ObjectAddressSet (referenced , RelationRelationId , indexInfo -> ii_AuxiliaryForIndexId );
1192+ recordDependencyOn (& myself , & referenced , DEPENDENCY_AUTO );
1193+ }
1194+
11841195 /* placeholder for normal dependencies */
11851196 addrs = new_object_addresses ();
11861197
@@ -1413,7 +1424,8 @@ index_concurrently_create_copy(Relation heapRelation, Oid oldIndexId,
14131424 true,
14141425 indexRelation -> rd_indam -> amsummarizing ,
14151426 oldInfo -> ii_WithoutOverlaps ,
1416- false);
1427+ false,
1428+ InvalidOid );
14171429
14181430 /*
14191431 * Extract the list of column names and the column numbers for the new
@@ -1581,7 +1593,8 @@ index_concurrently_create_aux(Relation heapRelation, Oid mainIndexId,
15811593 true,
15821594 false, /* aux are not summarizing */
15831595 false, /* aux are not without overlaps */
1584- true /* auxiliary */ );
1596+ true /* auxiliary */ ,
1597+ mainIndexId /* auxiliaryForIndexId */ );
15851598
15861599 /*
15871600 * Extract the list of column names and the column numbers for the new
@@ -2634,7 +2647,8 @@ BuildIndexInfo(Relation index)
26342647 false,
26352648 index -> rd_indam -> amsummarizing ,
26362649 indexStruct -> indisexclusion && indexStruct -> indisunique ,
2637- index -> rd_rel -> relam == STIR_AM_OID /* auxiliary iff STIR */ );
2650+ index -> rd_rel -> relam == STIR_AM_OID /* auxiliary iff STIR */ ,
2651+ InvalidOid /* auxiliary_for_index_id is set only during build */ );
26382652
26392653 /* fill in attribute numbers */
26402654 for (i = 0 ; i < numAtts ; i ++ )
@@ -2695,7 +2709,8 @@ BuildDummyIndexInfo(Relation index)
26952709 false,
26962710 index -> rd_indam -> amsummarizing ,
26972711 indexStruct -> indisexclusion && indexStruct -> indisunique ,
2698- index -> rd_rel -> relam == STIR_AM_OID /* auxiliary iff STIR */ );
2712+ index -> rd_rel -> relam == STIR_AM_OID /* auxiliary iff STIR */ ,
2713+ InvalidOid );
26992714
27002715 /* fill in attribute numbers */
27012716 for (i = 0 ; i < numAtts ; i ++ )
@@ -3870,6 +3885,7 @@ reindex_index(const ReindexStmt *stmt, Oid indexId,
38703885 heapRelation ;
38713886 Oid heapId ;
38723887 Oid save_userid ;
3888+ Oid junkAuxIndexId ;
38733889 int save_sec_context ;
38743890 int save_nestlevel ;
38753891 IndexInfo * indexInfo ;
@@ -3926,6 +3942,19 @@ reindex_index(const ReindexStmt *stmt, Oid indexId,
39263942 pgstat_progress_update_multi_param (2 , progress_cols , progress_vals );
39273943 }
39283944
3945+ /* Check for the auxiliary index for that index, it needs to dropped */
3946+ junkAuxIndexId = get_auxiliary_index (indexId );
3947+ if (OidIsValid (junkAuxIndexId ))
3948+ {
3949+ ObjectAddress object ;
3950+ object .classId = RelationRelationId ;
3951+ object .objectId = junkAuxIndexId ;
3952+ object .objectSubId = 0 ;
3953+ performDeletion (& object , DROP_RESTRICT ,
3954+ PERFORM_DELETION_INTERNAL |
3955+ PERFORM_DELETION_QUIETLY );
3956+ }
3957+
39293958 /*
39303959 * Open the target index relation and get an exclusive lock on it, to
39313960 * ensure that no one else is touching this particular index.
@@ -4214,7 +4243,8 @@ reindex_relation(const ReindexStmt *stmt, Oid relid, int flags,
42144243{
42154244 Relation rel ;
42164245 Oid toast_relid ;
4217- List * indexIds ;
4246+ List * indexIds ,
4247+ * auxIndexIds = NIL ;
42184248 char persistence ;
42194249 bool result = false;
42204250 ListCell * indexId ;
@@ -4303,13 +4333,30 @@ reindex_relation(const ReindexStmt *stmt, Oid relid, int flags,
43034333 else
43044334 persistence = rel -> rd_rel -> relpersistence ;
43054335
4336+ foreach (indexId , indexIds )
4337+ {
4338+ Oid indexOid = lfirst_oid (indexId );
4339+ Oid indexAm = get_rel_relam (indexOid );
4340+
4341+ /* All STIR indexes are auxiliary indexes */
4342+ if (indexAm == STIR_AM_OID )
4343+ {
4344+ if (flags & REINDEX_REL_SUPPRESS_INDEX_USE )
4345+ RemoveReindexPending (indexOid );
4346+ auxIndexIds = lappend_oid (auxIndexIds , indexOid );
4347+ }
4348+ }
4349+
43064350 /* Reindex all the indexes. */
43074351 i = 1 ;
43084352 foreach (indexId , indexIds )
43094353 {
43104354 Oid indexOid = lfirst_oid (indexId );
43114355 Oid indexNamespaceId = get_rel_namespace (indexOid );
4312- Oid indexAm = get_rel_relam (indexOid );
4356+
4357+ /* Auxiliary indexes are going to be dropped during main index rebuild */
4358+ if (list_member_oid (auxIndexIds , indexOid ))
4359+ continue ;
43134360
43144361 /*
43154362 * Skip any invalid indexes on a TOAST table. These can only be
@@ -4335,18 +4382,6 @@ reindex_relation(const ReindexStmt *stmt, Oid relid, int flags,
43354382 continue ;
43364383 }
43374384
4338- if (indexAm == STIR_AM_OID )
4339- {
4340- ereport (WARNING ,
4341- (errcode (ERRCODE_FEATURE_NOT_SUPPORTED ),
4342- errmsg ("skipping reindex of auxiliary index \"%s.%s\"" ,
4343- get_namespace_name (indexNamespaceId ),
4344- get_rel_name (indexOid ))));
4345- if (flags & REINDEX_REL_SUPPRESS_INDEX_USE )
4346- RemoveReindexPending (indexOid );
4347- continue ;
4348- }
4349-
43504385 reindex_index (stmt , indexOid , !(flags & REINDEX_REL_CHECK_CONSTRAINTS ),
43514386 persistence , params );
43524387
0 commit comments