uqa_engine/notifications/session_policy.rs
1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Retain the host's notification transport policy with its SQL session.
8
9use std::sync::atomic::Ordering;
10use uqa_sql::SQLError;
11
12use crate::Engine;
13
14impl Engine {
15 /// Require independently owned subscriptions instead of SQL `LISTEN`/`UNLISTEN` for this session and subsequently created sibling sessions. Stateless SQL hosts must enable this before serving requests. `NOTIFY` and `pg_notify` retain their transactional behavior.
16 ///
17 /// This is irreversible for the session and survives rollback, DISCARD and nested execution. Enabling it fails with SQLSTATE 55000 during SQL execution, inside a transaction or while SQL channels are registered. Repeated calls after successful configuration are harmless. Independently owned subscription handles are unaffected.
18 pub fn require_notification_subscriptions(&self) -> Result<(), SQLError> {
19 let _statement = self.lock_statement_gate();
20 if self.notification_subscriptions_required() {
21 return Ok(());
22 }
23 if self.runtime.sql_execution_depth.load(Ordering::Relaxed) != 0
24 || !self.session.transactions.lock().is_empty()
25 || !self.session.state.read().listened_channels.is_empty()
26 {
27 return Err(SQLError::Routine {
28 sqlstate: "55000".into(),
29 message: "notification subscription policy requires an idle session without SQL listeners".into(),
30 });
31 }
32 self.session
33 .notification_subscriptions_required
34 .store(true, Ordering::Release);
35 Ok(())
36 }
37
38 pub(crate) fn notification_subscriptions_required(&self) -> bool {
39 self.session
40 .notification_subscriptions_required
41 .load(Ordering::Acquire)
42 }
43}