Line data Source code
1 : //!
2 : //! Common utilities for dealing with PostgreSQL non-relation files.
3 : //!
4 : use crate::pg_constants;
5 : use crate::transaction_id_precedes;
6 : use bytes::BytesMut;
7 :
8 : use super::bindings::MultiXactId;
9 :
10 0 : pub fn transaction_id_set_status(xid: u32, status: u8, page: &mut BytesMut) {
11 0 : tracing::trace!(
12 0 : "handle_apply_request for RM_XACT_ID-{} (1-commit, 2-abort, 3-sub_commit)",
13 : status
14 : );
15 :
16 0 : let byteno: usize =
17 0 : ((xid % pg_constants::CLOG_XACTS_PER_PAGE) / pg_constants::CLOG_XACTS_PER_BYTE) as usize;
18 :
19 0 : let bshift: u8 =
20 0 : ((xid % pg_constants::CLOG_XACTS_PER_BYTE) * pg_constants::CLOG_BITS_PER_XACT as u32) as u8;
21 :
22 0 : page[byteno] =
23 0 : (page[byteno] & !(pg_constants::CLOG_XACT_BITMASK << bshift)) | (status << bshift);
24 0 : }
25 :
26 0 : pub fn transaction_id_get_status(xid: u32, page: &[u8]) -> u8 {
27 0 : let byteno: usize =
28 0 : ((xid % pg_constants::CLOG_XACTS_PER_PAGE) / pg_constants::CLOG_XACTS_PER_BYTE) as usize;
29 :
30 0 : let bshift: u8 =
31 0 : ((xid % pg_constants::CLOG_XACTS_PER_BYTE) * pg_constants::CLOG_BITS_PER_XACT as u32) as u8;
32 :
33 0 : (page[byteno] >> bshift) & pg_constants::CLOG_XACT_BITMASK
34 0 : }
35 :
36 : // See CLOGPagePrecedes in clog.c
37 0 : pub const fn clogpage_precedes(page1: u32, page2: u32) -> bool {
38 0 : let mut xid1 = page1 * pg_constants::CLOG_XACTS_PER_PAGE;
39 0 : xid1 += pg_constants::FIRST_NORMAL_TRANSACTION_ID + 1;
40 0 : let mut xid2 = page2 * pg_constants::CLOG_XACTS_PER_PAGE;
41 0 : xid2 += pg_constants::FIRST_NORMAL_TRANSACTION_ID + 1;
42 :
43 0 : transaction_id_precedes(xid1, xid2)
44 0 : && transaction_id_precedes(xid1, xid2 + pg_constants::CLOG_XACTS_PER_PAGE - 1)
45 0 : }
46 :
47 : // See SlruMayDeleteSegment() in slru.c
48 0 : pub fn slru_may_delete_clogsegment(segpage: u32, cutoff_page: u32) -> bool {
49 0 : let seg_last_page = segpage + pg_constants::SLRU_PAGES_PER_SEGMENT - 1;
50 :
51 0 : assert_eq!(segpage % pg_constants::SLRU_PAGES_PER_SEGMENT, 0);
52 :
53 0 : clogpage_precedes(segpage, cutoff_page) && clogpage_precedes(seg_last_page, cutoff_page)
54 0 : }
55 :
56 : // Multixact utils
57 :
58 40 : pub fn mx_offset_to_flags_offset(xid: MultiXactId) -> usize {
59 40 : ((xid / pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP as u32)
60 40 : % pg_constants::MULTIXACT_MEMBERGROUPS_PER_PAGE as u32
61 40 : * pg_constants::MULTIXACT_MEMBERGROUP_SIZE as u32) as usize
62 40 : }
63 :
64 20 : pub fn mx_offset_to_flags_bitshift(xid: MultiXactId) -> u16 {
65 20 : (xid as u16) % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP
66 20 : * pg_constants::MXACT_MEMBER_BITS_PER_XACT
67 20 : }
68 :
69 : /* Location (byte offset within page) of TransactionId of given member */
70 20 : pub fn mx_offset_to_member_offset(xid: MultiXactId) -> usize {
71 20 : mx_offset_to_flags_offset(xid)
72 20 : + (pg_constants::MULTIXACT_FLAGBYTES_PER_GROUP
73 20 : + (xid as u16 % pg_constants::MULTIXACT_MEMBERS_PER_MEMBERGROUP) * 4) as usize
74 20 : }
75 :
76 40 : fn mx_offset_to_member_page(xid: u32) -> u32 {
77 40 : xid / pg_constants::MULTIXACT_MEMBERS_PER_PAGE as u32
78 40 : }
79 :
80 20 : pub fn mx_offset_to_member_segment(xid: u32) -> i32 {
81 20 : (mx_offset_to_member_page(xid) / pg_constants::SLRU_PAGES_PER_SEGMENT) as i32
82 20 : }
83 :
84 : #[cfg(test)]
85 : mod tests {
86 : use super::*;
87 :
88 : #[test]
89 4 : fn test_multixid_calc() {
90 : // Check that the mx_offset_* functions produce the same values as the
91 : // corresponding PostgreSQL C macros (MXOffsetTo*). These test values
92 : // were generated by calling the PostgreSQL macros with a little C
93 : // program.
94 4 : assert_eq!(mx_offset_to_member_segment(0), 0);
95 4 : assert_eq!(mx_offset_to_member_page(0), 0);
96 4 : assert_eq!(mx_offset_to_flags_offset(0), 0);
97 4 : assert_eq!(mx_offset_to_flags_bitshift(0), 0);
98 4 : assert_eq!(mx_offset_to_member_offset(0), 4);
99 4 : assert_eq!(mx_offset_to_member_segment(1), 0);
100 4 : assert_eq!(mx_offset_to_member_page(1), 0);
101 4 : assert_eq!(mx_offset_to_flags_offset(1), 0);
102 4 : assert_eq!(mx_offset_to_flags_bitshift(1), 8);
103 4 : assert_eq!(mx_offset_to_member_offset(1), 8);
104 4 : assert_eq!(mx_offset_to_member_segment(123456789), 2358);
105 4 : assert_eq!(mx_offset_to_member_page(123456789), 75462);
106 4 : assert_eq!(mx_offset_to_flags_offset(123456789), 4780);
107 4 : assert_eq!(mx_offset_to_flags_bitshift(123456789), 8);
108 4 : assert_eq!(mx_offset_to_member_offset(123456789), 4788);
109 4 : assert_eq!(mx_offset_to_member_segment(u32::MAX - 1), 82040);
110 4 : assert_eq!(mx_offset_to_member_page(u32::MAX - 1), 2625285);
111 4 : assert_eq!(mx_offset_to_flags_offset(u32::MAX - 1), 5160);
112 4 : assert_eq!(mx_offset_to_flags_bitshift(u32::MAX - 1), 16);
113 4 : assert_eq!(mx_offset_to_member_offset(u32::MAX - 1), 5172);
114 4 : assert_eq!(mx_offset_to_member_segment(u32::MAX), 82040);
115 4 : assert_eq!(mx_offset_to_member_page(u32::MAX), 2625285);
116 4 : assert_eq!(mx_offset_to_flags_offset(u32::MAX), 5160);
117 4 : assert_eq!(mx_offset_to_flags_bitshift(u32::MAX), 24);
118 4 : assert_eq!(mx_offset_to_member_offset(u32::MAX), 5176);
119 4 : }
120 : }
|