Skip to main content

uqa_execution/query/scope/
statement_commands.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! The commands of one statement and what they leave for the statement's end.
8
9use crate::mutation::candidate::PhysicalDocumentIdentity;
10use crate::mutation::triggers::queue::AfterTriggerQueue;
11use std::collections::BTreeSet;
12use std::sync::atomic::{AtomicBool, Ordering};
13use uqa_sql::plan::CtePlan;
14
15/// The commands of one statement and what they leave for its end, shared by every scope of the statement. `PostgreSQL` runs a statement under one command id and one AFTER trigger query level: the AFTER events of the statement's commands, its data-modifying WITH items and the referential actions those take among them, wait in one queue until the statement has finished (`AfterTriggerEndQuery`), and a row that one of its commands wrote is `TM_SelfModified` to the others.
16#[derive(Default)]
17pub struct StatementCommands {
18    /// Whether the statement's WITH modifies data, which makes its commands leave their AFTER events for the statement's end.
19    modifies_with: AtomicBool,
20    /// The WITH items that run once the primary query has finished, in the order they run.
21    postponed: parking_lot::Mutex<Vec<CtePlan>>,
22    /// The rows the statement's commands inserted, updated or deleted, which a statement whose WITH modifies data keeps.
23    written: parking_lot::Mutex<BTreeSet<PhysicalDocumentIdentity>>,
24    /// The rows that statements the statement started wrote: statements of its triggers and of the functions it calls, and their referential actions, which run under later command ids.
25    triggered: parking_lot::Mutex<BTreeSet<PhysicalDocumentIdentity>>,
26    after_triggers: AfterTriggerQueue,
27}
28
29impl StatementCommands {
30    /// Note that the statement's WITH modifies data, and keep `postponed`, the items that run once the primary query has finished.
31    pub(crate) fn begin_data_modifying_with(&self, postponed: Vec<CtePlan>) {
32        self.modifies_with.store(true, Ordering::Relaxed);
33        *self.postponed.lock() = postponed;
34    }
35
36    pub fn modifies_with(&self) -> bool {
37        self.modifies_with.load(Ordering::Relaxed)
38    }
39
40    pub fn after_triggers(&self) -> &AfterTriggerQueue {
41        &self.after_triggers
42    }
43
44    /// The items that run once the primary query has finished, which the statement runs once.
45    pub fn take_postponed(&self) -> Vec<CtePlan> {
46        std::mem::take(&mut *self.postponed.lock())
47    }
48
49    pub fn note_written(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
50        self.written.lock().extend(rows);
51    }
52
53    /// Whether one of the statement's commands wrote `row`, which the statement's later commands find in their snapshot as a row the statement already modified.
54    pub fn wrote(&self, row: &PhysicalDocumentIdentity) -> bool {
55        self.written.lock().contains(row)
56    }
57
58    pub fn note_triggered(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
59        self.triggered.lock().extend(rows);
60    }
61
62    /// Whether a statement that this statement started wrote `row`, which `PostgreSQL` finds modified by a later command than the statement's own (`TM_SelfModified` with another `cmax`).
63    pub fn triggered_write(&self, row: &PhysicalDocumentIdentity) -> bool {
64        self.triggered.lock().contains(row)
65    }
66}