PostgreSQL 向量扩展插件pgvector安装和使用

打印 上一主题 下一主题

主题 910|帖子 910|积分 2730

文章目次



  • PostgreSQL 向量扩展插件pgvector安装和使用


    • 安装postgresql
    • pgvector下载和安装
    • 安装错误调试


      • 错误调试1


        • 实验解决 AP1 :启动postgresql

      • 错误调试2


        • 实验解决 AP2 : 使用apt-get install postgresql-server

      • 错误调试3


        • 实验解决 AP3 :卸载apt-get 安装

      • 错误调试4


        • 设置环境变量PG_CONFIG

      • 编译成功

    • 使用pgvector


      • 测试例
      • 列出当前数据库中已安装的扩展(extensions)


  • 参考
PostgreSQL 向量扩展插件pgvector安装和使用

pgvector 是一个 PostgreSQL 扩展,可以或许让PostgreSQL 提供向量(vector)数据类型和相关操纵功能。
参考:
https://github.com/pgvector/pgvector
安装postgresql

使用pgvector 前提是事先安装好postgresql,安装postgresql例可参考下文。
参考:
Linux(Ubuntu)源码安装postgresql16.3
https://editor.csdn.net/md/articleId=142500497
pgvector下载和安装

安装下令如下:
  1. cd
  2. git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
  3. cd pgvector
  4. make
  5. make install # may need sudo
复制代码
参考:
https://github.com/pgvector/pgvector
例:下载
  1. root@autodl-container-616f40a3b3-41cb82d9:~#  su - postgres
  2. postgres@autodl-container-616f40a3b3-41cb82d9:~$ pwd
  3. /var/lib/postgresql
  4. postgres@autodl-container-616f40a3b3-41cb82d9:~$ ls
  5. 10  pgsql16  postgresql-16.3  postgresql-16.3.tar.gz
  6. postgres@autodl-container-616f40a3b3-41cb82d9:~$ git clone --branch v0.7.4 https://github.com/pgvector/pgvector.git
  7. Cloning into 'pgvector'...
  8. remote: Enumerating objects: 10650, done.
  9. remote: Counting objects: 100% (4165/4165), done.
  10. remote: Compressing objects: 100% (954/954), done.
  11. remote: Total 10650 (delta 3388), reused 3493 (delta 3193), pack-reused 6485 (from 1)
  12. Receiving objects: 100% (10650/10650), 1.60 MiB | 544.00 KiB/s, done.
  13. Resolving deltas: 100% (7952/7952), done.
  14. Note: checking out '103ac50f1a90b47a72003e8e8628a55ec372f202'.
  15. You are in 'detached HEAD' state. You can look around, make experimental
  16. changes and commit them, and you can discard any commits you make in this
  17. state without impacting any branches by performing another checkout.
  18. If you want to create a new branch to retain commits you create, you may
  19. do so (now or later) by using -b with the checkout command again. Example:
  20.   git checkout -b <new-branch-name>
  21. postgres@autodl-container-616f40a3b3-41cb82d9:~$
复制代码
安装错误调试

错误调试1

实验编译和安装
例:
  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
  2. You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
  3. cp sql/vector.sql sql/vector--0.7.4.sql
复制代码
编译时候汇报如上错误可能是由于postgresql没有启动。
  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ psql
  2. psql: error: connection to server on socket "/tmp/.s.PGSQL.5432" failed: Connection refused
  3.         Is the server running locally and accepting connections on that socket?
  4. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
  5. postgres@autodl-container-616f40a3b3-41cb82d9:~$ ps -xf|grep postgres
  6.   1504 pts/3    S+     0:00  _ grep postgres
  7. postgres@autodl-container-616f40a3b3-41cb82d9:~$ pg_ctl -D /var/lib/postgresql/pgsql16/data status
  8. pg_ctl: no server running
