When building semantic search or retrieval-augmented generation (RAG) pipelines, generating text embeddings typically requires a separate service outside your database. The rds_embedding extension lets you call an external embedding model directly from ApsaraDB RDS for PostgreSQL, store the resulting vectors alongside your data, and run cosine similarity queries — all within the database. Due to security risks, this extension is restricted from being created on all versions. The content in this topic applies only to instances where the extension is already created.
Due to security risks, the rds_embedding extension is restricted from being created on all major and minor engine versions of RDS PostgreSQL. Upgrading the minor engine version does not lift this restriction. Instances where the extension is already created are not affected and can continue to use it. The content in this topic applies only to such instances.
Prerequisites
Before you begin, ensure that you have:
An RDS instance running PostgreSQL 14 or later
An instance where the
rds_embeddingextension is already created. The extension is restricted from being created on all versions, and upgrading the minor engine version does not lift this restrictionAn Alibaba Cloud Model Studio API key. To get one, see Get your API key
A NAT Gateway configured for the virtual private cloud (VPC) where your RDS instance runs. RDS PostgreSQL instances cannot reach the internet by default, so a NAT Gateway is required to call external embedding models
Enable the extension
Run the following commands using a privileged account. Enable vector first — it provides the vector data type and operations that rds_embedding depends on. The rds_embedding extension is restricted from being created on all versions. The following statement applies only to instances where the extension is already created.
CREATE EXTENSION vector;
CREATE EXTENSION rds_embedding;To disable the extensions:
DROP EXTENSION rds_embedding;
DROP EXTENSION vector;Generate and query embeddings
The following example uses the text-embedding-v3 model from Alibaba Cloud Model Studio to generate 1024-dimensional vectors and run a cosine similarity query. For more information about the model, see Model introduction.
Step 1: Create a table
Create a table with a text column for your content and a vector(1024) column for the embeddings.
CREATE TABLE test(info text, vec vector(1024) NOT NULL);Step 2: Register the embedding model
Register text-embedding-v3 by providing its endpoint URL, authorization header template, request body template, and the JSON path to extract the embedding from the response.
SELECT rds_embedding.add_model(
'text-embedding-v3',
'https://dashscope-intl.aliyuncs.com/api/v1/services/embeddings/text-embedding/text-embedding',
'Authorization: Bearer sk-****',
'{"input":{"texts":["%s"]},"model":"text-embedding-v3","parameters":{"text_type":"query"}}',
'->''output''->''embeddings''->0->>''embedding'''
);Replace sk-**** with your API key.
Step 3: Insert text and embeddings
Insert each row by calling rds_embedding.get_embedding_by_model() inline. The function calls the model API and returns the embedding vector for the given text.
INSERT INTO test SELECT 'Windy high sky, apes cry sadly',
rds_embedding.get_embedding_by_model('text-embedding-v3', 'sk-****', 'Windy high sky, apes cry sadly')::real[];
INSERT INTO test SELECT 'Clear islet, white sand, birds fly back',
rds_embedding.get_embedding_by_model('text-embedding-v3', 'sk-****', 'Clear islet, white sand, birds fly back')::real[];
INSERT INTO test SELECT 'Boundless falling leaves rustle down',
rds_embedding.get_embedding_by_model('text-embedding-v3', 'sk-****', 'Boundless falling leaves rustle down')::real[];
INSERT INTO test SELECT 'Endless Yangtze River rolls on',
rds_embedding.get_embedding_by_model('text-embedding-v3', 'sk-****', 'Endless Yangtze River rolls on')::real[];Replace sk-**** with your API key.
Step 4: Query by vector similarity
Use the <=> operator (cosine distance) to rank rows by semantic similarity to a query string. A distance of 0 means identical vectors; lower values indicate greater similarity.
SELECT
info,
vec <=> rds_embedding.get_embedding_by_model(
'text-embedding-v3',
'sk-****',
'Endless Yangtze River rolls on'
)::real[]::vector AS distance
FROM
test
ORDER BY
vec <=> rds_embedding.get_embedding_by_model(
'text-embedding-v3',
'sk-****',
'Endless Yangtze River rolls on'
)::real[]::vector;Replace sk-**** with your API key.
Expected output:
info | distance
-----------------------------------------+--------------------
Endless Yangtze River rolls on | 0
Boundless falling leaves rustle down | 0.42740682200152647
Clear islet, white sand, birds fly back | 0.5161883811726116
Windy high sky, apes cry sadly | 0.5247695147991147
(4 rows)The query returns "Endless Yangtze River rolls on" first (distance 0, exact match), followed by "Boundless falling leaves rustle down" as the closest semantic neighbor.



