Source code tour on Apache AGE and PostgreSQL - Our First Function
Tags: apacheage, postgres
Published: 2023-05-29 18:38:25

Apache AGE is a **PostgreSQL extension** that provides **graph database functionality**. AGE stands for **A Graph Extension** and is inspired by Bitnine’s fork of PostgreSQL 10, AgensGraph, which is a multi-model database.

___

Here I'll start a blog series explaining how I am contributing to Apache AGE, so you could understand it a little more and enter the world of open source.

You'll need to create a fork of its [GitHub repository](https://github.com/apache/age). For that, go to the provided link and click the "Fork" button, which will guide you to create a copy of the repository on your GitHub account.

After that, you'll need to clone it to your machine to start working on it.

I put the Apache AGE source code folder side-by-side with postgres', so it should recognize its builtin functions on your IDE. 

So the folder structure will be like this:

```
workspace
  |-- age
  |-- postgres
```

So create a folder and enter it on your terminal, then clone apache-age and postgres.
I like to use VSCode, so I open the "workspace" folder so it will appear both "age" and "postgres" folders.

After that, compile and install postgres and AGE and we will be ready to start contributing.

___

For starters, we will prepare the field to create a new function on an already existing file. For that, we will use the [graph_generation.c](https://github.com/apache/age/blob/master/src/backend/utils/graph_generation.c) file. We will be creating functions that make pre-modeled graphs.

So we need to make postgres recognize our new function. 

For that we will need to add the signature of our new function to the [age--1.3.0.sql](https://github.com/apache/age/blob/master/age--1.3.0.sql) file:

```sql
CREATE FUNCTION ag_catalog.my_new_function()
RETURNS void
LANGUAGE c
CALLED ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';
```

___

Now we will head to the [graph_generation.c](https://github.com/apache/age/blob/master/src/backend/utils/graph_generation.c) file and add our function there with its registration:

```cpp
// function registration
PG_FUNCTION_INFO_V1(my_new_function);
Datum my_new_function(PG_FUNCTION_ARGS)
{
	elog(NOTICE, "HELLO APACHE AGE!");
	return PG_RETURN_VOID();
}
```

This function will only output "HELLO APACHE AGE!" on the psql command line.
___ 

After that, compile and install AGE:

```
make uninstall && make && make install
```

___

As we have changed the [age--1.3.0.sql](https://github.com/apache/age/blob/master/age--1.3.0.sql) file, we will need to enter `psql` terminal, drop the extension and create it again:

```
DROP EXTENSION age CASCADE;
CREATE EXTENSION age;

LOAD 'age';
SET search_path TO ag_catalog;
```

With only that, we can already call our function on psql terminal, just issue the command 

`SELECT my_new_function();`

And we will see our new program in action! 

```sql
NOTICE:  HELLO POSTGRES!
 my_new_function
-----------------

(1 row)
```

That's it for now, experiment with it and have fun!

I'll teach how to create a pre modeled graph with this function on the next post.

___

(1) Apache AGE. https://age.apache.org/.
(2) Overview — Apache AGE master documentation. https://age.apache.org/age-manual/master/intro/overview.html.
(3) GitHub - apache/age: Graph database optimized for fast analysis and .... https://github.com/apache/age..