@@ -94,20 +94,21 @@ static char *ChooseIndexName(const char *tabname, Oid namespaceId,
9494 bool primary , bool isconstraint );
9595static char * ChooseIndexNameAddition (const List * colnames );
9696static List * ChooseIndexColumnNames (const List * indexElems );
97- static void ReindexIndex (const RangeVar * indexRelation , const ReindexParams * params ,
97+ static void ReindexIndex (const ReindexStmt * stmt , const ReindexParams * params ,
9898 bool isTopLevel );
9999static void RangeVarCallbackForReindexIndex (const RangeVar * relation ,
100100 Oid relId , Oid oldRelId , void * arg );
101- static Oid ReindexTable (const RangeVar * relation , const ReindexParams * params ,
101+ static Oid ReindexTable (const ReindexStmt * stmt , const ReindexParams * params ,
102102 bool isTopLevel );
103- static void ReindexMultipleTables (const char * objectName ,
104- ReindexObjectType objectKind , const ReindexParams * params );
103+ static void ReindexMultipleTables (const ReindexStmt * stmt ,
104+ const ReindexParams * params );
105105static void reindex_error_callback (void * arg );
106- static void ReindexPartitions (Oid relid , const ReindexParams * params ,
107- bool isTopLevel );
108- static void ReindexMultipleInternal (const List * relids ,
106+ static void ReindexPartitions (const ReindexStmt * stmt , Oid relid ,
107+ const ReindexParams * params , bool isTopLevel );
108+ static void ReindexMultipleInternal (const ReindexStmt * stmt , const List * relids ,
109109 const ReindexParams * params );
110- static bool ReindexRelationConcurrently (Oid relationOid ,
110+ static bool ReindexRelationConcurrently (const ReindexStmt * stmt ,
111+ Oid relationOid ,
111112 const ReindexParams * params );
112113static void update_relispartition (Oid relationId , bool newval );
113114static inline void set_indexsafe_procflags (void );
@@ -2735,10 +2736,10 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
27352736 switch (stmt -> kind )
27362737 {
27372738 case REINDEX_OBJECT_INDEX :
2738- ReindexIndex (stmt -> relation , & params , isTopLevel );
2739+ ReindexIndex (stmt , & params , isTopLevel );
27392740 break ;
27402741 case REINDEX_OBJECT_TABLE :
2741- ReindexTable (stmt -> relation , & params , isTopLevel );
2742+ ReindexTable (stmt , & params , isTopLevel );
27422743 break ;
27432744 case REINDEX_OBJECT_SCHEMA :
27442745 case REINDEX_OBJECT_SYSTEM :
@@ -2754,7 +2755,7 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
27542755 (stmt -> kind == REINDEX_OBJECT_SCHEMA ) ? "REINDEX SCHEMA" :
27552756 (stmt -> kind == REINDEX_OBJECT_SYSTEM ) ? "REINDEX SYSTEM" :
27562757 "REINDEX DATABASE" );
2757- ReindexMultipleTables (stmt -> name , stmt -> kind , & params );
2758+ ReindexMultipleTables (stmt , & params );
27582759 break ;
27592760 default :
27602761 elog (ERROR , "unrecognized object type: %d" ,
@@ -2768,8 +2769,9 @@ ExecReindex(ParseState *pstate, const ReindexStmt *stmt, bool isTopLevel)
27682769 * Recreate a specific index.
27692770 */
27702771static void
2771- ReindexIndex (const RangeVar * indexRelation , const ReindexParams * params , bool isTopLevel )
2772+ ReindexIndex (const ReindexStmt * stmt , const ReindexParams * params , bool isTopLevel )
27722773{
2774+ const RangeVar * indexRelation = stmt -> relation ;
27732775 struct ReindexIndexCallbackState state ;
27742776 Oid indOid ;
27752777 char persistence ;
@@ -2802,16 +2804,16 @@ ReindexIndex(const RangeVar *indexRelation, const ReindexParams *params, bool is
28022804 relkind = get_rel_relkind (indOid );
28032805
28042806 if (relkind == RELKIND_PARTITIONED_INDEX )
2805- ReindexPartitions (indOid , params , isTopLevel );
2807+ ReindexPartitions (stmt , indOid , params , isTopLevel );
28062808 else if ((params -> options & REINDEXOPT_CONCURRENTLY ) != 0 &&
28072809 persistence != RELPERSISTENCE_TEMP )
2808- ReindexRelationConcurrently (indOid , params );
2810+ ReindexRelationConcurrently (stmt , indOid , params );
28092811 else
28102812 {
28112813 ReindexParams newparams = * params ;
28122814
28132815 newparams .options |= REINDEXOPT_REPORT_PROGRESS ;
2814- reindex_index (indOid , false, persistence , & newparams );
2816+ reindex_index (stmt , indOid , false, persistence , & newparams );
28152817 }
28162818}
28172819
@@ -2891,10 +2893,11 @@ RangeVarCallbackForReindexIndex(const RangeVar *relation,
28912893 * Recreate all indexes of a table (and of its toast table, if any)
28922894 */
28932895static Oid
2894- ReindexTable (const RangeVar * relation , const ReindexParams * params , bool isTopLevel )
2896+ ReindexTable (const ReindexStmt * stmt , const ReindexParams * params , bool isTopLevel )
28952897{
28962898 Oid heapOid ;
28972899 bool result ;
2900+ const RangeVar * relation = stmt -> relation ;
28982901
28992902 /*
29002903 * The lock level used here should match reindex_relation().
@@ -2911,11 +2914,11 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe
29112914 RangeVarCallbackOwnsTable , NULL );
29122915
29132916 if (get_rel_relkind (heapOid ) == RELKIND_PARTITIONED_TABLE )
2914- ReindexPartitions (heapOid , params , isTopLevel );
2917+ ReindexPartitions (stmt , heapOid , params , isTopLevel );
29152918 else if ((params -> options & REINDEXOPT_CONCURRENTLY ) != 0 &&
29162919 get_rel_persistence (heapOid ) != RELPERSISTENCE_TEMP )
29172920 {
2918- result = ReindexRelationConcurrently (heapOid , params );
2921+ result = ReindexRelationConcurrently (stmt , heapOid , params );
29192922
29202923 if (!result )
29212924 ereport (NOTICE ,
@@ -2927,7 +2930,7 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe
29272930 ReindexParams newparams = * params ;
29282931
29292932 newparams .options |= REINDEXOPT_REPORT_PROGRESS ;
2930- result = reindex_relation (heapOid ,
2933+ result = reindex_relation (stmt , heapOid ,
29312934 REINDEX_REL_PROCESS_TOAST |
29322935 REINDEX_REL_CHECK_CONSTRAINTS ,
29332936 & newparams );
@@ -2949,9 +2952,9 @@ ReindexTable(const RangeVar *relation, const ReindexParams *params, bool isTopLe
29492952 * That means this must not be called within a user transaction block!
29502953 */
29512954static void
2952- ReindexMultipleTables (const char * objectName , ReindexObjectType objectKind ,
2953- const ReindexParams * params )
2955+ ReindexMultipleTables (const ReindexStmt * stmt , const ReindexParams * params )
29542956{
2957+
29552958 Oid objectOid ;
29562959 Relation relationRelation ;
29572960 TableScanDesc scan ;
@@ -2963,6 +2966,8 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
29632966 int num_keys ;
29642967 bool concurrent_warning = false;
29652968 bool tablespace_warning = false;
2969+ const char * objectName = stmt -> name ;
2970+ const ReindexObjectType objectKind = stmt -> kind ;
29662971
29672972 Assert (objectKind == REINDEX_OBJECT_SCHEMA ||
29682973 objectKind == REINDEX_OBJECT_SYSTEM ||
@@ -3158,7 +3163,7 @@ ReindexMultipleTables(const char *objectName, ReindexObjectType objectKind,
31583163 * Process each relation listed in a separate transaction. Note that this
31593164 * commits and then starts a new transaction immediately.
31603165 */
3161- ReindexMultipleInternal (relids , params );
3166+ ReindexMultipleInternal (stmt , relids , params );
31623167
31633168 MemoryContextDelete (private_context );
31643169}
@@ -3188,7 +3193,7 @@ reindex_error_callback(void *arg)
31883193 * by the caller.
31893194 */
31903195static void
3191- ReindexPartitions (Oid relid , const ReindexParams * params , bool isTopLevel )
3196+ ReindexPartitions (const ReindexStmt * stmt , Oid relid , const ReindexParams * params , bool isTopLevel )
31923197{
31933198 List * partitions = NIL ;
31943199 char relkind = get_rel_relkind (relid );
@@ -3264,7 +3269,7 @@ ReindexPartitions(Oid relid, const ReindexParams *params, bool isTopLevel)
32643269 * Process each partition listed in a separate transaction. Note that
32653270 * this commits and then starts a new transaction immediately.
32663271 */
3267- ReindexMultipleInternal (partitions , params );
3272+ ReindexMultipleInternal (stmt , partitions , params );
32683273
32693274 /*
32703275 * Clean up working storage --- note we must do this after
@@ -3282,7 +3287,7 @@ ReindexPartitions(Oid relid, const ReindexParams *params, bool isTopLevel)
32823287 * and starts a new transaction when finished.
32833288 */
32843289static void
3285- ReindexMultipleInternal (const List * relids , const ReindexParams * params )
3290+ ReindexMultipleInternal (const ReindexStmt * stmt , const List * relids , const ReindexParams * params )
32863291{
32873292 ListCell * l ;
32883293
@@ -3341,7 +3346,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params)
33413346 ReindexParams newparams = * params ;
33423347
33433348 newparams .options |= REINDEXOPT_MISSING_OK ;
3344- (void ) ReindexRelationConcurrently (relid , & newparams );
3349+ (void ) ReindexRelationConcurrently (stmt , relid , & newparams );
33453350 /* ReindexRelationConcurrently() does the verbose output */
33463351 }
33473352 else if (relkind == RELKIND_INDEX )
@@ -3350,7 +3355,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params)
33503355
33513356 newparams .options |=
33523357 REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK ;
3353- reindex_index (relid , false, relpersistence , & newparams );
3358+ reindex_index (stmt , relid , false, relpersistence , & newparams );
33543359 PopActiveSnapshot ();
33553360 /* reindex_index() does the verbose output */
33563361 }
@@ -3361,7 +3366,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params)
33613366
33623367 newparams .options |=
33633368 REINDEXOPT_REPORT_PROGRESS | REINDEXOPT_MISSING_OK ;
3364- result = reindex_relation (relid ,
3369+ result = reindex_relation (stmt , relid ,
33653370 REINDEX_REL_PROCESS_TOAST |
33663371 REINDEX_REL_CHECK_CONSTRAINTS ,
33673372 & newparams );
@@ -3406,7 +3411,7 @@ ReindexMultipleInternal(const List *relids, const ReindexParams *params)
34063411 * anyway, and a non-concurrent reindex is more efficient.
34073412 */
34083413static bool
3409- ReindexRelationConcurrently (Oid relationOid , const ReindexParams * params )
3414+ ReindexRelationConcurrently (const ReindexStmt * stmt , Oid relationOid , const ReindexParams * params )
34103415{
34113416 typedef struct ReindexIndexInfo
34123417 {
@@ -3843,6 +3848,20 @@ ReindexRelationConcurrently(Oid relationOid, const ReindexParams *params)
38433848 SetUserIdAndSecContext (save_userid , save_sec_context );
38443849
38453850 table_close (heapRel , NoLock );
3851+
3852+ /*
3853+ * If a statement is available, telling that this comes from a REINDEX
3854+ * command, collect the new index for event triggers.
3855+ */
3856+ if (stmt )
3857+ {
3858+ ObjectAddress address ;
3859+
3860+ ObjectAddressSet (address , RelationRelationId , newIndexId );
3861+ EventTriggerCollectSimpleCommand (address ,
3862+ InvalidObjectAddress ,
3863+ (Node * ) stmt );
3864+ }
38463865 }
38473866
38483867 /*
0 commit comments