Yandex Cloud
Search
Discuss with expertTry it for free
  • Customer Stories
  • Documentation
  • Blog
  • All Services
    • Cloud Interconnect
    • Cloud Backup
    • Cloud Registry
    • Yandex AI Studio
    • Compute Cloud
    • Object Storage
    • Managed Service for Kubernetes®
    • Yandex BareMetal
    • Smart Web Security
    • Security Deck
    • Managed Service for PostgreSQL
    • Managed Service for ClickHouse®
    • Monium
    • Cloud CDN
    • Network Load Balancer
    • Virtual Private Cloud
    • Cloud DNS
    • Application Load Balancer
    • Yandex Cloud Video
    • Stackland
    • Yandex Cloud Router
    • Yandex Managed Service for Trino
    • Managed Service for MySQL®
    • Managed Service for Valkey™
    • Managed Service for Apache Spark™
    • Yandex StoreDoc
    • Managed Service for OpenSearch
    • Managed Service for Apache Kafka®
    • Data Transfer
    • Yandex MPP Analytics Engine for PostgreSQL
    • Yandex Managed Service for Apache Airflow®
    • Data Processing
    • Yandex MetaData Hub
    • Managed Service for YDB
    • Managed Service for Sharded PostgreSQL
    • Managed Service for YTsaurus
    • Yandex WebSQL
    • DataLens
    • Yandex Search API
    • SpeechSense
    • SpeechKit
    • DataSphere
    • Vision OCR
    • Translate
    • Yandex Identity Hub
    • Key Management Service
    • Certificate Manager
    • Yandex Lockbox
    • Audit Trails
    • SmartCaptcha
    • Yandex SIEM
    • SourceCraft Code Assistant
    • Container Registry
    • Managed Service for GitLab
    • Managed Service for Prometheus®
    • Cloud Functions
    • API Gateway
    • Yandex Cloud Postbox
    • Message Queue
    • Data Streams
    • Serverless Containers
    • Cloud Notification Service
    • Identity and Access Management
    • Yandex Cloud Console
    • Resource Manager
    • Yandex Cloud Billing
    • Yandex Cloud Quota Manager
    • Cloud Apps
  • Marketplace
    • Featured
    • Infrastructure & Network
    • Data Platform
    • AI for business
    • Security
    • DevOps tools
    • Serverless
    • Monitoring & Resources
  • All Solutions
    • By industry
    • By use case
    • Economics and Pricing
    • Security
    • Technical Support
    • Start testing with double trial credits
    • Cloud credits to scale your IT product
    • Gateway to Russia
    • Cloud for Startups
    • Center for Technologies and Society
    • Yandex Cloud Partner program
    • Price calculator
    • Pricing plans
  • Customer Stories
  • Documentation
  • Blog
© 2026 Direct Cursus Technology L.L.C.
Yandex Managed Service for PostgreSQL
  • Getting started
    • All tutorials
    • Creating a PostgreSQL cluster for 1C
    • Creating a cluster of 1C:Enterprise Linux servers with a Managed Service for PostgreSQL cluster
    • Exporting a database to Yandex Data Processing
    • Troubleshooting cluster performance
    • Performance analysis and optimization
    • Setting up a connection from a container in Serverless Containers
    • Delivering data to Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
    • Delivering data to Yandex Managed Service for YDB using Yandex Data Transfer
    • Delivering data to Yandex Managed Service for Apache Kafka® using Debezium
    • PostgreSQL change data capture and delivery to YDS
    • Delivering data from Yandex Managed Service for Apache Kafka® using Yandex Data Transfer
    • Transferring data from Yandex Object Storage using Yandex Data Transfer
    • Configuring a fault-tolerant architecture in Yandex Cloud
    • Monitoring the status of geographically distributed devices
    • Writing load balancer logs to PostgreSQL
    • Creating an MLFlow server for logging experiments and artifacts
    • Working with data using Query
    • Federated data queries using Query
    • Fixing string sorting issues after upgrading glibc
    • Writing data from a device to a database
    • Connecting to Managed Service for PostgreSQL from a PySpark job in Yandex Managed Service for Apache Spark™ using a Yandex Lockbox secret
    • Configuring permissions for access to a secret created by Connection Manager for a Managed Service for PostgreSQL user
    • Executing analytical queries in Managed Service for PostgreSQL with processing in Managed Service for ClickHouse®
    • Running a vector search with `pgvector` in Yandex Managed Service for OpenSearch
  • Access management
  • Inspections and recommendations
  • Pricing policy
  • Terraform reference
  • Monitoring metrics
  • Audit Trails events
  • Public materials
  • Release notes

