Pgai Tutorial - Vector Embeddings in PostgreSQL Made Easy

Dave EbbelaarAbout 5 min readMay 27, 2025Watch original
THE SUMMARYAI-generated

Key Concepts

  • Vector Databases: Specialized databases for storing and querying vector embeddings.
  • PGAAI: A PostgreSQL extension by Timescale that allows creating and syncing embeddings directly from PostgreSQL.
  • Embeddings: Numerical representations of data (text, images, etc.) that capture semantic meaning.
  • Vector Search: Finding data points with similar embeddings.
  • RAG (Retrieval-Augmented Generation): An AI framework that combines information retrieval with text generation.
  • Vectorizers: Components within PGAAI that automate the creation and synchronization of embeddings.
  • TimeScaleDB: A time-series database built on PostgreSQL.
  • Cosine Similarity: A measure of similarity between two non-zero vectors.
  • Hybrid Search: Combining vector search with traditional keyword-based search.

PGAAI: A Paradigm Shift in Vector Management

The video introduces PGAAI, a Timescale extension for PostgreSQL, as a solution to the challenges of managing vector embeddings. Traditional vector databases often require separate systems for non-vector data (text, application data), leading to synchronization issues and increased complexity. PGAAI aims to solve this by enabling the creation and synchronization of embeddings directly within PostgreSQL.

Key Points:

  • Problem: Juggling multiple systems (vector database, traditional database, lexical search) for AI applications.
  • Solution: PGAAI allows managing embeddings directly within PostgreSQL, eliminating the need for separate vector databases.
  • Paradigm Shift: Moving the embedding logic into the database itself, ensuring data consistency and simplifying the architecture.

Setting Up PGAAI with Docker

The video provides a step-by-step guide to setting up PGAAI using Docker.

Step-by-Step Process:

  1. Clone the Repository: Clone the provided GitHub repository containing the necessary Docker configuration and code examples.
  2. Configure Environment Variables: Set the OpenAI API key in both the docker/.env and app/.env files. The timescale_service_url is pre-configured.
  3. Docker Compose Up: Navigate to the docker directory and run docker-compose up -d to start the PostgreSQL database with Timescale extensions and the vectorizer worker.
  4. Database Initialization: The initdb.d folder contains SQL scripts that are automatically executed during the initial database setup. These scripts install the PGAAI extension, create a table for news articles (news), and define a vectorizer.

Docker Compose File:

  • Defines two services:
    • timescaledb: PostgreSQL database with Timescale extensions and PGAAI.
    • vectorizer-worker: A worker process that automatically generates and synchronizes embeddings.
  • The timescaledb service uses the OpenAI API key for embedding generation.

SQL Initialization Scripts:

  • 00_extensions.sql: Installs the ai (PGAAI) and vector extensions.
  • 01_table.sql: Creates the news table with columns for id, article, highlights, and summary.
  • 02_vectorizer.sql: Defines a vectorizer using ai.create_vectorize to create an embedding table (news_embedding) and configure the embedding model (e.g., openai_small).

Data Ingestion and Embedding Generation

The video demonstrates how to ingest data into the PostgreSQL database and trigger the automatic embedding generation process.

Steps:

  1. Load Data: Use Python code to load a dataset of news articles (CNN Daily Mail) into a Pandas DataFrame.
  2. Add Context (Summary): Generate a summary for each article using OpenAI and add it as a new column to the DataFrame.
  3. Insert Data into Database: Use SQL queries to insert the data from the DataFrame into the news table.
  4. Automatic Embedding Generation: The vectorizer worker automatically detects changes in the news table and adds the new data to a queue for embedding generation.
  5. Manual Trigger (Optional): To speed up the process, the video demonstrates how to stop and restart the vectorizer-worker Docker container to force immediate processing of the queue.
  6. Verify Embeddings: Check the news_embedding table to confirm that the embeddings have been generated and stored.

Key Points:

  • The vectorizer worker runs periodically (default: every 5 minutes) to synchronize the source data table and the embedding table.
  • The ai.vectorizer_status function can be used to check the status of the vectorizer and the number of pending items.
  • The embedding table stores the embeddings, the corresponding chunk of text, and a reference to the original data in the news table.
  • A view is created to join the original data with the embeddings for easier querying.

Similarity Search with PGAAI

The video shows how to perform similarity search using the generated embeddings.

Steps:

  1. Construct SQL Query: Create a SQL query that uses the ai.openai_embed function to generate an embedding for the search query.
  2. Calculate Cosine Similarity: Use the cosine similarity operator (<->) to compare the query embedding with the embeddings in the news_embedding table.
  3. Order Results: Order the results by cosine similarity to find the most similar articles.
  4. Execute Query: Execute the SQL query to retrieve the results.

Key Points:

  • The ai.openai_embed function abstracts the embedding logic to the database level, allowing you to perform similarity search directly in SQL.
  • The video uses a view that combines the original data with the embeddings for easier querying.
  • The video also mentions the possibility of using the Timescale Vector library for more advanced search capabilities (hybrid search, metadata filtering), but notes that the integration with PGAAI is still under development.

Example Query:

SELECT
    uid,
    chunk
FROM
    news_embedding_openai_small
ORDER BY
    embedding <-> ai.openai_embed('text embedding 3 small', 'Is there any news about London?')
LIMIT 15;

Comparing Different Embedding Models

The video demonstrates how easy it is to compare different embedding models using PGAAI.

Steps:

  1. Create a New Vectorizer: Create a new vectorizer using ai.create_vectorize with a different embedding model (e.g., text embedding 3 large).
  2. Automatic Embedding Generation: The vectorizer worker automatically generates embeddings for the new model.
  3. Modify SQL Query: Modify the SQL query to use the new embedding model in the ai.openai_embed function.
  4. Compare Results: Compare the results of the similarity search using the different embedding models.

Key Points:

  • PGAAI allows you to easily switch between different embedding models without modifying your application code.
  • This makes it easy to experiment with different models and find the best one for your specific use case.
  • The video emphasizes the importance of running experiments and tracking metrics (recall, groundedness, correctness) to evaluate the performance of different models.

Conclusion

PGAAI offers a compelling solution for managing vector embeddings within PostgreSQL. By moving the embedding logic into the database, PGAAI simplifies the architecture, ensures data consistency, and makes it easier to experiment with different embedding models. The video concludes that PGAAI has the potential to streamline AI development workflows and improve the scalability of AI applications.

Main Takeaways:

  • PGAAI simplifies vector management by integrating it directly into PostgreSQL.
  • Automatic embedding generation and synchronization ensure data consistency.
  • Easy experimentation with different embedding models.
  • Potential to streamline AI development workflows and improve scalability.

AI summaries can miss context or contain errors. Check important details against the original video.

Go a little deeper.

Have a question about this video? Load its transcript to open the video chat.