Skip to content

fix: use pg_lsn cast for LSN comparison in slot advance resource#391

Open
tsivaprasad wants to merge 1 commit into
mainfrom
PLAT-627-lsn-string-comparison-in-replication-slot-advance-from-cts-resource-causes-silent-slot-advance-skip
Open

fix: use pg_lsn cast for LSN comparison in slot advance resource#391
tsivaprasad wants to merge 1 commit into
mainfrom
PLAT-627-lsn-string-comparison-in-replication-slot-advance-from-cts-resource-causes-silent-slot-advance-skip

Conversation

@tsivaprasad
Copy link
Copy Markdown
Contributor

@tsivaprasad tsivaprasad commented May 21, 2026

Summary

This PR fixes an issue where ReplicationSlotAdvanceFromCTSResource compared target and current LSNs using Go’s string <= operator, which can produce incorrect results across segment boundaries (for example, "F/FFFFFFFF" is lexicographically greater than "10/00000000" even though it is numerically smaller). This could silently skip replication slot advancement during add-node workflows. The fix delegates the comparison to PostgreSQL’s native pg_lsn type for correct LSN ordering.

Changes

  • Add LsnAtOrBefore(lsn1, lsn2 string) query to postgres/create_db.go using:
    SELECT @lsn1::pg_lsn <= @lsn2::pg_lsn

  • Replace the Go string comparison (targetLSN <= currentLSN) in ReplicationSlotAdvanceFromCTSResource.Create() with LsnAtOrBefore() to ensure correct LSN ordering semantics.

PLAT-627

@coderabbitai
Copy link
Copy Markdown

coderabbitai Bot commented May 21, 2026

Review Change Stack

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Pro

Run ID: ecce51e9-5725-4652-b1af-30098ca7c2ec

📥 Commits

Reviewing files that changed from the base of the PR and between 00fd2d7 and b538cdb.

📒 Files selected for processing (2)
  • server/internal/database/replication_slot_advance_from_cts_resource.go
  • server/internal/postgres/create_db.go

📝 Walkthrough

Walkthrough

This PR replaces Go string-based LSN comparison with a PostgreSQL-native comparison. A new LsnAtOrBefore query helper is introduced to safely compare LSN values using pg_lsn type casting. The replication slot advancement logic is updated to use this helper instead of direct string ordering.

Changes

LSN Comparison and Replication Slot Advancement

Layer / File(s) Summary
PostgreSQL LSN comparison helper
server/internal/postgres/create_db.go
New exported LsnAtOrBefore(lsn1, lsn2 string) Query[bool] function that performs LSN comparison in SQL by casting both inputs to PostgreSQL pg_lsn type.
Replication slot advancement with database LSN comparison
server/internal/database/replication_slot_advance_from_cts_resource.go
ReplicationSlotAdvanceFromCTSResource.Create now uses postgres.LsnAtOrBefore(targetLSN, currentLSN) to determine whether advancement is necessary, replacing prior in-code string comparison and adding early return when slot position already satisfies the target.

Poem

A rabbit hops through slots so grand,
Comparing LSNs across the land—
No more string tricks, we trust the source,
PostgreSQL guides our faithful course! 🐰✨

🚥 Pre-merge checks | ✅ 5
✅ Passed checks (5 passed)
Check name Status Explanation
Title check ✅ Passed The title accurately and concisely describes the main fix: using PostgreSQL's pg_lsn type for correct LSN comparison instead of Go string comparison, which is the core issue addressed in this PR.
Description check ✅ Passed The description comprehensively covers the required template sections: Summary explains the bug, Changes lists both modifications, Testing section is present, Checklist is included, and the issue is linked via PLAT-627.
Docstring Coverage ✅ Passed Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%.
Linked Issues check ✅ Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check ✅ Passed Check skipped because no linked issues were found for this pull request.

✏️ Tip: You can configure your own custom pre-merge checks in the settings.

✨ Finishing Touches
📝 Generate docstrings
  • Create stacked PR
  • Commit on current branch
🧪 Generate unit tests (beta)
  • Create PR with unit tests
  • Commit unit tests in branch PLAT-627-lsn-string-comparison-in-replication-slot-advance-from-cts-resource-causes-silent-slot-advance-skip

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 and usage tips.

@codacy-production
Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 0 duplication

Metric Results
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

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.

2 participants