复制代码
实验解决 AP1 :启动postgresql

  1. postgres@autodl-container-616f40a3b3-41cb82d9:~$ pg_ctl -D /var/lib/postgresql/pgsql16/data -l logfile start
  2. pg_ctl: another server might be running; trying to start server anyway
  3. waiting for server to start.... done
  4. server started
  5. postgres@autodl-container-616f40a3b3-41cb82d9:~$  ps -xf|grep postgres
  6.   1197 pts/0    S+     0:00  _ grep postgres
  7.   1188 ?        Ss     0:00 /var/lib/postgresql/pgsql16/bin/postgres -D /var/lib/postgresql/pgsql16/data
  8.   1189 ?        Ss     0:00  _ postgres: checkpointer
  9.   1190 ?        Ss     0:00  _ postgres: background writer
  10.   1192 ?        Ss     0:00  _ postgres: walwriter
  11.   1193 ?        Ss     0:00  _ postgres: autovacuum launcher
  12.   1194 ?        Ss     0:00  _ postgres: logical replication launcher
  13. postgres@autodl-container-616f40a3b3-41cb82d9:~$  psql
  14. psql (16.3)
  15. Type "help" for help.
  16. postgres=# help
  17. You are using psql, the command-line interface to PostgreSQL.
  18. Type:  copyright for distribution terms
  19.        h for help with SQL commands
  20.        ? for help with psql commands
  21.        g or terminate with semicolon to execute query
  22.        q to quit
  23. postgres=#
复制代码
错误调试2

再次编译,出错。
  1. postgres@autodl-container-616f40a3b3-41cb82d9:~$ pwd
  2. /var/lib/postgresql
  3. postgres@autodl-container-616f40a3b3-41cb82d9:~$ cd pgvector
  4. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
  5. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
  6. You need to install postgresql-server-dev-X.Y for building a server-side extension or libpq-dev for building a client-side application.
  7. make: Nothing to be done for 'all'.
  8. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ postgres --version
  9. postgres (PostgreSQL) 16.3
  10. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ psql --version
  11. psql (PostgreSQL) 16.3
