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, bool)>>,
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, bool)>) {
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 self.take_scheduled_postponed()
47 .into_iter()
48 .map(|(plan, _)| plan)
49 .collect()
50 }
51
52 pub(crate) fn take_scheduled_postponed(&self) -> Vec<(CtePlan, bool)> {
53 std::mem::take(&mut *self.postponed.lock())
54 }
55
56 pub fn note_written(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
57 self.written.lock().extend(rows);
58 }
59
60 /// 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.
61 pub fn wrote(&self, row: &PhysicalDocumentIdentity) -> bool {
62 self.written.lock().contains(row)
63 }
64
65 pub fn note_triggered(&self, rows: impl IntoIterator<Item = PhysicalDocumentIdentity>) {
66 self.triggered.lock().extend(rows);
67 }
68
69 /// 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`).
70 pub fn triggered_write(&self, row: &PhysicalDocumentIdentity) -> bool {
71 self.triggered.lock().contains(row)
72 }
73}