ousia 2.0.0

Postgres ORM with native double-entry ledger, graph relations, and atomic money operations for Rust
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
mod adapter_impl;
mod helper;
mod traversal_impl;
mod unique_impl;

#[cfg(feature = "ledger")]
mod ledger_impl;

use sqlx::{PgPool, Postgres, Transaction};

use crate::adapters::Error;

/// PostgreSQL adapter using a partitioned JSON storage model.
///
/// # Schema overview
///
/// All three main tables (`objects`, `edges`, `unique_constraints`) use
/// `PARTITION BY LIST (type)` so each registered type gets its own physical
/// table with independent vacuum, statistics, and index sizing.
///
/// Call [`PostgresAdapter::init_schema_typed`] at application startup with
/// the full list of object and edge type names.  For tests or minimal setups
/// call [`PostgresAdapter::init_schema`] which is equivalent to
/// `init_schema_typed(&[], &[])` — only the `_default` catch-all partitions
/// are created.
pub struct PostgresAdapter {
    pub(crate) pool: PgPool,
}

impl PostgresAdapter {
    pub fn from_pool(pool: PgPool) -> Self {
        Self { pool }
    }

    // ------------------------------------------------------------------ //
    //  Public schema helpers                                               //
    // ------------------------------------------------------------------ //

    /// Initialise the schema with no pre-declared type partitions.
    ///
    /// Equivalent to `init_schema_typed(&[], &[])`.  All rows land in the
    /// `_default` catch-all partitions until specific partitions are created
    /// via [`Self::init_schema_typed`].
    pub async fn init_schema(&self) -> Result<(), Error> {
        self.init_schema_typed(&[], &[]).await
    }

