Extending the Database: C Functions

* You have to be a database superuser to insert C functions into the database.

* C functions are written in C and compiled into a shared library.

* You can use the Server Programming Interface (SPI) to access database data from within those routines.

* Example:

postgres:/home/postgres/lib# cat double.c
#include <stdio.h>

extern int double_me(int a)
    return a*2;
postgres:/home/postgres/lib# gcc -shared -Wl,-soname,libpgdouble.so.1 \
>                                  -o libpgdouble.so double.c
postgres:/home/postgres/lib# psql test
Welcome to the POSTGRESQL interactive sql monitor:
  Please read the file COPYRIGHT for copyright terms of POSTGRESQL
[PostgreSQL 6.5.2 on i686-pc-linux-gnu, compiled by gcc egcs-2.91.66]

   type \? for help on slash commands
   type \q to quit
   type \g or terminate with semicolon to execute query
 You are currently connected to the database: test

test=> CREATE FUNCTION double_me(int4)
test-> RETURNS int4
test-> AS '/home/postgres/lib/libpgdouble.so'
test-> LANGUAGE 'c' \g
test=> SELECT double_me(5) \g
(1 row)