pgvector扩展在IvorySQL Oracle兼容模式下的应用实践
01
安装IvorySQL
1.1 设置PG_CONFIG环境变量
export PG_CONFIG=/usr/local/ivorysql/ivorysql-3/bin/pg_config
1.2 获取pg_vector源码
git clone --branch v0.6.2 https://github.com/pgvector/pgvector.git
1.3 安装 pgvector
cd pgvector
sudo --preserve-env=PG_CONFIG make
sudo --preserve-env=PG_CONFIG make instal
1.4 psql连接创建扩展
psql -U ivorysql -d ivorysql
ivorysql=# create extension vector;
CREATE EXTENSION
02
向量相似的对比方法介绍
03
pgvector提供的方法
ivorysql=# CREATE TABLE items (id bigserial PRIMARY KEY, name varchar2(20), embedding vector(3));
CREATE TABLE
ivorysql=# select * from items;
id | name | embedding
----+----------------+-----------
1 | ora_demo | [1,2,3]
2 | ora_compatible | [4,5,6]
(2 rows)
3.1 欧式距离
ivorysql=# SELECT *, embedding <-> '[3,1,2]' result FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
id | name | embedding | result
----+----------------+-----------+-------------------
1 | ora_demo | [1,2,3] | 2.449489742783178
2 | ora_compatible | [4,5,6] | 5.744562646538029
(2 rows)
3.2 内积
ivorysql=# SELECT *, embedding <#> '[3,1,2]' result FROM items ORDER BY embedding <#> '[3,1,2]' LIMIT 5;
id | name | embedding | result
----+----------------+-----------+--------
2 | ora_compatible | [4,5,6] | -29
1 | ora_demo | [1,2,3] | -11
(2 rows)
3.3 余弦相似度
ivorysql=# SELECT *, embedding <=> '[3,1,2]' result FROM items ORDER BY embedding <=> '[3,1,2]' LIMIT 5;
id | name | embedding | result
----+----------------+-----------+---------------------
2 | ora_compatible | [4,5,6] | 0.11673988938389968
1 | ora_demo | [1,2,3] | 0.2142857142857143
(2 rows)
ivorysql=# SELECT * FROM items WHERE id != 1 ORDER BY embedding <-> (SELECT embedding FROM items WHERE id = 1) LIMIT 5;
id | name | embedding
----+----------------+-----------
2 | ora_compatible | [4,5,6]
(1 row)
04
pgvector提供的索引算法
4.1 HNSW
(1) L2 distance HNSW index
ivorysql=# CREATE INDEX ON items USING hnsw (embedding vector_l2_ops);
CREATE INDEX
(2)Inner product HNSW index
ivorysql=# CREATE INDEX ON items USING hnsw (embedding vector_ip_ops);
CREATE INDEX
(3) Cosine distance HNSW index
ivorysql=# CREATE INDEX ON items USING hnsw (embedding vector_cosine_ops);
CREATE INDEX
4.2 ivfflat
(1)L2 distance ivfflat index
ivorysql=# CREATE INDEX ON items USING ivfflat (embedding vector_l2_ops);
CREATE INDEX
(2) Inner product ivfflat index
ivorysql=# CREATE INDEX ON items USING ivfflat (embedding vector_ip_ops);
CREATE INDEX
(3) Cosine distance ivfflat index
ivorysql=# CREATE INDEX ON items USING ivfflat (embedding vector_cosine_ops);
CREATE INDEX
05
其他类型
5.1 Binary Vectors
Use thebit type to store binary vectors
ivorysql=# CREATE TABLE items5 (id bigserial PRIMARY KEY, name varchar2(20), num number(20), embedding bit(3));
CREATE TABLE
ivorysql=# INSERT INTO items5 (name, num, embedding) VALUES ('1st oracle data',0, '000'), ('2nd oracle data', 111, '111');
INSERT 0 2
ivorysql=# SELECT * FROM items5 ORDER BY bit_count(embedding # '101') LIMIT 5;
id | name | num | embedding
----+-----------------+-----+-----------
2 | 2nd oracle data | 111 | 111
1 | 1st oracle data | 0 | 000
(2 rows)
06
Oracle兼容特性与pgvector适配
6.1 匿名块
ivorysql=# declare
i vector(3) := '[1,2,3]';
begin
raise notice '%', i;
end;
ivorysql-# /
NOTICE: [1,2,3]
DO
6.2 存储过程
ivorysql=# CREATE OR REPLACE PROCEDURE ora_procedure()
AS
p vector(3) := '[4,5,6]';
begin
raise notice '%', p;
end;
/
CREATE PROCEDURE
ivorysql=# call ora_procedure();
NOTICE: [4,5,6]
CALL
6.3 函数
ivorysql=# CREATE OR REPLACE FUNCTION AddVector(a vector(3), b vector(3))
RETURN vector(3)
IS
BEGIN
RETURN a + b;
END;
/
CREATE FUNCTION
ivorysql=# SELECT AddVector('[1,2,3]','[4,5,6]') FROM DUAL;
addvector
----------------
[5,7,9]
(1 row)
关注公众号,了解更多社区动态
- 我们是谁 -
lvorySQL是由浪潮软件集团瀚高股份主导研发的,一款深度兼容Oracle的PostgreSQL开源数据库系统。IvorySQL从底层代码层面深入把握开源技术的发展趋势,基于PostgreSQL 16.2的最新内核进行构建,同时提供了更加全面灵活的Oracle兼容功能,具备高度的SQL和PL/SQL兼容性,能够满足企业对于数据库系统多样化和高兼容性的需求。
【IvorySQL技术指南】
IvorySQL Operator | 一键部署IvorySQL集群,高效管理与个性化配置尽在掌控
IvorySQL 3.2 贡献独门秘籍,可以平滑迁移Oracle???
IvorySQL 3.2 :基于PG16.2,新增Oracle XML函数兼容功能
基于IvorySQL+Patroni+vip-manager构建高可用集群
备份恢复新体验!pgBackRest与IvorySQL的完美融合
最新版本!PG16.0最新内核,实现兼容Oracle数据库再升级
IvorySQL内核的Serverless数据库--HGNeon
IvorySQL