Skip to content

Commit 04cbea5

Browse files
harshil-goelserprex
authored andcommitted
Fix LSN format for start replication
1 parent a64f4c9 commit 04cbea5

1 file changed

Lines changed: 39 additions & 13 deletions

File tree

‎src/source_feed.rs‎

Lines changed: 39 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use postgres_protocol::message::backend::Message;
1313
use tokio::net::{TcpStream, UnixStream};
1414
use tokio_postgres::config::SslMode as TpSslMode;
1515
use tokio_postgres::{Client, NoTls};
16-
use walross::pg::backup::parse_pg_lsn;
16+
use walross::pg::backup::{format_pg_lsn, parse_pg_lsn};
1717
use walross::pg::replication::conn::{PgConfig, ReplicationConn, error_message, message_kind};
1818
use walross::pg::replication::stream::{Frame, build_status_update, decode_frame};
1919
use walross::pg::replication::tls::{SocketStream, SslMode, maybe_upgrade};
@@ -75,6 +75,19 @@ fn clamp_status(status: StandbyStatus, floors: &mut StatusFloors) -> (u64, u64,
7575
(floors.write, floors.flush, floors.apply)
7676
}
7777

78+
/// Build the `START_REPLICATION` command string for a physical replication
79+
/// start at `start_lsn`. The LSN is rendered via [`format_pg_lsn`] so both
80+
/// halves use PG's hexadecimal `<hi>/<lo>` `pg_lsn` text form.
81+
fn build_start_replication_cmd(slot: Option<&str>, start_lsn: u64, timeline: u32) -> String {
82+
let lsn = format_pg_lsn(start_lsn);
83+
match slot {
84+
Some(slot) => {
85+
format!("START_REPLICATION SLOT {slot} PHYSICAL {lsn} TIMELINE {timeline}")
86+
}
87+
None => format!("START_REPLICATION {lsn} TIMELINE {timeline}"),
88+
}
89+
}
90+
7891
/// `IDENTIFY_SYSTEM` result.
7992
#[derive(Debug, Clone)]
8093
pub struct SystemIdentity {
@@ -170,18 +183,7 @@ impl SourceFeed {
170183
start_lsn: u64,
171184
timeline: u32,
172185
) -> Result<()> {
173-
let cmd = match slot {
174-
Some(slot) => format!(
175-
"START_REPLICATION SLOT {slot} PHYSICAL {}/{:X} TIMELINE {timeline}",
176-
start_lsn >> 32,
177-
start_lsn as u32
178-
),
179-
None => format!(
180-
"START_REPLICATION {}/{:X} TIMELINE {timeline}",
181-
start_lsn >> 32,
182-
start_lsn as u32
183-
),
184-
};
186+
let cmd = build_start_replication_cmd(slot, start_lsn, timeline);
185187
self.conn.send_query(&cmd).await?;
186188
self.conn.expect_copy_both_open().await?;
187189
self.floors = StatusFloors {
@@ -383,4 +385,28 @@ mod tests {
383385
);
384386
assert_eq!((w, f, a), (5000, 4000, 4000));
385387
}
388+
389+
/// Both halves of the start LSN must be rendered in hexadecimal, matching
390+
/// PG's `pg_lsn` text form (`<hi>/<lo>`). A high word of 0xB must serialize
391+
/// as "B", not "11" — otherwise the source parses it as hex 0x11 and the
392+
/// requested position lands ahead of its WAL flush position.
393+
#[test]
394+
fn start_replication_renders_high_word_in_hex() {
395+
// 0xB/4C000000 — high word past 0x9, where hex and decimal diverge.
396+
let lsn = (0xB_u64 << 32) | 0x4C00_0000;
397+
398+
let with_slot = build_start_replication_cmd(Some("phys"), lsn, 1);
399+
assert_eq!(
400+
with_slot,
401+
"START_REPLICATION SLOT phys PHYSICAL B/4C000000 TIMELINE 1",
402+
"high word must be hex (B), not decimal (11)"
403+
);
404+
405+
let no_slot = build_start_replication_cmd(None, lsn, 1);
406+
assert_eq!(
407+
no_slot,
408+
"START_REPLICATION B/4C000000 TIMELINE 1",
409+
"high word must be hex (B), not decimal (11)"
410+
);
411+
}
386412
}

0 commit comments

Comments
 (0)