Postgres Database

R2R uses PostgreSQL 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 chose 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.

  1. Edit the database section in your r2r.toml file:
r2r.toml
[database]
provider = "postgres" # currently only `postgres` is supported

# optional parameters which can be set by environment instead
user = "your_postgres_user"
password = "your_postgres_password"
host = "your_postgres_host"
port = "your_postgres_port"
db_name = "your_database_name"
your_project_name = "your_project_collection_name"
  1. Alternatively, you can set the following environment variables:
export POSTGRES_USER=your_postgres_user
export POSTGRES_PASSWORD=your_postgres_password
export POSTGRES_HOST=your_postgres_host
export POSTGRES_PORT=your_postgres_port
export POSTGRES_DBNAME=your_database_name
export POSTGRES_PROJECT_NAME=your_vector_collection_name

Advanced Postgres Features in R2R

R2R leverages several advanced PostgreSQL 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:

class Vector(UserDefinedType):
    # ... (implementation details)

    class comparator_factory(UserDefinedType.Comparator):
        def l2_distance(self, other):
            return self.op("<->", return_type=Float)(other)

        def max_inner_product(self, other):
            return self.op("<#>", return_type=Float)(other)

        def cosine_distance(self, other):
            return self.op("<=>", return_type=Float)(other)

This allows R2R to perform vector similarity searches using different distance measures.

R2R implements a sophisticated hybrid search that combines full-text search and vector similarity search. This approach provides more accurate and contextually relevant results. Key components of the hybrid search include:

  1. Full-Text Search: Utilizes PostgreSQL’s built-in full-text search capabilities with ts_rank and websearch_to_tsquery.
  2. Semantic Search: Performs vector similarity search using pgvector.
  3. Reciprocal Rank Fusion (RRF): Merges results from full-text and semantic searches.

The collection.py file includes methods for building complex SQL queries that implement this hybrid search approach.

GIN Indexing

R2R uses GIN (Generalized Inverted Index) indexing to optimize both full-text and JSON searches:

Index(
    f"idx_{name}_fts_trgm",
    table.c.fts,
    table.c.text,
    postgresql_using="gin",
    postgresql_ops={"text": "gin_trgm_ops"},
)

This indexing strategy allows for efficient full-text search and trigram similarity matching.

JSON Support

R2R leverages PostgreSQL’s JSONB type for flexible metadata storage:

Column(
    "metadata",
    postgresql.JSONB,
    server_default=text("'{}'::jsonb"),
    nullable=False,
)

This allows for efficient storage and querying of structured metadata alongside vector embeddings.

Performance Considerations

When setting up PostgreSQL for R2R, consider the following performance optimizations:

  1. 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.

  2. Hardware: For large-scale deployments, consider using dedicated PostgreSQL instances with sufficient CPU and RAM to handle vector operations efficiently.

  3. Vacuuming: Regular vacuuming helps maintain database performance, especially for tables with frequent updates or deletions.

  4. Partitioning: For very large datasets, consider table partitioning to improve query performance.

By leveraging these advanced PostgreSQL features and optimizations, R2R provides a powerful and flexible foundation for building sophisticated retrieval and search systems.