From e7fc9ee10ce25446cfe4782018eb27d8ec81855a Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 27 Oct 2025 17:42:51 +0100 Subject: [PATCH 1/2] Improve the snapshot list query --- packages/db/queries/get_snapshots_with_cursor.sql | 9 +++------ .../db/queries/get_snapshots_with_cursor.sql.go | 13 +++++-------- 2 files changed, 8 insertions(+), 14 deletions(-) diff --git a/packages/db/queries/get_snapshots_with_cursor.sql b/packages/db/queries/get_snapshots_with_cursor.sql index a3429c31e8..d1688fe7f5 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql +++ b/packages/db/queries/get_snapshots_with_cursor.sql @@ -22,11 +22,8 @@ WHERE -- And NULL does not match with empty json s.metadata @> @metadata OR @metadata = '{}'::jsonb ) - AND ( - s.sandbox_started_at < @cursor_time - OR - (s.sandbox_started_at = @cursor_time AND s.sandbox_id > @cursor_id) - ) + -- The order here is important, we want started_at descending, but sandbox_id ascending + AND (s.sandbox_started_at, @cursor_id) < (@cursor_time, s.sandbox_id) AND NOT (s.sandbox_id = ANY (@snapshot_exclude_sbx_ids::text[])) -ORDER BY s.sandbox_started_at DESC, s.sandbox_id +ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1; diff --git a/packages/db/queries/get_snapshots_with_cursor.sql.go b/packages/db/queries/get_snapshots_with_cursor.sql.go index b73e6e86be..2939b29e7a 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql.go +++ b/packages/db/queries/get_snapshots_with_cursor.sql.go @@ -37,13 +37,10 @@ WHERE -- And NULL does not match with empty json s.metadata @> $3 OR $3 = '{}'::jsonb ) - AND ( - s.sandbox_started_at < $4 - OR - (s.sandbox_started_at = $4 AND s.sandbox_id > $5) - ) + -- The order here is important, we want started_at descending, but sandbox_id ascending + AND (s.sandbox_started_at, $4) < ($5, s.sandbox_id) AND NOT (s.sandbox_id = ANY ($6::text[])) -ORDER BY s.sandbox_started_at DESC, s.sandbox_id +ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1 ` @@ -51,8 +48,8 @@ type GetSnapshotsWithCursorParams struct { Limit int32 TeamID uuid.UUID Metadata types.JSONBStringMap + CursorID pgtype.Timestamptz CursorTime pgtype.Timestamptz - CursorID string SnapshotExcludeSbxIds []string } @@ -67,8 +64,8 @@ func (q *Queries) GetSnapshotsWithCursor(ctx context.Context, arg GetSnapshotsWi arg.Limit, arg.TeamID, arg.Metadata, - arg.CursorTime, arg.CursorID, + arg.CursorTime, arg.SnapshotExcludeSbxIds, ) if err != nil { From 3c3e2fa33d0a1f328df30f0aeb25e34213a05372 Mon Sep 17 00:00:00 2001 From: Jakub Novak Date: Mon, 27 Oct 2025 20:01:57 +0100 Subject: [PATCH 2/2] Fix type issue --- packages/db/queries/get_snapshots_with_cursor.sql | 2 +- packages/db/queries/get_snapshots_with_cursor.sql.go | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/db/queries/get_snapshots_with_cursor.sql b/packages/db/queries/get_snapshots_with_cursor.sql index d1688fe7f5..e849112c5b 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql +++ b/packages/db/queries/get_snapshots_with_cursor.sql @@ -23,7 +23,7 @@ WHERE s.metadata @> @metadata OR @metadata = '{}'::jsonb ) -- The order here is important, we want started_at descending, but sandbox_id ascending - AND (s.sandbox_started_at, @cursor_id) < (@cursor_time, s.sandbox_id) + AND (s.sandbox_started_at, @cursor_id::text) < (@cursor_time, s.sandbox_id) AND NOT (s.sandbox_id = ANY (@snapshot_exclude_sbx_ids::text[])) ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1; diff --git a/packages/db/queries/get_snapshots_with_cursor.sql.go b/packages/db/queries/get_snapshots_with_cursor.sql.go index 2939b29e7a..2859ad1d89 100644 --- a/packages/db/queries/get_snapshots_with_cursor.sql.go +++ b/packages/db/queries/get_snapshots_with_cursor.sql.go @@ -38,7 +38,7 @@ WHERE s.metadata @> $3 OR $3 = '{}'::jsonb ) -- The order here is important, we want started_at descending, but sandbox_id ascending - AND (s.sandbox_started_at, $4) < ($5, s.sandbox_id) + AND (s.sandbox_started_at, $4::text) < ($5, s.sandbox_id) AND NOT (s.sandbox_id = ANY ($6::text[])) ORDER BY s.sandbox_started_at DESC, s.sandbox_id ASC LIMIT $1 @@ -48,7 +48,7 @@ type GetSnapshotsWithCursorParams struct { Limit int32 TeamID uuid.UUID Metadata types.JSONBStringMap - CursorID pgtype.Timestamptz + CursorID string CursorTime pgtype.Timestamptz SnapshotExcludeSbxIds []string }