复制代码
无法识别postgresql-server-dev-X.Y 。
实验解决 AP2 : 使用apt-get install postgresql-server

  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ apt-get install postgresql-server*
  2. E: Could not open lock file /var/lib/dpkg/lock-frontend - open (13: Permission denied)
  3. E: Unable to acquire the dpkg frontend lock (/var/lib/dpkg/lock-frontend), are you root?
  4. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ exit
  5. logout
  6. root@autodl-container-616f40a3b3-41cb82d9:~# apt-get install postgresql-server*
  7. Reading package lists... Done
  8. Building dependency tree      
  9. Reading state information... Done
  10. Note, selecting 'postgresql-server-dev-all' for glob 'postgresql-server*'
  11. Note, selecting 'postgresql-server' for glob 'postgresql-server*'
  12. Note, selecting 'postgresql-server-dev-10' for glob 'postgresql-server*'
  13. The following additional packages will be installed:
  14.   dctrl-tools iproute2 libatm1 libmnl0 libpq-dev libxtables12
  15. Suggested packages:
  16.   debtags iproute2-doc postgresql-doc-10
  17. The following NEW packages will be installed:
  18.   dctrl-tools iproute2 libatm1 libmnl0 libpq-dev libxtables12 postgresql-server-dev-10 postgresql-server-dev-all
  19. 0 upgraded, 8 newly installed, 0 to remove and 169 not upgraded.
  20. Need to get 1983 kB of archives.
  21. After this operation, 10.9 MB of additional disk space will be used.
  22. Do you want to continue? [Y/n] Y
  23. Get:1 https://repo.huaweicloud.com/ubuntu bionic/main amd64 libmnl0 amd64 1.0.4-2 [12.3 kB]
  24. Get:2 https://repo.huaweicloud.com/ubuntu bionic-updates/main amd64 iproute2 amd64 4.15.0-2ubuntu1.3 [721 kB]
  25. Get:3 https://repo.huaweicloud.com/ubuntu bionic/main amd64 libatm1 amd64 1:2.5.1-2build1 [21.9 kB]
  26. Get:4 https://repo.huaweicloud.com/ubuntu bionic-updates/main amd64 libxtables12 amd64 1.6.1-2ubuntu2.1 [28.1 kB]
  27. Get:5 https://repo.huaweicloud.com/ubuntu bionic/main amd64 dctrl-tools amd64 2.24-2build1 [60.9 kB]
  28. Get:6 https://repo.huaweicloud.com/ubuntu bionic-security/main amd64 libpq-dev amd64 10.23-0ubuntu0.18.04.2 [219 kB]
  29. Get:7 https://repo.huaweicloud.com/ubuntu bionic-security/universe amd64 postgresql-server-dev-10 amd64 10.23-0ubuntu0.18.04.2 [905 kB]
  30. Get:8 https://repo.huaweicloud.com/ubuntu bionic-security/universe amd64 postgresql-server-dev-all all 190ubuntu0.1 [14.1 kB]
  31. Fetched 1983 kB in 1s (3436 kB/s)                     
  32. debconf: delaying package configuration, since apt-utils is not installed
  33. Selecting previously unselected package libmnl0:amd64.
  34. (Reading database ... 44409 files and directories currently installed.)
  35. Preparing to unpack .../0-libmnl0_1.0.4-2_amd64.deb ...
  36. Unpacking libmnl0:amd64 (1.0.4-2) ...
  37. Selecting previously unselected package iproute2.
  38. Preparing to unpack .../1-iproute2_4.15.0-2ubuntu1.3_amd64.deb ...
  39. Unpacking iproute2 (4.15.0-2ubuntu1.3) ...
  40. Selecting previously unselected package libatm1:amd64.
  41. Preparing to unpack .../2-libatm1_1%3a2.5.1-2build1_amd64.deb ...
  42. Unpacking libatm1:amd64 (1:2.5.1-2build1) ...
  43. Selecting previously unselected package libxtables12:amd64.
  44. Preparing to unpack .../3-libxtables12_1.6.1-2ubuntu2.1_amd64.deb ...
  45. Unpacking libxtables12:amd64 (1.6.1-2ubuntu2.1) ...
  46. Selecting previously unselected package dctrl-tools.
  47. Preparing to unpack .../4-dctrl-tools_2.24-2build1_amd64.deb ...
  48. Unpacking dctrl-tools (2.24-2build1) ...
  49. Selecting previously unselected package libpq-dev.
  50. Preparing to unpack .../5-libpq-dev_10.23-0ubuntu0.18.04.2_amd64.deb ...
  51. Unpacking libpq-dev (10.23-0ubuntu0.18.04.2) ...
  52. Selecting previously unselected package postgresql-server-dev-10.
  53. Preparing to unpack .../6-postgresql-server-dev-10_10.23-0ubuntu0.18.04.2_amd64.deb ...
  54. Unpacking postgresql-server-dev-10 (10.23-0ubuntu0.18.04.2) ...
  55. Selecting previously unselected package postgresql-server-dev-all.
  56. Preparing to unpack .../7-postgresql-server-dev-all_190ubuntu0.1_all.deb ...
  57. Unpacking postgresql-server-dev-all (190ubuntu0.1) ...
  58. Setting up libpq-dev (10.23-0ubuntu0.18.04.2) ...
  59. Setting up dctrl-tools (2.24-2build1) ...
  60. Setting up postgresql-server-dev-10 (10.23-0ubuntu0.18.04.2) ...
  61. Setting up libatm1:amd64 (1:2.5.1-2build1) ...
  62. Setting up libxtables12:amd64 (1.6.1-2ubuntu2.1) ...
  63. Setting up libmnl0:amd64 (1.0.4-2) ...
  64. Setting up iproute2 (4.15.0-2ubuntu1.3) ...
  65. Setting up postgresql-server-dev-all (190ubuntu0.1) ...
  66. Processing triggers for libc-bin (2.27-3ubuntu1.2) ...
  67. root@autodl-container-616f40a3b3-41cb82d9:~#
