-- Create the Apache AGE extensionCREATE EXTENSION IF NOT EXISTS age;-- Add ag_catalog to search_pathSET search_path = ag_catalog, "$user", public;-- Or set it permanently (recommended)ALTER DATABASE mydb SET search_path = ag_catalog, "$user", public;
Concept | Description |
Graph (Graph) | A named graph space that contains vertices and edges. |
Vertex (Vertex) | An entity in a graph that can have tags and attributes. |
Edge (Edge) | A relationship between vertices that is directed and can have attributes. |
Label (Tag) | A type classification for vertices or edges |
Property (attribute) | A key-value pair on a vertex or edge |
-- Create a new graphSELECT create_graph('knowledge_graph');
-- Create a node with Tags and attributesSELECT * FROM cypher('knowledge_graph', $$CREATE (n:Person {name: 'Zhang San', age: 30, role: 'Engineer'})RETURN n$$) AS (node agtype);-- Create nodes in batchesSELECT * FROM cypher('knowledge_graph', $$CREATE (a:Person {name: 'Li Si', role: 'Manager'}),(b:Company {name: 'Tencent Cloud', industry: 'Cloud'}),(c:Technology {name: 'PostgreSQL', type: 'Database'})RETURN a, b, c$$) AS (a agtype, b agtype, c agtype);
-- Create relationships between nodesSELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person {name: 'Zhang San'}), (c:Company {name: 'Tencent Cloud'})CREATE (p)-[r:WORKS_AT {since: '2020-01-01'}]->(c)RETURN r$$) AS (rel agtype);-- Create a USES relationshipSELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person {name: 'Zhang San'}), (t:Technology {name: 'PostgreSQL'})CREATE (p)-[r:USES {level: 'expert'}]->(t)RETURN r$$) AS (rel agtype);
-- Find all Person nodesSELECT * FROM cypher('knowledge_graph', $$MATCH (n:Person)RETURN n.name, n.role$$) AS (name agtype, role agtype);-- Find relationship pathsSELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person)-[r:WORKS_AT]->(c:Company)RETURN p.name, c.name, r.since$$) AS (person agtype, company agtype, since agtype);-- Multi-hop querySELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person)-[:WORKS_AT]->(c:Company),(p)-[:USES]->(t:Technology)RETURN p.name, c.name, t.name$$) AS (person agtype, company agtype, tech agtype);-- Path query (2-3 hops)SELECT * FROM cypher('knowledge_graph', $$MATCH path = (a:Person)-[*2..3]->(b)RETURN path$$) AS (path agtype);
-- Update node attributesSELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person {name: 'Zhang San'})SET p.age = 31RETURN p$$) AS (node agtype);-- Delete relationshipsSELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person {name: 'Zhang San'})-[r:USES]->(t:Technology)DELETE r$$) AS (result agtype);-- Delete nodes (requires deleting associated edges first)SELECT * FROM cypher('knowledge_graph', $$MATCH (t:Technology {name: 'PostgreSQL'})DETACH DELETE t$$) AS (result agtype);
-- Graph query results combined with relational tablesSELECT g.person_name, u.email, u.departmentFROM (SELECT * FROM cypher('knowledge_graph', $$MATCH (p:Person)-[:WORKS_AT]->(c:Company {name: 'Tencent Cloud'})RETURN p.name AS person_name$$) AS (person_name agtype)) gJOIN users u ON u.name = g.person_name::text;
-- Build a product knowledge graphSELECT create_graph('product_kg');SELECT * FROM cypher('product_kg', $$CREATE (pg:Product {name: 'PostgreSQL', category: 'Database'}),(vec:Feature {name: 'Vector search', desc: 'Based on pgvector'}),(ai:Feature {name: 'AI Invocation', desc: 'Based on tencentdb_ai'}),(pg)-[:HAS_FEATURE]->(vec),(pg)-[:HAS_FEATURE]->(ai)RETURN pg, vec, ai$$) AS (pg agtype, vec agtype, ai agtype);
-- After entities are extracted and stored in the graph, a hybrid recall using both graph and vector methods is performed during search:-- 1. Vector recall related documents-- 2. Find the relationship chains of relevant entities from the graph-- 3. Send the graph context + document content together to the large model
-- Record the interaction relationships between AgentsSELECT * FROM cypher('agent_memory', $$CREATE (a1:Agent {name: 'Planner', role: 'planning'}),(a2:Agent {name: 'Researcher', role: 'research'}),(a1)-[:DELEGATES {task: 'Research competing products', timestamp: '2026-01-15'}]->(a2)RETURN a1, a2$$) AS (a1 agtype, a2 agtype);
-- Create an index on the attributes of a node TagCREATE INDEX ON knowledge_graph."Person" ((properties->>'name'));
ag_catalog to search_path.cypher() function, and the column types for the results must be specified.agtype (a JSON-like type customized by Apache AGE).Was this page helpful?
You can also Contact sales or Submit a Ticket for help.
Help us improve! Rate your documentation experience in 5 mins.
Feedback