A production-ready hybrid search system combines the semantic understanding of vector embeddings with the hard filtering rules of a PostgreSQL database. By building a real-time sync pipeline using Change Data Capture, you can query vector databases and relational databases simultaneously for instant, context-aware document retrieval.
In a demo environment, document search feels incredibly easy. You convert a few text files into vector embeddings, dump them into a vector database, and let an LLM run semantic queries. But when you move this setup into production, reality quickly catches up. Your core business data does not live in isolated text files; it lives in dynamic, transactional databases like PostgreSQL.
If a customer changes their account permissions, updates a document, or deletes a record in your primary database, those changes must reflect in your search results instantly. If your vector database is out of sync with your live PostgreSQL schemas, your AI agents and search interfaces will retrieve outdated, incorrect, or unauthorized information. To solve this, you need to build a hybrid search system that keeps your vector database and relational database in perfect, real-time sync.
The Architecture of a Real-Time Hybrid Search System
A true hybrid search system combines two distinct search paradigms: semantic search (which understands the meaning of words using a vector database) and keyword or relational search (which filters results by exact matches, dates, and permissions using a PostgreSQL database). To make this work in real time, your system needs a reliable bridge between these two worlds.
Instead of running slow, expensive batch jobs every night to re-index your entire database, a modern hybrid search architecture relies on event-driven streaming. When a row changes in PostgreSQL, an event is emitted, an embedding is generated, and the vector database is updated in milliseconds. This guarantees that your PostgreSQL document retrieval pipeline is always working with fresh, accurate data.
The Core Components
- The Source of Truth: Your live PostgreSQL database holding structured text, metadata, user permissions, and transactional records.
- The Sync Engine: An event-driven middleware layer that listens for database updates and orchestrates the embedding pipeline.
- The Vector Database: A dedicated vector store optimized for high-speed, nearest-neighbor similarity searches.
- The Query Orchestrator: A backend service that receives search requests, queries both databases, merges the results, and returns the unified answer.
Step 1: Setting Up Change Data Capture (CDC) in PostgreSQL
The first step in building a real-time vector sync pipeline is capturing changes in your PostgreSQL database as they happen. Querying your database tables repeatedly for new updates is inefficient and adds unnecessary load to your production database.
Instead, we use Change Data Capture (CDC). PostgreSQL writes every single database modification to a Write-Ahead Log (WAL) before committing it to disk. By tapping into this log using a logical replication slot, we can stream updates instantly without affecting database performance. Tooling like Debezium, or lightweight serverless listeners, can watch your specific document tables and emit clean JSON events whenever a row is inserted, updated, or deleted.
Step 2: Processing the Event and Generating Embeddings
Once your sync engine receives a database change event, it must transform that raw relational data into a format your vector database understands. This is where your embedding pipeline comes in.
If the event is an INSERT or UPDATE, your sync engine extracts the relevant text fields, sends them to your embedding model API (or an in-house model), and retrieves the mathematical vector representation. Along with the vector, you must also extract metadata—such as tenant IDs, user permissions, and tags—directly from the PostgreSQL event payload.
If the event is a DELETE, the sync engine bypasses the embedding model entirely and immediately sends a delete instruction to the vector database, matching the unique identifier of the PostgreSQL row. This prevents the vector store from returning references to deleted documents.
Step 3: Handling System Failures and Network Latency
In a production-grade hybrid search system, you cannot assume your external services will always be online. Embedding generation APIs can suffer from temporary outages, rate limits, or network spikes. If your sync engine crashes mid-update, your databases will fall out of sync.
To prevent this, you must introduce a resilient queuing layer, such as RabbitMQ or Apache Kafka, between your PostgreSQL CDC stream and your embedding generator. If an embedding API call fails, the event is not lost; it remains in a dead-letter queue to be retried automatically. Additionally, assigning a sequential version number to your PostgreSQL rows ensures that out-of-order webhooks do not overwrite newer vector states with older data.
Step 4: Executing the Hybrid Query
With real-time sync established, querying your data becomes a highly coordinated process. When a user or an AI agent submits a search query, your query orchestrator executes two parallel steps:
- The Vector Query: The search term is converted into an embedding and sent to the vector database to find semantically similar records.
- The Relational Filter: The orchestrator applies strict SQL filtering criteria (such as "only show files belonging to User X" or "only show documents created after 2024") directly to PostgreSQL.
By merging these two datasets, your application avoids the common pitfalls of pure vector search. For example, a pure vector search might return a highly relevant document that the current user does not have permission to view. By combining vector scores with live PostgreSQL permission tables, you guarantee that search results are both contextually smart and perfectly secure.
Building vs. Buying Your Search Infrastructure
Setting up a real-time, event-driven search pipeline requires careful consideration of your team's engineering capacity. While open-source tools make it easier than ever to build prototype search features, scaling a hybrid pipeline to handle millions of real-time updates without data drift requires deep database expertise.
At Oracon Global, our senior in-house team specializes in building production-ready AI architectures, custom database systems, and stateful workflow automations. We design systems where you own 100% of the code and intellectual property, ensuring your AI-native applications are fast, secure, and built to scale.
If you are ready to connect your live relational databases to advanced AI search pipelines, get in touch with Oracon Global today to discuss your architecture.
Frequently asked questions
Why can we not just use pgvector for everything?
While pgvector is excellent for smaller scales or simple applications, dedicated vector databases often handle ultra-low latency searches and complex index builds better at massive scales, requiring a hybrid sync approach.
How does Change Data Capture help with vector search?
Change Data Capture monitors your primary PostgreSQL database for any new, updated, or deleted documents and immediately triggers an embedding pipeline to update your vector database in real time.
What happens if the embedding API goes down during a real-time sync?
Your sync pipeline must use a transactional message queue to retry failed embedding generation jobs, ensuring your PostgreSQL database and vector database do not permanently drift out of sync.
How do you combine vector scores with SQL filter results?
You can use Reciprocal Rank Fusion or structured pre-filtering, where metadata from PostgreSQL is sent along with the vector query to restrict the vector search space to only authorized or relevant documents.
Read next
Beyond Chatbots: How to Build AI Agents That Actually Do Work for Your Business
Most businesses use AI to answer questions. Here is how to build custom AI agents that actually take action, connect to your internal tools, and handle complex workflows.
Beyond the Wrapper: How to Build Custom AI Agents for Business That Actually Work
Many businesses invest in basic AI wrappers only to find they lack the security and context needed for real work. Here is how to build custom AI agents that integrate deeply with your workflows and databases.
Enterprise AI Maintenance Costs: Budgeting for Year Two and Beyond
Building an AI system is only half the battle. Discover the practical, ongoing operational costs of enterprise AI, including token management, model drift, and continuous security audits.
Oracon Global builds production-grade AI agents, automation and apps — and you own the code and IP. Tell us what you want to automate.
Book a call →See our work
