DEV Community

Marco Aurélio Silva de Souza Júnior
Marco Aurélio Silva de Souza Júnior

Posted on

Source code tour on Apache AGE and PostgreSQL - Our First Function

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. 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
Enter fullscreen mode Exit fullscreen mode

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 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 file:

CREATE FUNCTION ag_catalog.my_new_function()
RETURNS void
LANGUAGE c
CALLED ON NULL INPUT
PARALLEL SAFE
AS 'MODULE_PATHNAME';
Enter fullscreen mode Exit fullscreen mode

Now we will head to the graph_generation.c file and add our function there with its registration:

// 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();
}
Enter fullscreen mode Exit fullscreen mode

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


After that, compile and install AGE:

make uninstall && make && make install
Enter fullscreen mode Exit fullscreen mode

As we have changed the 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;
Enter fullscreen mode Exit fullscreen mode

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!

NOTICE:  HELLO POSTGRES!
 my_new_function
-----------------

(1 row)
Enter fullscreen mode Exit fullscreen mode

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..

Top comments (0)