Skip to content

fix(replication): correct Postgres epoch constant in replication client - #4433

Closed
deepshekhardas wants to merge 1 commit into
triggerdotdev:mainfrom
deepshekhardas:fix/replication-postgres-epoch-constant
Closed

fix(replication): correct Postgres epoch constant in replication client#4433
deepshekhardas wants to merge 1 commit into
triggerdotdev:mainfrom
deepshekhardas:fix/replication-postgres-epoch-constant

Conversation

@deepshekhardas

Copy link
Copy Markdown

Rebased version of #4020. Corrects the Postgres epoch constant from \946080000000\ to \946684800000\ (2000-01-01) in the replication client, mirroring \POSTGRES_EPOCH_JDATE\ in pgoutput.ts. Adds a named \POSTGRES_EPOCH_MS\ constant with docs. Closes #4020.

@changeset-bot

changeset-bot Bot commented Jul 31, 2026

Copy link
Copy Markdown

⚠️ No Changeset found

Latest commit: 984ab00

Merging this PR will not cause a version bump for any packages. If these changes should not result in a new version, you're good to go. If these changes should result in a version bump, you need to add a changeset.

This PR includes no changesets

When changesets are added to this PR, you'll see the packages that this PR includes changesets for and the associated semver types

Click here to learn what changesets are, and how to add one.

Click here if you're a maintainer who wants to add a changeset to this PR

@github-actions

Copy link
Copy Markdown
Contributor

Hi @deepshekhardas, thanks for your interest in contributing!

This project requires that pull request authors are vouched, and you are not in the list of vouched users.

This PR will be closed automatically. See https://github.com/triggerdotdev/trigger.dev/blob/main/CONTRIBUTING.md for more details.

@github-actions github-actions Bot closed this Jul 31, 2026

@devin-ai-integration devin-ai-integration Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Devin Review found 2 potential issues.

Open in Devin Review

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;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The 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. upperTimestamp correctly computes micros >> 32 (now / 4294967.296), but lowerTimestamp is computed as now - upperTimestamp * 4294967.296, which is still in milliseconds — it should be multiplied by 1000 to yield the low 32 bits of the microsecond value (internal-packages/replication/src/client.ts:846). The result is a reply timestamp that can be off by up to ~71 minutes in pg_stat_replication.reply_time. This is pre-existing and unrelated to the epoch constant fix, but sits directly adjacent to the changed line; worth fixing in the same pass since the PR is about timestamp correctness.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +15 to +21
/**
* 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;

Copy link
Copy Markdown
Contributor

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/replication ships only inside the webapp/server image (consumed by apps/webapp/app/services/runsReplicationService.server.ts and sessionsReplicationService.server.ts), so per AGENTS.md / CONTRIBUTING.md a .server-changes/ entry may be expected for server-only changes. The rule text enumerates apps/webapp/, apps/supervisor/, "etc.", so whether internal packages qualify is ambiguous; a maintainer should confirm. The behavioral impact is limited to the heartbeat timestamp value emitted to listeners, and neither consumer reads it (both destructure only lsn and shouldRespond), so the fix has no user-visible effect today.

Open in Devin Review

Was this helpful? React with 👍 or 👎 to provide feedback.

@coderabbitai

coderabbitai Bot commented Jul 31, 2026

Copy link
Copy Markdown
Contributor

Review Change Stack

Caution

Review failed

The pull request is closed.

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Repository UI

Review profile: CHILL

Plan: Pro Plus

Run ID: d0bfd780-b3b6-4b43-adf1-bfade897319d

📥 Commits

Reviewing files that changed from the base of the PR and between debfa2b and 984ab00.

📒 Files selected for processing (1)
  • internal-packages/replication/src/client.ts

Walkthrough

Added the shared POSTGRES_EPOCH_MS constant in the replication client. Updated primary keepalive timestamp conversion and acknowledgement timestamp generation to use the constant instead of duplicated numeric offsets.

✨ Finishing Touches
🧪 Generate unit tests (beta)
  • Create PR with unit tests

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant