### Install pgvecto.rs from Source Source: https://docs.vectorchord.ai/getting-started/installation.html This snippet outlines the process of building and installing pgvecto.rs from its source code. It involves setting up the development environment (referencing a separate guide), modifying the control file with the correct version number, copying upgrade SQL files, and then using `cargo pgrx install` to build and install the extension. Finally, it includes steps to configure PostgreSQL settings and enable the extension. ```sh sed -e "s/@CARGO_VERSION@/0.0.0/g" ./vectors.control # 0.0.0 here is the version number that you need sudo cp -a ./sql/upgrade/. $(pg_config --sharedir)/extension cargo pgrx install --sudo --release ``` ```sh psql -U postgres -c 'ALTER SYSTEM SET shared_preload_libraries = "vectors.so"' psql -U postgres -c 'ALTER SYSTEM SET search_path TO "$user", public, vectors' ``` -------------------------------- ### Setup Modal for Cloud Operations Source: https://docs.vectorchord.ai/vectorchord/use-case/colqwen2-modal.html These commands install the Modal Python package and set up authentication for your Modal account. The 'modal setup' command will provide a link to authorize your account in a web browser. ```bash $ pip install modal $ python3 -m modal setup # click the link to authorize ``` -------------------------------- ### Download, Build, and Install VectorChord from Source Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Provides commands to download the VectorChord source code, build it using `make`, and install it. This method is for users who prefer to compile the extension themselves. It includes steps for downloading, navigating to the directory, and running build and install commands. ```sh curl -fsSL https://github.com/tensorchord/VectorChord/archive/refs/tags/1.1.1.tar.gz | tar -xz cd VectorChord-1.1.1 make build make install # or `sudo make install` ``` -------------------------------- ### Install VectorChord via PGXN Client Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Installs the VectorChord PostgreSQL extension using the `pgxnclient` tool. This is an alternative installation method for systems with `pgxnclient` set up. The `--sudo` option can be used if root privileges are required for installation. ```sh pgxnclient install vchord # or `pgxnclient install vchord --sudo` ``` -------------------------------- ### Install pgvecto.rs from ZIP Package Source: https://docs.vectorchord.ai/getting-started/installation.html This snippet details the installation of pgvecto.rs from a ZIP package, suitable for non-Debian systems and requiring GLIBC >= 2.31. It includes steps for installing necessary packages (like unzip, wget, libpq-devel), setting up PostgreSQL, verifying PostgreSQL configuration paths, downloading and extracting the ZIP, copying extension files to the PostgreSQL directory, configuring PostgreSQL settings, and enabling the extension. ```sh # Example for RHEL 9 dnf # Please check your package manager sudo dnf install -y unzip wget libpq-devel sudo dnf module install -y postgresql:15/server sudo postgresql-setup --initdb sudo systemctl start postgresql.service sudo systemctl enable postgresql.service ``` ```sh pg_config --pkglibdir # Print something similar to: # /usr/lib/postgresql/15/lib or # /usr/lib64/pgsql pg_config --sharedir # Print something similar to: # /usr/share/postgresql/15 or # /usr/share/pgsql ``` ```sh wget https://github.com/tensorchord/pgvecto.rs/releases/download/v0.2.1/vectors-pg15_x86_64-unknown-linux-gnu_0.2.1.zip -O vectors.zip unzip vectors.zip -d vectors ``` ```sh # Copy library to `$pkglibdir` sudo cp vectors/vectors.so $(pg_config --pkglibdir)/ # Copy schema to `$shardir` sudo cp vectors/vectors--* $(pg_config --sharedir)/extension/ sudo cp vectors/vectors.control $(pg_config --sharedir)/extension/ ``` ```sh psql -U postgres -c 'ALTER SYSTEM SET shared_preload_libraries = "vectors.so"' psql -U postgres -c 'ALTER SYSTEM SET search_path TO "$user", public, vectors' # You need restart the PostgreSQL cluster to take effects. sudo systemctl restart postgresql.service # for pgvecto.rs running with systemd ``` ```sql DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors; ``` -------------------------------- ### Example VectorChord Cloud Metrics Output Source: https://docs.vectorchord.ai/cloud/monitoring/collect-metrics.html This snippet displays a sample of the metrics data provided by VectorChord Cloud. It includes various metrics related to PostgreSQL backend activity, collector performance, and cluster status, formatted in the Prometheus exposition format. ```shell # HELP cnpg_backends_max_tx_duration_seconds Maximum duration of a transaction in seconds # TYPE cnpg_backends_max_tx_duration_seconds gauge cnpg_backends_max_tx_duration_seconds{application_name="cnpg_metrics_exporter",datname="bb",state="active",usename="postgres"} 0 # HELP cnpg_backends_total Number of backends # TYPE cnpg_backends_total gauge cnpg_backends_total{application_name="cnpg_metrics_exporter",datname="bb",state="active",usename="postgres"} 1 # HELP cnpg_backends_waiting_total Total number of backends that are currently waiting on other queries # TYPE cnpg_backends_waiting_total gauge cnpg_backends_waiting_total 0 # HELP cnpg_collector_collection_duration_seconds Collection time duration in seconds # TYPE cnpg_collector_collection_duration_seconds gauge cnpg_collector_collection_duration_seconds{collector="Collect.cnpg"} 0.05290234 cnpg_collector_collection_duration_seconds{collector="Collect.up"} 0.002278657 # HELP cnpg_collector_collections_total Total number of times PostgreSQL was accessed for metrics. # TYPE cnpg_collector_collections_total counter cnpg_collector_collections_total 272 # HELP cnpg_collector_fencing_on 1 if the instance is fenced, 0 otherwise # TYPE cnpg_collector_fencing_on gauge cnpg_collector_fencing_on 0 # HELP cnpg_collector_first_recoverability_point The first point of recoverability for the cluster as a unix timestamp # TYPE cnpg_collector_first_recoverability_point gauge cnpg_collector_first_recoverability_point 1.733281603e+09 # HELP cnpg_collector_last_available_backup_timestamp The last available backup as a unix timestamp # TYPE cnpg_collector_last_available_backup_timestamp gauge cnpg_collector_last_available_backup_timestamp 1.733281603e+09 ``` -------------------------------- ### List Installed PostgreSQL Extensions (Shell) Source: https://docs.vectorchord.ai/vectorchord/admin/migration.html Displays a list of all installed PostgreSQL extensions, including their versions, schemas, and descriptions. This is useful for verifying the installation of VectorChord and other extensions. ```shell postgres=# \dx List of installed extensions Name | Version | Schema | Description ---------+---------+------------+---------------------------------------------------------------------------------------------- plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language vchord | 0.1.0 | public | vchord: Vector database plugin for Postgres, written in Rust, specifically designed for LLM vector | 0.8.0 | public | vector data type and ivfflat and hnsw access methods vectors | 0.4.0 | vectors | vectors: Vector database plugin for Postgres, written in Rust, specifically designed for LLM ``` -------------------------------- ### Run VectorChord PostgreSQL Docker Container Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Launches a Docker container with VectorChord and PostgreSQL pre-installed. It sets a password for the 'postgres' user and maps the default PostgreSQL port. This is the simplest way to start using VectorChord. ```sh docker run \ --name vchord-demo \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ -d tensorchord/vchord-postgres:pg18-v1.1.1 ``` -------------------------------- ### Install PostgreSQL Extension Source: https://docs.vectorchord.ai/faqs/general.html This SQL command installs a PostgreSQL extension. It is used when the required 'vectors' extension is not found after checking the list of installed extensions. ```sql CREATE EXTENSION vectors; ``` -------------------------------- ### Start Postgres with VectorChord Extensions using Docker Source: https://docs.vectorchord.ai/vectorchord/use-case/hybrid-search.html This command starts a PostgreSQL instance with VectorChord-BM25 and VectorChord extensions enabled using a Docker image. It maps the default PostgreSQL port and runs the container in detached mode. ```bash docker run \ --name vchord-suite \ -e POSTGRES_PASSWORD=postgres \ -p 5432:5432 \ -d tensorchord/vchord-suite:pg17-latest ``` -------------------------------- ### Vector Indexing Examples with Various Algorithms and Settings (SQL) Source: https://docs.vectorchord.ai/usage/indexing.html A collection of SQL examples showcasing the creation of vector indexes using different algorithms (HNSW, IVF, brute force) and configurations, including FP16 vector types, product quantization (PQ), scalar quantization (SQ), and options for optimizing build threads and graph size. ```sql -- HNSW algorithm, default settings. CREATE INDEX ON items USING vectors (embedding vector_l2_ops); -- Use FP16 vector type for optimizing data storage. CREATE TABLE items (embedding vecf16(3)); CREATE INDEX ON items USING vectors (embedding vecf16_l2_ops) --- Or using brute force with product quantization (PQ). CREATE INDEX ON items USING vectors (embedding vector_l2_ops) WITH (options = $$ [indexing.flat] quantization.product.ratio = "x16" $$); -- Or using brute force with scalar quantization (SQ). CREATE INDEX ON items USING vectors (embedding vector_l2_ops) WITH (options = "[indexing.hnsw.quantization.scalar]"); --- Or using IVFPQ algorithm. CREATE INDEX ON items USING vectors (embedding vector_l2_ops) WITH (options = $$ [indexing.ivf] quantization.product.ratio = "x16" $$); -- Use more threads for background building the index. CREATE INDEX ON items USING vectors (embedding vector_l2_ops) WITH (options = $$ optimizing.optimizing_threads = 16 $$); -- Prefer smaller HNSW graph. CREATE INDEX ON items USING vectors (embedding vector_l2_ops) WITH (options = $$ segment.max_growing_segment_size = 200000 $$); ``` -------------------------------- ### Install pgvecto.rs using Docker Source: https://docs.vectorchord.ai/getting-started/installation.html This snippet demonstrates how to run pgvecto.rs using a Docker container. It pulls the latest image, sets up a container with a persistent password, and maps the PostgreSQL port. It also includes commands to connect to the database and enable the extension. ```sh docker run \ --name pgvecto-rs-demo \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ -d tensorchord/pgvecto-rs:pg16-v0.2.0 ``` ```sh psql postgresql://postgres:mysecretpassword@localhost:5432/postgres ``` ```sql DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors; ``` -------------------------------- ### Run VectorChord Suite Docker Container Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Launches a Docker container for the VectorChord Suite, which includes VectorChord and all official TensorChord extensions. This image is suitable for developers needing a comprehensive set of tools. The example uses a specific tag `pg18-latest`. ```sh docker run \ --name vchord-suite \ -e POSTGRES_PASSWORD=postgres \ -p 5432:5432 \ -d tensorchord/vchord-suite:pg18-latest \ # If you want to use ghcr image, you can change the image to `ghcr.io/tensorchord/vchord-suite:pg18-latest`. \ # if you want to use the specific version, you can use the tag `pg17-20250815`, supported version can be found in the support matrix. ``` -------------------------------- ### Run VectorChord PostgreSQL Docker Image Source: https://docs.vectorchord.ai/vectorchord/getting-started/overview.html This snippet demonstrates how to run the VectorChord PostgreSQL Docker image. It starts a container named 'vectorchord-demo', sets the PostgreSQL password, maps the default PostgreSQL port, and runs the container in detached mode. This is the recommended way for new users to quickly get started with VectorChord. ```bash docker run \ --name vectorchord-demo \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ -d tensorchord/vchord-postgres:pg18-v1.1.1 ``` -------------------------------- ### Start Database Sync with Docker Source: https://docs.vectorchord.ai/admin/logical_replication.html Starts the data synchronization between bootstrapped PostgreSQL databases. This command sets up the necessary publication and subscription configurations. It requires source and target database connection URLs and the group name defined during bootstrap. ```shell $ docker run -e SOURCE_DB_URL="postgres://postgres:password@192.168.64.2:5432/postgres" -e TARGET_DB_URL="postgres://postgres:password@192.168.64.3:5432/postgres" -it --rm --network=pg_regress_localnet shayonj/pg_easy_replicate:latest pg_easy_replicate start_sync --group-name database-cluster-1 ``` -------------------------------- ### Install pgvecto.rs from Debian Package Source: https://docs.vectorchord.ai/getting-started/installation.html This snippet shows how to install pgvecto.rs using a Debian package (.deb file). It requires GLIBC >= 2.31. The steps involve installing the package, configuring PostgreSQL by modifying system settings for shared libraries and search paths, restarting the PostgreSQL service, and finally enabling the extension within the database. ```sh psql -U postgres -c 'ALTER SYSTEM SET shared_preload_libraries = "vectors.so"' psql -U postgres -c 'ALTER SYSTEM SET search_path TO "$user", public, vectors' # You need restart the PostgreSQL cluster to take effects. sudo systemctl restart postgresql.service # for pgvecto.rs running with systemd ``` ```sql DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors; ``` -------------------------------- ### Verify Vectorchord Extension Installation (Shell) Source: https://docs.vectorchord.ai/vectorchord/admin/kubernetes.html This shell command sequence demonstrates how to verify the successful installation of the vectorchord extension after creating the PostgreSQL cluster. It involves port-forwarding to the PostgreSQL service and then connecting via psql to list installed extensions. This confirms that 'vchord' is recognized and installed. ```shell $ kubectl port-forward services/vchord-rw 5432:5432 $ psql -h $ psql -h 0.0.0.0 -U tensorchord -d tensorchord Password for user tensorchord: psql (13.3 (Ubuntu 13.3-1.pgdg16.04+1), server 18.0 (Debian 18.0-1.pgdg12+3)) WARNING: psql major version 13, server major version 18. Some psql features might not work. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, bits: 256, compression: off) Type "help" for help. tensorchord=> \dx List of installed extensions Name | Version | Schema | Description ---------+---------+------------+--------------------------------------------------------------------------------------------- plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language vchord | 1.1.1 | public | vchord: Vector database plugin for Postgres, written in Rust, specifically designed for LLM vector | 0.8.1 | public | vector data type and ivfflat and hnsw access methods (3 rows) ``` -------------------------------- ### Run pgvecto.rs Docker Image Source: https://docs.vectorchord.ai/getting-started/overview.html This command starts a Docker container for pgvecto.rs, exposing the PostgreSQL port and setting a password. It's a quick way to set up a local PostgreSQL instance with the extension. ```shell docker run \ --name pgvecto-rs-demo \ -e POSTGRES_PASSWORD=mysecretpassword \ -p 5432:5432 \ -d tensorchord/pgvecto-rs:pg16-v0.2.0 ``` -------------------------------- ### Build VectorChord from Scratch Image in Dockerfile Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Demonstrates how to use the VectorChord scratch image within a Dockerfile to install VectorChord into a PostgreSQL image. This method allows for custom image builds by copying VectorChord files into a base PostgreSQL image. ```dockerfile FROM tensorchord/vchord-scratch:pg18-v1.1.1 AS vchord_scratch FROM postgres:18-bookworm RUN apt-get update && apt-get install -y postgresql-18-pgvector COPY --from=vchord_scratch / / CMD ["postgres", "-c", \"shared_preload_libraries=vchord,vector\"] ``` -------------------------------- ### Check Installed PostgreSQL Extensions Source: https://docs.vectorchord.ai/faqs/general.html This command lists all installed extensions in PostgreSQL. It helps verify if the 'vectors' extension, crucial for VectorChord, is present and its version. If the extension is not found, it needs to be created. ```bash postgres=# \dx ``` -------------------------------- ### Install VectorChord Extension (SQL) Source: https://docs.vectorchord.ai/vectorchord/admin/migration.html Installs the VectorChord extension in PostgreSQL. This command ensures that VectorChord is enabled and ready for use, typically in the 'public' schema. ```sql CREATE EXTENSION vchord CASCADE; ``` -------------------------------- ### Copy pgvector Binaries to PostgreSQL Directories (Shell) Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html This snippet demonstrates how to copy the pgvector extension's library and shared files to their respective PostgreSQL directories. It uses `pg_config` to dynamically determine the correct paths, ensuring compatibility with the installed PostgreSQL version. This is a manual step often required when installing from source or custom builds. ```shell cp -r ./pkglibdir/. $(pg_config --pkglibdir) cp -r ./sharedir/. $(pg_config --sharedir) ``` -------------------------------- ### List Installed Extensions (Shell) Source: https://docs.vectorchord.ai/admin/migration.html A psql command to list all installed PostgreSQL extensions. This helps verify that both 'vector' (pgvector) and 'vectors' (PGVecto.rs) are present and correctly identified. ```shell postgres=# \dx List of installed extensions Name | Version | Schema | Description ---------+---------+------------+---------------------------------------------------------------------------------------------- plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language vector | 0.7.2 | public | vector data type and ivfflat and hnsw access methods vectors | 0.3.0 | vectors | vectors: Vector database plugin for Postgres, written in Rust, specifically designed for LLM ``` -------------------------------- ### Create VectorChord and related extensions in PostgreSQL Source: https://docs.vectorchord.ai/vectorchord/use-case/hybrid-search.html These SQL commands create the necessary extensions for VectorChord, including `vchord`, `pg_tokenizer`, and `vchord_bm25`. The `CASCADE` option ensures that dependencies are also handled. The `\dx` command lists installed extensions. ```sql CREATE EXTENSION IF NOT EXISTS vchord CASCADE; CREATE EXTENSION IF NOT EXISTS pg_tokenizer CASCADE; CREATE EXTENSION IF NOT EXISTS vchord_bm25 CASCADE; \dx pg_tokenizer | 0.1.0 | tokenizer_catalog | pg_tokenizer vchord | 0.3.0 | public | vchord: Vector database plugin for Postgres, written in Rust, specifically designed for LLM vchord_bm25 | 0.2.0 | bm25_catalog | vchord_bm25: A postgresql extension for bm25 ranking algorithm vector | 0.8.0 | public | vector data type and ivfflat and hnsw access methods ``` -------------------------------- ### List Valid CPU Options for Rust Compiler Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Displays all valid CPU options that can be used with `rustc --target-cpu` and identifies the default CPU for your current target triple. This is useful for understanding available optimizations. ```sh rustc --print target-cpus ``` -------------------------------- ### Create VectorChord Extensions in PostgreSQL Source: https://docs.vectorchord.ai/vectorchord/use-case/colqwen2-modal.html These SQL commands create the necessary extensions for VectorChord, pg_tokenizer, and vchord_bm25 within a PostgreSQL instance. The '\dx' command lists installed extensions and their versions. ```sql CREATE EXTENSION IF NOT EXISTS vchord CASCADE; CREATE EXTENSION IF NOT EXISTS pg_tokenizer CASCADE; CREATE EXTENSION IF NOT EXISTS vchord_bm25 CASCADE; \dx ``` -------------------------------- ### Example Question Answering with Context (Python) Source: https://docs.vectorchord.ai/use-case/question-answering.html Demonstrates how to use the `add_context` function to get a more accurate answer from a language model by providing relevant context. This example queries for the release year and plot of 'Cat Napping'. Assumes a `complete` function is defined elsewhere. ```python base_prompt_2 = "What is the release year and overall plot of Cat Napping?" context_prompt_2 = add_context(base_prompt_2) context_response_2 = complete(context_prompt_2) print(context_response_2) ``` -------------------------------- ### Evaluation Setup with Vechord Source: https://docs.vectorchord.ai/vectorchord/use-case/colqwen2-modal.html Describes the final step of evaluating retrieval performance using vechord. This involves running queries against the indexed data to assess metrics like NDCG@10 and Recall@10, and potentially testing VectorChord's WARP optimization for MaxSim calculations. ```python TOP_K = 10 # ... (Code for running queries and evaluating results using vechord) ``` -------------------------------- ### Build PostgreSQL Image with pgvecto.rs Extension Source: https://docs.vectorchord.ai/admin/kubernetes.html Creates a custom Docker image for PostgreSQL that includes the pgvecto.rs extension. It uses an official CloudNative-PG image as a base and installs the pgvecto.rs .deb package. The PostgreSQL image tag must start with CNPG_TAG. ```dockerfile # syntax=docker/dockerfile-upstream:master ARG CNPG_TAG FROM ghcr.io/cloudnative-pg/postgresql:$CNPG_TAG ARG CNPG_TAG ARG PGVECTORS_TAG ARG TARGETARCH # drop to root to install packages USER root # install pgvecto.rs ADD https://github.com/tensorchord/pgvecto.rs/releases/download/$PGVECTORS_TAG/vectors-pg${CNPG_TAG%.*}_${PGVECTORS_TAG#"v"}_$TARGETARCH.deb ./pgvectors.deb RUN apt install ./pgvectors.deb USER postgres ``` ```shell docker build -t :15 --build-arg CNPG_TAG=15 --build-arg PGVECTORS_TAG=v0.1.13 --build-arg TARGETARCH=amd64 . ``` -------------------------------- ### Install VectorChord PostgreSQL Extension on Debian Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Installs the VectorChord PostgreSQL extension on Debian-based systems. This involves downloading the `.deb` package and installing it using `apt`. Ensure PostgreSQL is installed and configured correctly beforehand. ```sh wget https://github.com/tensorchord/VectorChord/releases/download/1.1.1/postgresql-18-vchord_1.1.1-1_$(dpkg --print-architecture).deb sudo apt install ./postgresql-18-vchord_1.1.1-1_$(dpkg --print-architecture).deb ``` -------------------------------- ### Initialize PGVecto_rs from Existing Database (Python) Source: https://docs.vectorchord.ai/integration/langchain.html Initializes a PGVecto_rs instance from an existing database using provided embeddings, collection name, dimension, and database URL. It then demonstrates adding documents and performing a similarity search. ```python db = PGVecto_rs( embedding=embeddings, collection_name=COLLECTION_NAME, # OpenAI embedding has 1536 dimensions. dimension=1536, db_url=URL, ) db.add_documents([Document(page_content="foo")]) docs_with_score = db.similarity_search_with_score("foo") print(docs_with_score[0]) ``` -------------------------------- ### Run Dataset Download Function with Modal Source: https://docs.vectorchord.ai/vectorchord/use-case/colqwen2-modal.html This command executes the 'download_dataset' function defined in the 'dataset.py' file using the Modal CLI. Modal will build the necessary Docker image and run the function in the cloud, downloading the dataset to the specified Modal Volume. ```bash $ modal run dataset.py::download_dataset ``` -------------------------------- ### Benchmark KNN Queries with pgbench (Bash) Source: https://docs.vectorchord.ai/use-case/adaptive-retrieval.html Bash script to run performance tests using pgbench for Approximate Nearest Neighbor (KNN) queries. It sets up environment variables for database connection and vector dimension, then executes pgbench with varying client counts to measure Queries Per Second (QPS). ```bash export PGHOST="localhost" export PGUSER="postgres" export PGPASSWORD="mysecretpassword" export PGDATABASE="postgres" export PGPORT=5433 export VECTOR_DIM=3072 TEST_TIME=300 for clients in 2 4 8 16 32; do pgbench -n -T "${TEST_TIME}" --file=query.sql -c "${clients}" -j "${clients}" done ``` -------------------------------- ### Sample Data for Partitioning - SQL Source: https://docs.vectorchord.ai/vectorchord/usage/external-index-precomputation.html Demonstrates how to sample data from the 'val' column of table 't' using the tsm_system_rows extension. This sampled data is used for partitioning. ```sql CREATE EXTENSION IF NOT EXISTS tsm_system_rows; SELECT val FROM t TABLESAMPLE SYSTEM_ROWS(1000); ``` -------------------------------- ### Install VectorChord Suite PostgreSQL Extensions Source: https://docs.vectorchord.ai/vectorchord/getting-started/vectorchord-suite.html These SQL commands install the VectorChord, pg_tokenizer, and vchord_bm25 extensions within a PostgreSQL database. The `CASCADE` option ensures that any dependent objects are also created. The `\dx` command lists all installed extensions and their versions. ```sql CREATE EXTENSION IF NOT EXISTS vchord CASCADE; CREATE EXTENSION IF NOT EXISTS pg_tokenizer CASCADE; CREATE EXTENSION IF NOT EXISTS vchord_bm25 CASCADE; \dx pg_tokenizer | 0.1.1 | tokenizer_catalog | pg_tokenizer vchord | 1.1.1 | public | vchord: Vector database plugin for Postgres, written in Rust, specifically designed for LLM vchord_bm25 | 0.3.0 | bm25_catalog | vchord_bm25: A postgresql extension for bm25 ranking algorithm vector | 0.8.1 | public | vector data type and ivfflat and hnsw access methods ``` -------------------------------- ### Install LlamaIndex and pgvecto.rs SDK Source: https://docs.vectorchord.ai/integration/llama-index.html Installs the necessary Python packages for LlamaIndex and the pgvecto.rs SDK. This is the first step to enable the integration. ```shell pip install llama-index "pgvecto_rs[sdk]" ``` -------------------------------- ### Connect to VectorChord Cluster using psql Source: https://docs.vectorchord.ai/cloud/getting-started/quick-start.html Connect to the VectorChord cluster using the `psql` command-line tool with the provided connection string. This command establishes an SSL connection and allows you to interact with the database. After connecting, you can run `\dx` to list installed extensions and verify the 'vectors' extension is present. ```shell $ psql 'postgres://test_user:vaBgs6CcrtMu@test-f5ys31pvbca4x6f9.us-east-1-dev.aws.vectorchord.com:5432/test?sslmode=require' psql (15.3, server 16.5) WARNING: psql major version 15, server major version 16. Some psql features might not work. SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) Type "help" for help. test=> \dx List of installed extensions Name | Version | Schema | Description ---------+---------+------------+---------------------------------------------------------------------------------------------- pgaudit | 16.0 | public | provides auditing functionality plpgsql | 1.0 | pg_catalog | PL/pgSQL procedural language vectors | 0.4.0 | vectors | vectors: Vector database plugin for Postgres, written in Rust, specifically designed for LLM (3 rows) ``` -------------------------------- ### Install LangChain and pgvecto.rs Dependencies Source: https://docs.vectorchord.ai/integration/langchain.html Installs the necessary Python packages for LangChain, OpenAI embeddings, and the pgvecto.rs integration. These are required to utilize LangChain's capabilities with pgvecto.rs. ```shell pip install langchain langchain-openai pgvecto-rs ``` -------------------------------- ### Bootstrap Database Sync with Docker Source: https://docs.vectorchord.ai/admin/logical_replication.html Initializes the synchronization process between two PostgreSQL databases. This command creates a superuser for orchestration and sets up internal metadata tables. It requires source and target database connection URLs and a group name for the cluster. ```shell $ docker run -e SOURCE_DB_URL="postgres://postgres:password@:5432/postgres" -e TARGET_DB_URL="postgres://postgres:password@:5432/postgres" -it --rm --network=pg_regress_localnet shayonj/pg_easy_replicate:latest pg_easy_replicate bootstrap --group-name database-cluster-1 --copy-schema ``` -------------------------------- ### Apply Terraform Configuration and Connect to Restored Cluster Source: https://docs.vectorchord.ai/cloud/manage/terraform.html This section demonstrates applying the Terraform configuration to create the VectorChord AI cluster and shows the expected output during the apply process. It also includes an example of how to connect to the newly created cluster using psql and query data, verifying the restore operation. ```shell $ terraform apply Terraform used the selected providers to generate the following execution plan. Resource actions are indicated with the following symbols: + create Terraform will perform the following actions: # pgvecto-rs-cloud_cluster.enterprise_plan_cluster will be created + resource "pgvecto-rs-cloud_cluster" "enterprise_plan_cluster" { + account_id = "8364ded2-5580-45c4-a394-edfa582e35a0" + cluster_name = "enterprise-plan-cluster" + cluster_provider = "aws" + connect_endpoint = (known after apply) + database_name = "test" + enable_restore = true + first_recoverability_point = (known after apply) + id = (known after apply) + image = "15-v0.3.0-public" + last_archived_wal_time = (known after apply) + last_updated = (known after apply) + pg_data_disk_size = "20" + plan = "Enterprise" + region = "eu-west-1" + server_resource = "aws-r7i-large-2c-16g" + status = (known after apply) + target_cluster_id = "43f17a52-0d07-43d5-91b6-9fbe4641578c" + target_time = "2024-09-20T12:00:00+08:00" } Plan: 1 to add, 0 to change, 0 to destroy. Changes to Outputs: + psql_endpoint_enterprise = (known after apply) Do you want to perform these actions? Terraform will perform the actions described above. Only 'yes' will be accepted to approve. Enter a value: yes pgvecto-rs-cloud_cluster.enterprise_plan_cluster: Creating... [10s elapsed] ... pgvecto-rs-cloud_cluster.enterprise_plan_cluster: Still creating... [5m20s elapsed] # you can check the table and index $ psql 'postgres://test_user:RKC4wr7MRcwm@enterprise-plan-cluster-4paxqvbzwxudmvja.eu-west-1-dev.aws.vectorchord.com:5432/test?sslmode=require' psql (15.3, server 15.8 (Debian 15.8-1.pgdg120+1)) SSL connection (protocol: TLSv1.3, cipher: TLS_AES_256_GCM_SHA384, compression: off) Type "help" for help. test=> \dt List of relations Schema | Name | Type | Owner --------+------+-------+----------- public | test | table | test_user (1 row) test=> SELECT * FROM test ORDER BY embedding <-> '[0.40671515, 0.24202824, 0.37059402]' LIMIT 10; id | embedding ----+-------------------------------------- 34 | [0.4047166, 0.2152912, 0.30516902] 56 | [0.39068437, 0.3153389, 0.40011033] 42 | [0.43917453, 0.22915891, 0.44726416] 45 | [0.47589824, 0.33998397, 0.39237767] 43 | [0.46702152, 0.09506769, 0.4343289] 95 | [0.4987348, 0.08346876, 0.28716254] 60 | [0.5421089, 0.3585022, 0.25259662] 80 | [0.61970145, 0.2186208, 0.36950335] 53 | [0.3399207, 0.3417044, 0.55435336] 54 | [0.3198571, 0.445044, 0.36041933] (10 rows) ``` -------------------------------- ### Start Citus Cluster Containers Source: https://docs.vectorchord.ai/integration/citus.html Command to start all defined services in the Docker Compose file in detached mode. This brings up the Citus coordinator and worker nodes. ```shell docker compose up -d ``` -------------------------------- ### Initialize Terraform Configuration Source: https://docs.vectorchord.ai/cloud/manage/terraform.html This bash command initializes the Terraform working directory. It downloads the necessary provider plugins, including the 'pgvecto-rs-cloud' provider, and prepares your environment for Terraform operations like 'terraform plan' or 'terraform apply'. ```bash $ terraform init Initializing the backend... Initializing provider plugins... - Reusing previous version of tensorchord/pgvecto-rs-cloud from the dependency lock file - Using previously-installed tensorchord/pgvecto-rs-cloud v0.0.2 Terraform has been successfully initialized! You may now begin working with Terraform. Try running "terraform plan" to see any changes that are required for your infrastructure. All Terraform commands should now work. If you ever set or change modules or backend configuration for Terraform, rerun this command to reinitialize your working directory. If you forget, other commands will detect it and remind you to do so if necessary. ``` -------------------------------- ### Configure PostgreSQL for VectorChord Source Installation Source: https://docs.vectorchord.ai/vectorchord/getting-started/installation.html Configures PostgreSQL to load the VectorChord extension by modifying the `shared_preload_libraries` setting. After applying this change, the PostgreSQL cluster must be restarted for the configuration to take effect. ```sh psql -U postgres -c 'ALTER SYSTEM SET shared_preload_libraries = "vchord"' ``` -------------------------------- ### Create and Query Binary Vector Table in PostgreSQL Source: https://docs.vectorchord.ai/faqs/comparison-pgvector.html Demonstrates how to create a table with a bvector column, insert binary vector data, create a bvector index, and perform a nearest neighbor search using the bvector_l2_ops operator. ```sql CREATE TABLE items ( id bigserial PRIMARY KEY, embedding bvector(3) NOT NULL ); INSERT INTO items (embedding) VALUES ('[1,0,1]'), ('[0,1,0]'); CREATE INDEX your_index_name ON items USING vectors (embedding bvector_l2_ops); SELECT * FROM items ORDER BY embedding <-> '[1,0,1]' LIMIT 5; ``` -------------------------------- ### Enable pgvecto.rs Extension in PostgreSQL Source: https://docs.vectorchord.ai/getting-started/installation.html Connects to the PostgreSQL database and enables the 'vectors' extension. This is a prerequisite for using pgvecto.rs functionalities within PostgreSQL. It first drops the extension if it already exists to ensure a clean installation. ```sql DROP EXTENSION IF EXISTS vectors; CREATE EXTENSION vectors; ``` -------------------------------- ### Example idx_options JSON Structure Source: https://docs.vectorchord.ai/usage/monitoring.html The `idx_options` column in `pg_vector_index_stat` is a JSON string detailing various index configuration parameters. This example shows the structure for vector, segment, optimizing, and indexing settings. ```json { "vector": { "dimensions": 256, "distance": "L2", "kind": "F32" }, "segment": { "max_growing_segment_size": 20000, "max_sealed_segment_size": 1000000 }, "optimizing": { "sealing_secs": 60, "sealing_size": 1, "delete_threshold": 0.2, "optimizing_threads": 2 }, "indexing": { "hnsw": { "m": 12, "ef_construction": 300, "quantization": { "trivial": {} } } } } ``` -------------------------------- ### Install CloudNative-PG using Helm Source: https://docs.vectorchord.ai/admin/kubernetes.html Installs the CloudNative-PG operator in a Kubernetes cluster using Helm. This operator manages PostgreSQL clusters. Ensure Helm version is >= 2.4.1 and kubectl version is >= 1.14. ```shell sudo helm repo add cnpg https://cloudnative-pg.github.io/charts sudo helm upgrade --install cnpg --namespace cnpg-system --create-namespace cnpg/cloudnative-pg helm list --all-namespaces kubectl get pod -n cnpg-system ``` -------------------------------- ### Run Indexer Script Source: https://docs.vectorchord.ai/vectorchord/use-case/colqwen2-modal.html Instruction to execute the vechord indexer script. This action initiates the process of table creation, index building, and data insertion into the VectorChord database. ```bash Run the indexer script, vechord handles table creation, index creation, and data insertion. ```