AnalyticDB for PostgreSQL supports creating vector columns in regular tables and accelerating similarity search through Nova vector indexes. This topic describes how to select vector types and provides corresponding table creation examples.
Prerequisites
Before you use the vector search feature, make sure the following conditions are met:
-
Vector search engine optimization is enabled. For more information, see Enable or disable vector search engine optimization.
-
To use the Nova vector index, the instance must be AnalyticDB for PostgreSQL V7.0 with a kernel version of V7.4.2.7 or later.
-
To use the pgvector compatibility mode, apply for access as described in Nova vector search engine.
Select a vector type
Nova supports dense vectors, sparse vectors, binary vectors, and various compressed vector types.
|
Numeric precision |
Vector type |
Description |
Maximum dimensions |
Applicable scenarios |
|
FP32 |
|
Native floating-point array of AnalyticDB for PostgreSQL |
8,000 |
Standard dense vectors. Recommended for new workloads. |
|
|
pgvector-compatible dense vector |
8,000 |
Workloads migrated from pgvector |
|
|
FP16 |
|
Native half-precision floating-point array of AnalyticDB for PostgreSQL |
16,000 |
Array syntax workloads that require reduced storage |
|
|
pgvector-compatible half-precision vector. Storage is approximately half of |
16,000 |
High-dimensional vector workloads migrated from pgvector |
|
|
INT8 |
|
Dense vector compressed with SQ8 quantization |
8,000 |
Dense vector workloads that are sensitive to memory and storage costs |
|
FP32, single-precision sparse floating-point |
|
Native sparse vector of AnalyticDB for PostgreSQL. Only non-zero elements are stored. |
1,000,000,000 |
Keyword search, sparse features, and hybrid sparse-dense retrieval |
|
|
pgvector-compatible sparse vector. Only non-zero elements are stored. |
1,000,000,000 |
Sparse vector workloads migrated from pgvector |
|
|
Binary |
|
Binary vector |
256,000 |
Hash fingerprints and binary encoding similarity search |
|
INT16 |
|
Two-byte integer array |
Subject to index limits |
Primarily for legacy HNSW workload compatibility |
Notes:
-
Nrepresents the vector dimension. -
svectorandsparseveccan contain up to 4,000 non-zero elements. -
For new dense vector workloads, we recommend that you use
real[]first. To reduce storage usage, you can selectfloat2[],halfvec, orsq8vectorbased on your precision requirements. -
sq8vectoruses INT8 format to compress vectors. We recommend that you test the recall rate with actual data before you use this type. -
smallint[]is primarily used for legacy workload compatibility. For new Nova workloads, we recommend that you select other vector types.
Table creation syntax
The syntax for creating a vector table is the same as the syntax for creating a regular table:
CREATE TABLE <TABLE_NAME> (
<ID_COLUMN> <DATA_TYPE> PRIMARY KEY,
<OTHER_COLUMNS>,
<VECTOR_COLUMN> <VECTOR_TYPE> NOT NULL
) DISTRIBUTED BY (<DISTRIBUTION_COLUMN>);
Parameters:
-
TABLE_NAME: the name of the vector table. -
ID_COLUMN: the primary key column. -
VECTOR_COLUMN: the vector column. -
VECTOR_TYPE: the vector data type. -
DISTRIBUTION_COLUMN: the distribution key. We recommend that you select a column with uniform data distribution that is not frequently updated.
A table can contain multiple vector columns.
Examples
Create a single-precision dense vector table
real[] and float4[] are equivalent. The following example creates a table that uses real[] to store text vectors:
CREATE TABLE chunks_float4 (
id BIGINT PRIMARY KEY,
content TEXT,
embedding REAL[] NOT NULL
) DISTRIBUTED BY (id);
Array types do not specify vector dimensions in the CREATE TABLE statement. When you create a vector index, you must specify the dimension through the dim parameter.
Create a pgvector-compatible dense vector table
The following example creates a vector column with a dimension of 1,536:
CREATE TABLE chunks_vector (
id BIGINT PRIMARY KEY,
content TEXT,
embedding VECTOR(1536) NOT NULL
) DISTRIBUTED BY (id);
This type is suitable for workloads that require compatibility with pgvector data read and write formats.
Create a half-precision vector table
Create a float2[] vector column by using native array syntax:
CREATE TABLE chunks_float2 (
id BIGINT PRIMARY KEY,
content TEXT,
embedding FLOAT2[] NOT NULL
) DISTRIBUTED BY (id);
Create a halfvec vector column with a dimension of 1,536 by using pgvector-compatible syntax:
CREATE TABLE chunks_halfvec (
id BIGINT PRIMARY KEY,
content TEXT,
embedding HALFVEC(1536) NOT NULL
) DISTRIBUTED BY (id);
Both float2[] and halfvec use half-precision floating-point numbers to store vectors, which reduces storage and memory usage for high-dimensional vectors.
Create an SQ8-compressed vector table
The following example creates a sq8vector column with a dimension of 1,536:
CREATE TABLE chunks_sq8 (
id BIGINT PRIMARY KEY,
content TEXT,
embedding SQ8VECTOR(1536) NOT NULL
) DISTRIBUTED BY (id);
sq8vector uses INT8 format to store vectors and can be used with Novam or Novad indexes. This type is suitable for scenarios that require reduced memory and storage costs.
Create a native sparse vector table
The following example creates an svector sparse vector column with a dimension of 100,000:
CREATE TABLE sparse_documents (
id BIGINT PRIMARY KEY,
content TEXT,
embedding SVECTOR(100000) NOT NULL
) DISTRIBUTED BY (id);
svector uses indices and values to represent non-zero elements. For example, the following data represents an 8-dimensional sparse vector where the values at dimensions 2, 3, and 6 are 1.2, 1.1, and 0.7 respectively:
SELECT
'{"indices":[2,3,6],"values":[1.2,1.1,0.7]}'::SVECTOR(8);
When you use svector, take note of the following items:
-
The subscripts in
indicesstart from 0. -
The valid subscript range of
SVECTOR(N)is 0 toN-1. -
indicesandvaluesmust contain the same number of elements and correspond to each other by position. -
valuescannot containNaN,Infinity, or-Infinity. -
Each vector can contain up to 4,000 non-zero elements.
Create a pgvector-compatible sparse vector table
The following example creates a sparsevec column with a dimension of 100,000:
CREATE TABLE sparse_documents_compat (
id BIGINT PRIMARY KEY,
content TEXT,
embedding SPARSEVEC(100000) NOT NULL
) DISTRIBUTED BY (id);
This type is suitable for workloads that require compatibility with pgvector sparse vector data formats.
Create a binary vector table
The following example creates a binary vector column with a dimension of 256:
CREATE TABLE binary_vectors (
id BIGINT PRIMARY KEY,
content TEXT,
embedding BIT(256) NOT NULL
) DISTRIBUTED BY (id);
The bit type is suitable for hash fingerprints and binary encoding scenarios. It supports similarity search by using Hamming distance and Jaccard distance.
Usage notes
-
The vector dimensions of the written data must match the dimensions defined for the vector column.
-
For array types such as
real[],float4[],float2[], andsmallint[], the vector dimensions must be specified through thedimparameter when you create an index. -
For
vector(N),halfvec(N),sq8vector(N),svector(N),sparsevec(N), andbit(N), the vector dimensions are specified when you create the table. -
A table can contain multiple vector columns, and you can create separate indexes for different vector columns.