Skip to content

Conversation

@jadepark-dev
Copy link
Collaborator

Ensures consistent query ordering by appending deterministic tiebreakers (tx_lt, msg_index) to ORDER BY clauses.

Changes

  • Add tx_lt and msg_index as tiebreakers when explicit sort fields are provided
  • Tiebreakers inherit direction from the last sort field

@jadepark-dev jadepark-dev marked this pull request as ready for review January 6, 2026 12:41
@jadepark-dev jadepark-dev requested a review from a team as a code owner January 6, 2026 12:41
Copilot AI review requested due to automatic review settings January 6, 2026 12:41
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR adds deterministic tiebreakers (tx_lt, msg_index) to log query ORDER BY clauses to ensure consistent query ordering across PostgreSQL instances. The tiebreakers inherit their sort direction from the last explicit sort field.

Key Changes:

  • Modified addOrderBy to append tx_lt and msg_index tiebreakers when explicit sort fields are provided
  • Tiebreakers use the same direction (ASC/DESC) as the last sort field to maintain consistent ordering
  • Added comprehensive tests for timestamp sorting with tiebreakers

Reviewed changes

Copilot reviewed 3 out of 3 changed files in this pull request and generated no comments.

File Description
pkg/logpoller/store/postgres/queryparser.go Refactored addOrderBy to append tiebreaker fields and added helper function for direction conversion
pkg/logpoller/store/postgres/queryparser_test.go Updated existing test expectation and added new test cases for timestamp sorting with ASC/DESC directions
integration-tests/logpoller/pg_logs_test.go Refactored existing test and added test case for timestamp collision scenarios with tiebreaker verification

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

patricios-space
patricios-space previously approved these changes Jan 6, 2026
// Check exact SQL with ORDER BY
expectedSQL := `SELECT id, filter_id, chain_id, address, event_sig, data_header, data_payload, tx_hash, tx_lt, msg_index, tx_timestamp, block_workchain, block_shard, block_seqno, block_root_hash, block_file_hash, master_block_seqno, msg_lt, created_at FROM ton.log_poller_logs WHERE chain_id = :chain_id AND address = :address AND event_sig = :event_sig ORDER BY tx_lt DESC`
// Check exact SQL with ORDER BY - includes msg_index tiebreaker (tx_lt already primary sort)
expectedSQL := `SELECT id, filter_id, chain_id, address, event_sig, data_header, data_payload, tx_hash, tx_lt, msg_index, tx_timestamp, block_workchain, block_shard, block_seqno, block_root_hash, block_file_hash, master_block_seqno, msg_lt, created_at FROM ton.log_poller_logs WHERE chain_id = :chain_id AND address = :address AND event_sig = :event_sig ORDER BY tx_lt DESC, msg_index DESC`
Copy link
Collaborator

Choose a reason for hiding this comment

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

All these test cases could be refactored to use TableDrivenTests pattern. This would make the test cases clearer. If not, it would be great to at least extract the repeated string of the expectedSQL to a constant.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Yeah good point! didn't see that opportunity.

@jadepark-dev jadepark-dev merged commit a3839e6 into main Jan 9, 2026
35 checks passed
@jadepark-dev jadepark-dev deleted the NONEVM-3304-Non-Deterministic-Timestamp-Sorting-in-Commit-Report-Queries branch January 9, 2026 15:06
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