ladderlore
  • Type a service name, like S3 or IAM.

Concept · Builds on AIF-C01

Aurora and RDS PostgreSQL with pgvector

Vectors in PostgreSQL: column, HNSW or IVFFlat index, distance operators, one query with filters.

You know from AI Practitioner

  • Aurora PostgreSQL can store vectors next to your rows.
  • It answers vector search with SQL.
  • OpenSearch is the separate option built for search at scale.
Read the AI Practitioner lesson
Application
One SQL query
Aurora PostgreSQL
Filter rows
Nearest vectors
  1. 1The application sends one SQL statement.
  2. 2It names a distance operator and an optional filter.
  3. 3Aurora applies the filter on normal columns.
  4. 4It ranks the remaining rows by vector distance.
One SQL query filters rows and ranks by distance.

Step by step

  1. 1The application sends one SQL statement.
  2. 2It names a distance operator and an optional filter.
  3. 3Aurora applies the filter on normal columns.
  4. 4It ranks the remaining rows by vector distance.

One SQL query filters rows and ranks by distance.

  • Build

    HNSW
    Slower, no training
    IVFFlat
    Faster, after data load
  • Recall

    HNSW
    Better
    IVFFlat
    Good, tune probes
  • Memory

    HNSW
    Higher
    IVFFlat
    Lower
  • Query setting

    HNSW
    hnsw.ef_search
    IVFFlat
    ivfflat.probes
HNSW and IVFFlat indexes compared.

What it is

Turn it on with CREATE EXTENSION vector. Then add a column of type vector(n), where n is the model output size, for example vector(1024). Distance operators are <-> for L2 distance, <=> for cosine distance and <#> for negative inner product. An ORDER BY with the operator and a LIMIT returns the nearest rows. Without an index the search scans every row: exact, but slow on large tables.

What it is

Two index types make it approximate and fast. HNSW builds a graph: slower to build and heavier on memory, with better recall and speed, no training needed, and a tuning knob at query time (hnsw.ef_search). IVFFlat splits vectors into lists: faster to build and lighter, but build it after the table has data, and raise the probes setting at query time for better recall. Indexes cover vectors of up to 2,000 dimensions.

What it is

The strength is one query over vectors and ordinary columns: WHERE tenant_id = 7 AND created_at > ... ORDER BY embedding <=> query LIMIT 5. A selective filter can leave an approximate index with fewer results than the limit, and newer pgvector versions add iterative scans to fix that. Choose pgvector when vectors belong with relational data, the scale is moderate and you already run PostgreSQL. Choose a separate store for very large or search-heavy loads.

Remember

  • CREATE EXTENSION vector, then a vector(n) column. n must match the model.
  • Operators: <-> L2, <=> cosine, <#> negative inner product.
  • HNSW: better recall, slower build, more memory. IVFFlat: faster build, build after loading data.
  • Indexes cover up to 2,000 dimensions.
  • One SQL query can filter relational columns and rank by distance.

Remember

  • Selective filters can return fewer than k rows from an approximate index.

On the exam

  • “Find similar items and filter by tenant in one SQL statement” points to pgvector in Aurora PostgreSQL.
  • “Better recall with no training step” points to an HNSW index.
  • “Faster index build on loaded data with lower memory” points to IVFFlat.