reddb-io-server 1.40.4

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
//! Paced table-history maintenance for standalone embedded runtimes.
use std::time::{Duration, Instant};

use super::RedDBRuntime;
use crate::storage::profile::DeployProfile;
use crate::storage::unified::store::MvccReclaimCursor;
use crate::storage::StoreError;

#[derive(Default)]
pub(super) struct MvccMaintenance {
    current: Option<MvccReclaimCursor>,
    after_collection: Option<String>,
    retry_after: Option<Instant>,
    last_warning: Option<Instant>,
}

impl RedDBRuntime {
    /// Foreground pressure and the periodic worker share one paced cursor.
    /// Never wait behind another collector while a mutation holds row locks.
    pub(super) fn try_reclaim_history_tick(&self) -> bool {
        let Some(mut state) = self.inner.mvcc_maintenance.try_lock() else {
            return false;
        };
        self.reclaim_history_tick(&mut state)
    }

    fn reclaim_history_tick(&self, state: &mut MvccMaintenance) -> bool {
        let options = self.inner.db.options();
        // Replicated profiles need a distributed reader/replica low-water
        // mark before local physical history can be reclaimed automatically.
        if options.read_only
            || options.storage_profile.deploy_profile != DeployProfile::Embedded
            || options.replication.role != crate::replication::ReplicationRole::Standalone
            || self.inner.lifecycle.phase() != super::lifecycle::Phase::Ready
            || !self.config_bool("runtime.mvcc.autovacuum.enabled", true)
        {
            state.current = None;
            return false;
        }
        // A collector must not append physical GC to an unrelated, possibly
        // aborted DML event capture. The periodic worker can resume this cursor.
        if crate::storage::UnifiedStore::deferred_store_wal_capture_active() {
            return false;
        }
        if state.retry_after.is_some_and(|at| Instant::now() < at) {
            return false;
        }
        let store = self.inner.db.store();
        if state
            .current
            .as_ref()
            .is_some_and(|cursor| store.get_collection(&cursor.collection).is_none())
        {
            state.current = None;
        }
        if state.current.is_none() {
            let mut collections = store.list_collections();
            collections.sort_unstable();
            if collections.is_empty() {
                return false;
            }
            let index = state.after_collection.as_ref().map_or(0, |after| {
                collections.partition_point(|name| name <= after) % collections.len()
            });
            state.current = store.mvcc_reclaim_cursor(collections[index].clone());
        }
        let Some(cursor) = state.current.as_mut() else {
            return false;
        };
        // Native GC retains one leaf and decodes one current row at a time.
        // Charge encoded/decoded workspace before taking the publication fence.
        let _workspace = if store.config().resident_memory_bytes_max.is_some() {
            let Some(reservation) =
                self.try_reserve_query_buffer(store.mvcc_reclaim_read_limit().saturating_mul(2))
            else {
                return false;
            };
            Some(reservation)
        } else {
            None
        };
        let scan_budget = self
            .config_u64("runtime.mvcc.autovacuum.scan_rows_per_tick", 4096)
            .clamp(1, 4096) as usize;
        let reclaim_budget = self
            .config_u64("runtime.mvcc.autovacuum.reclaim_rows_per_tick", 256)
            .clamp(1, 256) as usize;
        let result = store.reclaim_mvcc_tick_with(
            cursor,
            &self.snapshot_manager(),
            |collection| {
                self.mvcc_vacuum_cutoff_xid_for(collection)
                    .map_err(|error| StoreError::Serialization(error.to_string()))
            },
            scan_budget,
            reclaim_budget,
        );
        match result {
            Ok(reclaimed) => {
                state.retry_after = None;
                if reclaimed > 0 {
                    tracing::debug!(collection = %cursor.collection, reclaimed_versions = reclaimed,
                        "paced MVCC history reclamation");
                }
                if cursor.finished() {
                    state.after_collection = Some(cursor.collection.clone());
                    state.current = None;
                }
                true
            }
            Err(error) => {
                state.retry_after = Some(Instant::now() + Duration::from_secs(5));
                if state
                    .last_warning
                    .is_none_or(|at| at.elapsed() >= Duration::from_secs(60))
                {
                    tracing::warn!(%error, "MVCC history reclamation deferred");
                    state.last_warning = Some(Instant::now());
                }
                // An unsuccessful WAL-backed batch is not recorded as progress.
                state.current = None;
                false
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use crate::{RedDBOptions, RedDBRuntime};

    #[cfg(unix)]
    #[test]
    fn constant_live_data_churn_reuses_pages_after_cold_gc() {
        use reddb_types::Value;
        use std::os::unix::fs::MetadataExt;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("churn.rdb");
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(&path)
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        // Scheduling is part of this fixture: no sleeps or races against the
        // periodic worker. Every cycle drives the same bounded production GC.
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE churn_rows (id INTEGER PRIMARY KEY, payload TEXT)")
            .unwrap();
        let payload = |generation: usize, id: usize| {
            let mut state = (generation * 32 + id) as u64 + 1;
            // Exercise small inline, near-inline and overflow values in every
            // fixed cycle without making CI depend on bulk-throughput speed.
            let bytes = [180, 4 * 1024, 64 * 1024][id % 3];
            (0..bytes)
                .map(|_| {
                    state ^= state << 13;
                    state ^= state >> 7;
                    state ^= state << 17;
                    (b'a' + (state % 26) as u8) as char
                })
                .collect::<String>()
        };
        let allocated = || {
            [&path, &reddb_file::layout::unified_wal_path(&path)]
                .iter()
                .map(|p| std::fs::metadata(p).map_or(0, |m| m.blocks() * 512))
                .sum::<u64>()
        };
        let mut warmup_allocated = 0;
        let mut peak_after_warmup = 0;
        for generation in 0..120 {
            for start in (0..32).step_by(8) {
                runtime
                    .execute_query(&format!(
                        "DELETE FROM churn_rows WHERE id >= {start} AND id < {}",
                        start + 8,
                    ))
                    .unwrap();
            }
            for start in (0..32).step_by(8) {
                let values = (start..start + 8)
                    .map(|id| format!("({id}, '{}')", payload(generation, id)))
                    .collect::<Vec<_>>()
                    .join(",");
                runtime
                    .execute_query(&format!(
                        "INSERT INTO churn_rows (id, payload) VALUES {values}"
                    ))
                    .unwrap();
            }
            // Force the regression's hard case: obsolete versions are cold,
            // and the cleanup must find them without a resident-only scan.
            runtime.checkpoint().unwrap();
            let store = runtime.db().store();
            let mut cursor = store.mvcc_reclaim_cursor("churn_rows".into()).unwrap();
            let mut reclaimed = 0;
            for _ in 0..128 {
                let count = store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        64,
                        8,
                    )
                    .unwrap();
                assert!(count <= 8, "bounded deletion work");
                reclaimed += count;
                if cursor.finished() {
                    break;
                }
            }
            assert!(
                cursor.finished(),
                "bounded sweep must converge at cycle {generation}"
            );
            assert_eq!(
                reclaimed,
                if generation == 0 { 0 } else { 32 },
                "cycle {generation}"
            );
            drop(cursor);
            runtime.checkpoint().unwrap();
            let result = runtime
                .execute_query("SELECT COUNT(*) AS n FROM churn_rows")
                .unwrap();
            assert_eq!(
                result.result.records[0].get("n").unwrap().display_string(),
                "32"
            );
            if generation == 19 {
                warmup_allocated = allocated();
            }
            if generation >= 20 {
                peak_after_warmup = peak_after_warmup.max(allocated());
            }
            if generation % 20 == 19 {
                eprintln!(
                    "churn_cycles={} allocated_bytes={} reclaimed_versions={reclaimed}",
                    generation + 1,
                    allocated()
                );
            }
        }
        let growth = peak_after_warmup.saturating_sub(warmup_allocated);
        eprintln!("churn_cycles=120 warmup_allocated={warmup_allocated} peak_after_warmup={peak_after_warmup} growth_bytes={growth}");
        assert!(
            growth <= 64 * 1024 * 1024,
            "storage grew {growth} bytes with constant live data"
        );
        drop(runtime);
        let runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(&path)
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        for id in 0..32 {
            let result = runtime
                .execute_query(&format!("SELECT payload FROM churn_rows WHERE id = {id}"))
                .unwrap();
            assert_eq!(result.result.records.len(), 1);
            assert_eq!(
                result.result.records[0].get("payload"),
                Some(&Value::text(payload(119, id).as_str()))
            );
        }
    }

    /// #2895: heartbeat-style `UPDATE ... WHERE id = $1` on a TEXT primary
    /// key must not retain every superseded version, and the primary-key
    /// lookup and a full scan must agree on exactly one live row.
    #[test]
    fn repeated_pk_updates_reclaim_superseded_versions() {
        use reddb_types::Value;
        let directory = tempfile::tempdir().unwrap();
        let path = directory.path().join("heartbeat.rdb");
        let open = || {
            let mut runtime = RedDBRuntime::with_options(
                RedDBOptions::persistent(&path)
                    .with_memory_budget(32 * 1024 * 1024)
                    .with_bounded_disk_residency(true),
            )
            .unwrap();
            drop(runtime.background_tasks.take());
            runtime
        };
        let mut runtime = open();
        // Control stores create their schema through applied migrations,
        // and every applied migration records a VCS commit.
        runtime
            .execute_query(
                "CREATE MIGRATION heartbeats_v1 AS CREATE TABLE heartbeats \
                 (id TEXT PRIMARY KEY, observed_at INTEGER, owner TEXT)",
            )
            .unwrap();
        runtime
            .execute_query("APPLY MIGRATION heartbeats_v1")
            .unwrap();
        runtime
            .execute_query(
                "INSERT INTO heartbeats (id, observed_at, owner) VALUES ('node-a', 0, 'w0')",
            )
            .unwrap();
        let physical = |runtime: &RedDBRuntime| {
            runtime
                .db()
                .store()
                .get_collection("heartbeats")
                .unwrap()
                .count()
        };
        let reported_entities = |runtime: &RedDBRuntime| {
            let result = runtime
                .execute_query("SELECT entities FROM red.collections WHERE name = 'heartbeats'")
                .unwrap();
            result.result.records[0]
                .get("entities")
                .unwrap()
                .display_string()
                .parse::<usize>()
                .unwrap()
        };
        // Drive the same paced collector the maintenance thread runs.
        let collect = |runtime: &RedDBRuntime| {
            for _ in 0..16 {
                runtime.try_reclaim_history_tick();
            }
        };
        const UPDATES: usize = 2000;
        let mut peak = 0;
        for generation in 1..=UPDATES {
            runtime
                .execute_query_with_params(
                    "UPDATE heartbeats SET observed_at = $1, owner = $2 WHERE id = $3",
                    &[
                        Value::Integer(generation as i64),
                        Value::text(format!("w{generation}")),
                        Value::text("node-a"),
                    ],
                )
                .unwrap();
            collect(&runtime);
            if generation % 500 == 0 {
                runtime.checkpoint().unwrap();
                collect(&runtime);
            }
            if generation == UPDATES / 2 {
                // Restarts rehydrate persisted commit horizons.
                drop(runtime);
                runtime = open();
            }
            peak = peak.max(physical(&runtime));
            assert!(
                peak <= 64,
                "superseded versions accumulated after {generation} updates: \
                 {peak} physical versions for one live row"
            );
        }
        runtime.checkpoint().unwrap();
        collect(&runtime);
        let count = runtime
            .execute_query("SELECT COUNT(*) AS n FROM heartbeats")
            .unwrap();
        assert_eq!(
            count.result.records[0].get("n").unwrap().display_string(),
            "1"
        );
        let retained = reported_entities(&runtime);
        eprintln!(
            "updates={UPDATES} peak_physical={peak} retained_physical={} reported_entities={retained}",
            physical(&runtime)
        );
        assert!(
            retained <= 4,
            "SHOW COLLECTIONS reports {retained} entities for one live row"
        );
        let point = runtime
            .execute_query_with_params(
                "SELECT id, observed_at FROM heartbeats WHERE id = $1",
                &[Value::text("node-a")],
            )
            .unwrap();
        assert_eq!(point.result.records.len(), 1, "PK lookup");
        assert_eq!(
            point.result.records[0].get("observed_at"),
            Some(&Value::Integer(UPDATES as i64))
        );
        let scan = runtime
            .execute_query("SELECT id, observed_at FROM heartbeats")
            .unwrap();
        assert_eq!(scan.result.records.len(), 1, "full scan");
        drop(runtime);
        let runtime = open();
        let point = runtime
            .execute_query("SELECT id, observed_at FROM heartbeats WHERE id = 'node-a'")
            .unwrap();
        assert_eq!(point.result.records.len(), 1, "PK lookup after reopen");
        let scan = runtime
            .execute_query("SELECT id, observed_at FROM heartbeats")
            .unwrap();
        assert_eq!(scan.result.records.len(), 1, "full scan after reopen");
    }

    /// #2895: a VCS commit pin protects only history that `AS OF` can read.
    /// Open snapshots still protect every collection.
    /// #2895: after heavy churn of one key, an indexed `WHERE id = $1` reads
    /// O(1) entries instead of every retained version, and EXPLAIN does not
    /// estimate the whole collection for the seek.
    #[test]
    fn indexed_pk_lookup_stays_bounded_under_update_churn() {
        use crate::storage::{EntityId, StoreError};
        use reddb_engine::storage::collection_read::{
            CollectionCursor, CollectionRead, EncodedRecord,
        };
        use reddb_types::Value;
        use std::sync::atomic::{AtomicUsize, Ordering};
        use std::sync::Arc;
        struct CountReads {
            inner: Arc<dyn CollectionRead + Send + Sync>,
            scans: Arc<AtomicUsize>,
            gets: Arc<AtomicUsize>,
        }
        impl CollectionRead for CountReads {
            fn get_record(
                &self,
                id: EntityId,
                bytes: usize,
            ) -> Result<Option<EncodedRecord>, StoreError> {
                self.gets.fetch_add(1, Ordering::Relaxed);
                self.inner.get_record(id, bytes)
            }
            fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.cursor()
            }
            fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.owned_cursor()
            }
        }
        const LIVE_ROWS: usize = 50;
        const UPDATES: i64 = 300;
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("lookup.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query(
                "CREATE MIGRATION windows_v1 AS CREATE TABLE windows \
                 (id TEXT PRIMARY KEY, observed_at INTEGER)",
            )
            .unwrap();
        runtime.execute_query("APPLY MIGRATION windows_v1").unwrap();
        let values = (0..LIVE_ROWS)
            .map(|key| format!("('k{key}', 0)"))
            .collect::<Vec<_>>()
            .join(",");
        runtime
            .execute_query(&format!(
                "INSERT INTO windows (id, observed_at) VALUES {values}"
            ))
            .unwrap();
        runtime.checkpoint().unwrap();
        let collect = |runtime: &RedDBRuntime| {
            for _ in 0..16 {
                runtime.try_reclaim_history_tick();
            }
        };
        for generation in 1..=UPDATES {
            runtime
                .execute_query_with_params(
                    "UPDATE windows SET observed_at = $1 WHERE id = $2",
                    &[Value::Integer(generation), Value::text("k0")],
                )
                .unwrap();
            collect(&runtime);
        }
        runtime.checkpoint().unwrap();
        collect(&runtime);
        let store = runtime.db().store();
        let manager = store.get_collection("windows").unwrap();
        assert!(
            manager.count() <= LIVE_ROWS + 2,
            "{} physical entities for {LIVE_ROWS} live rows",
            manager.count()
        );
        let mut resident = 0;
        manager.for_each_entity(|_| {
            resident += 1;
            true
        });
        assert!(
            resident <= 4,
            "{resident} resident versions after reclamation"
        );
        let root = manager.backing_root_page().unwrap();
        let count = manager.count() as u64;
        let (source, _) = manager.current_backing_overlay().unwrap();
        let scans = Arc::new(AtomicUsize::new(0));
        let gets = Arc::new(AtomicUsize::new(0));
        manager.install_backing(
            Arc::new(CountReads {
                inner: Arc::clone(&source),
                scans: Arc::clone(&scans),
                gets: Arc::clone(&gets),
            }),
            root,
            store.format_version(),
            1024 * 1024,
            count,
        );
        let point = runtime
            .execute_query_with_params(
                "SELECT observed_at FROM windows WHERE id = $1",
                &[Value::text("k0")],
            )
            .unwrap();
        assert_eq!(point.result.records.len(), 1);
        assert_eq!(
            point.result.records[0].get("observed_at"),
            Some(&Value::Integer(UPDATES))
        );
        assert_eq!(
            scans.load(Ordering::Relaxed),
            0,
            "an indexed point lookup must not scan the collection"
        );
        assert!(
            gets.load(Ordering::Relaxed) <= 4,
            "point lookup read {} stored records",
            gets.load(Ordering::Relaxed)
        );
        manager.install_backing(source, root, store.format_version(), 1024 * 1024, count);
        let explain = runtime
            .execute_query("EXPLAIN SELECT observed_at FROM windows WHERE id = 'k0'")
            .unwrap();
        let seek = explain
            .result
            .records
            .iter()
            .find(|record| {
                matches!(record.get("op"), Some(Value::Text(op)) if op.as_ref() == "index_seek")
            })
            .expect("indexed point lookup plans an index seek");
        let estimated = match seek.get("estimated_rows") {
            Some(Value::Float(rows)) => *rows,
            other => panic!("estimated_rows: {other:?}"),
        };
        assert!(
            estimated < LIVE_ROWS as f64,
            "index seek estimated {estimated} rows, the whole collection"
        );
    }