In this article:

  • Getting started
  • Required paid resources
  • Set up your infrastructure
  • Prepare your test data
  • Run search queries
  • Create an index to make your search faster
  • HNSW
  • IVFFlat
  • Delete the resources you created
  1. Tutorials
  2. Running a vector search with `pgvector` in Yandex Managed Service for OpenSearch

Running a vector search with vector in Yandex Managed Service for OpenSearch

Written by
Yandex Cloud
Updated at November 16, 2026
View in Markdown
  • Getting started
    • Required paid resources
  • Set up your infrastructure
  • Prepare your test data
  • Run search queries
  • Create an index to make your search faster
    • HNSW
    • IVFFlat
  • Delete the resources you created

With the vector extension, you can store vector data and run a vector search in Yandex Managed Service for PostgreSQL.

The extension supports:

  • Exact and approximate nearest neighbor search.
  • Various vector types: single-precision, half-precision, binary, and sparse vectors.
  • Several vector comparison metrics: L2 (Euclidean distance), cosine distance, dot product, L1 (Manhattan distance), Hamming and Jaccard distance.
  • Any programming language that supports a connection to PostgreSQL.

Vector search can be combined with familiar PostgreSQL and SQL features.

By default, vector performs exact search where it compares all vectors. For large data volumes, you can create vector indexes and use approximate search. This search is faster, but the results may prove less accurate.

vector supports two index types:

  • HNSW, which organizes vectors into a data structure which allows you to go from one similar vector to the next and find nearest neighbors without going through the entire table. This structure is based on a multilayer graph.
  • IVFFlat, which breaks all vectors up into several groups (lists). The search does not scan all groups but only those that are closest to the target vector. Compared to HNSW, this index is built faster and uses less memory; however, it is inferior in terms of quality and search speed.

The vector extension is available in Managed Service for PostgreSQL clusters of version 13 and higher. For more information about vector, see this tutorial.

To run a vector search in Managed Service for PostgreSQL:

  1. Set up your infrastructure.
  2. Prepare your test data.
  3. Run search queries.
  4. Create an index to make your search faster.

If you no longer need the resources you created, delete them.

Getting startedGetting started

Sign up for Yandex Cloud and create a billing account:

  1. Navigate to the management console and log in to Yandex Cloud or create a new account.
  2. On the Yandex Cloud Billing page, make sure you have a billing account linked and it has the ACTIVE or TRIAL_ACTIVE status. If you do not have a billing account, create one and link a cloud to it.

If you have an active billing account, you can create or select a folder for your infrastructure on the cloud page.

Learn more about clouds and folders here.

Required paid resourcesRequired paid resources

  • Managed Service for PostgreSQL cluster: use of computing resources allocated to hosts, as well as the storage and backup size (see Managed Service for PostgreSQL pricing).
  • Public IP addresses if public access is enabled for cluster hosts (see Yandex Virtual Private Cloud pricing).

Set up your infrastructureSet up your infrastructure

  1. Create a cloud network named demo-network.

    When creating a network, three subnets in different availability zones are created automatically.

  2. In demo-network, create a security group named mpg-sg for the Managed Service for PostgreSQL cluster and add the following inbound rule to the group to allow internet access to the cluster:

    • Port range: 6432.
    • Protocol: TCP.
    • Destination name: Address range.
    • IPv4 CIDR: 0.0.0.0/0.
  3. Create a Managed Service for PostgreSQL cluster of any suitable configuration with the following settings:

    • Version: 13 or higher.

    • Network: demo-network.

    • Security group: mpg-sg

    • Database: pgdb.

    When creating your cluster, enable host public access.

    Note

    Public access to cluster hosts is required if you plan to connect to the cluster via the internet. This connection option is simpler and is recommended for the purposes of this guide. You can connect to non-public hosts as well but only from Yandex Cloud virtual machines located in the same cloud network as the cluster.

  4. In the Managed Service for PostgreSQL cluster, add the vector extension to the pgdb database.

Prepare your test dataPrepare your test data

  1. Connect to the Managed Service for PostgreSQL cluster.

  2. Create a column vector of size 3:

    CREATE TABLE items (
      id bigserial PRIMARY KEY, 
      embedding vector(3)
    );
    
  3. Add vectors:

    INSERT INTO items (embedding) 
    VALUES
    ('[1,2,3]'),
    ('[4,5,6]'),
    ('[7,8,9]'),
    ('[10,11,12]'),
    ('[13,14,15]'),
    ('[16,17,18]');
    

