uqa-engine 0.6.2

Engine: schema-aware table store, catalog restore, transactions
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Durable automatic-statistics scheduling, independent of query planning.

mod cache;
mod coordinator;
pub(crate) use coordinator::{shared_statistics, StatisticsCoordinator};
pub(crate) mod value_size;
mod worker;
pub(crate) use cache::StatisticsSnapshots;

use std::collections::BTreeMap;
use std::sync::atomic::{AtomicUsize, Ordering};
use std::sync::{mpsc, Arc};
use std::time::{SystemTime, UNIX_EPOCH};

use parking_lot::Mutex;
use uqa_storage::StorageBackendResult;

use crate::{Engine, TableState};

pub(crate) use uqa_storage::statistics_maintenance::StatisticsMaintenance as MaintenanceState;

type SampledDataGenerations = Arc<[(String, Option<u64>)]>;

#[derive(Clone, Default)]
pub(crate) struct StatisticsChange {
    object_id: [u8; 16],
    count: u64,
    reset: bool,
    /// For changes a session keeps: a commit sequence at or after the last commit that made them, or `None` when its provider numbers no commits. An analysis that sampled at or after it has seen them.
    through: Option<u64>,
    /// Data generations a staged sample observed. The committing writer rechecks them after earlier publications, including writes whose counters stayed private.
    sampled_generations: Option<SampledDataGenerations>,
}

pub(crate) type StatisticsChanges = BTreeMap<String, StatisticsChange>;

/// What a commit did with the statistics changes of its transaction. The session takes it over once the commit has succeeded: a failed or undone commit leaves what the session kept as it was.
#[derive(Clone, Default)]
pub(crate) struct StatisticsSettlement {
    /// Tables whose maintenance record the transaction wrote, with every change the session had kept of them.
    recorded: Vec<String>,
    /// Changes the session keeps to itself, including the ones it kept before.
    kept: StatisticsChanges,
}

impl StatisticsSettlement {
    /// Whether the transaction wrote a maintenance record, which the automatic statistics worker should see.
    pub(crate) fn recorded(&self) -> bool {
        !self.recorded.is_empty()
    }
}

/// Process-local health of the database's automatic statistics worker.
/// A failed refresh retains its durable pending state and is retried.
#[derive(Clone, Debug, Default)]
pub struct AutomaticStatisticsStatus {
    pub running: bool,
    pub completed: u64,
    pub last_error: Option<String>,
}

#[derive(Default)]
pub(crate) struct AutomaticStatistics {
    clients: AtomicUsize,
    control: Mutex<Option<StatisticsWorker>>,
    status: Mutex<AutomaticStatisticsStatus>,
}

struct StatisticsWorker {
    sender: mpsc::SyncSender<()>,
    cancellation: uqa_core::CancellationToken,
    thread: std::thread::JoinHandle<()>,
}

pub(crate) fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map_or(0, |duration| {
            u64::try_from(duration.as_millis()).unwrap_or(u64::MAX)
        })
}

impl Engine {
    pub fn automatic_statistics_status(&self) -> AutomaticStatisticsStatus {
        self.statistics.automatic_statistics.status.lock().clone()
    }

    pub fn automatic_diskann_maintenance_status(&self) -> crate::DiskANNMaintenanceStatus {
        self.statistics.diskann.lock().clone()
    }

    /// Database-shared, process-local rebuild thresholds for future maintenance admissions.
    pub fn diskann_rebuild_policy(&self) -> crate::DiskANNRebuildPolicy {
        *self.statistics.diskann_policy.lock()
    }

    /// Revisit pending changes under this policy at the next maintenance poll. Current builds retain their admitted policy and resource limits; this setting is not persisted.
    pub fn set_diskann_rebuild_policy(&self, policy: crate::DiskANNRebuildPolicy) {
        *self.statistics.diskann_policy.lock() = policy;
        self.start_automatic_statistics();
    }

    pub(crate) fn record_statistics_change(
        &self,
        name: &str,
        table: &TableState,
        count: u64,
    ) -> StorageBackendResult<()> {
        if self.storage.catalog.is_none()
            || table.persistence == uqa_sql::ast::RelationPersistence::Temporary
        {
            let previous_rows = table
                .column_stats
                .read()
                .values()
                .next()
                .map(|stats| stats.row_count);
            return table.statistics_maintenance.lock().record_changes(
                table.object_id(),
                count,
                previous_rows,
                now_ms(),
            );
        }
        let mut stack = self.session.transactions.lock();
        if let Some(frame) = stack.last_mut() {
            let change = frame.statistics_changes.entry(name.to_owned()).or_default();
            if change.object_id != table.object_id() {
                *change = StatisticsChange {
                    object_id: table.object_id(),
                    count: 0,
                    reset: false,
                    through: None,
                    sampled_generations: None,
                };
            }
            change.count = change.count.saturating_add(count);
            return Ok(());
        }
        drop(stack);
        let changes = BTreeMap::from([(
            name.to_owned(),
            StatisticsChange {
                object_id: table.object_id(),
                count,
                reset: false,
                through: None,
                sampled_generations: None,
            },
        )]);
        let settlement = self.persist_statistics_changes(&changes)?;
        self.settle_statistics_changes(settlement);
        self.start_automatic_statistics();
        Ok(())
    }

