S3 Vectors
AWS S3 Vectors is a managed service that stores and queries high-dimensional vectors at scale, optimized for machine learning and artificial intelligence applications.
The S3 Vectors Wrapper allows you to read, write, and perform vector similarity search operations on S3 Vectors within your Postgres database.
Preparation
Before you can query S3 Vectors, you need to enable the Wrappers extension and store your credentials in Postgres.
Enable Wrappers
Make sure the wrappers extension is installed on your database:
1 | |
Enable the S3 Vectors Wrapper
Enable the s3_vectors_wrapper FDW:
1 2 3 | |
Store your credentials (optional)
By default, Postgres stores FDW credentials inside pg_catalog.pg_foreign_server in plain text. Anyone with access to this table will be able to view these credentials. Wrappers is designed to work with Vault, which provides an additional level of security for storing credentials. We recommend using Vault to store your credentials.
1 2 3 4 5 6 7 8 9 10 11 12 | |
Connecting to S3 Vectors
We need to provide Postgres with the credentials to connect to S3 Vectors. We can do this using the create server command.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 | |
Create a schema
We recommend creating a schema to hold all the foreign tables:
1 | |
Options
The full list of foreign table options are below:
bucket_name- The name of the S3 Vector bucket, required.index_name- The name of the S3 Vector index, required.rowid_column- The column to use as the row identifier for INSERT/DELETE operations, required.
Entities
We can use SQL import foreign schema to import foreign table definitions from S3 Vectors.
For example, using below SQL can automatically create foreign tables in the s3_vectors schema.
1 2 3 4 5 6 | |
S3 Vector Tables
This is an object representing S3 Vector index.
Operations
| Object | Select | Insert | Update | Delete | Truncate |
|---|---|---|---|---|---|
| table | ✅ | ✅ | ❌ | ✅ | ❌ |
Usage
You can also manually create the foreign table like below if you did not use import foreign schema.
1 2 3 4 5 6 7 8 9 10 11 | |
Custom Data Types
embd
The embd type is a custom PostgreSQL data type designed to store and work with high-dimensional vectors for machine learning and AI applications.
Structure:
The embd type internally contains:
- Vector data as an array of 32-bit floating point numbers (Float32)
- Additional metadata fields for internal use
Input Formats:
The embd type accepts input in JSON array format:
1 2 3 4 5 | |
Output Format:
When displayed, the embd type shows a summary format:
1 | |
Usage Examples:
See the following sections for complete examples:
- Inserting Vectors - Examples of inserting data with
embdtype - Querying Vectors - Basic queries and vector similarity search
- Vector Similarity Search with Filtering - Advanced search with metadata filtering
- Advanced Example: Semantic Search - Complete semantic search implementation
Operations:
- Vector similarity search: Use the
<==>operator for approximate nearest neighbor search - Distance calculation: Use
embd_distance()function to get similarity scores - Type casting: Convert JSON arrays to
embdtype using::embdcast
Constraints:
- Only supports 32-bit floating point vectors (Float32)
- Vector dimensions should be consistent within the same index
- Cannot be null when used as vector data in S3 Vectors tables
Functions
embd_distance(embd)
Returns the distance score from the most recent vector similarity search operation.
Syntax:
1 | |
Parameters:
vector_data- Anembdtype column containing vector data
Returns:
real- The distance score from the vector similarity search. This value is only meaningful when used in queries with the<==>operator for vector similarity search.
Usage:
1 2 3 4 5 6 | |
Notes:
- The distance value is only populated during vector similarity search operations using the
<==>operator - For other query types (key-based lookups, list all), the distance will be 0.0
- Lower distance values indicate higher similarity
Query Pushdown Support
This FDW supports limited query pushdown with specific operators based on the type of operation:
Vector Similarity Search (ANN)
For approximate nearest neighbor search using the <==> operator:
| Operation | Note |
|---|---|
data <==> vector_value |
Vector similarity search with embeddings |
metadata <==> json_filter |
Metadata filtering using S3 Vectors filter expressions |
Metadata Filtering Syntax:
The json_filter uses S3 Vectors metadata filtering expressions with the following operators:
- Equality:
$eq,$ne- Exact match or not equal - Numeric Comparisons:
$gt,$gte,$lt,$lte- Greater than, less than comparisons - Array Operations:
$in,$nin- Match any/none of the values in array - Existence Check:
$exists- Check if field exists - Logical Operations:
$and,$or- Combine multiple conditions
Examples:
1 2 3 4 5 6 7 8 9 10 11 | |
For more details on metadata filtering syntax, see the AWS S3 Vectors metadata filtering documentation.
Key-based Queries
For exact key lookups:
| Operation | Note |
|---|---|
key = 'value' |
Exact key match |
key in ('val1', 'val2') |
Multiple key lookup |
Supported Query Patterns
-
List all vectors (no WHERE clause):
1select * from s3_vectors.embeddings; -
Get a specific vector by key:
1select * from s3_vectors.embeddings where key = 'vector_001'; -
Vector similarity search:
1 2 3 4 5
select embd_distance(data) as distance, * from s3_vectors.embeddings where data <==> '[0.1, 0.2, 0.3, ...]'::embd order by 1 limit 10; -
Vector search with metadata filtering:
1 2 3 4 5 6
select embd_distance(data) as distance, * from s3_vectors.embeddings where data <==> '[0.1, 0.2, 0.3, ...]'::embd and metadata <==> '{"category": "product"}'::jsonb order by 1 limit 5;
Note
Only above specific query patterns are supported. Complex queries with unsupported operators or combinations may result in errors.
Supported Data Types
| Postgres Type | S3 Vectors Type |
|---|---|
| text | String (for vector key) |
| embd | Float32 vector data |
| jsonb | Document metadata |
Limitations
This section describes important limitations and considerations when using this FDW:
- Only supports specific query patterns as described in the Query Pushdown section
- Vector similarity search is limited to Float32 vectors
- UPDATE operations are not supported (use DELETE + INSERT instead)
- Complex WHERE clauses with AND/OR combinations are not supported except for specific vector search patterns
- Materialized views using these foreign tables may fail during logical backups
Examples
Basic Setup
First, create a server for S3 Vectors:
1 2 3 4 5 6 7 | |
Import the foreign table:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | |
Querying Vectors
1 2 3 4 5 6 7 8 9 10 11 12 | |
Inserting Vectors
1 2 3 4 5 6 7 8 9 10 11 12 13 | |
Deleting Vectors
1 2 3 4 5 | |
Vector Similarity Search with Filtering
1 2 3 4 5 6 7 | |
Advanced Example: Semantic Search
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | |