-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
fix(replication): correct Postgres epoch constant in replication client #4433
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -12,6 +12,14 @@ import { | |
| PgoutputParser, | ||
| } from "./pgoutput.js"; | ||
|
|
||
| /** | ||
| * Milliseconds between the Unix epoch (1970-01-01) and the Postgres epoch | ||
| * (2000-01-01), used to convert between Postgres replication timestamps and | ||
| * `Date.now()`. Mirrors `(POSTGRES_EPOCH_JDATE - UNIX_EPOCH_JDATE) * USECS_PER_DAY` | ||
| * in pgoutput.ts (946684800000000 micros). | ||
| */ | ||
| const POSTGRES_EPOCH_MS = 946684800000; | ||
|
|
||
| export interface LogicalReplicationClientOptions { | ||
| /** | ||
| * The pg client config. | ||
|
|
@@ -521,7 +529,7 @@ export class LogicalReplicationClient { | |
| } else if (buffer[0] === 0x6b) { | ||
| // Primary keepalive message | ||
| const timestamp = Math.floor( | ||
| buffer.readUInt32BE(9) * 4294967.296 + buffer.readUInt32BE(13) / 1000 + 946080000000 | ||
| buffer.readUInt32BE(9) * 4294967.296 + buffer.readUInt32BE(13) / 1000 + POSTGRES_EPOCH_MS | ||
| ); | ||
| const shouldRespond = !!buffer.readInt8(17); | ||
| this.events.emit("heartbeat", { lsn, timestamp, shouldRespond }); | ||
|
|
@@ -833,7 +841,7 @@ export class LogicalReplicationClient { | |
| const slice = lsn.split("/"); | ||
| let [upperWAL, lowerWAL]: [number, number] = [parseInt(slice[0], 16), parseInt(slice[1], 16)]; | ||
| // Timestamp as microseconds since midnight 2000-01-01 | ||
| const now = Date.now() - 946080000000; | ||
| const now = Date.now() - POSTGRES_EPOCH_MS; | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🔍 Standby status low-order timestamp word is still scaled in milliseconds, not microseconds The acknowledge path builds a Postgres standby status update whose timestamp field must be microseconds since 2000-01-01, split into two 32-bit words. Was this helpful? React with 👍 or 👎 to provide feedback. |
||
| const upperTimestamp = Math.floor(now / 4294967.296); | ||
| const lowerTimestamp = Math.floor(now - upperTimestamp * 4294967.296); | ||
| if (lowerWAL === 4294967295) { | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
🔍 No .server-changes note for a server-affecting fix
internal-packages/replicationships only inside the webapp/server image (consumed byapps/webapp/app/services/runsReplicationService.server.tsandsessionsReplicationService.server.ts), so per AGENTS.md / CONTRIBUTING.md a.server-changes/entry may be expected for server-only changes. The rule text enumeratesapps/webapp/,apps/supervisor/, "etc.", so whether internal packages qualify is ambiguous; a maintainer should confirm. The behavioral impact is limited to the heartbeattimestampvalue emitted to listeners, and neither consumer reads it (both destructure onlylsnandshouldRespond), so the fix has no user-visible effect today.Was this helpful? React with 👍 or 👎 to provide feedback.