数据库PostgreSQL
FANSEAPostgreSQL
数据结构
PostgreSQL 是一个功能强大的开源关系型数据库系统,它支持多种数据类型和高级特性,包括数组类型、键值对存储(如 hstore)以及 JSON 和 JSONB 类型。
数组类型
在 PostgreSQL 中,你可以为几乎所有内置的数据类型创建数组。数组类型用一对花括号 {} 包围起来,例如一个整数数组可以表示为 {1, 2, 3}。
创建表时定义数组列:
1 2 3 4 5
   | CREATE TABLE products (     id serial PRIMARY KEY,     name text NOT NULL,     dimensions integer[]  );
   | 
 
插入数据:
1
   | INSERT INTO products (name, dimensions) VALUES ('Widget', '{10, 20, 30}');
  | 
 
查询数据:
1
   | SELECT * FROM products WHERE dimensions[1] = 10; 
   | 
 
键值对存储(hstore)
hstore 是一个特殊的 PostgreSQL 扩展模块,用于存储键值对。它非常适合用来存储配置信息或者标签等。
安装 hstore 扩展:
1
   | CREATE EXTENSION IF NOT EXISTS hstore;
   | 
 
使用 hstore:
1 2 3 4 5 6 7 8 9
   | CREATE TABLE users (     id serial PRIMARY KEY,     name text NOT NULL,     attributes hstore );
  INSERT INTO users (name, attributes) VALUES ('Alice', 'a=>1, b=>2');
  SELECT * FROM users WHERE attributes ->> 'a' = '1'; 
   | 
 
JSON 和 JSONB 类型
JSON 类型允许你在数据库中存储 JSON 文档。json 类型会保留文档的编码形式,而 jsonb 类型则会对文档进行二进制化处理,使得查询更快且支持索引。
创建表时定义 JSON 列:
1 2 3 4
   | CREATE TABLE settings (     id serial PRIMARY KEY,     config jsonb NOT NULL );
   | 
 
插入 JSON 数据:
1
   | INSERT INTO settings (config) VALUES ('{"color": "blue", "size": "large"}');
  | 
 
查询 JSON 数据:
1
   | SELECT * FROM settings WHERE config->>'color' = 'blue'; 
   | 
 
使用 JSON 函数:
PostgreSQL 提供了许多用于处理 JSON 的函数,比如 jsonb_set 可以用来修改 JSON 文档中的值:
1
   | UPDATE settings SET config = jsonb_set(config, '{size}', '"small"', false);
  | 
 
以上就是 PostgreSQL 中关于数组、键值对存储以及 JSON 支持的基本介绍。根据你的具体需求,可以选择最适合你应用场景的数据类型。
MybatisPlus整合PgSQL
- 引入依赖
 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17
   |          <dependency>             <groupId>com.baomidou</groupId>             <artifactId>mybatis-plus-boot-starter</artifactId>             <version>3.3.1</version>         </dependency>                  <dependency>             <groupId>com.alibaba</groupId>             <artifactId>druid</artifactId>             <version>1.1.22</version>         </dependency> 		<dependency>             <groupId>org.postgresql</groupId>             <artifactId>postgresql</artifactId>             <scope>runtime</scope>         </dependency>
 
  | 
 
- 配置
yaml文件 
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
   | spring:   datasource:     driver-class-name: org.postgresql.Driver     url: jdbc:postgresql://localhost:5432/warmcp?currentSchema=test     username: postgres     password: 123456     hikari:        maximum-pool-size: 10       minimum-idle: 5       idle-timeout: 30000       max-lifetime: 1800000       connection-timeout: 30000       pool-name: okapi2       leak-detection-threshold: 15000       connection-test-query: SELECT 1       validation-timeout: 5000
 
  mybatis-plus:   configuration:     log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
   | 
 
高级应用

PGVector
 利用 PostgreSQL 实现高效的向量存储与检索 
PGVector: 利用 PostgreSQL 实现高效的向量存储与检索_postgresql vector-CSDN博客
查询向量存储
简单相似性搜索
1 2 3 4 5 6
   | results = vector_store.similarity_search(     "kitty", k=10, filter={"id": {"$in": [1, 5, 2, 9]}}  ) for doc in results:     print(f"* {doc.page_content} [{doc.metadata}]")
 
   | 
 
参数解释
- **
"kitty"**:
- 查询字符串。这是要搜索的关键字或短语。在这个例子中,
"kitty" 是我们要搜索的内容。 
 
- **
k=10**:
- 指定返回结果的数量。这里设置为 
10,意味着返回最多 10 个与查询 "kitty" 最相似的结果。 
 
带分数的相似性搜索
1 2 3 4
   | results = vector_store.similarity_search_with_score(query="cats", k=1) for doc, score in results:     print(f"* [SIM={score:3f}] {doc.page_content} [{doc.metadata}]")
 
   | 
 
复杂过滤查询
PGVector 支持多种过滤操作,如 $eq, $ne, $lt, $gt, $in, $like 等。例如:
1 2 3 4 5 6 7 8 9 10 11
   | vector_store.similarity_search(     "ducks",     k=10,     filter={         "$and": [             {"id": {"$in": [1, 5, 2, 9]}},             {"location": {"$in": ["pond", "market"]}},         ]     }, )
 
   |