|
| 1 | +//! Utilities for working with the PostgreSQL replication copy both format. |
| 2 | +
|
| 3 | +use crate::connection::ConnectionRef; |
| 4 | +use crate::lazy_pin::LazyPin; |
| 5 | +use crate::{CopyBoth, Error}; |
| 6 | +use bytes::Bytes; |
| 7 | +use fallible_iterator::FallibleIterator; |
| 8 | +use futures::Stream; |
| 9 | +use postgres_protocol::message::backend::{LogicalReplicationMessage, ReplicationMessage}; |
| 10 | +use std::task::Poll; |
| 11 | +use std::time::SystemTime; |
| 12 | +use tokio_postgres::replication::{LogicalReplicationStream, ReplicationStream}; |
| 13 | +use tokio_postgres::types::PgLsn; |
| 14 | + |
| 15 | +/// A type which deserializes the postgres replication protocol. |
| 16 | +/// |
| 17 | +/// This type can be used with both physical and logical replication to get |
| 18 | +/// access to the byte content of each replication message. |
| 19 | +/// |
| 20 | +/// This is the sync (blocking) version of [`ReplicationStream`] |
| 21 | +pub struct ReplicationIter<'a> { |
| 22 | + connection: ConnectionRef<'a>, |
| 23 | + stream: LazyPin<ReplicationStream>, |
| 24 | +} |
| 25 | + |
| 26 | +impl<'a> ReplicationIter<'a> { |
| 27 | + /// Creates a new `ReplicationIter`. |
| 28 | + pub fn new(copyboth: CopyBoth<'a>) -> Self { |
| 29 | + let unpinned_copyboth = copyboth |
| 30 | + .stream_sink |
| 31 | + .into_unpinned() |
| 32 | + .expect("copy-both stream has already been used"); |
| 33 | + let stream = ReplicationStream::new(unpinned_copyboth); |
| 34 | + Self { |
| 35 | + connection: copyboth.connection, |
| 36 | + stream: LazyPin::new(stream), |
| 37 | + } |
| 38 | + } |
| 39 | + |
| 40 | + /// Send standby update to server. |
| 41 | + pub fn standby_status_update( |
| 42 | + &mut self, |
| 43 | + write_lsn: PgLsn, |
| 44 | + flush_lsn: PgLsn, |
| 45 | + apply_lsn: PgLsn, |
| 46 | + timestamp: SystemTime, |
| 47 | + reply: u8, |
| 48 | + ) -> Result<(), Error> { |
| 49 | + self.connection.block_on( |
| 50 | + self.stream |
| 51 | + .pinned() |
| 52 | + .standby_status_update(write_lsn, flush_lsn, apply_lsn, timestamp, reply), |
| 53 | + ) |
| 54 | + } |
| 55 | + |
| 56 | + /// Send hot standby feedback message to server. |
| 57 | + pub fn hot_standby_feedback( |
| 58 | + &mut self, |
| 59 | + timestamp: SystemTime, |
| 60 | + global_xmin: u32, |
| 61 | + global_xmin_epoch: u32, |
| 62 | + catalog_xmin: u32, |
| 63 | + catalog_xmin_epoch: u32, |
| 64 | + ) -> Result<(), Error> { |
| 65 | + self.connection |
| 66 | + .block_on(self.stream.pinned().hot_standby_feedback( |
| 67 | + timestamp, |
| 68 | + global_xmin, |
| 69 | + global_xmin_epoch, |
| 70 | + catalog_xmin, |
| 71 | + catalog_xmin_epoch, |
| 72 | + )) |
| 73 | + } |
| 74 | +} |
| 75 | + |
| 76 | +impl<'a> FallibleIterator for ReplicationIter<'a> { |
| 77 | + type Item = ReplicationMessage<Bytes>; |
| 78 | + type Error = Error; |
| 79 | + |
| 80 | + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { |
| 81 | + let pinstream = &mut self.stream; |
| 82 | + |
| 83 | + self.connection |
| 84 | + .poll_block_on(|cx, _, _| match pinstream.pinned().poll_next(cx) { |
| 85 | + Poll::Ready(x) => Poll::Ready(x.transpose()), |
| 86 | + Poll::Pending => Poll::Pending, |
| 87 | + }) |
| 88 | + } |
| 89 | +} |
| 90 | + |
| 91 | +/// A type which deserializes the postgres logical replication protocol. This |
| 92 | +/// type gives access to a high level representation of the changes in |
| 93 | +/// transaction commit order. |
| 94 | +/// |
| 95 | +/// This is the sync (blocking) version of [`LogicalReplicationStream`] |
| 96 | +pub struct LogicalReplicationIter<'a> { |
| 97 | + connection: ConnectionRef<'a>, |
| 98 | + stream: LazyPin<LogicalReplicationStream>, |
| 99 | +} |
| 100 | + |
| 101 | +impl<'a> LogicalReplicationIter<'a> { |
| 102 | + /// Creates a new `ReplicationThing`. |
| 103 | + pub fn new(copyboth: CopyBoth<'a>) -> Self { |
| 104 | + let unpinned_copyboth = copyboth |
| 105 | + .stream_sink |
| 106 | + .into_unpinned() |
| 107 | + .expect("copy-both stream has already been used"); |
| 108 | + let stream = LogicalReplicationStream::new(unpinned_copyboth); |
| 109 | + Self { |
| 110 | + connection: copyboth.connection, |
| 111 | + stream: LazyPin::new(stream), |
| 112 | + } |
| 113 | + } |
| 114 | + |
| 115 | + /// Send standby update to server. |
| 116 | + pub fn standby_status_update( |
| 117 | + &mut self, |
| 118 | + write_lsn: PgLsn, |
| 119 | + flush_lsn: PgLsn, |
| 120 | + apply_lsn: PgLsn, |
| 121 | + timestamp: SystemTime, |
| 122 | + reply: u8, |
| 123 | + ) -> Result<(), Error> { |
| 124 | + self.connection.block_on( |
| 125 | + self.stream |
| 126 | + .pinned() |
| 127 | + .standby_status_update(write_lsn, flush_lsn, apply_lsn, timestamp, reply), |
| 128 | + ) |
| 129 | + } |
| 130 | + |
| 131 | + /// Send hot standby feedback message to server. |
| 132 | + pub fn hot_standby_feedback( |
| 133 | + &mut self, |
| 134 | + timestamp: SystemTime, |
| 135 | + global_xmin: u32, |
| 136 | + global_xmin_epoch: u32, |
| 137 | + catalog_xmin: u32, |
| 138 | + catalog_xmin_epoch: u32, |
| 139 | + ) -> Result<(), Error> { |
| 140 | + self.connection |
| 141 | + .block_on(self.stream.pinned().hot_standby_feedback( |
| 142 | + timestamp, |
| 143 | + global_xmin, |
| 144 | + global_xmin_epoch, |
| 145 | + catalog_xmin, |
| 146 | + catalog_xmin_epoch, |
| 147 | + )) |
| 148 | + } |
| 149 | +} |
| 150 | + |
| 151 | +impl<'a> FallibleIterator for LogicalReplicationIter<'a> { |
| 152 | + type Item = ReplicationMessage<LogicalReplicationMessage>; |
| 153 | + type Error = Error; |
| 154 | + |
| 155 | + fn next(&mut self) -> Result<Option<Self::Item>, Self::Error> { |
| 156 | + let pinstream = &mut self.stream; |
| 157 | + |
| 158 | + self.connection |
| 159 | + .poll_block_on(|cx, _, _| match pinstream.pinned().poll_next(cx) { |
| 160 | + Poll::Ready(x) => Poll::Ready(x.transpose()), |
| 161 | + Poll::Pending => Poll::Pending, |
| 162 | + }) |
| 163 | + } |
| 164 | +} |
0 commit comments