Run search queriesRun search queries

  1. Find the three nearest neighbors using L2 distance:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    To get a distance, the vector extension uses the following functions:

    • <->: L2 distance (Euclidean distance).
    • <#>: Negative dot product.
    • <=>: Cosine distance.
    • <+>: L1 distance (Manhattan distance).
    • <~>: Hamming distance (for binary vectors).
    • <%>: Jaccard distance (for binary vectors).
  2. Find three lines that bear the closest resemblance to the first one:

    SELECT *
    FROM items
    WHERE id != 1
    ORDER BY embedding <->
    (
    SELECT embedding
    FROM items
    WHERE id = 1
    )
    LIMIT 3;
    
  3. Find the lines that lie within the boundaries of the set distance:

    SELECT *
    FROM items
    WHERE embedding <-> '[3,1,2]' < 15;
    
  4. Get the distance between each vector and vector [3,1,2]:

    SELECT embedding <-> '[3,1,2]'
    AS distance
    FROM items;
    
  5. To calculate the dot product, multiply the result by -1, since the <#> returns the negative dot product:

    SELECT (embedding <#> '[3,1,2]') * -1 AS inner_product
    FROM items;
    
  6. To find out how similar each vector is to [3,1,2] using cosine similarity, use 1 - cosine distance:

    SELECT 1 - (embedding <=> '[3,1,2]') AS cosine_similarity
    FROM items;
    
  7. Find the mean vector:

    SELECT AVG(embedding)
    FROM items;
    

Create an index to make your search fasterCreate an index to make your search faster

Create an index of each type: HNSW and IVFFlat.

HNSWHNSW

  1. Create an HNSW index for L2 distance search:

    CREATE INDEX ON items
    USING hnsw (embedding vector_l2_ops)
    WITH (
    m = 16,
    ef_construction = 64
    );
    

    Where:

    • m: Maximum number of connections a graph node can have at each layer. The default value is 16.
    • ef_construction: Number of candidates considered when building the graph. The default value is 64. The higher the value, the greater the probability of finding the most relevant records; however, index build time increases, and insertion operations become slower.

    These parameters are set one time when creating an index. To modify their values, re-create the index.

    You need to create a dedicated HNSW index for each distance calculation function. To learn more, see the vector extension guide.

  2. Configure the search settings:

    SET hnsw.ef_search = 3;
    

    Where ef_search is the number of vectors the algorithm considers during the search. The default value is 40. The higher the value, the greater the probability of finding the most relevant records; however, the query runs slower.

    Unlike the m and ef_construction index parameters, the ef_search parameter can be modified before running queries.

  3. Run a search for the nearest neighbors:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    If the search results are not accurate enough, increase the ef_search value and repeat the query.

IVFFlatIVFFlat

  1. Create an IVFFlat index for L2 distance search:

    CREATE INDEX ON items
    USING ivfflat (embedding vector_l2_ops)
    WITH (
    lists = 3
    );
    

    Where lists is the number of groups (lists) into which the vectors will be divided. The recommended value depends on the amount of data. The more groups there are, the faster the search, but the search quality may decrease.

    This parameter is set one time when creating an index. To change its value, re-create the index.

    You need to create a dedicated IVFFlat index for each distance calculation function. To learn more, see the vector extension guide.

  2. Configure the search settings:

    SET ivfflat.probes = 1;
    

    Where probes is the number of groups (lists) viewed when running a search. The default value is 1. The higher the value, the greater the probability of finding the most relevant records; however, the query runs slower.

    Unlike the lists index parameter, the probes search parameter can be modified before running queries.

  3. Run a search for the nearest neighbors:

    SELECT *
    FROM items
    ORDER BY embedding <-> '[3,1,2]'
    LIMIT 3;
    

    If the search results are not accurate enough, increase the probes value and repeat the query.

Delete the resources you createdDelete the resources you created

If you no longer intend to use the Managed Service for PostgreSQL cluster, delete it to avoid paying for it.

Was the article helpful?

Previous
Executing analytical queries in Managed Service for PostgreSQL with processing in Managed Service for ClickHouse®
Next
Logical replication in PostgreSQL
© 2026 Direct Cursus Technology L.L.C.