    /// Initialise (or upgrade) the production schema.
    ///
    /// Safe to call on every application start — all operations use
    /// `IF NOT EXISTS` and are run inside a single transaction.
    ///
    /// ## What it does
    /// 1. Creates `sequence_registry` and `ousia_meta` bookkeeping tables.
    /// 2. Creates partitioned `objects`, `edges`, `unique_constraints` tables
    ///    (parent + indexes + storage tuning).
    /// 3. Creates `_default` catch-all partitions for all three tables.
    /// 4. Creates a dedicated partition for every type in `obj_types` /
    ///    `edge_types`.
    /// 5. Drops orphaned empty partitions that are no longer in the registry
    ///    (non-empty orphans are logged as warnings and skipped).
    /// 6. Drops redundant thin indexes left over from the non-partitioned era.
    ///
    /// ## Partition naming
    /// - Object partitions: `objects_{type_lower}`, `unique_constraints_{type_lower}`
    /// - Edge partitions: `edges_{type_lower}`
    /// - `{type_lower}` is the type name lowercased with `-` → `_`.
    pub async fn init_schema_typed(
        &self,
        obj_types: &[&'static str],
        edge_types: &[&'static str],
    ) -> Result<(), Error> {
        let mut tx = self
            .pool
            .begin()
            .await
            .map_err(|e| Error::Storage(e.to_string()))?;

        Self::create_bookkeeping_tables(&mut tx).await?;
        Self::create_objects_table(&mut tx).await?;
        Self::create_edges_table(&mut tx).await?;
        Self::create_unique_constraints_table(&mut tx).await?;
        Self::create_default_partitions(&mut tx).await?;
        Self::create_type_partitions(&mut tx, obj_types, edge_types).await?;
        Self::drop_old_thin_indexes(&mut tx).await?;
        Self::drop_orphaned_partitions(&mut tx, "objects", obj_types).await?;
        Self::drop_orphaned_partitions(&mut tx, "edges", edge_types).await?;
        Self::drop_orphaned_uc_partitions(&mut tx, obj_types).await?;

        tx.commit()
            .await
            .map_err(|e| Error::Storage(e.to_string()))?;

        #[cfg(feature = "ledger")]
        {
            use ledger::adapters::postgres::PostgresSchemaLedgerAdapter;
            self.init_ledger_schema().await.map_err(|me| match me {
                ledger::MoneyError::Storage(e) => Error::Storage(e),
                _ => Error::Storage(me.to_string()),
            })?;
        }

        Ok(())
    }

    /// Ensure a named DB-native sequence exists and is registered in
    /// `sequence_registry`.  Idempotent — safe to call on every request.
    ///
    /// Sequences are created with `CACHE 1` (default) so values are strictly
    /// sequential.  For high-throughput sequences run
    /// `ALTER SEQUENCE <name> CACHE 100` after creation — see `TUNING.md`.
    pub(crate) async fn ensure_sequence(&self, name: &str) -> Result<(), Error> {
        // Quote the identifier so names containing hyphens or other special
        // characters are accepted by PG (e.g. "my-key" → `"my-key"`).
        let create_sql = format!(
            "CREATE SEQUENCE IF NOT EXISTS {}",
            pg_quote_ident(name)
        );
        sqlx::query(&create_sql)
            .execute(&self.pool)
            .await
            .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            "INSERT INTO sequence_registry (id, name) \
             VALUES (gen_random_uuid(), $1) ON CONFLICT (name) DO NOTHING",
        )
        .bind(name)
        .execute(&self.pool)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    // ------------------------------------------------------------------ //
    //  ousia_meta helpers (used by check_schema in Engine)                //
    // ------------------------------------------------------------------ //

    pub(crate) async fn read_schema_hash_impl(
        &self,
        type_name: &'static str,
    ) -> Result<Option<String>, Error> {
        let key = format!("schema:{}", type_name);
        sqlx::query_scalar::<_, String>("SELECT value FROM ousia_meta WHERE key = $1")
            .bind(&key)
            .fetch_optional(&self.pool)
            .await
            .map_err(|e| Error::Storage(e.to_string()))
    }

    pub(crate) async fn upsert_schema_hash_impl(
        &self,
        type_name: &'static str,
        hash: &str,
    ) -> Result<(), Error> {
        let key = format!("schema:{}", type_name);
        sqlx::query(
            "INSERT INTO ousia_meta (key, value) VALUES ($1, $2) \
             ON CONFLICT (key) DO UPDATE SET value = EXCLUDED.value",
        )
        .bind(&key)
        .bind(hash)
        .execute(&self.pool)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;
        Ok(())
    }

    // ------------------------------------------------------------------ //
    //  Private schema builders                                             //
    // ------------------------------------------------------------------ //

    async fn create_bookkeeping_tables(tx: &mut Transaction<'_, Postgres>) -> Result<(), Error> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS sequence_registry (
                id   UUID PRIMARY KEY DEFAULT gen_random_uuid(),
                name TEXT NOT NULL UNIQUE
            )
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS ousia_meta (
                key   TEXT PRIMARY KEY,
                value TEXT NOT NULL
            )
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    async fn create_objects_table(tx: &mut Transaction<'_, Postgres>) -> Result<(), Error> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS public.objects (
                id         UUID        NOT NULL,
                type       TEXT        NOT NULL,
                owner      UUID        NOT NULL,
                created_at TIMESTAMPTZ NOT NULL,
                updated_at TIMESTAMPTZ NOT NULL,
                data       BYTEA       NOT NULL DEFAULT ''::bytea,
                index_meta JSONB       NOT NULL DEFAULT '{}'::jsonb,
                PRIMARY KEY (type, id)
            ) PARTITION BY LIST (type)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Covering index: owner scoped queries with timestamp sort
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_objects_owner_created
                ON objects(type, owner, created_at DESC)
                INCLUDE (id, updated_at)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_objects_owner_updated
                ON objects(type, owner, updated_at DESC)
                INCLUDE (id, created_at)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Global type-scoped range queries (no owner filter)
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_objects_type_created
                ON objects(type, created_at DESC)
                INCLUDE (owner, id)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_objects_type_updated
                ON objects(type, updated_at DESC)
                INCLUDE (owner, id)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // GIN for index_meta field queries
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_objects_index_meta
                ON objects USING GIN (index_meta jsonb_path_ops)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    async fn create_edges_table(tx: &mut Transaction<'_, Postgres>) -> Result<(), Error> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS public.edges (
                "from"     UUID        NOT NULL,
                "to"       UUID        NOT NULL,
                type       TEXT        NOT NULL,
                created_at TIMESTAMPTZ NOT NULL DEFAULT now(),
                data       BYTEA       NOT NULL DEFAULT ''::bytea,
                index_meta JSONB       NOT NULL DEFAULT '{}'::jsonb
            ) PARTITION BY LIST (type)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Unique constraint includes partition key
        sqlx::query(
            r#"
            CREATE UNIQUE INDEX IF NOT EXISTS idx_edges_key
                ON edges("from", "to", type)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Covering outgoing-edge index — index-only scan for all common reads
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_edges_from_covering
                ON edges("from", type, created_at DESC)
                INCLUDE ("to", data, index_meta)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Covering incoming-edge index
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_edges_to_covering
                ON edges("to", type, created_at DESC)
                INCLUDE ("from", data, index_meta)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_edges_index_meta
                ON edges USING GIN (index_meta jsonb_path_ops)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    async fn create_unique_constraints_table(
        tx: &mut Transaction<'_, Postgres>,
    ) -> Result<(), Error> {
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS public.unique_constraints (
                id    UUID NOT NULL,
                type  TEXT NOT NULL,
                key   TEXT NOT NULL,
                field TEXT NOT NULL,
                PRIMARY KEY (type, key)
            ) PARTITION BY LIST (type)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        // Reverse lookup by object id (used for cascade deletes)
        sqlx::query(
            r#"
            CREATE INDEX IF NOT EXISTS idx_unique_id
                ON unique_constraints(id)
            "#,
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    async fn create_default_partitions(tx: &mut Transaction<'_, Postgres>) -> Result<(), Error> {
        sqlx::query(
            "CREATE TABLE IF NOT EXISTS objects_default PARTITION OF objects DEFAULT",
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query("CREATE TABLE IF NOT EXISTS edges_default PARTITION OF edges DEFAULT")
            .execute(&mut **tx)
            .await
            .map_err(|e| Error::Storage(e.to_string()))?;

        sqlx::query(
            "CREATE TABLE IF NOT EXISTS unique_constraints_default \
             PARTITION OF unique_constraints DEFAULT",
        )
        .execute(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        Ok(())
    }

    async fn create_type_partitions(
        tx: &mut Transaction<'_, Postgres>,
        obj_types: &[&'static str],
        edge_types: &[&'static str],
    ) -> Result<(), Error> {
        for &type_name in obj_types {
            let safe = partition_name_segment(type_name);

            let sql = format!(
                "CREATE TABLE IF NOT EXISTS objects_{safe} \
                 PARTITION OF objects FOR VALUES IN ('{type_name}')"
            );
            sqlx::query(&sql)
                .execute(&mut **tx)
                .await
                .map_err(|e| Error::Storage(e.to_string()))?;

            let sql = format!(
                "CREATE TABLE IF NOT EXISTS unique_constraints_{safe} \
                 PARTITION OF unique_constraints FOR VALUES IN ('{type_name}')"
            );
            sqlx::query(&sql)
                .execute(&mut **tx)
                .await
                .map_err(|e| Error::Storage(e.to_string()))?;
        }

        for &type_name in edge_types {
            let safe = partition_name_segment(type_name);
            let sql = format!(
                "CREATE TABLE IF NOT EXISTS edges_{safe} \
                 PARTITION OF edges FOR VALUES IN ('{type_name}')"
            );
            sqlx::query(&sql)
                .execute(&mut **tx)
                .await
                .map_err(|e| Error::Storage(e.to_string()))?;
        }

        Ok(())
    }

    async fn drop_old_thin_indexes(tx: &mut Transaction<'_, Postgres>) -> Result<(), Error> {
        for name in &[
            "idx_objects_type_owner",
            "idx_objects_type_owner_created",
            "idx_objects_type_owner_updated",
            "idx_edges_from_key",
            "idx_edges_to_key",
            "idx_unique_type_key",
        ] {
            sqlx::query(&format!("DROP INDEX IF EXISTS {}", name))
                .execute(&mut **tx)
                .await
                .map_err(|e| Error::Storage(e.to_string()))?;
        }
        Ok(())
    }

    /// Drop empty orphaned partitions of `parent_table`.
    /// `known_types` is the current registry — all other non-default partitions
    /// whose names follow the `{parent}_{safe}` convention are candidates.
    async fn drop_orphaned_partitions(
        tx: &mut Transaction<'_, Postgres>,
        parent_table: &str,
        known_types: &[&'static str],
    ) -> Result<(), Error> {
        let known_names: Vec<String> = known_types
            .iter()
            .map(|t| format!("{}_{}", parent_table, partition_name_segment(t)))
            .chain(std::iter::once(format!("{}_default", parent_table)))
            .collect();

        let partitions: Vec<String> = sqlx::query_scalar(
            "SELECT c.relname::text \
             FROM pg_class c \
             JOIN pg_inherits i ON i.inhrelid = c.oid \
             JOIN pg_class p ON p.oid = i.inhparent \
             WHERE p.relname = $1",
        )
        .bind(parent_table)
        .fetch_all(&mut **tx)
        .await
        .map_err(|e| Error::Storage(e.to_string()))?;

        for partition in partitions {
            if known_names.contains(&partition) {
                continue;
            }
            let count: i64 =
                sqlx::query_scalar(&format!("SELECT COUNT(*) FROM {} LIMIT 1", partition))
                    .fetch_one(&mut **tx)
                    .await
                    .map_err(|e| Error::Storage(e.to_string()))?;

            if count == 0 {
                sqlx::query(&format!("DROP TABLE IF EXISTS {}", partition))
                    .execute(&mut **tx)
                    .await
                    .map_err(|e| Error::Storage(e.to_string()))?;
            } else {
                eprintln!(
                    "[ousia warn] Orphaned partition '{}' has {} row(s) — skipping drop. \
                     Detach or migrate manually.",
                    partition, count
                );
            }
        }

        Ok(())
    }

    async fn drop_orphaned_uc_partitions(
        tx: &mut Transaction<'_, Postgres>,
        obj_types: &[&'static str],
    ) -> Result<(), Error> {
        Self::drop_orphaned_partitions(tx, "unique_constraints", obj_types).await
    }
}

/// Convert a type name to a safe partition name segment.
/// Lowercases and replaces `-` with `_`.
fn partition_name_segment(type_name: &str) -> String {
    type_name.to_lowercase().replace('-', "_")
}

/// Wrap an identifier in double-quotes, escaping any embedded double-quotes.
/// E.g. `my-key` → `"my-key"`, `weird"name` → `"weird""name"`.
pub(crate) fn pg_quote_ident(name: &str) -> String {
    format!("\"{}\"", name.replace('"', "\"\""))
}

/// Return the identifier in the form expected by `nextval()` / `last_value`:
/// a single-quoted string containing the double-quoted identifier.
/// E.g. `my-key` → `'"my-key"'`.
pub(crate) fn pg_quote_ident_as_literal(name: &str) -> String {
    format!("'{}'", pg_quote_ident(name))
}