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
//! Process-scoped sequence store backing `CREATE SEQUENCE` /
//! `nextval` / `currval` / `setval`.
//!
//! Scope (v3.13.1):
//! * In-memory, shared across all connections in a process.
//! * Not persisted to disk — restart resets all sequences.
//! * No cycle / min_value / max_value / increment_by honouring beyond
//! the basic `setval` update.
//!
//! This is enough to unblock Prisma / Drizzle / Django migrations
//! that emit sequence DDL but don't rely on cross-process monotonicity.
//! A RocksDB-backed version is tracked as a follow-up.
use HashMap;
use OnceLock;
use Mutex;
/// Register a new sequence. Returns `Ok(())` on success, or `Err` if
/// the sequence already exists and `if_not_exists == false`.
/// `nextval(name)` — atomically advance the sequence and return the
/// new value. Auto-creates the sequence if it doesn't exist, matching
/// Postgres' behaviour when called with an unregistered name in
/// certain contexts (SERIAL internals, for instance).
/// `currval(name)` — return the last value produced by `nextval` for
/// this sequence. Returns 0 if `nextval` has never been called (unlike
/// Postgres, which raises). Document this divergence.
/// `setval(name, value)` — set the counter to `value`. Subsequent
/// `nextval` calls return `value + 1`, `value + 2`, ….