pub struct StatsManager { /* private fields */ }Expand description
Manages GPU-resident statistics for all relations.
The StatsManager is the central repository for relation statistics and join
selectivity information. It provides methods for:
- Registering new relations and tracking their statistics
- Updating cardinality and access patterns
- Estimating join cardinalities using cached selectivity data
- Managing relation “heat” for LRU-style eviction
§Thread Safety
This type is not thread-safe. For concurrent access, wrap in appropriate
synchronization primitives (e.g., RwLock).
§Example
use xlog_stats::StatsManager;
use xlog_core::RelId;
let mut mgr = StatsManager::new();
// Register relations
mgr.register_relation(RelId(1));
mgr.register_relation(RelId(2));
// Update statistics
mgr.update_cardinality(RelId(1), 10_000);
mgr.update_cardinality(RelId(2), 5_000);
// Estimate join cardinality
let estimate = mgr.estimate_join_cardinality(RelId(1), RelId(2), &[0], &[0]);Implementations§
Source§impl StatsManager
impl StatsManager
Sourcepub fn register_relation(&mut self, rel_id: RelId)
pub fn register_relation(&mut self, rel_id: RelId)
Registers a new relation for statistics tracking.
If the relation is already registered, this is a no-op.
§Arguments
rel_id- The unique identifier for the relation
Sourcepub fn snapshot(&self) -> StatsSnapshot
pub fn snapshot(&self) -> StatsSnapshot
Create a snapshot of all currently tracked statistics.
Sourcepub fn merge_snapshot(&mut self, snapshot: &StatsSnapshot)
pub fn merge_snapshot(&mut self, snapshot: &StatsSnapshot)
Merge a previously captured snapshot into this manager.
Existing entries are overwritten with the snapshot values.
Sourcepub fn unregister_relation(&mut self, rel_id: RelId) -> Option<RelationStats>
pub fn unregister_relation(&mut self, rel_id: RelId) -> Option<RelationStats>
Sourcepub fn get_relation_stats(&self, rel_id: RelId) -> Option<&RelationStats>
pub fn get_relation_stats(&self, rel_id: RelId) -> Option<&RelationStats>
Sourcepub fn get_relation_stats_mut(
&mut self,
rel_id: RelId,
) -> Option<&mut RelationStats>
pub fn get_relation_stats_mut( &mut self, rel_id: RelId, ) -> Option<&mut RelationStats>
Sourcepub fn update_cardinality(&mut self, rel_id: RelId, rows: u64)
pub fn update_cardinality(&mut self, rel_id: RelId, rows: u64)
Updates the cardinality (row count) for a relation.
If the relation is not registered, this is a no-op.
§Arguments
rel_id- The relation to updaterows- The new cardinality estimate
Sourcepub fn update_byte_size(&mut self, rel_id: RelId, bytes: u64)
pub fn update_byte_size(&mut self, rel_id: RelId, bytes: u64)
Updates the byte size estimate for a relation.
If the relation is not registered, this is a no-op.
§Arguments
rel_id- The relation to updatebytes- The estimated total size in bytes
Sourcepub fn record_access(&mut self, rel_id: RelId)
pub fn record_access(&mut self, rel_id: RelId)
Records an access to a relation, updating its heat and timestamp.
If the relation is not registered, this is a no-op.
§Arguments
rel_id- The relation that was accessed
Sourcepub fn add_column_stats(&mut self, rel_id: RelId, col_stats: ColumnStats)
pub fn add_column_stats(&mut self, rel_id: RelId, col_stats: ColumnStats)
Adds column statistics to a relation.
If the relation is not registered, this is a no-op.
§Arguments
rel_id- The relation to updatecol_stats- The column statistics to add
Sourcepub fn estimate_join_cardinality(
&self,
left_rel: RelId,
right_rel: RelId,
left_keys: &[usize],
right_keys: &[usize],
) -> u64
pub fn estimate_join_cardinality( &self, left_rel: RelId, right_rel: RelId, left_keys: &[usize], right_keys: &[usize], ) -> u64
Estimates the output cardinality for a join between two relations.
Uses cached selectivity if available, otherwise uses a default heuristic.
The estimation formula is: left_card * right_card * selectivity.
§Arguments
left_rel- The left relation in the joinright_rel- The right relation in the joinleft_keys- Column indices used as join keys on the left (currently for future use)right_keys- Column indices used as join keys on the right (currently for future use)
§Returns
The estimated output cardinality (minimum of 1)
Sourcepub fn record_join_result(
&mut self,
left_rel: RelId,
right_rel: RelId,
left_keys: Vec<usize>,
right_keys: Vec<usize>,
input_rows: u64,
output_rows: u64,
)
pub fn record_join_result( &mut self, left_rel: RelId, right_rel: RelId, left_keys: Vec<usize>, right_keys: Vec<usize>, input_rows: u64, output_rows: u64, )
Records the result of a join execution to improve future estimates.
Updates the selectivity model using exponential moving average:
new_selectivity = old_selectivity * 0.7 + observed_selectivity * 0.3
§Arguments
left_rel- The left relation in the joinright_rel- The right relation in the joinleft_keys- Column indices used as join keys on the leftright_keys- Column indices used as join keys on the rightinput_rows- Product of input relation cardinalitiesoutput_rows- Actual output row count
Sourcepub fn set_join_selectivity(
&mut self,
left_rel: RelId,
right_rel: RelId,
left_keys: Vec<usize>,
right_keys: Vec<usize>,
selectivity: f64,
)
pub fn set_join_selectivity( &mut self, left_rel: RelId, right_rel: RelId, left_keys: Vec<usize>, right_keys: Vec<usize>, selectivity: f64, )
Set (or overwrite) the join selectivity between two relations.
This is useful for seeding the optimizer from external observations (e.g., runtime stats).
Sourcepub fn get_join_selectivity(
&self,
left_rel: RelId,
right_rel: RelId,
) -> Option<&JoinSelectivity>
pub fn get_join_selectivity( &self, left_rel: RelId, right_rel: RelId, ) -> Option<&JoinSelectivity>
Sourcepub fn decay_all_heat(&mut self, factor: f32)
pub fn decay_all_heat(&mut self, factor: f32)
Decays the heat of all relations by a multiplicative factor.
This should be called periodically (e.g., during garbage collection or memory pressure events) to allow unused relations to cool down.
§Arguments
factor- Multiplicative decay factor (typically 0.0 to 1.0)
Sourcepub fn hot_relations(&self, threshold: f32) -> Vec<RelId>
pub fn hot_relations(&self, threshold: f32) -> Vec<RelId>
Returns the IDs of all “hot” relations above a given heat threshold.
This is useful for identifying frequently accessed relations that should be kept in GPU memory.
§Arguments
threshold- The minimum heat value to be considered “hot”
§Returns
A vector of RelIds for all relations with heat >= threshold
Sourcepub fn cold_relations(&self, threshold: f32) -> Vec<RelId>
pub fn cold_relations(&self, threshold: f32) -> Vec<RelId>
Sourcepub fn relation_count(&self) -> usize
pub fn relation_count(&self) -> usize
Returns the total number of registered relations.
Sourcepub fn relation_ids(&self) -> impl Iterator<Item = RelId> + '_
pub fn relation_ids(&self) -> impl Iterator<Item = RelId> + '_
Returns an iterator over all registered relation IDs.
Sourcepub fn total_byte_size(&self) -> u64
pub fn total_byte_size(&self) -> u64
Returns the total estimated bytes across all relations.
Sourcepub fn total_cardinality(&self) -> u64
pub fn total_cardinality(&self) -> u64
Returns the total cardinality across all relations.