    /// Write the maintenance records the changes of a committing transaction call for. A session keeps the changes that decide nothing ([`MaintenanceState::defers`]) to itself and records them with a later commit, where an analysis learns of every committed write from the table's data generation instead of from this record.
    pub(crate) fn persist_statistics_changes(
        &self,
        changes: &StatisticsChanges,
    ) -> StorageBackendResult<StatisticsSettlement> {
        let mut settlement = StatisticsSettlement::default();
        let Some(catalog) = self.storage.catalog.as_deref() else {
            return Ok(settlement);
        };
        // A session keeps changes only where an analysis can do without the record: cache generations tell it of every committed write to what it sampled, and numbered commits tell the session which of its changes the analysis saw.
        let keeps = self.epochs.storage_cache_revisions.lock().is_some()
            && self
                .storage
                .backend
                .as_ref()
                .is_some_and(|backend| backend.transaction_model().is_versioned());
        let kept = self.session.kept_statistics.lock().clone();
        let now = now_ms();
        let revisions = if changes
            .values()
            .any(|change| change.sampled_generations.is_some())
        {
            catalog.cache_revisions()?
        } else {
            None
        };
        let tables = self.storage.tables.read();
        for (name, change) in changes {
            if change.count == 0 && change.sampled_generations.is_none() {
                continue;
            }
            // DROP/recreate and rename cannot attach the old relation's
            // pending maintenance to a different durable object.
            let Some(table) = tables.iter().find_map(|(relation, table)| {
                (relation.qualified_name() == *name && table.object_id() == change.object_id)
                    .then_some(table)
            }) else {
                continue;
            };
            let mut state = MaintenanceState::load_for(catalog, name, change.object_id)?;
            if let Some(sampled) = &change.sampled_generations {
                let stale_sample = revisions.as_ref().is_none_or(|current| {
                    sampled.iter().any(|(member, generation)| {
                        current.table_data.get(member).copied() != *generation
                    })
                });
                if stale_sample && !state.dirty() {
                    state.record_changes(change.object_id, 1, None, now)?;
                    state.save(catalog, name)?;
                    table.column_stats_dirty.store(true, Ordering::Release);
                    settlement.recorded.push(name.clone());
                }
                if change.count == 0 {
                    continue;
                }
            }
            // What the session kept counts unless an analysis has sampled the commits that made it.
            let earlier = kept
                .get(name)
                .filter(|earlier| {
                    earlier.object_id == change.object_id && !state.covers(earlier.through)
                })
                .map_or(0, |earlier| earlier.count);
            let count = earlier.saturating_add(change.count);
            if keeps && state.defers(count, now) {
                settlement.kept.insert(
                    name.clone(),
                    StatisticsChange {
                        object_id: change.object_id,
                        count,
                        reset: false,
                        through: None,
                        sampled_generations: None,
                    },
                );
                continue;
            }
            state.record_changes(
                change.object_id,
                count,
                table
                    .column_stats
                    .read()
                    .values()
                    .next()
                    .map(|stats| stats.row_count),
                now,
            )?;
            state.save(catalog, name)?;
            settlement.recorded.push(name.clone());
        }
        Ok(settlement)
    }

    /// Take over what a successful commit did with its statistics changes. The caller has left the transaction that committed them.
    pub(crate) fn settle_statistics_changes(&self, settlement: StatisticsSettlement) {
        // Read after the commit, so that it lies at or after every commit of the kept changes. A session that is still inside a transaction would read that transaction's earlier view.
        let through = (!settlement.kept.is_empty())
            .then(|| {
                let backend = self.storage.backend.as_ref()?;
                if backend.in_transaction() {
                    return None;
                }
                backend.change_version().ok()?
            })
            .flatten();
        let mut kept = self.session.kept_statistics.lock();
        for name in settlement.recorded {
            kept.remove(&name);
        }
        kept.extend(settlement.kept.into_iter().map(|(name, mut change)| {
            change.through = through;
            (name, change)
        }));
    }

