Apache AGE and Postgres Source Code Tour: Understanding the Complete Graph Function
Tags: apacheage, postgres
Published: 2023-06-13 15:00:40

In the following blog post, we are going to review a function used to create a complete graph within the Apache AGE. A complete graph is a simple undirected graph in which every pair of distinct vertices is connected by a unique edge. This will be important for the [Barbell Graph][1] function we will be creating later.

To begin, let's break down this code section by section:

1. **Variable Declarations:** The function begins by declaring various variables that will be used throughout the code. These include variables for the graph ID, graph name, number of vertices, vertex and edge labels, and various cache data.

```c
Oid graph_id;
Name graph_name;

int64 no_vertices;
int64 i,j,vid = 1, eid, start_vid, end_vid;

...
```

2. **Error Handling:** Next, the function checks if any of the arguments (i.e., the graph name, number of nodes, and edge label) are null, and if so, throws an error.

```c
if (PG_ARGISNULL(0))
{
    ereport(ERROR, (errcode(ERRCODE_INVALID_PARAMETER_VALUE),
                    errmsg("graph name can not be NULL")));
}

...
```

3. **Argument Assignments and Conversions:** The function assigns argument variables and sets defaults for non-obligatory ones. It also converts names to strings for easier manipulation. If a vertex label is given as a parameter and it's the same as the edge label, the function throws an error.

```c
graph_name = PG_GETARG_NAME(0);
no_vertices = (int64) PG_GETARG_INT64(1);
edge_label_name = PG_GETARG_NAME(2);
...

...
```

4. **Graph Creation and Assertions:** If the graph doesn't exist, it creates an empty one. It assigns the edge and node properties, checks if the labels exist, and creates them if they don't. The function also retrieves the label IDs to be used in creating graph IDs.

```c
if (!graph_exists(graph_name_str))
{
    DirectFunctionCall1(create_graph, CStringGetDatum(graph_name));
}
graph_id = get_graph_oid(graph_name_str);
...

...
```

5. **Vertices Creation:** The function creates vertices using the sequential ID for each vertex. For each vertex, it generates a unique ID and inserts the vertex into the graph.

```c
for (i=(int64)1;i<=no_vertices;i++)
{   
    vid = nextval_internal(vtx_seq_id, true);
    object_graph_id = make_graphid(vtx_label_id, vid);
    insert_vertex_simple(
        graph_id,
        vtx_name_str,
        object_graph_id,
        node_properties);
}
```

6. **Edges Creation:** Finally, the function creates edges connecting every pair of vertices in the graph. It uses the last ID of the vertices created to create edges between all possible vertex pairs. After creating the edge, it inserts it into the graph.

```c
for (i = 1;i<=no_vertices-1;i++)
{   
    start_vid = lid-no_vertices+i;
    for(j=i+1;j<=no_vertices;j++)
    {  
        end_vid = lid-no_vertices+j;
        eid = nextval_internal(edge_seq_id, true);
        object_graph_id = make_graphid(edge_label_id, eid);

        start_vertex_graph_id = make_graphid(vtx_label_id, start_vid);
        end_vertex_graph_id = make_graphid(vtx_label_id, end_vid);
        insert_edge_simple(graph_id, edge_name_str,
                        object_graph_id, start_vertex_graph_id,
                        end_vertex_graph_id, edge_properties);
    }
}
```

In summary, this function is a comprehensive example of how to create a complete graph in Apache AGE using PostgreSQL. It provides the ability to customize the graph name, number of nodes, edge label, and node properties, while ensuring that all possible edges between nodes are created. It is a robust tool that is essential in any graph database.

It is a good starting point to understand how Apache AGE works.

___

Check Apache AGE: https://age.apache.org/.

Overview — Apache AGE master documentation. https://age.apache.org/age-manual/master/intro/overview.html.

GitHub - apache/age: https://github.com/apache/age.. 

[1]: https://en.wikipedia.org/wiki/Barbell_graph