    #[test]
    fn commit_pins_protect_only_versioned_history() {
        use crate::application::vcs::{Author, CreateCommitInput};
        use reddb_types::Value;
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("pins.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        for table in ["plain", "tracked"] {
            runtime
                .execute_query(&format!(
                    "CREATE TABLE {table} (id TEXT PRIMARY KEY, v INTEGER)"
                ))
                .unwrap();
        }
        runtime.vcs_set_versioned("tracked", true).unwrap();
        for table in ["plain", "tracked"] {
            runtime
                .execute_query(&format!("INSERT INTO {table} (id, v) VALUES ('a', 1)"))
                .unwrap();
        }
        let commit = runtime
            .vcs_commit(CreateCommitInput {
                connection_id: 0,
                message: "before update".into(),
                author: Author {
                    name: "test".into(),
                    email: "test@reddb.io".into(),
                },
                committer: None,
                amend: false,
                allow_empty: true,
            })
            .unwrap();
        let (_, reader) = runtime.snapshot_manager().fresh_retained_snapshot();
        for table in ["plain", "tracked"] {
            runtime
                .execute_query(&format!("UPDATE {table} SET v = 2 WHERE id = 'a'"))
                .unwrap();
        }
        let physical = |table: &str| runtime.db().store().get_collection(table).unwrap().count();
        let collect = || {
            for _ in 0..256 {
                runtime.try_reclaim_history_tick();
            }
        };
        collect();
        assert_eq!(physical("plain"), 2, "an open reader keeps the preimage");
        assert_eq!(physical("tracked"), 2);
        drop(reader);
        collect();
        assert_eq!(
            physical("plain"),
            1,
            "no AS OF read can address this history"
        );
        assert_eq!(physical("tracked"), 2, "the commit still reads its version");
        let historical = runtime
            .execute_query(&format!(
                "SELECT v FROM tracked AS OF COMMIT '{}' WHERE id = 'a'",
                commit.hash
            ))
            .unwrap();
        assert_eq!(historical.result.records.len(), 1);
        assert_eq!(
            historical.result.records[0].get("v"),
            Some(&Value::Integer(1))
        );
        let current = runtime
            .execute_query("SELECT v FROM plain WHERE id = 'a'")
            .unwrap();
        assert_eq!(current.result.records.len(), 1);
        assert_eq!(current.result.records[0].get("v"), Some(&Value::Integer(2)));
    }

