[PR #19931] [CLOSED] perf: Knowledge has access to file improved #48445

Closed
opened 2026-04-30 00:20:04 -05:00 by GiteaMirror · 0 comments
Owner

📋 Pull Request Information

Original PR: https://github.com/open-webui/open-webui/pull/19931
Author: @Classic298
Created: 12/13/2025
Status: Closed

Base: devHead: optimize-file-access-check-8864712883276852158


📝 Commits (7)

📊 Changes

2 files changed (+35 additions, -15 deletions)

View changed files

📝 backend/open_webui/models/knowledge.py (+17 -0)
📝 backend/open_webui/routers/files.py (+18 -15)

📄 Description

Pull Request Checklist

Changelog Entry

Description

This change optimizes the has_access_to_file function in backend/open_webui/routers/files.py to significantly improve performance.

  1. Direct Knowledge Base Lookup: Instead of iterating through all knowledge bases associated with a file and instantiating full Pydantic models for each, a new method get_file_access_control_data was added to KnowledgeTable. This method executes a targeted SQL query joining the knowledge and knowledge_file tables to return only the necessary user_id and access_control columns.
  2. Avoid Redundant File Fetch: The router logic was reordered to perform this knowledge base access check before fetching the file object. If the user has access via a knowledge base, the function returns True immediately, skipping the Files.get_file_by_id call entirely. This saves a database round-trip in the most common success path.
  3. Optimized Legacy Fallback: If the initial check fails, the file is fetched to support legacy collection_name metadata. The logic for checking this metadata was also optimized to fetch the specific knowledge base by ID (O(1)) rather than fetching all knowledge bases for the user and iterating (O(N)).

Contributor License Agreement

By submitting this pull request, I confirm that I have read and fully agree to the Contributor License Agreement (CLA), and I am providing my contributions under its terms.

Note

Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in.


🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.

## 📋 Pull Request Information **Original PR:** https://github.com/open-webui/open-webui/pull/19931 **Author:** [@Classic298](https://github.com/Classic298) **Created:** 12/13/2025 **Status:** ❌ Closed **Base:** `dev` ← **Head:** `optimize-file-access-check-8864712883276852158` --- ### 📝 Commits (7) - [`fe6783c`](https://github.com/open-webui/open-webui/commit/fe6783c16699911c7be17392596d579333fb110c) Merge pull request #19030 from open-webui/dev - [`fc05e0a`](https://github.com/open-webui/open-webui/commit/fc05e0a6c5d39da60b603b4d520f800d6e36f748) Merge pull request #19405 from open-webui/dev - [`e3faec6`](https://github.com/open-webui/open-webui/commit/e3faec62c58e3a83d89aa3df539feacefa125e0c) Merge pull request #19416 from open-webui/dev - [`9899293`](https://github.com/open-webui/open-webui/commit/9899293f050ad50ae12024cbebee7e018acd851e) Merge pull request #19448 from open-webui/dev - [`140605e`](https://github.com/open-webui/open-webui/commit/140605e660b8186a7d5c79fb3be6ffb147a2f498) Merge pull request #19462 from open-webui/dev - [`6f1486f`](https://github.com/open-webui/open-webui/commit/6f1486ffd0cb288d0e21f41845361924e0d742b3) Merge pull request #19466 from open-webui/dev - [`4bb60b1`](https://github.com/open-webui/open-webui/commit/4bb60b17100473318e9d332b212b66cc67cf3f85) Optimize `has_access_to_file` to prioritize direct `knowledge_file` lookups ### 📊 Changes **2 files changed** (+35 additions, -15 deletions) <details> <summary>View changed files</summary> 📝 `backend/open_webui/models/knowledge.py` (+17 -0) 📝 `backend/open_webui/routers/files.py` (+18 -15) </details> ### 📄 Description # Pull Request Checklist # Changelog Entry ### Description This change optimizes the `has_access_to_file` function in `backend/open_webui/routers/files.py` to significantly improve performance. 1. **Direct Knowledge Base Lookup**: Instead of iterating through all knowledge bases associated with a file and instantiating full Pydantic models for each, a new method `get_file_access_control_data` was added to `KnowledgeTable`. This method executes a targeted SQL query joining the `knowledge` and `knowledge_file` tables to return only the necessary `user_id` and `access_control` columns. 2. **Avoid Redundant File Fetch**: The router logic was reordered to perform this knowledge base access check *before* fetching the file object. If the user has access via a knowledge base, the function returns `True` immediately, skipping the `Files.get_file_by_id` call entirely. This saves a database round-trip in the most common success path. 3. **Optimized Legacy Fallback**: If the initial check fails, the file is fetched to support legacy `collection_name` metadata. The logic for checking this metadata was also optimized to fetch the specific knowledge base by ID (O(1)) rather than fetching all knowledge bases for the user and iterating (O(N)). ### Contributor License Agreement By submitting this pull request, I confirm that I have read and fully agree to the [Contributor License Agreement (CLA)](https://github.com/open-webui/open-webui/blob/main/CONTRIBUTOR_LICENSE_AGREEMENT), and I am providing my contributions under its terms. > [!NOTE] > Deleting the CLA section will lead to immediate closure of your PR and it will not be merged in. --- <sub>🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.</sub>
GiteaMirror added the pull-request label 2026-04-30 00:20:04 -05:00
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: github-starred/open-webui#48445