fix: paged lists repeat some entries and skip others (#35281)

The admin user list, feedbacks, groups, the workspace models, prompts and skills lists, prompt history, notes, shared chats, automations, calendar search, analytics messages and SCIM user listings are loaded page by page, and when many rows share the sorted value (a role, a bulk-import timestamp, a rating) the database is free to return those ties in a different order for each page. On PostgreSQL 16, paging through 300 users sorted by role showed 16 users twice and never showed 16 others, and with an id tiebreaker every user showed up exactly once. These lists now sort by each entry's id as a last step, as the chats and knowledge lists already do.

Fixes #35280
This commit is contained in:
Classic298 authored and GitHub committed 2026-11-30 18:41:44 +04:00
1 parent 00ad719de0
commit 2d03af81ef
12 files changed
+24 -8

No files matched your search

+1 -1
View File
@@ -293,7 +293,7 @@ class AutomationTable:
elif status == 'paused':
stmt = stmt.filter(Automation.is_active == False)
stmt = stmt.order_by(Automation.created_at.desc())
stmt = stmt.order_by(Automation.created_at.desc(), Automation.id)
# Get total count
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
+1 -1
View File
@@ -628,7 +628,7 @@ class CalendarEventTable:
)
)
stmt = stmt.order_by(CalendarEvent.start_at.desc())
stmt = stmt.order_by(CalendarEvent.start_at.desc(), CalendarEvent.id)
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()
+2 -2
View File
@@ -548,7 +548,7 @@ class ChatMessageTable:
result = await db.execute(
select(ChatMessage)
.filter_by(user_id=user_id)
.order_by(ChatMessage.created_at.desc())
.order_by(ChatMessage.created_at.desc(), ChatMessage.id)
.offset(skip)
.limit(limit)
)
@@ -570,7 +570,7 @@ class ChatMessageTable:
stmt = stmt.filter(ChatMessage.created_at >= start_date)
if end_date:
stmt = stmt.filter(ChatMessage.created_at <= end_date)
stmt = stmt.order_by(ChatMessage.created_at.desc()).offset(skip).limit(limit)
stmt = stmt.order_by(ChatMessage.created_at.desc(), ChatMessage.id).offset(skip).limit(limit)
result = await db.execute(stmt)
messages = result.scalars().all()
return [ChatMessageModel.model_validate(message) for message in messages]
+3 -1
View File
@@ -254,6 +254,8 @@ class FeedbackTable:
else:
stmt = stmt.order_by(Feedback.created_at.desc())
stmt = stmt.order_by(Feedback.id)
# Count before pagination without wrapping the ordered item query.
count_result = await db.execute(count_stmt)
total = count_result.scalar() or 0
@@ -435,7 +437,7 @@ class FeedbackTable:
select(Feedback, User)
.join(User, Feedback.user_id == User.id)
.filter(Feedback.user_id == user_id)
.order_by(Feedback.updated_at.desc())
.order_by(Feedback.updated_at.desc(), Feedback.id)
)
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
+1 -1
View File
@@ -419,7 +419,7 @@ class GroupTable:
result = await db.execute(
select(Group, member_count)
.where(Group.id.in_(select(stmt.subquery().c.id)))
.order_by(Group.updated_at.desc())
.order_by(Group.updated_at.desc(), Group.id)
.offset(skip)
.limit(limit)
)
+2
View File
@@ -574,6 +574,8 @@ class ModelsTable:
else:
stmt = stmt.order_by(Model.created_at.desc())
stmt = stmt.order_by(Model.id)
# Count BEFORE pagination
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()
+3 -1
View File
@@ -701,6 +701,8 @@ class NoteTable:
else:
stmt = stmt.order_by(Note.updated_at.desc())
stmt = stmt.order_by(Note.id)
# Count BEFORE pagination
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()
@@ -747,7 +749,7 @@ class NoteTable:
user_groups = await Groups.get_groups_by_member_id(user_id, db=db, include_inherited=True)
user_group_ids = [group.id for group in user_groups]
stmt = select(Note).order_by(Note.updated_at.desc())
stmt = select(Note).order_by(Note.updated_at.desc(), Note.id)
stmt = self._has_permission(db, stmt, {'user_id': user_id, 'group_ids': user_group_ids}, permission)
if skip is not None:
+1 -1
View File
@@ -81,7 +81,7 @@ class PromptHistoryTable:
result = await db.execute(
select(PromptHistory)
.filter(PromptHistory.prompt_id == prompt_id)
.order_by(PromptHistory.created_at.desc())
.order_by(PromptHistory.created_at.desc(), PromptHistory.id.desc())
.offset(offset)
.limit(limit)
)
+2
View File
@@ -382,6 +382,8 @@ class PromptsTable:
else:
query = query.order_by(Prompt.updated_at.desc())
query = query.order_by(Prompt.id)
# Count BEFORE pagination
count_result = await session.execute(select(func.count()).select_from(query.subquery()))
total = count_result.scalar()
@@ -188,6 +188,8 @@ class SharedChatsTable:
else:
stmt = stmt.order_by(SharedChat.updated_at.desc())
stmt = stmt.order_by(SharedChat.id)
if skip:
stmt = stmt.offset(skip)
if limit:
+2
View File
@@ -344,6 +344,8 @@ class SkillsTable:
else:
stmt = stmt.order_by(Skill.updated_at.desc())
stmt = stmt.order_by(Skill.id)
# Count BEFORE pagination
count_result = await db.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()
+4
View File
@@ -529,6 +529,8 @@ class UsersTable:
if order_by == 'created_at':
stmt = stmt.order_by(User.created_at.asc() if direction == 'asc' else User.created_at.desc())
stmt = stmt.order_by(User.id)
count_result = await session.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()
@@ -679,6 +681,8 @@ class UsersTable:
elif not filter:
stmt = stmt.order_by(User.created_at.desc())
stmt = stmt.order_by(User.id)
# Count BEFORE pagination
count_result = await session.execute(select(func.count()).select_from(stmt.subquery()))
total = count_result.scalar()