    #[test]
    fn cold_history_reclamation_waits_for_a_retained_reader() {
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("reader.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE cold_history (id INTEGER)")
            .unwrap();
        runtime
            .execute_query("INSERT INTO cold_history (id) VALUES (1)")
            .unwrap();
        let (_, retention) = runtime.snapshot_manager().fresh_retained_snapshot();
        runtime
            .execute_query("DELETE FROM cold_history WHERE id = 1")
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        let manager = store.get_collection("cold_history").unwrap();
        assert!(manager.has_backing());
        let sweep = || {
            let mut cursor = store.mvcc_reclaim_cursor("cold_history".into()).unwrap();
            let mut reclaimed = 0;
            for _ in 0..8 {
                reclaimed += store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1,
                    )
                    .unwrap();
                if cursor.finished() {
                    break;
                }
            }
            assert!(cursor.finished());
            reclaimed
        };
        assert_eq!(sweep(), 0);
        assert_eq!(
            manager.count(),
            1,
            "old reader still needs the cold preimage"
        );
        drop(retention);
        assert_eq!(sweep(), 1);
        assert_eq!(manager.count(), 0);
    }

    #[test]
    fn cold_gc_skips_live_payloads_using_the_visibility_projection() {
        use crate::storage::{EntityId, StoreError};
        use reddb_engine::storage::collection_read::{
            CollectionCursor, CollectionRead, EncodedRecord,
        };
        use std::sync::Arc;
        struct RefusePayload;
        impl CollectionRead for RefusePayload {
            fn get_record(
                &self,
                _: EntityId,
                _: usize,
            ) -> Result<Option<EncodedRecord>, StoreError> {
                Err(StoreError::Serialization(
                    "GC read an unneeded live payload".into(),
                ))
            }
            fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
                Err(StoreError::Serialization("GC scanned live payloads".into()))
            }
            fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
                Err(StoreError::Serialization(
                    "GC retained live payloads".into(),
                ))
            }
        }
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("inventory.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE live_rows (id INTEGER, payload TEXT)")
            .unwrap();
        runtime
            .execute_query("INSERT INTO live_rows (id, payload) VALUES (1, 'retained')")
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        assert_eq!(
            store.try_count_from_index("live_rows", None).unwrap(),
            Some(1)
        );
        let manager = store.get_collection("live_rows").unwrap();
        let root = manager.backing_root_page().unwrap();
        let count = manager.count() as u64;
        let (source, _) = manager.current_backing_overlay().unwrap();
        manager.install_backing(
            Arc::new(RefusePayload),
            root,
            store.format_version(),
            1024,
            count,
        );
        let mut cursor = store.mvcc_reclaim_cursor("live_rows".into()).unwrap();
        for _ in 0..8 {
            assert_eq!(
                store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1
                    )
                    .unwrap(),
                0
            );
            if cursor.finished() {
                break;
            }
        }
        assert!(cursor.finished());
        assert_eq!(manager.count(), 1);
        manager.install_backing(source, root, store.format_version(), 1024, count);
    }

    #[test]
    fn paused_cold_gc_does_not_force_a_checkpoint_to_copy_live_history() {
        use crate::storage::{EntityId, StoreError};
        use reddb_engine::storage::collection_read::{
            CollectionCursor, CollectionRead, EncodedRecord,
        };
        use std::sync::{
            atomic::{AtomicUsize, Ordering},
            Arc,
        };
        struct CountScans {
            inner: Arc<dyn CollectionRead + Send + Sync>,
            scans: Arc<AtomicUsize>,
        }
        impl CollectionRead for CountScans {
            fn get_record(
                &self,
                id: EntityId,
                bytes: usize,
            ) -> Result<Option<EncodedRecord>, StoreError> {
                self.inner.get_record(id, bytes)
            }
            fn cursor(&self) -> Result<Box<dyn CollectionCursor + '_>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.cursor()
            }
            fn owned_cursor(&self) -> Result<Box<dyn CollectionCursor>, StoreError> {
                self.scans.fetch_add(1, Ordering::Relaxed);
                self.inner.owned_cursor()
            }
        }
        let directory = tempfile::tempdir().unwrap();
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::persistent(directory.path().join("pins.rdb"))
                .with_memory_budget(32 * 1024 * 1024)
                .with_bounded_disk_residency(true),
        )
        .unwrap();
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE append_rows (id INTEGER, payload TEXT)")
            .unwrap();
        runtime
            .execute_query(
                "INSERT INTO append_rows (id, payload) VALUES (1, 'old one'), (2, 'old two')",
            )
            .unwrap();
        runtime.checkpoint().unwrap();
        let store = runtime.db().store();
        let manager = store.get_collection("append_rows").unwrap();
        let root = manager.backing_root_page().unwrap();
        let count = manager.count() as u64;
        let (source, _) = manager.current_backing_overlay().unwrap();
        let scans = Arc::new(AtomicUsize::new(0));
        manager.install_backing(
            Arc::new(CountScans {
                inner: source,
                scans: Arc::clone(&scans),
            }),
            root,
            store.format_version(),
            1024 * 1024,
            count,
        );
        let mut cursor = store.mvcc_reclaim_cursor("append_rows".into()).unwrap();
        assert_eq!(
            store
                .reclaim_mvcc_tick(
                    &mut cursor,
                    &runtime.snapshot_manager(),
                    runtime.mvcc_vacuum_cutoff_xid(),
                    1,
                    1
                )
                .unwrap(),
            0
        );
        assert!(
            !cursor.finished(),
            "maintenance is deliberately paused mid-inventory"
        );
        runtime
            .execute_query("INSERT INTO append_rows (id, payload) VALUES (3, 'new')")
            .unwrap();
        runtime.checkpoint().unwrap();
        assert_eq!(
            scans.load(Ordering::Relaxed),
            0,
            "a paused GC tick must not force live history through full replacement"
        );
        for _ in 0..8 {
            assert_eq!(
                store
                    .reclaim_mvcc_tick(
                        &mut cursor,
                        &runtime.snapshot_manager(),
                        runtime.mvcc_vacuum_cutoff_xid(),
                        1,
                        1
                    )
                    .unwrap(),
                0
            );
            if cursor.finished() {
                break;
            }
        }
        assert!(
            cursor.finished(),
            "resume from the current generation after checkpoint"
        );
        assert_eq!(manager.count(), 3);
        drop(cursor);
    }

    fn fixture() -> RedDBRuntime {
        let mut runtime = RedDBRuntime::with_options(
            RedDBOptions::in_memory().with_memory_budget(2 * 1024 * 1024),
        )
        .expect("bounded runtime");
        drop(runtime.background_tasks.take());
        runtime
            .execute_query("CREATE TABLE pressure_history (id INTEGER)")
            .expect("table");
        runtime
            .vcs_set_versioned("pressure_history", false)
            .expect("no VCS history");
        runtime
            .execute_query("INSERT INTO pressure_history (id) VALUES (1)")
            .expect("seed row");
        runtime
    }

    fn physical_count(runtime: &RedDBRuntime) -> usize {
        runtime
            .db()
            .store()
            .get_collection("pressure_history")
            .expect("manager")
            .count()
    }

    fn collect(runtime: &RedDBRuntime) {
        for _ in 0..64 {
            runtime.try_reclaim_history_tick();
        }
    }

    #[test]
    fn foreground_history_reclamation_waits_for_a_retained_reader() {
        let runtime = fixture();
        let (_snapshot, retention) = runtime.snapshot_manager().fresh_retained_snapshot();
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        collect(&runtime);
        assert_eq!(
            physical_count(&runtime),
            1,
            "old reader still needs the preimage"
        );
        drop(retention);
        collect(&runtime);
        assert_eq!(
            physical_count(&runtime),
            0,
            "released reader permits reclamation"
        );
    }

    #[test]
    fn foreground_history_reclamation_does_not_join_a_deferred_wal_capture() {
        use crate::storage::UnifiedStore;
        struct CaptureGuard;
        impl Drop for CaptureGuard {
            fn drop(&mut self) {
                if UnifiedStore::deferred_store_wal_capture_active() {
                    drop(UnifiedStore::take_deferred_store_wal_capture());
                }
            }
        }
        let runtime = fixture();
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        UnifiedStore::begin_deferred_store_wal_capture();
        let guard = CaptureGuard;
        for _ in 0..64 {
            assert!(!runtime.try_reclaim_history_tick());
        }
        assert_eq!(physical_count(&runtime), 1);
        let actions = UnifiedStore::take_deferred_store_wal_capture();
        assert!(
            actions.is_empty(),
            "physical GC cannot join a caller's rollback journal"
        );
        drop(guard);
        collect(&runtime);
        assert_eq!(physical_count(&runtime), 0);
    }

    #[test]
    fn foreground_history_reclamation_respects_autovacuum_disable() {
        let runtime = fixture();
        runtime
            .execute_query("SET CONFIG runtime.mvcc.autovacuum.enabled = false")
            .expect("disable automatic GC");
        runtime
            .execute_query("DELETE FROM pressure_history WHERE id = 1")
            .expect("delete");
        for _ in 0..64 {
            assert!(!runtime.try_reclaim_history_tick());
        }
        assert_eq!(physical_count(&runtime), 1);
    }
}