pg_infer 1.0.0:Transformer 模型知識作為 PostgreSQL SQL 關係與索引
postgresql.org · 2026-05-22
pg_infer 是由 Greg Burd 開發的 PostgreSQL 18+ 擴充套件,於 2026 年 5 月 22 日釋出 1.0.0 穩定版。它將 transformer 模型的推理能力以 SQL 關係與自訂索引存取方法(Index AM) 的形式嵌入 PostgreSQL,讓模型知識成為查詢規劃器可見的一等資料來源,無需外部服務呼叫。
SQL 介面
核心函式透過 pgrx 以 Rust 實作:
-- 載入模型(.vindex 格式)
SELECT infer_create_model('qwen05b', '/data/qwen-0.5b.vindex');
-- 查詢模型對實體的學習關聯(含信心分數與來源層編號)
SELECT * FROM describe('France');
-- capital | Paris | 42.7 | 18
-- 測試概念間的方向性支援
SELECT implies('fire', 'smoke'); -- → true
-- 以模型知識排序文件
SELECT * FROM documents
ORDER BY title <~> 'artificial intelligence'
LIMIT 5;<~> 運算子有對應的 Index AM 支援,讓查詢規劃器可利用 b-tree 索引做相似度排序,並在 EXPLAIN (ANALYZE, BUFFERS) 中顯示成本估計。
儲存模式與後端
pg_infer 提供兩種索引模式:
- model 模式:整個 vindex 以 WAL log 寫入 PostgreSQL 頁面,支援備份、複製與 PITR
- column 模式:將模型附加到文字欄位,直接用於
ORDER BY優化
後端運算支援本地 mmap(多個 PG backend 共享作業系統頁面快取中的模型頁面)與遠端 larql-server/router(HTTP/2 或 Unix socket,按層分片路由)。支援 BitNet b1.58 兩位元三值權重模型,可在一般 CPU 上執行推理,不需 GPU;OpenBLAS 提供 BLAS 層線性代數加速。
與 pg_mentat 的整合
同日釋出的 pg_mentat 1.3.0 將 pg_infer 列為軟整合目標,允許在 Datalog where 子句中直接使用 infer-near、infer-similar、infer-implies、infer-walk、infer-describe、infer-predict 等 Datalog 函式,橋接向量語意搜尋與知識圖譜查詢。
pg_mentat 1.3.0:Datomic 相容的 Datalog 與十個擴充套件的軟整合
postgresql.org · 2026-05-22
pg_mentat 1.3.0 在 2026 年 5 月 22 日以「Postgres Extension Family」版本釋出,此版本的核心主題是與 pgvector、pg_infer、PostGIS、RUM 等十個擴充套件的軟整合——自動偵測已安裝的擴充,並將其能力以 Datalog where 函式的形式暴露,不強制任何依賴。
Datomic 資料模型在 PostgreSQL
pg_mentat 以 Rust(pgrx 0.17)實作,支援 PostgreSQL 13–18。資料以不可變 datom(Entity, Attribute, Value, Transaction 四元組)儲存:每筆事實有時間戳記,過去狀態可隨時查詢。底層使用九張按型別分開的窄表(ref、long、string、bool、double、inst、kw、uuid、bytes),搭配四個正統 Datomic 索引(EAVT、AEVT、AVET、VAET)對應的 PostgreSQL b-tree 索引。
Datalog 查詢與 SQL 介面
-- 定義 schema 並插入資料
SELECT mentat.t('[
{:db/ident :person/name :db/valueType :db.type/string
:db/cardinality :db.cardinality/one :db/unique :db.unique/identity}]');
SELECT mentat.t('[{:person/name "Alice" :person/age 30}]');
-- Datalog 查詢(編譯為標準 SQL 執行)
SELECT mentat.q('[:find ?name ?age
:where [?e :person/name ?name]
[?e :person/age ?age]
[(> ?age 18)]]');支援 not/not-join、or/or-join、具名規則(named rules)與遞迴規則(含環路偵測)。mentat.mentat_with() 提供推測性交易(in-memory、不寫入);時光旅行函式 as-of、since、history、tx-range 可查詢過去任一時間點的資料庫狀態。
v1.3.0 新增的軟整合
每個整合以 mentat.has_pgvector() 等能力偵測函式判斷,存在才載入。目前支援:
- pgvector:
vector-near(最近鄰語意搜尋) - pg_infer:
infer-near、infer-similar、infer-implies、infer-walk、infer-describe、infer-predict - PostGIS:
geom-near、geom-within、geom-contains、geom-intersects - RUM(全文搜尋索引)、pg_trgm(相似度)、fuzzystrmatch(Levenshtein、Soundex、Daitch-Mokotoff)
GDPR 相容的刪除機制(excision)允許按 entity、attribute 或 value 永久刪除特定 datom,滿足「被遺忘權」的法律要求,同時保留不可變日誌的審計完整性。