From d4d1efce9394ec54fc530f1bd3bfe98299695b48 Mon Sep 17 00:00:00 2001 From: Johan Brandhorst-Satzkorn Date: Wed, 20 Dec 2023 10:38:32 -0800 Subject: [PATCH] internal/session: sort public_id descending The previous sort would run the risk of missing items which collide on update or create times because of the compound comparison using strictly less than for both the timestamp and the public_id. Sorting both descending avoids thi issue. --- .../80/05_session_base_table_updates.up.sql | 6 +++--- internal/session/query.go | 16 ++++++++-------- 2 files changed, 11 insertions(+), 11 deletions(-) diff --git a/internal/db/schema/migrations/oss/postgres/80/05_session_base_table_updates.up.sql b/internal/db/schema/migrations/oss/postgres/80/05_session_base_table_updates.up.sql index 1bffedf3f..403b98667 100644 --- a/internal/db/schema/migrations/oss/postgres/80/05_session_base_table_updates.up.sql +++ b/internal/db/schema/migrations/oss/postgres/80/05_session_base_table_updates.up.sql @@ -5,10 +5,10 @@ begin; -- Add new indexes for the create time and update time queries. create index session_create_time_public_id_idx - on session (create_time desc, public_id asc); + on session (create_time desc, public_id desc); create index session_update_time_public_id_idx - on session (update_time desc, public_id asc); + on session (update_time desc, public_id desc); analyze session; -commit; \ No newline at end of file +commit; diff --git a/internal/session/query.go b/internal/session/query.go index e9c26b1b0..c16ecd0bb 100644 --- a/internal/session/query.go +++ b/internal/session/query.go @@ -429,13 +429,13 @@ with session_ids as ( from session -- search condition for applying permissions is constructed where %s - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ) select * from session_list where session_list.public_id in (select * from session_ids) - order by create_time desc, public_id asc; + order by create_time desc, public_id desc; ` listSessionsPageTemplate = ` with session_ids as ( @@ -444,13 +444,13 @@ with session_ids as ( where (create_time, public_id) < (@last_item_create_time, @last_item_id) -- search condition for applying permissions is constructed and %s - order by create_time desc, public_id asc + order by create_time desc, public_id desc limit %d ) select * from session_list where session_list.public_id in (select * from session_ids) - order by create_time desc, public_id asc; + order by create_time desc, public_id desc; ` refreshSessionsTemplate = ` with session_ids as ( @@ -459,13 +459,13 @@ with session_ids as ( where update_time > @updated_after_time -- search condition for applying permissions is constructed and %s - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ) select * from session_list where session_list.public_id in (select * from session_ids) -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` refreshSessionsPageTemplate = ` with session_ids as ( @@ -475,13 +475,13 @@ with session_ids as ( and (update_time, public_id) < (@last_item_update_time, @last_item_id) -- search condition for applying permissions is constructed and %s - order by update_time desc, public_id asc + order by update_time desc, public_id desc limit %d ) select * from session_list where session_list.public_id in (select * from session_ids) -order by update_time desc, public_id asc; +order by update_time desc, public_id desc; ` estimateCountSessions = ` select reltuples::bigint as estimate from pg_class where oid in ('session'::regclass)