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
//
// Unified Query Algebra
//
// Copyright (c) 2023-2026 Cognica, Inc.
//
//! A session's temporary namespace, `pg_temp_N`, and the TOAST namespace that accompanies it, `pg_toast_temp_N`. The session's first temporary object creates both, as `InitTempTableNamespace` does, each taking the database counter's next OID before the object takes its own; a rollback past that creation forgets them, and the session's next temporary object creates them anew.
/// The name prefix of every temporary namespace, which a session's number completes.
pub const TEMPORARY_SCHEMA_PREFIX: &str = "pg_temp_";
/// The name prefix of every temporary namespace's TOAST namespace.
pub const TEMPORARY_TOAST_SCHEMA_PREFIX: &str = "pg_toast_temp_";
/// The OIDs of a session's temporary namespace and of its TOAST namespace, allocated in that order.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TemporaryNamespaceOids {
pub namespace: u32,
pub toast_namespace: u32,
}
/// A session's created temporary namespace as the catalog shows it.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct TemporaryNamespace {
/// `pg_temp_N`.
pub schema: String,
pub oids: TemporaryNamespaceOids,
}
impl TemporaryNamespace {
/// `pg_toast_temp_N`, named by the temporary namespace's number.
pub fn toast_schema(&self) -> String {
temporary_toast_schema_name(&self.schema)
}
/// The OID of `name` when it names the temporary namespace or its TOAST namespace.
pub fn namespace_oid(&self, name: &str) -> Option<u32> {
if name == self.schema {
Some(self.oids.namespace)
} else if name == self.toast_schema() {
Some(self.oids.toast_namespace)
} else {
None
}
}
/// Whether `oid` is the temporary namespace's or its TOAST namespace's.
pub fn holds_oid(&self, oid: i64) -> bool {
oid == i64::from(self.oids.namespace) || oid == i64::from(self.oids.toast_namespace)
}
}
/// The TOAST namespace name that accompanies the temporary namespace `schema`.
pub fn temporary_toast_schema_name(schema: &str) -> String {
let number = schema
.strip_prefix(TEMPORARY_SCHEMA_PREFIX)
.unwrap_or(schema);
format!("{TEMPORARY_TOAST_SCHEMA_PREFIX}{number}")
}
/// Whether `name` names some session's temporary namespace or its TOAST namespace, as `isAnyTempNamespace` decides by the name alone.
pub fn is_temporary_schema_name(name: &str) -> bool {
name.starts_with(TEMPORARY_SCHEMA_PREFIX) || name.starts_with(TEMPORARY_TOAST_SCHEMA_PREFIX)
}
#[cfg(test)]
mod tests;