Continuing on part3 we got into the stage where we have .sql
file left to build the extension.
We should name the file as follows add_two_numbers--0.0.1.sql
including the version.
The file will contain the following:
CREATE OR REPLACE FUNCTION
add_two_numbers(int,int) RETURNS int AS 'MODULE_PATHNAME','add_two_numbers'
LANGUAGE C STRICT;
Then we finally can install the extension:
sudo make install
You should get an output similar to:
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -O2 -fPIC -I. -I./ -I/usr/local/pgsql-13/include/server -I/usr/local/pgsql-13/include/internal -D_GNU_SOURCE -c -o add_two_numbers.o add_two_numbers.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -O2 -fPIC add_two_numbers.o -L/usr/local/pgsql-13/lib -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsql-13/lib',--enable-new-dtags -shared -o add_two_numbers.so
/usr/bin/mkdir -p '/usr/local/pgsql-13/share/extension'
/usr/bin/mkdir -p '/usr/local/pgsql-13/share/extension'
/usr/bin/mkdir -p '/usr/local/pgsql-13/lib'
/usr/bin/install -c -m 644 .//add_two_numbers.control '/usr/local/pgsql-13/share/extension/'
/usr/bin/install -c -m 644 .//add_two_numbers--0.0.1.sql '/usr/local/pgsql-13/share/extension/'
/usr/bin/install -c -m 755 add_two_numbers.so '/usr/local/pgsql-13/lib/'
This should mean that the extension is installed successfully. Now it's time to run a postgres instance and test the extension, start a postgres instance from the binaries directory, in my case it will be from usr/local/pgsql-13/bin
:
./pg_ctl start -l logfile -D ./data
Then the instance:
./psql postgres
Create the extension:
postgres=# CREATE EXTENSION add_two_numbers;
CREATE EXTENSION
Run a query to test the function:
postgres=# SELECT add_two_numbers(1,3);
add_two_numbers
-----------------
4
(1 row)
It worked!, congratulations for building your first extension, I hope that was helpful!.
Top comments (0)