@@ -1071,6 +1071,74 @@ tsl_rebuild_columnstore(PG_FUNCTION_ARGS)
10711071 PG_RETURN_VOID ();
10721072}
10731073
1074+ Datum
1075+ tsl_rebuild_sparse_index (PG_FUNCTION_ARGS )
1076+ {
1077+ Oid chunk_relid = PG_ARGISNULL (0 ) ? InvalidOid : PG_GETARG_OID (0 );
1078+ bool force = PG_ARGISNULL (1 ) ? false : PG_GETARG_BOOL (1 );
1079+
1080+ ts_feature_flag_check (FEATURE_HYPERTABLE_COMPRESSION );
1081+
1082+ TS_PREVENT_FUNC_IF_READ_ONLY ();
1083+
1084+ if (!OidIsValid (chunk_relid ))
1085+ {
1086+ ereport (ERROR , (errcode (ERRCODE_INVALID_PARAMETER_VALUE ), errmsg ("invalid chunk OID" )));
1087+ }
1088+
1089+ Chunk * chunk = ts_chunk_get_by_relid (chunk_relid , true);
1090+ ts_hypertable_permissions_check (chunk -> hypertable_relid , GetUserId ());
1091+
1092+ if (!ts_chunk_is_compressed (chunk ) || ts_chunk_is_frozen (chunk ))
1093+ {
1094+ ereport (NOTICE ,
1095+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1096+ errmsg ("chunk \"%s.%s\" is uncompressed or frozen, skipping" ,
1097+ NameStr (chunk -> fd .schema_name ),
1098+ NameStr (chunk -> fd .table_name ))));
1099+ PG_RETURN_VOID ();
1100+ }
1101+
1102+ CompressionSettings * chunk_settings = ts_compression_settings_get (chunk -> table_id );
1103+ CompressionSettings * ht_settings = ts_compression_settings_get (chunk -> hypertable_relid );
1104+
1105+ if (ht_settings -> fd .index == NULL )
1106+ {
1107+ ereport (NOTICE ,
1108+ (errmsg ("no sparse index configured on hypertable \"%s\" for chunk \"%s.%s\", "
1109+ "skipping" ,
1110+ get_rel_name (chunk -> hypertable_relid ),
1111+ NameStr (chunk -> fd .schema_name ),
1112+ NameStr (chunk -> fd .table_name ))));
1113+ PG_RETURN_VOID ();
1114+ }
1115+
1116+ /* Orderby changes require recompression since batch data is physically sorted by orderby */
1117+ if (!ts_array_equal (chunk_settings -> fd .orderby , ht_settings -> fd .orderby ))
1118+ {
1119+ ereport (NOTICE ,
1120+ (errmsg ("orderby settings for chunk \"%s.%s\" differ from hypertable \"%s\"" ,
1121+ NameStr (chunk -> fd .schema_name ),
1122+ NameStr (chunk -> fd .table_name ),
1123+ get_rel_name (chunk -> hypertable_relid )),
1124+ errhint ("Use compress_chunk(chunk, recompress => true) to recompress." )));
1125+ PG_RETURN_VOID ();
1126+ }
1127+
1128+ if (!force && ts_sparse_index_equal (chunk_settings -> fd .index , ht_settings -> fd .index ))
1129+ {
1130+ ereport (NOTICE ,
1131+ (errmsg ("sparse index settings for chunk \"%s.%s\" already match hypertable, "
1132+ "skipping (use force => true to override)" ,
1133+ NameStr (chunk -> fd .schema_name ),
1134+ NameStr (chunk -> fd .table_name ))));
1135+ PG_RETURN_VOID ();
1136+ }
1137+
1138+ rebuild_sparse_index_impl (chunk , force );
1139+ PG_RETURN_VOID ();
1140+ }
1141+
10741142/*
10751143 * This is hacky but it doesn't matter. We just want to check for the existence of such an index
10761144 * on the compressed chunk.
0 commit comments