A graph consists of a set of vertices and edges, where each individual node and edge possesses a map of properties. A vertex is the basic object of a graph, that can exist independently of everything else in the graph. An edge creates a directed connection between two vertices.

Create a Graph

To create a graph, use the create_graph function, located in the ag_catalog namespace.

create_graph()

Syntax: create_graph(graph_name);

Returns:

  1. void

Arguments:

Name Description
graph_name Name of the graph to be created

Considerations

  • This function will not return any results. The graph is created if there is no error message.
  • Tables needed to set up the graph are automatically created.

Example:

  1. SELECT * FROM ag_catalog.create_graph('graph_name');

Delete a Graph

To delete a graph, use the drop_graph function, located in the ag_catalog namespace.

drop_graph()

Syntax: drop_graph(graph_name, cascade);

Returns:

  1. void

Arguments:

Name Description
graph_name Name of the graph to be deleted
cascade A boolean that will will delete labels and data that depend on the graph.

Considerations:

  • This function will not return any results. If there is no error message the graph has been deleted.
  • It is recommended to set the cascade option to true, otherwise everything in the graph must be manually dropped with SQL DDL commands.

Example:

  1. SELECT * FROM ag_catalog.drop_graph('graph_name', true);

How Graphs Are Stored In Postgres

When creating graphs with AGE, a Postgres namespace will be generated for every individual graph. The name and namespace of the created graphs can be seen within the ag_graph table from the ag_catalog namespace:

  1. SELECT create_graph('new_graph');
  2. NOTICE: graph "new_graph" has been created
  3. create_graph
  4. --------------
  5. (1 row)
  6. SELECT * FROM ag_catalog.ag_graph;
  7. name | namespace
  8. -----------+-----------
  9. new_graph | new_graph
  10. (1 row)

After creating the graph, two tables are going to be created under the graph’s namespace to store vertices and edges: _ag_label_vertex and _ag_label_edge. These will be the parent tables of any new vertex or edge label. The query below shows how to retrieve the edge and vertex labels for all the graphs in the database.

  1. -- Before creating a new vertex label.
  2. SELECT * FROM ag_catalog.ag_label;
  3. name | graph | id | kind | relation | seq_name
  4. ------------------+-------+----+------+----------------------------+-------------------------
  5. _ag_label_vertex | 68484 | 1 | v | new_graph._ag_label_vertex | _ag_label_vertex_id_seq
  6. _ag_label_edge | 68484 | 2 | e | new_graph._ag_label_edge | _ag_label_edge_id_seq
  7. (2 rows)
  8. -- Creating a new vertex label.
  9. SELECT create_vlabel('new_graph', 'Person');
  10. NOTICE: VLabel "Person" has been created
  11. create_vlabel
  12. ---------------
  13. (1 row)
  14. -- After creating a new vertex label.
  15. SELECT * FROM ag_catalog.ag_label;
  16. name | graph | id | kind | relation | seq_name
  17. ------------------+-------+----+------+----------------------------+-------------------------
  18. _ag_label_vertex | 68484 | 1 | v | new_graph._ag_label_vertex | _ag_label_vertex_id_seq
  19. _ag_label_edge | 68484 | 2 | e | new_graph._ag_label_edge | _ag_label_edge_id_seq
  20. Person | 68484 | 3 | v | new_graph."Person" | Person_id_seq
  21. (3 rows)

Whenever a vertex label is created with the create_vlabel() function, a new table is generated within the new_graph‘s namespace: new_graph."<label>". The same works for the create_elabel() function for the creation of edge labels. Creating vertices and edges with Cypher will automatically make these tables.

  1. -- Creating two vertices and one edge.
  2. SELECT * FROM cypher('new_graph', $$
  3. CREATE (:Person {name: 'Daedalus'})-[:FATHER_OF]->(:Person {name: 'Icarus'})
  4. $$) AS (a agtype);
  5. a
  6. ---
  7. (0 rows)
  8. -- Showing the newly created tables.
  9. SELECT * FROM ag_catalog.ag_label;
  10. name | graph | id | kind | relation | seq_name
  11. ------------------+-------+----+------+----------------------------+-------------------------
  12. _ag_label_vertex | 68484 | 1 | v | new_graph._ag_label_vertex | _ag_label_vertex_id_seq
  13. _ag_label_edge | 68484 | 2 | e | new_graph._ag_label_edge | _ag_label_edge_id_seq
  14. Person | 68484 | 3 | v | new_graph."Person" | Person_id_seq
  15. FATHER_OF | 68484 | 4 | e | new_graph."FATHER_OF" | FATHER_OF_id_seq
  16. (4 rows)

Note: It is recommended that no DML or DDL commands are executed in the namespace that is reserved for the graph.