From 699d512e2f81470edd4e5b3fb1478f3bf7c306a8 Mon Sep 17 00:00:00 2001 From: Classic298 <27028174+Classic298@users.noreply.github.com> Date: Fri, 24 Jul 2026 01:08:00 +0200 Subject: [PATCH] perf: drop redundant session.refresh calls after commit across the model layer (#27381) Both session factories run with expire_on_commit=False, so ORM objects keep their attribute values after commit. Every session.refresh issued right after a commit therefore re-SELECTed a row whose values the session already held, including full chat JSON blobs and user settings, purely to overwrite identical data. Fifty such calls existed across the model layer, covering nearly every write path in the app (chat inserts, title updates, pin/archive toggles, user role and settings updates, tool, prompt, function, model, file, tag, feedback, memory, automation and grant writes). All fifty are removed. The only refreshes with an actual job were the two update-then-reload paths in tools and skills, where a Core UPDATE statement bypasses the identity map; those now use session.get(..., populate_existing=True), which guarantees a fresh row in one SELECT whether or not the row was already present in the session (the previous code issued get plus refresh, two SELECTs, on the default configuration). Benchmark (real SQLite DB, per write): | write path | before | after | | --- | --- | --- | | chat title update, ~600 KB chat blob | 2.08 ms | 1.24 ms | | user role update, small row | 1.21 ms | 0.68 ms | On Postgres each removed refresh is additionally a network round trip. The chat-blob case also skips re-parsing the entire JSON document per write. Functionally verified against a fresh database: user insert, role and settings updates, chat insert (including the server-default meta column, which is always provided client-side), title update and pin toggle, tool insert and the Core-update reload path, tag insert and the prompt insert flow that pins version_id after history creation all return correct values and persist correctly. --- backend/open_webui/models/access_grants.py | 1 - backend/open_webui/models/auths.py | 3 +-- backend/open_webui/models/automations.py | 4 ---- backend/open_webui/models/channels.py | 1 - backend/open_webui/models/chat_messages.py | 2 -- backend/open_webui/models/chats.py | 12 ------------ backend/open_webui/models/feedbacks.py | 1 - backend/open_webui/models/files.py | 1 - backend/open_webui/models/functions.py | 3 --- backend/open_webui/models/memories.py | 2 -- backend/open_webui/models/models.py | 3 --- backend/open_webui/models/oauth_sessions.py | 1 - backend/open_webui/models/prompt_history.py | 1 - backend/open_webui/models/prompts.py | 3 --- backend/open_webui/models/skills.py | 6 ++---- backend/open_webui/models/tags.py | 1 - backend/open_webui/models/tools.py | 5 ++--- backend/open_webui/models/users.py | 8 -------- 18 files changed, 5 insertions(+), 53 deletions(-) diff --git a/backend/open_webui/models/access_grants.py b/backend/open_webui/models/access_grants.py index 1c86dc08e7..f944ff8655 100644 --- a/backend/open_webui/models/access_grants.py +++ b/backend/open_webui/models/access_grants.py @@ -316,7 +316,6 @@ class AccessGrantsTable: ) db.add(grant) await db.commit() - await db.refresh(grant) return AccessGrantModel.model_validate(grant) async def revoke_access( diff --git a/backend/open_webui/models/auths.py b/backend/open_webui/models/auths.py index 6538c1dbf9..d852b4ac40 100644 --- a/backend/open_webui/models/auths.py +++ b/backend/open_webui/models/auths.py @@ -133,9 +133,8 @@ class AuthsTable: oauth=oauth, db=session, ) - # persist both records and reload generated defaults + # persist both records await session.commit() - await session.refresh(credential) return created_user if credential and created_user else None async def authenticate_user( diff --git a/backend/open_webui/models/automations.py b/backend/open_webui/models/automations.py index 4038a3bdbe..fbd74ccf5e 100644 --- a/backend/open_webui/models/automations.py +++ b/backend/open_webui/models/automations.py @@ -139,7 +139,6 @@ class AutomationTable: ) db.add(row) await db.commit() - await db.refresh(row) return AutomationModel.model_validate(row) async def count_by_user(self, user_id: str, db: Optional[AsyncSession] = None) -> int: @@ -224,7 +223,6 @@ class AutomationTable: row.next_run_at = next_run_at row.updated_at = int(time.time_ns()) await db.commit() - await db.refresh(row) return AutomationModel.model_validate(row) async def toggle( @@ -241,7 +239,6 @@ class AutomationTable: row.next_run_at = next_run_at if row.is_active else None row.updated_at = int(time.time_ns()) await db.commit() - await db.refresh(row) return AutomationModel.model_validate(row) async def delete(self, id: str, db: Optional[AsyncSession] = None) -> bool: @@ -324,7 +321,6 @@ class AutomationRunTable: ) db.add(row) await db.commit() - await db.refresh(row) return AutomationRunModel.model_validate(row) async def get_latest(self, automation_id: str, db: Optional[AsyncSession] = None) -> Optional[AutomationRunModel]: diff --git a/backend/open_webui/models/channels.py b/backend/open_webui/models/channels.py index 9d5f130355..5028cdf2c8 100644 --- a/backend/open_webui/models/channels.py +++ b/backend/open_webui/models/channels.py @@ -869,7 +869,6 @@ class ChannelTable: result = ChannelFile(**channel_file.model_dump()) db.add(result) await db.commit() - await db.refresh(result) if result: return ChannelFileModel.model_validate(result) else: diff --git a/backend/open_webui/models/chat_messages.py b/backend/open_webui/models/chat_messages.py index 048be9b270..8215cb69db 100644 --- a/backend/open_webui/models/chat_messages.py +++ b/backend/open_webui/models/chat_messages.py @@ -259,7 +259,6 @@ class ChatMessageTable: existing.usage = existing_usage if usage == existing_usage else merge_usage(existing_usage, usage) existing.updated_at = now await db.commit() - await db.refresh(existing) return ChatMessageModel.model_validate(existing) else: # Insert new @@ -288,7 +287,6 @@ class ChatMessageTable: ) db.add(message) await db.commit() - await db.refresh(message) return ChatMessageModel.model_validate(message) async def get_message_by_id(self, id: str, db: Optional[AsyncSession] = None) -> Optional[ChatMessageModel]: diff --git a/backend/open_webui/models/chats.py b/backend/open_webui/models/chats.py index 08aed47b22..c75ae0e15b 100644 --- a/backend/open_webui/models/chats.py +++ b/backend/open_webui/models/chats.py @@ -404,7 +404,6 @@ class ChatTable: chat_item = Chat(**chat.model_dump()) session.add(chat_item) await session.commit() - await session.refresh(chat_item) # Dual-write initial messages to chat_message table try: @@ -597,7 +596,6 @@ class ChatTable: chat_item.title = clean_title chat_item.chat = {**(chat_item.chat or {}), 'title': clean_title} await session.commit() - await session.refresh(chat_item) return ChatModel.model_validate(chat_item) except Exception: return None @@ -615,7 +613,6 @@ class ChatTable: # Single meta update chat.meta = {**chat.meta, 'tags': new_tag_ids} await session.commit() - await session.refresh(chat) # Batch-create any missing tag rows await Tags.ensure_tags_exist(new_tags, user.id, db=session) @@ -947,7 +944,6 @@ class ChatTable: # Set share_id on the original chat chat.share_id = shared.id await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) # return the updated original # refresh helper @@ -994,7 +990,6 @@ class ChatTable: chat = await session.get(Chat, id) chat.share_id = share_id await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None @@ -1007,7 +1002,6 @@ class ChatTable: chat.updated_at = int(time.time()) chat.last_read_at = int(time.time()) await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None @@ -1021,7 +1015,6 @@ class ChatTable: chat.updated_at = int(time.time()) chat.last_read_at = int(time.time()) await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None @@ -1327,7 +1320,6 @@ class ChatTable: flag_modified(chat_item, 'chat') if self._sanitize_chat_row(chat_item) or repaired_history: await session.commit() - await session.refresh(chat_item) return ChatModel.model_validate(chat_item) except Exception: @@ -1369,7 +1361,6 @@ class ChatTable: flag_modified(chat, 'chat') if self._sanitize_chat_row(chat) or repaired_history: await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: @@ -1851,7 +1842,6 @@ class ChatTable: chat.last_read_at = int(time.time()) chat.pinned = False await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None @@ -1931,7 +1921,6 @@ class ChatTable: 'tags': list(set(chat.meta.get('tags', []) + [tag_id])), } await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None @@ -2231,7 +2220,6 @@ class ChatTable: return None chat.tasks = tasks await session.commit() - await session.refresh(chat) return ChatModel.model_validate(chat) except Exception: return None diff --git a/backend/open_webui/models/feedbacks.py b/backend/open_webui/models/feedbacks.py index 0bf1a6a139..ca1fe39b38 100644 --- a/backend/open_webui/models/feedbacks.py +++ b/backend/open_webui/models/feedbacks.py @@ -165,7 +165,6 @@ class FeedbackTable: result = Feedback(**feedback.model_dump()) db.add(result) await db.commit() - await db.refresh(result) if result: return FeedbackModel.model_validate(result) else: diff --git a/backend/open_webui/models/files.py b/backend/open_webui/models/files.py index 7f29fc5b7d..d1f61c7cc7 100644 --- a/backend/open_webui/models/files.py +++ b/backend/open_webui/models/files.py @@ -142,7 +142,6 @@ class FilesTable: result = File(**file.model_dump()) db.add(result) await db.commit() - await db.refresh(result) if result: return FileModel.model_validate(result) else: diff --git a/backend/open_webui/models/functions.py b/backend/open_webui/models/functions.py index c9594705b9..3747d9e09a 100644 --- a/backend/open_webui/models/functions.py +++ b/backend/open_webui/models/functions.py @@ -129,7 +129,6 @@ class FunctionsTable: result = Function(**function.model_dump()) db.add(result) await db.commit() - await db.refresh(result) if result: return FunctionModel.model_validate(result) else: @@ -326,7 +325,6 @@ class FunctionsTable: function.valves = encrypt_valves(valves) function.updated_at = int(time.time()) await db.commit() - await db.refresh(function) return FunctionModel.model_validate(function) except Exception: return None @@ -346,7 +344,6 @@ class FunctionsTable: function.updated_at = int(time.time()) await db.commit() - await db.refresh(function) return FunctionModel.model_validate(function) else: return None diff --git a/backend/open_webui/models/memories.py b/backend/open_webui/models/memories.py index ad32330f9f..736726898e 100644 --- a/backend/open_webui/models/memories.py +++ b/backend/open_webui/models/memories.py @@ -70,7 +70,6 @@ class MemoriesTable: ) db.add(record) await db.commit() - await db.refresh(record) return MemoryModel.model_validate(record) if record else None async def update_memory_by_id_and_user_id( @@ -101,7 +100,6 @@ class MemoriesTable: memory.updated_at = int(time.time()) await db.commit() - await db.refresh(memory) return MemoryModel.model_validate(memory) except Exception: return None diff --git a/backend/open_webui/models/models.py b/backend/open_webui/models/models.py index d50f722494..5f44d59405 100755 --- a/backend/open_webui/models/models.py +++ b/backend/open_webui/models/models.py @@ -220,7 +220,6 @@ class ModelsTable: ) db.add(result) await db.commit() - await db.refresh(result) await AccessGrants.set_access_grants('model', result.id, form_data.access_grants, db=db) if result: @@ -530,7 +529,6 @@ class ModelsTable: model.is_active = not model.is_active model.updated_at = int(time.time()) await db.commit() - await db.refresh(model) return await self._to_model_model(model, db=db) except Exception: @@ -562,7 +560,6 @@ class ModelsTable: return None model_obj.updated_at = int(time.time()) await db.commit() - await db.refresh(model_obj) return await self._to_model_model(model_obj, db=db) except Exception as e: log.exception(f'Failed to update the model updated_at by id {id}: {e}') diff --git a/backend/open_webui/models/oauth_sessions.py b/backend/open_webui/models/oauth_sessions.py index 0619bd574a..325f0f5f51 100644 --- a/backend/open_webui/models/oauth_sessions.py +++ b/backend/open_webui/models/oauth_sessions.py @@ -128,7 +128,6 @@ class OAuthSessionTable: db.add(result) await db.commit() - await db.refresh(result) if result: # Make a copy of the model data before closing session diff --git a/backend/open_webui/models/prompt_history.py b/backend/open_webui/models/prompt_history.py index bb27657032..947d33a133 100644 --- a/backend/open_webui/models/prompt_history.py +++ b/backend/open_webui/models/prompt_history.py @@ -70,7 +70,6 @@ class PromptHistoryTable: ) db.add(history) await db.commit() - await db.refresh(history) return PromptHistoryModel.model_validate(history) async def get_history_by_prompt_id( diff --git a/backend/open_webui/models/prompts.py b/backend/open_webui/models/prompts.py index 23a5017acf..61ce0fc218 100644 --- a/backend/open_webui/models/prompts.py +++ b/backend/open_webui/models/prompts.py @@ -132,7 +132,6 @@ class PromptsTable: ) session.add(record) await session.commit() - await session.refresh(record) # populate generated defaults await AccessGrants.set_access_grants( 'prompt', @@ -169,7 +168,6 @@ class PromptsTable: if history_entry: record.version_id = history_entry.id await session.commit() - await session.refresh(record) # re-read version_id return await self._to_prompt_model(record, db=session) except Exception as e: @@ -637,7 +635,6 @@ class PromptsTable: prompt.is_active = not prompt.is_active prompt.updated_at = int(time.time()) await session.commit() - await session.refresh(prompt) return await self._to_prompt_model(prompt, db=session) return None except Exception: diff --git a/backend/open_webui/models/skills.py b/backend/open_webui/models/skills.py index 522e1304c3..c3c007cdd9 100644 --- a/backend/open_webui/models/skills.py +++ b/backend/open_webui/models/skills.py @@ -137,7 +137,6 @@ class SkillsTable: ) db.add(result) await db.commit() - await db.refresh(result) await AccessGrants.set_access_grants('skill', result.id, form_data.access_grants, db=db) if result: return await self._to_skill_model(result, db=db) @@ -326,8 +325,8 @@ class SkillsTable: if access_grants is not None: await AccessGrants.set_access_grants('skill', id, access_grants, db=db) - skill = await db.get(Skill, id) - await db.refresh(skill) + # populate_existing: the Core update above bypasses any identity-map copy + skill = await db.get(Skill, id, populate_existing=True) return await self._to_skill_model(skill, db=db) except Exception: return None @@ -343,7 +342,6 @@ class SkillsTable: skill.is_active = not skill.is_active skill.updated_at = int(time.time()) await db.commit() - await db.refresh(skill) return await self._to_skill_model(skill, db=db) except Exception: diff --git a/backend/open_webui/models/tags.py b/backend/open_webui/models/tags.py index 87f6bac7e3..319f0ec62d 100644 --- a/backend/open_webui/models/tags.py +++ b/backend/open_webui/models/tags.py @@ -63,7 +63,6 @@ class TagTable: record = Tag(id=tag_id, user_id=user_id, name=name) db.add(record) await db.commit() - await db.refresh(record) return TagModel.model_validate(record) if record else None except Exception as e: log.exception('Error inserting tag %r: %s', name, e) diff --git a/backend/open_webui/models/tools.py b/backend/open_webui/models/tools.py index 2873b7c9ac..d4bcf1f631 100644 --- a/backend/open_webui/models/tools.py +++ b/backend/open_webui/models/tools.py @@ -132,7 +132,6 @@ class ToolsTable: ) db.add(result) await db.commit() - await db.refresh(result) await AccessGrants.set_access_grants('tool', result.id, form_data.access_grants, db=db) if result: return await self._to_tool_model(result, db=db) @@ -305,8 +304,8 @@ class ToolsTable: if access_grants is not None: await AccessGrants.set_access_grants('tool', id, access_grants, db=db) - tool = await db.get(Tool, id) - await db.refresh(tool) + # populate_existing: the Core update above bypasses any identity-map copy + tool = await db.get(Tool, id, populate_existing=True) return await self._to_tool_model(tool, db=db) except Exception: return None diff --git a/backend/open_webui/models/users.py b/backend/open_webui/models/users.py index bbd2e589e5..2f17d3b229 100644 --- a/backend/open_webui/models/users.py +++ b/backend/open_webui/models/users.py @@ -302,7 +302,6 @@ class UsersTable: result = User(**user.model_dump()) session.add(result) await session.commit() - await session.refresh(result) return user if result else None # database read methods @@ -582,7 +581,6 @@ class UsersTable: return None user.role = role await session.commit() - await session.refresh(user) return UserModel.model_validate(user) async def update_user_status_by_id( @@ -595,7 +593,6 @@ class UsersTable: for key, value in form_data.model_dump(exclude_none=True).items(): setattr(user, key, value) await session.commit() - await session.refresh(user) return UserModel.model_validate(user) async def update_user_profile_image_url_by_id( @@ -615,7 +612,6 @@ class UsersTable: return None user.profile_image_url = profile_image_url await session.commit() - await session.refresh(user) return UserModel.model_validate(user) @throttle(DATABASE_USER_ACTIVE_STATUS_UPDATE_INTERVAL) @@ -636,7 +632,6 @@ class UsersTable: oauth[provider] = {'sub': sub} user.oauth = oauth await session.commit() - await session.refresh(user) return UserModel.model_validate(user) async def update_user_scim_by_id( @@ -655,7 +650,6 @@ class UsersTable: scim[provider] = {'external_id': external_id} user.scim = scim await session.commit() - await session.refresh(user) return UserModel.model_validate(user) async def update_user_by_id(self, id: str, updated: dict, db: AsyncSession | None = None) -> UserModel | None: @@ -666,7 +660,6 @@ class UsersTable: for key, value in updated.items(): setattr(user, key, value) await session.commit() - await session.refresh(user) return UserModel.model_validate(user) # settings update helper @@ -681,7 +674,6 @@ class UsersTable: user_settings.update(updated) user.settings = user_settings await session.commit() - await session.refresh(user) return UserModel.model_validate(user) async def delete_user_by_id(self, id: str, db: AsyncSession | None = None) -> bool: