Skip to main content

pgrx_pg_sys/submodules/
transaction_id.rs

1//LICENSE Portions Copyright 2019-2021 ZomboDB, LLC.
2//LICENSE
3//LICENSE Portions Copyright 2021-2023 Technology Concepts & Design, Inc.
4//LICENSE
5//LICENSE Portions Copyright 2023-2023 PgCentral Foundation, Inc. <contact@pgcentral.org>
6//LICENSE
7//LICENSE All rights reserved.
8//LICENSE
9//LICENSE Use of this source code is governed by the MIT license that can be found in the LICENSE file.
10use pgrx_sql_entity_graph::metadata::{
11    ArgumentError, ReturnsError, ReturnsRef, SqlMappingRef, SqlTranslatable, TypeOrigin,
12};
13use std::fmt::{self, Debug, Display, Formatter};
14
15pub type MultiXactId = TransactionId;
16
17/// An `xid` type from PostgreSQL
18#[repr(transparent)]
19#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
20#[derive(serde::Deserialize, serde::Serialize)]
21pub struct TransactionId(u32);
22
23impl TransactionId {
24    pub const INVALID: Self = Self(0);
25    pub const BOOTSTRAP: Self = Self(1);
26    pub const FROZEN: Self = Self(2);
27    pub const FIRST_NORMAL: Self = Self(3);
28    pub const MAX: Self = Self(u32::MAX);
29
30    pub const fn from_inner(xid: u32) -> Self {
31        Self(xid)
32    }
33
34    pub const fn into_inner(self) -> u32 {
35        self.0
36    }
37}
38
39impl Default for TransactionId {
40    fn default() -> Self {
41        Self::INVALID
42    }
43}
44
45impl From<u32> for TransactionId {
46    #[inline]
47    fn from(xid: u32) -> Self {
48        Self::from_inner(xid)
49    }
50}
51
52impl From<TransactionId> for u32 {
53    #[inline]
54    fn from(xid: TransactionId) -> Self {
55        xid.into_inner()
56    }
57}
58
59impl From<TransactionId> for crate::Datum {
60    fn from(xid: TransactionId) -> Self {
61        xid.into_inner().into()
62    }
63}
64
65unsafe impl SqlTranslatable for TransactionId {
66    const TYPE_IDENT: &'static str = pgrx_sql_entity_graph::pgrx_resolved_type!(TransactionId);
67    const TYPE_ORIGIN: TypeOrigin = TypeOrigin::External;
68    const ARGUMENT_SQL: Result<SqlMappingRef, ArgumentError> = Ok(SqlMappingRef::literal("xid"));
69    const RETURN_SQL: Result<ReturnsRef, ReturnsError> =
70        Ok(ReturnsRef::One(SqlMappingRef::literal("xid")));
71}
72
73impl Debug for TransactionId {
74    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
75        Debug::fmt(&self.0, f)
76    }
77}
78
79impl Display for TransactionId {
80    fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
81        Display::fmt(&self.0, f)
82    }
83}