Skip to content

Non-deterministic pagination in /api/v2/search when rows share the same timestamp #2614

Open

Description

@iShiBin

Problem

The POST /api/v2/search endpoint (added in [hyperdxio/hyperdx#2258](#2258)) uses LIMIT/OFFSET pagination with results ordered by Timestamp. When multiple rows share the same timestamp, pagination becomes non-deterministic -- rows near a page boundary can be duplicated or skipped across pages.

Example: If rows 90-110 all have the same Timestamp value and the client requests maxResults=100, offset=0 followed by maxResults=100, offset=100, ClickHouse arbitrarily assigns which rows fall on which page. The client may see some rows twice and miss others entirely.

Why this happens in practice

While Timestamp is DateTime64(9) (nanosecond precision), duplicate timestamps are common in real-world OTEL pipelines:

  • Collector-assigned timestamps: When logs arrive at the OTEL collector without a timestamp, the collector assigns one. An entire batch receives the same timestamp.
  • Limited source precision: Many language runtimes only produce microsecond or millisecond precision. The remaining digits are zero-padded, reducing the effective keyspace.
  • High-throughput services: Multiple log lines from the same flush, HTTP request, or function call naturally share the same wall-clock time.

Adding search predicates reduces the likelihood (fewer matching rows means fewer ties), but the issue is still possible in any high-volume deployment.

Proposed solution

For tables with enable_block_number_column = 1 and enable_block_offset_column = 1 (which ClickStack already enables on otel_logs), add _block_number and _block_offset as tiebreaker columns in the ORDER BY clause:

ORDER BY Timestamp DESC, _block_number DESC, _block_offset DESC
LIMIT {maxResults} OFFSET {offset}

This produces a deterministic total ordering since (_block_number, _block_offset) uniquely identifies each row within a part. The additional columns should have negligible performance impact when using read_in_order.

Alternatives considered

  • Cursor-based pagination (filter by WHERE Timestamp < :last_seen): Avoids offset entirely but still non-deterministic when ties exist at the cursor boundary. Would also require a tiebreaker.
  • Nanosecond precision alone: Reduces probability but does not eliminate the issue, especially for collector-assigned timestamps.

Activity

  1. iShiBin commented on Jul 9, 2026

    @iShiBin
    Author

    Discussed with @knudtty in Slack. Aaron confirmed this would be accepted as an issue and suggested the block number/offset approach for deterministic ordering, with large-scale performance testing needed.

  2. knudtty commented on Jul 9, 2026

    @knudtty
    Contributor

    I was discussing with one of my colleagues, he highlighted that the /api/v2/search endpoint actually already allows the ORDER BY to be set here. In the case of the default schemas we provide, the order by you'd want to use is

    (toStartOfMinute(Timestamp) DESC, Timestamp DESC, _block_number DESC, _block_offset DESC)

  3. iShiBin commented on Jul 9, 2026

    @iShiBin
    Author

    @knudtty Great! Appending the _block_number DESC, _block_offset DESC will help to get the ordering deterministic.

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions