mirror of
https://github.com/open-webui/open-webui.git
synced 2026-07-17 08:21:12 -05:00
[PR #16584] [CLOSED] feat: PostgreSQL JSONB Support and Optimization for Chat Operations #79050
Reference in New Issue
Block a user
Delete Branch "%!s()"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
📋 Pull Request Information
Original PR: https://github.com/open-webui/open-webui/pull/16584
Author: @PVBLIC-F
Created: 8/13/2025
Status: ❌ Closed
Base:
main← Head:feat/jsonb-detection-support-2025-08-12📝 Commits (10+)
c09a4a2feat: add dynamic JSONB detection and optimization support97b9e0erefactor: enhance JSONB detection with enterprise-grade optimizations0a6726efeat: enhance JSONB detection and database optimization241833cfeat: refactor JSONB auto-detection with thread safety and migration1362eb6style: format files with Black0fe2356docs: add comprehensive professional comments to chats.pyc971f43prod: enhance production readiness with improved logging and validation50a7180resolve: merge conflict in chats.py - combine main branch indexes with JSONB featuresfe4b137Merge feat/jsonb-detection-support-2025-08-12: Add JSONB optimization and thread-safe database operations72d36c4fix: correct JSONB indexes migration syntax📊 Changes
2 files changed (+968 additions, -62 deletions)
View changed files
➕
backend/open_webui/migrations/versions/544ba9a2b077_add_jsonb_indexes.py(+157 -0)📝
backend/open_webui/models/chats.py(+811 -62)📄 Description
Pull Request Checklist
Note to first-time contributors: Please open a discussion post in Discussions and describe your changes before submitting a pull request.
Before submitting, make sure you've checked the following:
devbranch.Changelog Entry
Description
Added
Dynamic Column Type Detection: Automatically detects whether chat and meta columns use JSONB or JSON types
Runtime Capability Caching: Caches database capabilities per connection to avoid repeated schema queries
Graceful Fallback: Seamlessly falls back to standard JSON operations when JSONB is not available
JSONB Containment Operators: Uses PostgreSQL's native @> operator for efficient tag filtering with AND operations
Function Selection: Automatically chooses between jsonb_array_elements and json_array_elements based on actual column types
Optimized Tag Queries: Implements specialized query builders for single and multi-tag operations
GIN Index Creation: Automatically creates optimized GIN indexes for JSONB columns:
idx_chat_meta_tags_gin - For tag-based queries
idx_chat_meta_gin - For general meta field queries
idx_chat_messages_gin - For message content searches
Smart Index Management: Only creates indexes on actual JSONB columns, skips creation on non-PostgreSQL databases
Efficient Tag Filtering: Supports complex tag operations (tag:name, tag:none) with optimal query patterns
Content Search Optimization: Uses appropriate JSON functions based on column types for message content searches
Multi-criteria Search: Handles combined searches across title, content, tags, folders, and metadata
-- Open Web UI PostgreSQL JSONB Migration
-- Run this script to convert existing JSON columns to JSONB for better performance
BEGIN;
-- Step 1: Convert chat column to JSONB
ALTER TABLE chat ALTER COLUMN chat TYPE JSONB USING chat::JSONB;
-- Step 2: Convert meta column to JSONB
ALTER TABLE chat ALTER COLUMN meta TYPE JSONB USING meta::JSONB;
-- Step 3: Create optimized GIN indexes
CREATE INDEX IF NOT EXISTS idx_chat_meta_tags_gin ON chat USING GIN ((meta->'tags'));
CREATE INDEX IF NOT EXISTS idx_chat_meta_gin ON chat USING GIN (meta);
CREATE INDEX IF NOT EXISTS idx_chat_messages_gin ON chat USING GIN ((chat->'history'->'messages'));
-- Step 4: Update statistics
ANALYZE chat;
COMMIT;
-- Verify the changes
SELECT
column_name,
data_type
FROM information_schema.columns
WHERE table_name = 'chat'
AND column_name IN ('chat', 'meta');
Changed
Deprecated
Removed
Fixed
Security
Breaking Changes
Additional Information
Screenshots or Videos
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.
🔄 This issue represents a GitHub Pull Request. It cannot be merged through Gitea due to API limitations.