Component | Description |
Incremental task (Incremental Task) | Automatically captures INSERT / UPDATE through triggers and generates vectors in real time. |
Backfill task (Backfill Task) | Scans existing data in batches based on a primary key cursor and backfills historical vectors in one pass. |
Background worker (Bgworker) | Runs as a resident background process, asynchronously consumes message queues, invokes the embedding model, and writes back results. |
Parameter | Type | Default Value | Description |
tencentdb_ai.autoembedding_worker | bool | on | Whether to enable the background automatic vectorization worker process |
tencentdb_ai.autoembedding_database | string | postgres | Target database to which the worker process connects |
tencentdb_ai.autoembedding_batch_size | int | 32 | Maximum number of messages processed per round (1-10000) |
tencentdb_ai.autoembedding_max_retry | int | 5 | Maximum number of retries for a single message (0-1000) |
tencentdb_ai.autoembedding_retry_base_ms | int | 1000 | Base interval for retry backoff (milliseconds, 1-600000) |
tencentdb_ai.autoembedding_max_input_bytes | int | 65536 | Maximum input bytes for a single embedding call (1-1048576) |
tencentdb_ai.autoembedding_naptime_ms | int | 1000 | Idle sleep interval of the worker process (milliseconds, 10-600000) |
tencentdb_ai.add_incr_autoembedding_task(schema_name NAME, -- schema where the table residestable_name NAME, -- table namesource_columns NAME[], -- array of source text column namesmodel_name NAME, -- embedding model nametarget_column NAME DEFAULT NULL, -- target vector column nameindex_method TEXT DEFAULT 'hnsw', -- vector index methodoptions JSONB DEFAULT '{}' -- extension options) RETURNS BIGINT
Parameter | Type | Description |
schema_name | NAME | Name of the schema where the table resides |
table_name | NAME | Table name (must have a single-column primary key) |
source_columns | NAME[] | Array of text columns to be vectorized |
model_name | NAME | Embedding model name, such as 'kinfra-text-embedding-0.6b' |
target_column | NAME | Target vector column name, defaulting to {source_columns[1]}_embedding |
index_method | TEXT | Vector index method, supporting 'none', 'hnsw', and 'ivfflat', with 'hnsw' as the default (the current version only records this configuration and does not automatically create indexes) |
options | JSONB | Extension options, supporting vector_dim to override the default dimension |
-- Single-column embedding: Creates an embedding for the content column, with the target column automatically named content_embedding.SELECT tencentdb_ai.add_incr_autoembedding_task('public', 'articles',ARRAY['content'],'kinfra-text-embedding-0.6b');-- Multi-column embedding: Merges title + body to generate embeddings, with the target column and index method explicitly specified.SELECT tencentdb_ai.add_incr_autoembedding_task('public', 'articles',ARRAY['title', 'body'],'kinfra-text-embedding-0.6b',target_column => 'doc_embedding',index_method => 'hnsw');-- Override the vector dimension (when embedding_dim is not set in model_list).SELECT tencentdb_ai.add_incr_autoembedding_task('public', 'articles',ARRAY['content'],'kinfra-text-embedding-0.6b',options => '{"vector_dim": 1024}');
tencentdb_ai.add_backfill_autoembedding_task(schema_name NAME, -- schema where the table residestable_name NAME, -- table namesource_columns NAME[], -- array of source text column namesmodel_name NAME, -- embedding model nametarget_column NAME DEFAULT NULL, -- target vector column namestart_now BOOL DEFAULT false, -- whether to start backfill immediatelyoptions JSONB DEFAULT '{}' -- extension options) RETURNS BIGINT
Parameter | Type | Description |
schema_name | NAME | Name of the schema where the table resides |
table_name | NAME | Table Name |
source_columns | NAME[] | Array of text columns to be vectorized |
model_name | NAME | Embedding model name |
target_column | NAME | Target vector column name, defaulting to {source_columns[1]}_embedding |
start_now | BOOL | Whether to start backfill immediately. Defaults to false. If false, the task remains in the not_started state and must be started by calling run_backfill_autoembedding_task(). |
options | JSONB | Extension options, must be consistent with the incremental task configuration. |
-- First create an incremental task (automatically creates the vector column + triggers)SELECT tencentdb_ai.add_incr_autoembedding_task('public', 'articles',ARRAY['content'],'kinfra-text-embedding-0.6b');-- Then create a backfill task to immediately backfill historical data.SELECT tencentdb_ai.add_backfill_autoembedding_task('public', 'articles',ARRAY['content'],'kinfra-text-embedding-0.6b',start_now => true);-- Alternatively, you can register the task first (with start_now defaulting to false) and trigger it manually later.SELECT tencentdb_ai.add_backfill_autoembedding_task('public', 'articles',ARRAY['content'],'kinfra-text-embedding-0.6b');-- Start manually later.SELECT tencentdb_ai.run_backfill_autoembedding_task(1);
tencentdb_ai.drop_incr_autoembedding_task(task_id BIGINT) RETURNS VOIDtencentdb_ai.drop_backfill_autoembedding_task(task_id BIGINT) RETURNS VOID
-- Delete the incremental task.SELECT tencentdb_ai.drop_incr_autoembedding_task(1);-- Delete the backfill task.SELECT tencentdb_ai.drop_backfill_autoembedding_task(2);
tencentdb_ai.run_backfill_autoembedding_task(task_id BIGINT, -- Backfill task IDbatch_size INT DEFAULT 8192 -- Maximum number of rows a background worker scans and enqueues per round.) RETURNS VOID
-- Manually start the backfill.SELECT tencentdb_ai.run_backfill_autoembedding_task(1);
Column Name | Type | Description |
task_kind | TEXT | Task type: incr (incremental) or backfill (stock) |
task_id | BIGINT | Task ID |
schema_name | NAME | schema of the table |
table_name | NAME | Table Name |
source_columns | NAME[] | Source text column |
target_column | NAME | Target vector column |
model_name | NAME | Embedding model |
status | ENUM | Incremental task status: enabled disabled error |
backfill_state | ENUM | Backfill task status: not_started running done / failed |
backfilled_rows | BIGINT | Backfilled row quantity for the backfill task |
pending | BIGINT | Number of pending messages in the queue |
failed_count | BIGINT | Number of errors recorded in the error table |
last_error | TEXT | Last error message |
-- View the status of all tasks.SELECT task_kind, task_id, schema_name, table_name,target_column, status, backfill_state,pending, backfilled_rows, failed_countFROM tencentdb_ai.autoembedding_statusORDER BY task_id;
Column Name | Type | Description |
error_id | BIGSERIAL | Error record ID |
msg_id | BIGINT | Message ID |
task_kind | TEXT | Task type: incr or backfill |
task_id | BIGINT | Associated Task ID |
row_id | JSONB | Primary key value of the data row |
error_code | TEXT | Error Code |
error_message | TEXT | Error Message |
detail | TEXT | Detailed error information |
created_at | TIMESTAMPTZ | Error occurrence time |
-- View recent errors.SELECT task_kind, task_id, row_id, error_message, created_atFROM tencentdb_ai.autoembedding_errorORDER BY created_at DESCLIMIT 10;
-- Install extensions. pgmq / pgvector are installed automatically through CASCADE.CREATE EXTENSION IF NOT EXISTS tencentdb_ai CASCADE;-- Register an embedding model. The json_path parameter must be NULL.SELECT tencentdb_ai.add_model('kinfra-text-embedding-0.6b', NULL, NULL, NULL, 'tokenhub');SELECT tencentdb_ai.update_model_attr('kinfra-text-embedding-0.6b', 'api_key', 'your_api_key');-- Set the embedding dimension. This parameter is required. If it is not set, specify it through options.vector_dim when creating a task.UPDATE tencentdb_ai.model_list SET embedding_dim = 1024 WHERE model_name = 'kinfra-text-embedding-0.6b';
-- Create a knowledge base table.CREATE TABLE kb_articles (id bigserial PRIMARY KEY,title text,content text);-- Insert test data.INSERT INTO kb_articles (title, content) VALUES('Introduction to PostgreSQL', 'PostgreSQL is a powerful open-source object-relational database system...'),('Tencent Cloud VectorDB', 'Tencent Cloud VectorDB is a database system specifically designed for storing and searching high-dimensional vectors...'),('RAG', 'Retrieval-Augmented Generation (RAG) is an AI technology that combines search and generation capabilities...'),
-- Create an incremental task: automatically add the content_embedding column and create a trigger.SELECT tencentdb_ai.add_incr_autoembedding_task('public', 'kb_articles',ARRAY['content'],'kinfra-text-embedding-0.6b') AS incr_task_id;-- Create a backfill task: immediately backfill the existing 3 rows of historical data.SELECT tencentdb_ai.add_backfill_autoembedding_task('public', 'kb_articles',ARRAY['content'],'kinfra-text-embedding-0.6b',start_now => true) AS backfill_task_id;
-- Wait for the background process to complete. By default, a poll is performed every second.SELECT pg_sleep(3);-- Check the backfill progress.SELECT backfill_state, backfilled_rowsFROM tencentdb_ai.autoembedding_statusWHERE task_kind = 'backfill';
backfill_state | backfilled_rows----------------+-----------------done | 3(1 row)
-- Verify that the vector has been generated.SELECT id, title,content_embedding IS NOT NULL AS has_embedding,vector_dims(content_embedding) AS dimsFROM kb_articlesORDER BY id;
id | title | has_embedding | dims----+-----------------+---------------+------1 | Introduction to PostgreSQL | t | 10242 | Tencent Cloud VectorDB | t | 10243 | RAG | t | 1024(3 rows)
-- View task status.SELECT * FROM tencentdb_ai.autoembedding_status;
-- Insert new data. The trigger automatically enqueues it.INSERT INTO kb_articles (title, content) VALUES('Embedding model', 'An embedding model is a machine learning model that converts text into vector representations...');-- Wait for the background process to complete.SELECT pg_sleep(2);-- Verify that the vector for the new data has been generated.SELECT id, title, content_embedding IS NOT NULL AS has_embeddingFROM kb_articlesORDER BY id;
id | title | has_embedding----+-----------------+---------------1 | Introduction to PostgreSQL | t2 | Tencent Cloud VectorDB | t3 | RAG | t4 | Embedding model | t(4 rows)
Status | Description |
enabled | The task is registered, and written data is automatically enqueued. |
error | Task-level exception. Check the last_error field to troubleshoot. |
Status | Description | Operation |
not_started | Created but not started | Call run_backfill_autoembedding_task(). |
running | Backfilling in progress | The background process advances automatically. |
done | Backfill completed | All historical data has been processed. |
failed | Backfill failed | Check last_error. You can call run_backfill_autoembedding_task() to retry. |
Error Scenario | Error Message | Solution |
Unconfigured database | can only use tencentdb_ai autoembedding in database | Check the tencentdb_ai.autoembedding_database configuration. |
Missing primary key | The table does not have a single-column primary key. | Add a single-column primary key to the table. |
Source column does not exist. | source column X does not exist | Confirm that the source column name is correct and has not been deleted. |
Model not registered. | model X not found | First register the model through add_model. |
Missing vector dimension | embedding_dim is not set. | Set model_list.embedding_dim or specify it through options.vector_dim. |
Vector column does not exist. | target column X does not exist | First run add_incr_autoembedding_task to create the column. |
Configuration conflict | config conflicts with existing task | Ensure that incremental and existing task configurations for the same column are consistent. |
Dimension write-back mismatch | The vector dimension returned by the embedding model is inconsistent with the task vector_dim. Messages are archived and recorded to autoembedding_error. | Check whether the model_list.embedding_dim or options.vector_dim configuration matches the actual model dimension. |
Insufficient returned vectors | No error is reported. The number of pending tasks does not decrease for a long time, and no data is written back. | When the number of vectors returned by the embedding model in a single call is fewer than the number of input rows, back off and retry the entire batch of messages. After confirming that the model service status is normal, wait for recovery. |
Worker process not running. | When a task is created, the error "autoembedding worker was never registered" is reported; or the number of pending tasks does not decrease for a long time, and no data is written back. | Verify the shared_preload_libraries and autoembedding_database configurations in the prerequisites, modify them, and then restart the instance for the changes to take effect. |
-- Check the queue backlog.SELECT task_kind, task_id, pendingFROM tencentdb_ai.autoembedding_statusWHERE pending > 0;-- View error details.SELECT * FROM tencentdb_ai.autoembedding_errorORDER BY created_at DESC LIMIT 20;
フィードバック