Postgres
Configure your Postgres database
Postgres Database
R2R uses Postgres as the sole provider for relational and vector search queries. This means that Postgres is involved in handling authentication, document management, and search across R2R. For robust search capabilities, R2R leverages the pgvector
extension and ts_rank
to implement customizable hybrid search.
R2R chooses Postgres as its core technology for several reasons:
- Versatility: Postgres is a robust, advanced database that can handle both relational data and vector embeddings.
- Simplicity: By using Postgres for both traditional data and vector search, R2R eliminates the need for complex syncing between separate databases.
- Familiarity: Many developers are already comfortable with Postgres, making it easier to integrate R2R into existing workflows.
- Extensibility: Postgres’s rich ecosystem of extensions allows R2R to leverage advanced features and optimizations.
Read more about Postgres here.
Postgres Configuration
To customize the database settings, you can modify the database
section in your r2r.toml
file and set corresponding environment variables or provide the settings directly in the configuration file.
- Edit the
database
section in yourr2r.toml
file:
- Alternatively, you can set the following environment variables:
Advanced Postgres Features in R2R
R2R leverages several advanced Postgres features to provide powerful search and retrieval capabilities:
pgvector Extension
R2R uses the pgvector
extension to enable efficient vector similarity search. This is crucial for semantic search operations. The collection.py
file defines a custom Vector
type that interfaces with pgvector
:
This allows R2R to perform efficient vector similarity searches using different distance measures.
Hybrid Search
R2R implements a sophisticated hybrid search which combines full-text search and vector similarity search. This approach provides more accurate and contextually relevant results. Key components of the hybrid search include:
- Full-Text Search: Utilizes Postgres’s built-in full-text search capabilities with
ts_rank
andwebsearch_to_tsquery
. - Semantic Search: Performs vector similarity search using
pgvector
. - Reciprocal Rank Fusion (RRF): Merges results from full-text and semantic searches.
In addition, R2R offers robust logical filters on metadata which include operations like eq
, neq
, gt
, gte
, lt
, lte
, like
, ilike
, in
, and nin
. Refer to the retrieval API documentation for all available inputs.
Indexing
Vector similarity search
R2R supports two primary indexing methods for vector similarity search through pgvector: HNSW (Hierarchical Navigable Small World) and IVF-Flat (Inverted File with Flat Storage).
HNSW offers faster search times and better recall but requires more memory and slower build times, making it ideal for production environments where query speed is critical. IVF-Flat provides a balanced approach with faster index construction and lower memory usage, suitable for scenarios requiring a trade-off between build speed and query performance.
Both methods support cosine, L2, and inner product distance measures. See the index API Reference for detailed configuration options and management endpoints.
Full-text search
R2R uses GIN (Generalized Inverted Index) indexing to optimize full-text searches:
This indexing strategy allows for efficient full-text search.
JSON Support
R2R leverages Postgres’s JSONB type for flexible metadata storage:
This allows for efficient storage and querying of structured metadata alongside vector embeddings.
Performance Considerations
When setting up Postgres for R2R, consider the following performance optimizations:
-
Indexing: Ensure proper indexing for both full-text and vector searches. R2R automatically creates necessary indexes, but you may need to optimize them based on your specific usage patterns.
-
Hardware: For large-scale deployments, consider using dedicated Postgres instances with sufficient CPU and RAM to handle vector operations efficiently.
-
Vacuuming: Regular vacuuming helps maintain database performance, especially for tables with frequent updates or deletions.
-
Partitioning: For very large datasets, consider table partitioning to improve query performance.
By leveraging these advanced Postgres features and optimizations, R2R provides a powerful and flexible foundation for building sophisticated retrieval and search systems.