复制代码
安装的版本不是最新的版本而是10.23。
错误调试3

再次编译。
  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
  2. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Wendif-labels -Wmissing-format-attribute -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -g -g -O2 -fdebug-prefix-map=/build/postgresql-10-XHR5kl/postgresql-10-10.23=. -fstack-protector-strong -Wformat -Werror=format-security -fno-omit-frame-pointer -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -I. -I./ -I/usr/include/postgresql/10/server -I/usr/include/postgresql/internal  -Wdate-time -D_FORTIFY_SOURCE=2 -D_GNU_SOURCE -I/usr/include/libxml2  -I/usr/include/mit-krb5  -c -o src/bitutils.o src/bitutils.c
  3. In file included from src/bitutils.c:3:0:
  4. src/bitutils.h:8:2: error: #error "Requires PostgreSQL 12+"
  5. #error "Requires PostgreSQL 12+"
  6.   ^~~~~
  7. src/bitutils.c:5:10: fatal error: port/pg_bitutils.h: No such file or directory
  8. #include "port/pg_bitutils.h"
  9.           ^~~~~~~~~~~~~~~~~~~~
  10. compilation terminated.
  11. <builtin>: recipe for target 'src/bitutils.o' failed
  12. make: *** [src/bitutils.o] Error 1
  13. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
复制代码
make编译调用的postgresql不是之前安装的 16.3,而是apt-get 安装的10.23版本。
实验解决 AP3 :卸载apt-get 安装

卸载apt-get 安装的10.23版本
参考:
卸载apt-get 安装的PostgreSQL版本
https://blog.csdn.net/lukeUnique/article/details/142610650
错误调试4

再次编译。
  1. root@autodl-container-616f40a3b3-41cb82d9:~# su - postgres
  2. postgres@autodl-container-616f40a3b3-41cb82d9:~$ cd /var/lib/postgresql/pgvector
  3. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ ls
  4. CHANGELOG.md  Dockerfile  LICENSE  META.json  Makefile  Makefile.win  README.md  logfile  sql  src  test  vector.control
  5. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
  6. Makefile:48: /usr/lib/postgresql/10/lib/pgxs/src/makefiles/pgxs.mk: No such file or directory
  7. make: *** No rule to make target '/usr/lib/postgresql/10/lib/pgxs/src/makefiles/pgxs.mk'.  Stop.
  8. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
复制代码
make编译pgvector时无法识别安装的postgresql。
设置环境变量PG_CONFIG

为了让make下令找到安装的postgresql 16.3版本。
重新修改.bash_profile,设置PG_CONFIG和LD_LIBRARY_PATH,指定安装的postgresql 16.3版本。
例:
  1. $ vi .bash_profile
复制代码
修改成如下内容。
  1. # .bash_profile
  2. # Get the aliases and functions
  3. if [ -f ~/.bashrc ]; then
  4.         . ~/.bashrc
  5. fi
  6. # User specific environment and startup programs
  7. export PG_HOME=/var/lib/postgresql/pgsql16
  8. export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/var/lib/postgresql/pgsql16/lib
  9. export PG_CONFIG=/var/lib/postgresql/pgsql16/bin/pg_config
  10. export PGDATA=/var/lib/postgresql/pgsql16/data
  11. PATH=$PATH:$HOME/.local/bin:$HOME/bin:$PG_HOME/bin
  12. alias psql='/var/lib/postgresql/pgsql16/bin/psql'
  13. export PATH
复制代码
编译成功

根据上面的错误调试过程,我们可以看到在编译时候需要设置PG_CONFIG。
  1. root@autodl-container-616f40a3b3-41cb82d9:~# su - postgres
  2. postgres@autodl-container-616f40a3b3-41cb82d9:~$ ls
  3. logfile  pgsql16  pgvector  postgresql-16.3  postgresql-16.3.tar.gz
  4. postgres@autodl-container-616f40a3b3-41cb82d9:~$ cd pgvector
  5. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ ls
  6. CHANGELOG.md  Dockerfile  LICENSE  META.json  Makefile  Makefile.win  README.md  logfile  sql  src  test  vector.control
  7. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ make
  8. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/bitutils.o src/bitutils.c
  9. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/bitvec.o src/bitvec.c
  10. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/halfutils.o src/halfutils.c
  11. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/halfvec.o src/halfvec.c
  12. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnsw.o src/hnsw.c
  13. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnswbuild.o src/hnswbuild.c
  14. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnswinsert.o src/hnswinsert.c
  15. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnswscan.o src/hnswscan.c
  16. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnswutils.o src/hnswutils.c
  17. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/hnswvacuum.o src/hnswvacuum.c
  18. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfbuild.o src/ivfbuild.c
  19. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfflat.o src/ivfflat.c
  20. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfinsert.o src/ivfinsert.c
  21. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfkmeans.o src/ivfkmeans.c
  22. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfscan.o src/ivfscan.c
  23. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfutils.o src/ivfutils.c
  24. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/ivfvacuum.o src/ivfvacuum.c
  25. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/sparsevec.o src/sparsevec.c
  26. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -I. -I./ -I/var/lib/postgresql/pgsql16/include/server -I/var/lib/postgresql/pgsql16/include/internal  -D_GNU_SOURCE   -c -o src/vector.o src/vector.c
  27. gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wshadow=compatible-local -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -O2 -march=native -ftree-vectorize -fassociative-math -fno-signed-zeros -fno-trapping-math -fPIC -fvisibility=hidden -shared -o vector.so src/bitutils.o src/bitvec.o src/halfutils.o src/halfvec.o src/hnsw.o src/hnswbuild.o src/hnswinsert.o src/hnswscan.o src/hnswutils.o src/hnswvacuum.o src/ivfbuild.o src/ivfflat.o src/ivfinsert.o src/ivfkmeans.o src/ivfscan.o src/ivfutils.o src/ivfvacuum.o src/sparsevec.o src/vector.o -L/var/lib/postgresql/pgsql16/lib    -Wl,--as-needed -Wl,-rpath,'/var/lib/postgresql/pgsql16/lib',--enable-new-dtags  -fvisibility=hidden
  28. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
复制代码
make install
  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$  make install
  2. /bin/mkdir -p '/var/lib/postgresql/pgsql16/lib'
  3. /bin/mkdir -p '/var/lib/postgresql/pgsql16/share/extension'
  4. /bin/mkdir -p '/var/lib/postgresql/pgsql16/share/extension'
  5. /usr/bin/install -c -m 755  vector.so '/var/lib/postgresql/pgsql16/lib/vector.so'
  6. /usr/bin/install -c -m 644 .//vector.control '/var/lib/postgresql/pgsql16/share/extension/'
  7. /usr/bin/install -c -m 644 .//sql/vector--0.5.0--0.5.1.sql .//sql/vector--0.1.3--0.1.4.sql .//sql/vector--0.1.1--0.1.3.sql .//sql/vector--0.4.3--0.4.4.sql .//sql/vector--0.1.7--0.1.8.sql .//sql/vector--0.2.0--0.2.1.sql .//sql/vector--0.1.5--0.1.6.sql .//sql/vector--0.5.1--0.6.0.sql .//sql/vector--0.3.1--0.3.2.sql .//sql/vector--0.1.8--0.2.0.sql .//sql/vector--0.2.6--0.2.7.sql .//sql/vector--0.3.0--0.3.1.sql .//sql/vector--0.4.2--0.4.3.sql .//sql/vector--0.6.2--0.7.0.sql .//sql/vector--0.1.4--0.1.5.sql .//sql/vector--0.2.7--0.3.0.sql .//sql/vector--0.6.0--0.6.1.sql .//sql/vector--0.4.1--0.4.2.sql .//sql/vector--0.7.3--0.7.4.sql .//sql/vector--0.4.0--0.4.1.sql .//sql/vector--0.7.1--0.7.2.sql .//sql/vector--0.1.0--0.1.1.sql .//sql/vector--0.2.3--0.2.4.sql .//sql/vector--0.1.6--0.1.7.sql .//sql/vector--0.3.2--0.4.0.sql .//sql/vector--0.2.5--0.2.6.sql .//sql/vector--0.4.4--0.5.0.sql .//sql/vector--0.2.1--0.2.2.sql .//sql/vector--0.7.2--0.7.3.sql .//sql/vector--0.7.0--0.7.1.sql .//sql/vector--0.2.2--0.2.3.sql .//sql/vector--0.6.1--0.6.2.sql .//sql/vector--0.2.4--0.2.5.sql sql/vector--0.7.4.sql '/var/lib/postgresql/pgsql16/share/extension/'
  8. /bin/mkdir -p '/var/lib/postgresql/pgsql16/include/server/extension/vector/'
  9. /usr/bin/install -c -m 644   .//src/halfvec.h .//src/sparsevec.h .//src/vector.h '/var/lib/postgresql/pgsql16/include/server/extension/vector/'
  10. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$