    /// The commit sequence an analysis that samples now reads at, for the record it writes. `None` where commits are not numbered, and in a transaction that reads its data at an earlier fixed snapshot than its storage view.
    pub(crate) fn statistics_sample_sequence(&self) -> Option<u64> {
        let backend = self.storage.backend.as_ref()?;
        if !backend.transaction_model().is_versioned()
            || self.current_transaction_uses_fixed_snapshot()
        {
            return None;
        }
        backend.change_version().ok().flatten()
    }

    pub(crate) fn merge_statistics_changes(
        into: &mut StatisticsChanges,
        changes: StatisticsChanges,
    ) {
        for (name, change) in changes {
            let previous = into.entry(name).or_default();
            if previous.object_id == change.object_id && !change.reset {
                previous.count = previous.count.saturating_add(change.count);
                if change.sampled_generations.is_some() {
                    previous.sampled_generations = change.sampled_generations;
                }
            } else {
                *previous = change;
            }
        }
    }

    /// Keep a sample's observation in the same transaction/savepoint state as its staged statistics. Final commit rechecks it under writer preparation, rather than relying on the earlier publication decision.
    pub(crate) fn retain_statistics_sample(
        &self,
        name: &str,
        object_id: [u8; 16],
        generations: Option<Vec<(String, Option<u64>)>>,
    ) {
        let Some(generations) = generations else {
            return;
        };
        let mut stack = self.session.transactions.lock();
        let frame = stack
            .last_mut()
            .expect("statistics publication owns a transaction");
        let change = frame.statistics_changes.entry(name.to_owned()).or_default();
        if change.object_id != object_id {
            *change = StatisticsChange {
                object_id,
                ..StatisticsChange::default()
            };
        }
        change.sampled_generations = Some(generations.into());
    }

    pub(crate) fn clear_pending_statistics_changes(&self, name: &str, object_id: [u8; 16]) {
        if let Some(frame) = self.session.transactions.lock().last_mut() {
            // A child analysis covers its parent's pending changes only if the child commits. Preserve the parent entry across a child rollback.
            frame.statistics_changes.insert(
                name.to_owned(),
                StatisticsChange {
                    object_id,
                    count: 0,
                    reset: true,
                    through: None,
                    sampled_generations: None,
                },
            );
        }
    }

    pub(crate) fn start_automatic_statistics(&self) {
        let Some(provider) = self.storage.provider.as_ref() else {
            return;
        };
        if self.session.statistics_worker.load(Ordering::Acquire) {
            return;
        }
        let automatic = &self.statistics.automatic_statistics;
        if !self.session.statistics_client.swap(true, Ordering::AcqRel) {
            automatic.clients.fetch_add(1, Ordering::AcqRel);
        }
        let mut control = automatic.control.lock();
        if let Some(worker) = control.as_ref() {
            // Session admission only retains the worker. Sending a liveness
            // probe wakes its wait without advancing the maintenance deadline.
            if !worker.thread.is_finished() {
                return;
            }
            *control = None;
        }
        let (requests, receiver) = mpsc::sync_channel(1);
        let provider = Arc::downgrade(provider);
        let manager = Arc::downgrade(&self.row_locks);
        let statistics = Arc::downgrade(&self.statistics);
        let cancellation = uqa_core::CancellationToken::new();
        let worker_cancellation = cancellation.clone();
        match std::thread::Builder::new()
            .name("uqa-maintenance".into())
            .spawn(move || {
                worker::run(
                    &provider,
                    &manager,
                    &statistics,
                    &receiver,
                    &worker_cancellation,
                );
            }) {
            Ok(thread) => {
                *control = Some(StatisticsWorker {
                    sender: requests,
                    cancellation,
                    thread,
                });
            }
            Err(error) => {
                automatic.status.lock().last_error = Some(error.to_string());
            }
        }
    }

    pub(crate) fn release_automatic_statistics_client(&self) {
        if self.session.statistics_client.swap(false, Ordering::AcqRel) {
            let automatic = &self.statistics.automatic_statistics;
            if automatic.clients.fetch_sub(1, Ordering::AcqRel) != 1 {
                return;
            }
            let worker = {
                let mut control = automatic.control.lock();
                if automatic.clients.load(Ordering::Acquire) != 0 {
                    return;
                }
                control.take()
            };
            if let Some(worker) = worker {
                worker.cancellation.cancel();
                let _ = worker.sender.try_send(());
                // Release every maintenance connection before the final
                // client's drop returns. redb requires this for immediate
                // reopen; SQLite requires it for final WAL checkpointing.
                if worker.thread.join().is_err() {
                    automatic.status.lock().last_error =
                        Some("automatic statistics worker panicked".into());
                }
            }
        }
    }
}