DEV Community

Ahmed Hisham
Ahmed Hisham

Posted on

Here's How To Create Your First PostgrSQL Extension Part 3

After part2 we should have one file so far in our directory (Makefile), now we will get into the .control file which provides metadata about the extension, also we will be going through the .c file:

Starting by add_two_numbers.control file:

comment = 'Simple number add function'
default_version = '0.0.1'
relocatable = true
module_pathname = '$libdir/add_two_numbers'
Enter fullscreen mode Exit fullscreen mode

comment: a brief description for extension functionality

default_version: as the name suggests, it specifies the default version of the extension

relocatable: if set to true it means that this extension can be moved to another location or server without causing issues, some extension crashes once they are moved to a different location, they require a different setup, but in the case of our extension it doesn't.

module_pathname: specifies the location to the shared library contains the extension code (the .so file in specific), which in my case will be found in /usr/local/pgsql-13/lib, and you can specify it manually up above by replacing $libdir with /usr/local/pgsql-13/lib.

Creating add_two_numbers.c file:

The c file contain the actual functionality of the extension, which is the addition operation between two numbers, Here's how we create a extension function in postgres according to docs:

#include "postgres.h"
#include "fmgr.h"

PG_MODULE_MAGIC;

PG_FUNCTION_INFO_V1(add_two_numbers);

Datum
add_two_numbers(PG_FUNCTION_ARGS)
{
int32 arg1 = PG_GETARG_INT32(0);
int32 arg2 = PG_GETARG_INT32(1);

PG_RETURN_INT32(arg1 + arg2);
}
Enter fullscreen mode Exit fullscreen mode

So far if we ls in the following files should be shown:

Makefile  add_two_numbers.c  add_two_numbers.control
Enter fullscreen mode Exit fullscreen mode

In the next part we will create the last file, build the extension and run it together!.

Top comments (0)