uqa-engine 0.4.0

Engine: schema-aware table store, catalog restore, transactions
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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//

//! Scoped transaction callbacks and implicit transaction lifecycle.

use super::{
    panic_description, BackendTransactionMode, Engine, SQLError, SQLParam, SQLResult,
    StorageBackendError, StorageBackendResult, TransactionIntent, TransactionScope,
};

impl Engine {
    /// Run `f` inside one engine transaction. An error or panic from `f` rolls back the transaction. A successful callback commits; an indeterminate commit retains its sealed attempt in the session for resolution through `commit` or `rollback`, without replaying `f`.
    pub fn transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, SQLError>,
    ) -> Result<R, SQLError> {
        self.transaction_with_error(f, std::convert::identity)
    }

    pub(crate) fn transaction_with_error<R, E: std::fmt::Display>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, E>,
        map_transaction_error: impl Fn(SQLError) -> E,
    ) -> Result<R, E> {
        let _statement = self.runtime.statement_gate.lock();
        let scope = TransactionScope::begin(self).map_err(&map_transaction_error)?;
        self.run_transaction_scope(scope, f, map_transaction_error)
    }

    fn run_transaction_scope<R, E: std::fmt::Display>(
        &self,
        mut scope: TransactionScope<'_>,
        f: impl FnOnce(&Self) -> Result<R, E>,
        map_transaction_error: impl Fn(SQLError) -> E,
    ) -> Result<R, E> {
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self)));
        match result {
            Ok(Ok(value)) => {
                scope.commit().map_err(&map_transaction_error)?;
                Ok(value)
            }
            Ok(Err(err)) => {
                if let Err(rollback_err) = scope.rollback() {
                    return Err(map_transaction_error(Self::rollback_cleanup_error(&rollback_err, format!(
                        "transaction rollback after error failed: {rollback_err}; original error: {err}"
                    ))));
                }
                Err(err)
            }
            Err(payload) => match scope.rollback() {
                Ok(()) => std::panic::resume_unwind(payload),
                Err(rollback_err) => Err(map_transaction_error(Self::rollback_cleanup_error(
                    &rollback_err,
                    format!(
                    "transaction rollback after panic failed: {rollback_err}; original panic: {}",
                    panic_description(payload.as_ref())
                ),
                ))),
            },
        }
    }

    /// Admit a public query through the active transaction or an owned implicit frame. Queries that can persist calibration or create a graph need a writable snapshot, including on memory engines. Attached physical readers retain their existing view and completion owner.
    pub(crate) fn with_query_transaction_snapshot<R, E: std::fmt::Display>(
        &self,
        select_snapshot: bool,
        read_only: bool,
        query: impl FnOnce(&Self) -> Result<R, E>,
        map_transaction_error: impl Fn(SQLError) -> E,
    ) -> Result<R, E> {
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()
                .map_err(&map_transaction_error)?;
            return self.run_existing_transaction_operation(
                || {
                    self.runtime
                        .cancellation
                        .check()
                        .map_err(SQLError::from)
                        .map_err(&map_transaction_error)?;
                    if select_snapshot {
                        self.prepare_explicit_statement_snapshot(true)
                            .map_err(&map_transaction_error)?;
                        self.mark_transaction_snapshot_set();
                    }
                    query(self)
                },
                &map_transaction_error,
            );
        }
        self.runtime
            .cancellation
            .check()
            .map_err(SQLError::from)
            .map_err(&map_transaction_error)?;
        if self
            .storage
            .backend
            .as_ref()
            .map_or(read_only, |backend| backend.in_transaction())
        {
            return query(self);
        }
        let scope = TransactionScope::begin_implicit_statement(self, read_only)
            .map_err(&map_transaction_error)?;
        self.run_transaction_scope(
            scope,
            |engine| {
                engine
                    .prepare_explicit_statement_snapshot(true)
                    .map_err(&map_transaction_error)?;
                engine.mark_transaction_snapshot_set();
                query(engine)
            },
            &map_transaction_error,
        )
    }

    /// Make a direct persistent-engine mutation atomic. A failed mutation in an existing transaction aborts its current frame or user savepoint through the same recovery boundary as SQL. Memory stores validate fallible vector input before their infallible writes; explicit memory transactions retain writable snapshots whose document and inverted-index state is copied on mutation. Avoiding a whole-engine snapshot for each direct memory insert keeps bulk ingestion linear.
    pub(crate) fn with_implicit_transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, SQLError>,
    ) -> Result<R, SQLError> {
        let _statement = self.runtime.statement_gate.lock();
        if self.current_transaction_is_read_only() {
            return Err(SQLError::Routine {
                sqlstate: "25006".into(),
                message: "cannot execute direct mutation in a read-only transaction".into(),
            });
        }
        self.with_implicit_transaction_mutation(f)
    }

    pub(super) fn with_implicit_transaction_mutation<R>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, SQLError>,
    ) -> Result<R, SQLError> {
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()?;
            return self.run_existing_transaction_operation(
                || {
                    self.prepare_explicit_transaction_writer()?;
                    f(self)
                },
                std::convert::identity,
            );
        }
        if self.storage.backend.is_none() {
            return f(self);
        }
        self.transaction(|engine| {
            engine.prepare_explicit_transaction_writer()?;
            f(engine)
        })
    }

    /// Definition changes need transaction-owned relation locks even for direct memory APIs. Defer physical writer admission until execution has acquired and revalidated those locks.
    pub(crate) fn with_implicit_definition_transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, SQLError>,
    ) -> Result<R, SQLError> {
        let _statement = self.runtime.statement_gate.lock();
        if self.current_transaction_is_read_only() {
            return Err(SQLError::Routine {
                sqlstate: "25006".into(),
                message: "cannot execute direct mutation in a read-only transaction".into(),
            });
        }
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()?;
            self.run_existing_transaction_operation(
                || {
                    self.prepare_serializable_transaction_snapshot()?;
                    f(self)
                },
                std::convert::identity,
            )
        } else {
            self.transaction(|engine| {
                engine.prepare_serializable_transaction_snapshot()?;
                f(engine)
            })
        }
    }

    /// Error-type-preserving counterpart for direct APIs whose public error
    /// type is not [`SQLError`]. `map_transaction_error` is used only for
    /// begin/commit/rollback infrastructure failures; an error returned by
    /// `f` is passed through unchanged when rollback succeeds.
    pub(crate) fn with_implicit_mapped_transaction<R, E>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, E>,
        map_transaction_error: impl Fn(SQLError) -> E,
    ) -> Result<R, E>
    where
        E: std::fmt::Display,
    {
        let _statement = self.runtime.statement_gate.lock();
        if self.current_transaction_is_read_only() {
            return Err(map_transaction_error(SQLError::Routine {
                sqlstate: "25006".into(),
                message: "cannot execute direct mutation in a read-only transaction".into(),
            }));
        }
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()
                .map_err(&map_transaction_error)?;
            return self.run_existing_transaction_operation(
                || {
                    self.prepare_explicit_transaction_writer()
                        .map_err(&map_transaction_error)?;
                    f(self)
                },
                &map_transaction_error,
            );
        }
        if self.storage.backend.is_none() {
            return f(self);
        }
        let mut scope = TransactionScope::begin(self).map_err(&map_transaction_error)?;
        if let Err(error) = self.prepare_explicit_transaction_writer() {
            let error = map_transaction_error(error);
            return match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(map_transaction_error(Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction after promotion failure failed: {rollback_error}; original error: {error}"
                )))),
            };
        }
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self)));
        match result {
            Ok(Ok(value)) => {
                scope.commit().map_err(&map_transaction_error)?;
                Ok(value)
            }
            Ok(Err(error)) => match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(map_transaction_error(Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction failed: {rollback_error}; original error: {error}"
                )))),
            },
            Err(payload) => match scope.rollback() {
                Ok(()) => std::panic::resume_unwind(payload),
                Err(rollback_error) => Err(map_transaction_error(Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction after panic failed: {rollback_error}; original panic: {}",
                    panic_description(payload.as_ref())
                )))),
            },
        }
    }

    /// Storage-facing counterpart of [`Engine::with_implicit_transaction`].
    /// The storage error is retained verbatim when rollback succeeds so API
    /// callers can still classify the original backend failure.
    pub(crate) fn with_implicit_storage_transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> StorageBackendResult<R>,
    ) -> StorageBackendResult<R> {
        if self.current_transaction_is_read_only() {
            return Err(StorageBackendError::Other(
                "cannot execute storage mutation in a read-only transaction".into(),
            ));
        }
        self.with_implicit_storage_transaction_inner(false, true, f)
    }

    /// Run storage maintenance that `PostgreSQL` permits in a read-only transaction. The transaction remains logically read-only, while its physical backend is allowed to persist maintenance metadata such as ANALYZE statistics.
    pub(crate) fn with_read_only_compatible_storage_transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> StorageBackendResult<R>,
    ) -> StorageBackendResult<R> {
        self.with_storage_maintenance_scope(|engine| {
            engine.prepare_storage_maintenance_writer()?;
            f(engine)
        })
    }

    /// Establish the maintenance transaction before execution acquires logical locks or samples rows. Physical write admission is deferred until the caller has finished every required logical wait.
    pub(crate) fn with_storage_maintenance_scope<R>(
        &self,
        f: impl FnOnce(&Self) -> StorageBackendResult<R>,
    ) -> StorageBackendResult<R> {
        self.with_implicit_storage_transaction_inner(true, false, f)
    }

    pub(crate) fn prepare_storage_maintenance_writer(&self) -> StorageBackendResult<()> {
        self.prepare_explicit_transaction_writer()
            .map_err(|error| StorageBackendError::backend("maintenance transaction", error))?;
        if self.current_transaction_is_read_only() {
            // SQL access remains read-only. Physical maintenance admission is retained until the transaction ends, including after savepoint undo.
            for frame in self.session.transactions.lock().iter_mut() {
                frame.intent = TransactionIntent::ReadWrite;
            }
        }
        Ok(())
    }

    /// Automatic maintenance must release its relation locks instead of waiting behind a serialized writer that may need to upgrade those same locks.
    pub(crate) fn try_prepare_storage_maintenance_writer(&self) -> StorageBackendResult<bool> {
        let mark = self
            .session
            .transactions
            .lock()
            .first()
            .filter(|frame| frame.backend_mode == BackendTransactionMode::Deferred)
            .map(|frame| frame.begin_lock_mark);
        if let Some(mark) = mark {
            let admitted = self
                .row_locks
                .try_acquire_relation(
                    self.session_id,
                    self.row_locks.backend_writer_key(),
                    crate::row_locks::RelationLockMode::AccessExclusive,
                    mark,
                    &self.runtime.cancellation,
                )
                .map_err(|error| {
                    StorageBackendError::backend("maintenance writer admission", error)
                })?;
            if !admitted {
                return Ok(false);
            }
        }
        self.prepare_storage_maintenance_writer()?;
        Ok(true)
    }

    fn with_implicit_storage_transaction_inner<R>(
        &self,
        maintenance_can_override_default_read_only: bool,
        promote_writer: bool,
        f: impl FnOnce(&Self) -> StorageBackendResult<R>,
    ) -> StorageBackendResult<R> {
        let _statement = self.runtime.statement_gate.lock();
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()
                .map_err(|error| StorageBackendError::backend("storage transaction", error))?;
            return self.run_existing_transaction_operation(
                || {
                    self.prepare_serializable_transaction_snapshot()
                        .map_err(|error| {
                            StorageBackendError::backend("maintenance snapshot", error)
                        })?;
                    if promote_writer {
                        self.prepare_explicit_transaction_writer()
                            .map_err(|error| {
                                StorageBackendError::backend("promote storage transaction", error)
                            })?;
                    }
                    f(self)
                },
                |error| StorageBackendError::backend("transaction abort", error),
            );
        }
        let mut scope = TransactionScope::begin(self).map_err(|error| {
            StorageBackendError::backend("begin implicit storage transaction", error)
        })?;
        if maintenance_can_override_default_read_only {
            if let Some(frame) = self.session.transactions.lock().last_mut() {
                frame.intent = TransactionIntent::ReadWrite;
                frame.characteristics.read_only = false;
            }
        }
        let preparation = self
            .prepare_serializable_transaction_snapshot()
            .and_then(|()| {
                promote_writer
                    .then(|| self.prepare_explicit_transaction_writer())
                    .transpose()
                    .map(|_| ())
            });
        if let Err(error) = preparation {
            let error = StorageBackendError::backend("prepare implicit storage transaction", error);
            return match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(StorageBackendError::backend("rollback implicit storage transaction", Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction after promotion failure failed: {rollback_error}; original error: {error}"
                )))),
            };
        }
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self)));
        match result {
            Ok(Ok(value)) => {
                scope.commit().map_err(|error| {
                    StorageBackendError::backend("commit implicit storage transaction", error)
                })?;
                Ok(value)
            }
            Ok(Err(error)) => match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(StorageBackendError::backend("rollback implicit storage transaction", Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction failed: {rollback_error}; original error: {error}"
                )))),
            },
            Err(payload) => match scope.rollback() {
                Ok(()) => std::panic::resume_unwind(payload),
                Err(rollback_error) => Err(StorageBackendError::backend("rollback implicit storage transaction", Self::rollback_cleanup_error(&rollback_error, format!(
                    "rollback implicit engine transaction after panic failed: {rollback_error}; original panic: {}",
                    panic_description(payload.as_ref())
                )))),
            },
        }
    }

    pub(crate) fn with_implicit_string_transaction<R>(
        &self,
        f: impl FnOnce(&Self) -> Result<R, String>,
    ) -> Result<R, String> {
        let _statement = self.runtime.statement_gate.lock();
        if self.transaction_depth() != 0 {
            self.ensure_transaction_usable()
                .map_err(|error| error.to_string())?;
            return self.run_existing_transaction_operation(
                || {
                    self.prepare_explicit_transaction_writer()
                        .map_err(|error| {
                            format!("promote explicit engine transaction failed: {error}")
                        })?;
                    f(self)
                },
                |error| error.to_string(),
            );
        }
        let mut scope = TransactionScope::begin(self)
            .map_err(|error| format!("begin implicit engine transaction failed: {error}"))?;
        if let Err(error) = self.prepare_explicit_transaction_writer() {
            let error = format!("promote implicit engine transaction failed: {error}");
            return match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(format!(
                    "rollback implicit engine transaction after promotion failure failed: {rollback_error}; original error: {error}"
                )),
            };
        }
        let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| f(self)));
        match result {
            Ok(Ok(value)) => {
                scope
                    .commit()
                    .map_err(|error| format!("commit implicit engine transaction failed: {error}"))?;
                Ok(value)
            }
            Ok(Err(error)) => match scope.rollback() {
                Ok(()) => Err(error),
                Err(rollback_error) => Err(format!(
                    "rollback implicit engine transaction failed: {rollback_error}; original error: {error}"
                )),
            },
            Err(payload) => match scope.rollback() {
                Ok(()) => std::panic::resume_unwind(payload),
                Err(rollback_error) => Err(format!(
                    "rollback implicit engine transaction after panic failed: {rollback_error}; original panic: {}",
                    panic_description(payload.as_ref())
                )),
            },
        }
    }

    /// Execute multiple SQL statements inside one engine transaction.
    pub fn sql_batch(
        &self,
        statements: &[(&str, &[SQLParam])],
    ) -> Result<Vec<SQLResult>, SQLError> {
        self.transaction(|engine| {
            let mut results = Vec::with_capacity(statements.len());
            for (sql, params) in statements {
                results.push(engine.sql(sql, params)?);
            }
            Ok(results)
        })
    }
}