pg_llm
pg_llm · Integrates local and remote large-language models with chat, sessions, text-to-SQL, audit and vector-backed knowledge features.
Overview
| ID | Extension | Package | Version | Category | License | Language |
|---|---|---|---|---|---|---|
| 101069 | pg_llm | pg_llm | 1.1 | RAG | PostgreSQL | C++ |
PostgreSQL · 14, 15, 16, 17, 18
| Relationships | |
|---|---|
| Requires | vector |
Usage
Sources:
pg_llm integrates remote and local language models with PostgreSQL. Version 1.1 provides model registration, chat and streaming calls, multi-turn sessions, parallel model selection, text-to-SQL, SQL analysis/reporting, vector-backed knowledge search, feedback, audit logs, and request traces. It depends on vector and sends model prompts through backend processes.
Core Workflow
Preload the library and restart PostgreSQL:
Install vector, create the extension, register a model, and make a call. The example uses a psql variable so the key is not written literally into command history:
For multi-turn use, create a session with pg_llm_create_session(max_messages) and pass its returned ID to pg_llm_multi_turn_chat.
Main API Groups
- Models/chat:
pg_llm_add_model,pg_llm_remove_model,pg_llm_chat,pg_llm_chat_stream,pg_llm_chat_json, and parallel-chat variants. - Sessions:
pg_llm_create_session,pg_llm_multi_turn_chat, session lookup/state/update/delete/cleanup functions. - Data work:
pg_llm_text2sql[_json],pg_llm_execute_sql_with_analysis, andpg_llm_generate_report. - Retrieval:
pg_llm_get_embedding, vector storage/search,pg_llm_add_knowledge, andpg_llm_search_knowledge. - Observability:
pg_llm_record_feedback,pg_llm_get_audit_log, andpg_llm_get_trace.
Model, session, vector, audit, trace, report, knowledge, and feedback state is stored in _pg_llm_catalog; version 1.1 creates 64-dimensional pgvector columns and IVFFlat indexes for its internal retrieval tables.
Caveats
The extension requires PostgreSQL 14+, a C++20 build, libcurl, OpenSSL, JsonCpp, and pgvector. Preloading and outbound HTTP occur inside database server processes. Provider latency, rate limits, TLS/DNS failures, and large/streaming responses can therefore occupy backends and transactions.
Prompts, SQL text, query results, API keys, audit records, and knowledge documents may be sensitive. Restrict EXECUTE on network, text-to-SQL, and SQL-execution functions; protect _pg_llm_catalog; use a managed secret path; and review retention/logging. Treat generated SQL and model advice as untrusted input—validate permissions, use read-only roles where possible, and never execute it automatically in a privileged session.