@@ -1071,6 +1071,73 @@ 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+
1091+ if (!ts_chunk_is_compressed (chunk ) || ts_chunk_is_frozen (chunk ))
1092+ {
1093+ ereport (NOTICE ,
1094+ (errcode (ERRCODE_OBJECT_NOT_IN_PREREQUISITE_STATE ),
1095+ errmsg ("chunk \"%s.%s\" is uncompressed or frozen, skipping" ,
1096+ NameStr (chunk -> fd .schema_name ),
1097+ NameStr (chunk -> fd .table_name ))));
1098+ PG_RETURN_VOID ();
1099+ }
1100+
1101+ CompressionSettings * chunk_settings = ts_compression_settings_get (chunk -> table_id );
1102+ CompressionSettings * ht_settings = ts_compression_settings_get (chunk -> hypertable_relid );
1103+
1104+ if (ht_settings -> fd .index == NULL )
1105+ {
1106+ ereport (NOTICE ,
1107+ (errmsg ("no sparse index configured on hypertable \"%s\" for chunk \"%s.%s\", "
1108+ "skipping" ,
1109+ get_rel_name (chunk -> hypertable_relid ),
1110+ NameStr (chunk -> fd .schema_name ),
1111+ NameStr (chunk -> fd .table_name ))));
1112+ PG_RETURN_VOID ();
1113+ }
1114+
1115+ /* Orderby changes require recompression since batch data is physically sorted by orderby */
1116+ if (!ts_array_equal (chunk_settings -> fd .orderby , ht_settings -> fd .orderby ))
1117+ {
1118+ ereport (NOTICE ,
1119+ (errmsg ("orderby settings for chunk \"%s.%s\" differ from hypertable \"%s\"" ,
1120+ NameStr (chunk -> fd .schema_name ),
1121+ NameStr (chunk -> fd .table_name ),
1122+ get_rel_name (chunk -> hypertable_relid )),
1123+ errhint ("Use compress_chunk(chunk, recompress => true) to recompress." )));
1124+ PG_RETURN_VOID ();
1125+ }
1126+
1127+ if (!force && ts_sparse_index_equal (chunk_settings -> fd .index , ht_settings -> fd .index ))
1128+ {
1129+ ereport (NOTICE ,
1130+ (errmsg ("sparse index settings for chunk \"%s.%s\" already match hypertable, "
1131+ "skipping (use force => true to override)" ,
1132+ NameStr (chunk -> fd .schema_name ),
1133+ NameStr (chunk -> fd .table_name ))));
1134+ PG_RETURN_VOID ();
1135+ }
1136+
1137+ rebuild_sparse_index_impl (chunk , force );
1138+ PG_RETURN_VOID ();
1139+ }
1140+
10741141/*
10751142 * This is hacky but it doesn't matter. We just want to check for the existence of such an index
10761143 * on the compressed chunk.
0 commit comments