复制代码
pgvector的安装成功。
使用pgvector

使用参考https://github.com/pgvector/pgvector
  1. Getting Started
  2. Enable the extension (do this once in each database where you want to use it)
  3. CREATE EXTENSION vector;
  4. Create a vector column with 3 dimensions
  5. CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
  6. Insert vectors
  7. INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
  8. Get the nearest neighbors by L2 distance
  9. SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
  10. Also supports inner product (<#>), cosine distance (<=>), and L1 distance (<+>, added in 0.7.0)
  11. Note: <#> returns the negative inner product since Postgres only supports ASC order index scans on operators
复制代码
测试例

  1. postgres@autodl-container-616f40a3b3-41cb82d9:~/pgvector$ psql
  2. psql (16.3)
  3. Type "help" for help.
  4. postgres=# CREATE EXTENSION vector;
  5. CREATE EXTENSION
  6. postgres=# CREATE TABLE items (id bigserial PRIMARY KEY, embedding vector(3));
  7. CREATE TABLE
  8. postgres=# INSERT INTO items (embedding) VALUES ('[1,2,3]'), ('[4,5,6]');
  9. INSERT 0 2
  10. postgres=# SELECT * FROM items ORDER BY embedding <-> '[3,1,2]' LIMIT 5;
  11. id | embedding
  12. ----+-----------
  13.   1 | [1,2,3]
  14.   2 | [4,5,6]
  15. (2 rows)
复制代码
列出当前数据库中已安装的扩展(extensions)

  1. postgres=# dx
  2.                              List of installed extensions
  3.   Name   | Version |   Schema   |                     Description                     
  4. ---------+---------+------------+------------------------------------------------------
  5. plpgsql | 1.0     | pg_catalog | PL/pgSQL procedural language
  6. vector  | 0.7.4   | public     | vector data type and ivfflat and hnsw access methods
  7. (2 rows)
  8. postgres=#
复制代码
参考

实战PG vector 构建DBA 个人知识库之二: 向量数据库与 PG vector 先容
https://pgfans.cn/a/3606
数据库技术核心:向量检索(pgvector)
https://blog.csdn.net/ChaoMing_H/article/details/141257149
PostgreSQL向量数据插件–pgvector安装(附PostgreSQL安装)
https://blog.csdn.net/m0_62627802/article/details/135027327

免责声明:如果侵犯了您的权益,请联系站长,我们会及时删除侵权内容,谢谢合作!更多信息从访问主页:qidao123.com:ToB企服之家,中国第一个企服评测及商务社交产业平台。
回复

使用道具 举报

0 个回复

倒序浏览

快速回复

您需要登录后才可以回帖 登录 or 立即注册

本版积分规则

立山

金牌会员
这个人很懒什么都没写!
快速回复 返回顶部 返回列表