# Pg Graph

> Graph database skills for Apache AGE on PostgreSQL. Covers the full lifecycle: deriving an ontology from structured or unstructured data with a human feedback loop, building the graph, and querying it with openCypher, natural language to Cypher, and graph augmented retrieval.

- Skill: `microsoft/pg-graph` (Agent Skill, multi-file: 11 files)
- Install (CLI): `npx skillmds@latest add microsoft/pg-graph`
- Raw SKILL.md: https://api.skillmd.com/api/skills/microsoft/pg-graph/raw
- Safety review: pending
- Works with: Claude Code, Claude.ai, OpenAI Codex
- Category: Coding & Dev Tools
- Author: Microsoft (https://skillmd.com/u/microsoft)
- Updated: 2026-09-17
- Page: https://skillmd.com/skills/microsoft/pg-graph

---


# pg-graph — Apache AGE Graph Skills (Routing Table)

Use references as supplemental context, combined with your Apache AGE and openCypher knowledge. If reference guidance is incomplete, answer with appropriate caveats rather than inventing syntax.

This skill is the single home for graph work on PostgreSQL, covering the full lifecycle. The generic PostgreSQL skill (`postgresql-best-practices`) points here for anything involving Apache AGE, openCypher, `ag_catalog`, knowledge graphs, or ontology. Keep AGE specific guidance in this skill rather than duplicating it in the generic skill.

## Lifecycle: construct then consume

1. **Derive an ontology** from the user's structured or unstructured data, and run a human feedback loop before finalizing. See [ontology-derivation](references/ontology-derivation.md).
2. **Build the graph** by extracting, deduplicating, and MERGE loading into AGE using the finalized ontology. See [extract-to-graph](references/extract-to-graph.md).
3. **Consume the graph** with openCypher, natural language to Cypher, schema introspection, and graph augmented retrieval.

Construction uses only capabilities available today: agent driven extraction over the MCP query tools (works on any PostgreSQL with AGE), or the `azure_ai` extension for in-database work at scale on Azure. It does NOT depend on unreleased `ai.*` pipeline primitives. Never finalize an ontology without explicit user approval.

## Prerequisites (verify before graph work)

- Apache AGE is an extension. Confirm it is installed and load it once per session before any Cypher call.
- On managed Azure Database for PostgreSQL (Flexible Server and Azure HorizonDB), `age` must be allowlisted in `azure.extensions` **and** added to `shared_preload_libraries`, then created with `CREATE EXTENSION IF NOT EXISTS age CASCADE;`. Set both on Flexible Server via server parameters; on Azure HorizonDB via a parameter group connected to the cluster. Both auto-restart to apply. It cannot be enabled with `ALTER SYSTEM`. AGE is preloaded on both, so do NOT run `LOAD 'age';` there (a non-superuser gets `access to library "age" is not allowed`).
- Non superuser sessions must set `search_path` so `ag_catalog` is available but NOT first. Use `SET search_path = public, ag_catalog;`. Putting `ag_catalog` first makes ordinary `CREATE TABLE` fail with permission denied for schema `ag_catalog`.

## Key Constraints (what models get wrong)

| Fact | Detail |
|------|--------|
| Cypher is wrapped in a function | Every query runs as `SELECT * FROM ag_catalog.cypher('graph_name', $$ ... $$) AS (col agtype)`. It is not a bare statement. |
| Column definition list is required | The `AS (col agtype)` list must match the RETURN arity, and every returned column is typed `agtype`. |
| agtype casting | Scalars come back as `agtype`. Cast for SQL use rather than assuming a raw text or int is returned. State uncertainty about exact cast helpers instead of inventing function names. |
| search_path ordering | `SET search_path = public, ag_catalog;` (never `ag_catalog` first) for non superuser roles. |
| Graph must exist | Create the graph once with `create_graph('graph_name')` before MERGE or MATCH. |
| MERGE for idempotent load | Use `MERGE` on a stable business key to avoid duplicate vertices during repeated extraction. |
| Parameters | AGE Cypher does not accept host bind parameters inside `$$...$$` the way SQL does. Interpolate safely on the server side or wrap the cypher call in SQL. Never string concatenate untrusted input. |

## Routing Table

| Keyword triggers | Reference | When to use |
|---|---|---|
| derive ontology, suggest ontology, generate ontology, ontology from data, ontology from documents, propose ontology | [ontology-derivation](references/ontology-derivation.md) | Analyze structured or unstructured data, propose an ontology, and run a human feedback loop before finalizing |
| extract to graph, build graph from data, build knowledge graph, populate graph, extract entities to graph | [extract-to-graph](references/extract-to-graph.md) | Apply a finalized ontology: extract, deduplicate, and MERGE into the AGE graph |
| entity resolution, context dedup, deduplicate entities, canonicalize entities, merge duplicate entities | [context-dedup](references/context-dedup.md) | Resolve entity aliases using type, graph neighborhood, and source snippet, with scalable blocking and a persistent canonical map |
| apache age, opencypher, cypher(), create_graph, property graph, vertices and edges, MERGE node | [opencypher-age-patterns](references/opencypher-age-patterns.md) | AGE setup, Cypher wrapping, MATCH/MERGE/CREATE patterns, indexing vertices and edges |
| text to cypher, natural language to cypher, english to cypher, generate cypher, nl to cypher | [text-to-cypher](references/text-to-cypher.md) | Turning a user question into a validated openCypher query and running it |
| graph schema, list vertex labels, edge labels, ag_label, describe graph | [graph-schema-introspection](references/graph-schema-introspection.md) | Discovering labels, edge types, and properties so generated Cypher is grounded |
| visualize graph, vs code graph, render the graph, graph explorer, see the graph, plot the graph, ms-ossdata.vscode-pgsql | [text-to-cypher](references/text-to-cypher.md#visualizing-the-graph-in-the-vs-code-extension) | Generate visualization-ready Cypher (full vertex/edge objects, `disp_label`, matched `AS` columns) for the PostgreSQL extension for VS Code graph explorer |
| graph rag, graph augmented, graph augmented retrieval, hybrid graph retrieval | [graph-augmented-rag](references/graph-augmented-rag.md) | Retrieval that combines vector similarity with graph traversal and reranking |
| explainability, traceability, provenance, why this recommendation, reasoning path, audit graph answer | [graph-explainability](references/graph-explainability.md) | Make facts traceable to sources and recommendations explainable: provenance on vertices and edges, returned reasoning path, weakest link path confidence, and a reproducible reasoning trace log |
| graph semantic search, graph azure_ai embeddings, graph azure openai embeddings, enable azure_ai for graph, configure azure openai for graph | [azure-ai-semantic-search](references/azure-ai-semantic-search.md) | Enable and configure the `azure_ai` extension, prompt the user for endpoint/key/deployment, and generate embeddings for semantic search |
| cypher example, graph query example, worked graph example | [examples](references/examples.md) | End to end worked examples spanning schema, query, and results |

## Anti-Hallucination Rules

1. Do NOT present a Cypher query as a bare statement. Always wrap it in `ag_catalog.cypher(...)` with a column definition list.
2. Do NOT claim host bind parameters work inside the `$$...$$` body.
3. Do NOT put `ag_catalog` first in `search_path`.
4. Do NOT enable `age` with `ALTER SYSTEM` on managed Azure. Use the `azure.extensions` + `shared_preload_libraries` allowlist, set via server parameters (Flexible Server) or a parameter group connected to the cluster (Azure HorizonDB).
5. Verify labels and properties with schema introspection before generating Cypher against an unknown graph.
6. State uncertainty about exact `agtype` cast helpers rather than inventing function names.
7. Do NOT finalize a derived ontology without explicit user approval. Treat it as a proposal and run the feedback loop.
8. Do NOT rely on unreleased `ai.*` pipeline primitives. Use agent-driven extraction or the `azure_ai` extension over Apache AGE, both available today.
9. For semantic search, do NOT invent an Azure OpenAI endpoint, key, or embedding deployment name. Read current `azure_ai` settings and ask the user for anything missing. See [azure-ai-semantic-search](references/azure-ai-semantic-search.md).
10. Do NOT present a graph recommendation without its reasoning path, supporting evidence, and a confidence bounded by the weakest edge on the path. See [graph-explainability](references/graph-explainability.md).

