pg_synapse runs an agent loop from inside PostgreSQL:
SELECT synapse.execute('notes_agent', 'Add a note that says "Hello"');That call runs a real tool-calling loop against an LLM, and the agent’s tools can read and write Postgres tables directly — no separate service, no round trip out of the database to hold the loop state.
Why run the agent loop in the database?#
- The kernel stays small. Six traits, three reference executors (conversation, react, reflection), a
towermiddleware seam, a built-in MCP client, and aRuntimefacade. Everything opinionated — providers, memory, compaction, embeddings — is a plugin, not baked into the core. - Two host paths, same engine. A
pgrxPostgres extension for environments where you can load a shared library, and apg-synapse-sidecaraxum binary for managed Postgres where you can’t — both run the same kernel. - Provider-agnostic tool calling. Real OpenAI, vLLM, llama-cpp-server, LM Studio, Ollama, and Anthropic’s Messages API are all supported provider plugins, verified end-to-end against a live LLM rather than mocked.
- Middleware composition, not bespoke glue. Cost caps, retries, tracing, and dedup are
tower::Layers you compose, with recipes documented for common combinations. - A conformance suite that catches drift. Golden cassettes per provider plus a drift check catch silent serde-shape changes before they become a production surprise.
What works today#
An agent invoked from SQL that reads and writes tables via sql_query/sql_exec tool calls; local embeddings through ONNX Runtime (BGE family) callable as SELECT synapse.embed(...); HTTP, SQL, filesystem, calc, clock, and delegate (multi-agent) tools, plus a #[derive(Tool)] macro for adding your own; both the pgrx extension and the sidecar binary shipped and verified live.
What shipped since July#
- pg-one, the app builder. Describe an app in a sentence and it builds a schema, an agent and sample rows inside Postgres. Apps can run on a schedule, take revisions and be deleted, and an MCP server lets other agents use an app as a tool.
- Agents run as the caller. An agent now runs as the role that invoked it, not a superuser, with secrets encrypted at rest and the key kept outside the database. The
httptool has egress allowlisting, and rows can name the run that wrote them. - Runs that outlive the session. A background worker keeps a run going after the session that asked has gone, and a cap limits how many runs are in flight.
- Self-tuning. Harness components, a trajectory renderer, lessons, and a Propose-Probe-Compose tuner improve an agent from its own history (version 0.2.0).
- Inline triggers. An agent attached in inline mode runs inside the writing transaction and can roll the write back.
- Sidecar in the default compose file, a TypeScript client, a CLI that talks to Postgres, and a containerized demo at
localhost:8080.
Quick start#
cargo install --locked cargo-pgrx --version 0.18.0
cargo pgrx init
cargo pgrx install --features pg17 --no-default-featuresCREATE EXTENSION pg_synapse_pgrx;
SELECT synapse.llm_profile_set(
'llm', 'openai', '<model>', 'http://your-endpoint:8000/v1', NULL, '{}'::jsonb);
SELECT synapse.agent_create(
'asst', 'You are a helpful assistant that can query the database.',
'conversation', 'llm', ARRAY['sql_query','sql_exec'], 5, 60000);
SELECT synapse.execute('asst', 'How many rows are in public.users?');Docs#
All in the repo’s docs/ folder:
- Extension quickstart and sidecar quickstart
- pg-one, the app builder
- Why not a Python script on a cron?
- Threat model and replicas and failover
- Agent catalog, tool authoring, middleware recipes and the full SQL reference
Blog posts#
- pg-synapse: Run AI Agents From SQL, Like a Stored Procedure
- How to Actually Build an Agent on Postgres (a pg-synapse Tutorial)
- Why on Earth Would You Run AI Inside the Database?
- pg_synapse Grew Up: App Builder, Caller Isolation and a Self-Tuning Harness
- Plexus: One Postgres That Runs, Remembers and Tunes an Agent
Links#
- GitHub Repository
- Project page and narrated walkthrough
- Plexus: the full end-to-end demo built on pg-synapse
- License: MIT or Apache-2.0, at your option
