-1

I am connected to a database named 'testdb' using the PSQL utility with the 'age' extension loaded, How can I retrieve all the details(vertexes, relationship) of all graphs available in a database and also the number of graphs present in apache age?

I know using the query:

SELECT * FROM ag_catalog.ag_graph;

we can retrieve all the graph names but I need to access all vertex and relationships in each graph as well.

Kamlesh Kumar
  • 351
  • 1
  • 7

4 Answers4

2

You could use SELECT * FROM ag_catalog.ag_label to view information about the vertices and edges in all your graphs, this should output something like this:

       name       | graph | id | kind |         relation          |        seq_name
------------------+-------+----+------+---------------------------+-------------------------
 _ag_label_vertex | 16943 |  1 | v    | demo._ag_label_vertex     | _ag_label_vertex_id_seq
 _ag_label_edge   | 16943 |  2 | e    | demo._ag_label_edge       | _ag_label_edge_id_seq
 node             | 16943 |  3 | v    | demo.node                 | node_id_seq
 edge             | 16943 |  4 | e    | demo.edge                 | edge_id_seq
 _ag_label_vertex | 24579 |  1 | v    | my_graph._ag_label_vertex | _ag_label_vertex_id_seq
 _ag_label_edge   | 24579 |  2 | e    | my_graph._ag_label_edge   | _ag_label_edge_id_seq
 Person           | 24579 |  3 | v    | my_graph."Person"         | Person_id_seq
 Friends          | 24579 |  4 | e    | my_graph."Friends"        | Friends_id_seq
 Collegues        | 24579 |  5 | e    | my_graph."Collegues"      | Collegues_id_seq
(9 rows)

As you can see, the above contains the information of all the nodes and edges of all your graphs. You can see that the vertices are labeled 'v' and the edges are labeled 'e'. So from this you could run a query like;

SELECT * FROM my_graph."Friends";

And you will get the details of the relationship 'Friends';

        id        |    start_id     |     end_id      | properties
------------------+-----------------+-----------------+------------
 1125899906842625 | 844424930131969 | 844424930131970 | {}
(1 row)

Overall I think running SELECT * FROM ag_label; will really give you a view of all the nodes and egdes in all your graphs.

1

For a given graph, you can extract vertices and edges using 2 cypher queries.

For Vertices

SELECT * FROM cypher('graph_name', $$ MATCH (n) RETURN n $$) as (v agtype);

For Edges

SELECT * FROM cypher('graph_name', $$ MATCH ()-[r]->() RETURN r $$) as (e agtype);

ELSE you can execute a single cypher query with multiple returns to get the desired output.

SELECT * FROM cypher('graph_name', $$ MATCH (n), ()-[r]->() RETURN n, r $$) as (v agtype, e agtype);

You can try using the python driver or any other AGE driver to execute this easily. You can simply fetch all the graph names using :

SELECT * FROM ag_catalog.ag_graph;

and then execute above cypher queries iteratively over different graph names.

Safi50
  • 379
  • 1
  • 7
  • 1
    **DO NOT** use the `ELSE` query, which creates a cartesian product. With N nodes and R relationships, it will return N*R results. Every node will be returned R times, and every relationship will be returned N times. – cybersam May 02 '23 at 16:37
0

Try using these SQL statements:

SELECT * FROM age_graph.vertex; To get the vertices of the graph.

SELECT * FROM age_graph.edge; To get the edges.

I hope this was of help.

Shanzay
  • 19
  • 3
0

Running SELECT * FROM ag_label; should return the graphs, labels, vertices and edges in the database. However to run that statement, you should have SET the search_path already. If you haven't then SELECT * FROM ag_catalog.ag_label; works too.

Tito
  • 289
  • 8