PostgreSQL is written in C, so installing PostgreSQL software is nothing but compiling and running all the C-Programs in your source code.
To work with PostgreSQL internals it is highly recommended to learn the basics of C programming.
Table of Contents
How to create and compile a C Program on Linux
Create a C program called first.c
1 2 3 4 5 6 |
[root@prim final]# cat first.c #include<stdio.h> int main(void) { printf("Hello! This is a test prgoram.\n"); } |
To compile this program, enter:
1 2 3 4 5 6 |
[root@prim final]# gcc first.c -o first.o [root@prim final]# ls -lrt total 12 -rw-r--r--. 1 root root 81 May 13 19:10 first.c -rwxr-xr-x. 1 root root 6449 May 13 19:10 first.o [root@prim final]# |
The above command will compile first.c program and creates an executable file called first.o.
To run the first.o just do ./first.o
1 2 3 |
[root@prim final]# ./first.o Hello! This is a test prgoram. [root@prim final]# |
What is shared library
In programming, a library is an assortment of pre-compiled pieces of code that can be reused in a program.
For instance, if you are building an application that needs to perform math operations, you don’t have to create a new math function for that, you can simply use existing functions in libraries for that programming language.
libc (the standard C library) is one such standard library on Linux ecosystem.
Linux supports two classes of libraries, namely:
- Static libraries – are bound to a program statically at compile time.
- Dynamic or shared libraries – are loaded when a program is launched and loaded into memory and binding occurs at run time.
The shared library file can be created by gcc compiler and its extension would be .so
Locating Shared Libraries in Linux
A program can call a library using its library name or filename, and a library path stores directories where libraries can be found in the filesystem. By default, libraries are located in /usr/local/lib, /usr/local/lib64, /usr/lib and /usr/lib64; system startup libraries are in /lib and /lib64. Programmers can, however, install libraries in custom locations.
The default location of PostgreSQL shared libraries/Shared objects is <install_directory>/lib. For version 1o it is /usr/pgsql-10/lib
1 2 3 4 5 6 7 8 9 10 |
[postgres@prim lib]$ ls -lrt total 9384 -rwxr-xr-x. 1 root root 90640 Feb 12 02:47 postgres_fdw.so -rwxr-xr-x. 1 root root 7976 Feb 12 02:47 pg_prewarm.so -rwxr-xr-x. 1 root root 126512 Feb 12 02:47 pgcrypto.so ... ... [postgres@prim lib]$ pwd /usr/pgsql-10/lib [postgres@prim lib]$ |
To set it temporarily, use the LD_LIBRARY_PATH environment variable on the command line. If you want to keep the changes permanent, then add this line in the shell initialization file /etc/profile (global) or ~/.profile (user specific).
1 |
export LD_LIBRARY_PATH=/path/to/library/file |
How to create a shared object on Linux
Now that we understand a library file, we now going to create a shared object for the above C Program with the following command.
gcc -shared -o first.so -fPIC first.c
1 2 3 4 5 6 7 8 |
[root@prim final]# gcc -shared -o first.so -fPIC first.c [root@prim final]# [root@prim final]# ls -lrt total 20 -rw-r--r--. 1 root root 81 May 13 19:10 first.c -rwxr-xr-x. 1 root root 6449 May 13 19:10 first.o -rwxr-xr-x. 1 root root 5852 May 13 19:35 first.so [root@prim final]# |
Now, a shared object file called first.so is created.
To run this file execute below command
1 2 3 4 5 6 7 |
[root@prim final]# gcc first.c first.so [root@prim final]# ls -lrt total 28 -rw-r--r--. 1 root root 81 May 13 19:10 first.c -rwxr-xr-x. 1 root root 6449 May 13 19:10 first.o -rwxr-xr-x. 1 root root 5852 May 13 19:35 first.so -rwxr-xr-x. 1 root root 6737 May 13 19:36 a.out |
Here, it created the a.out file, run ./a.out to get the output.
1 2 3 4 |
[root@prim final]# ./a.out ./a.out: error while loading shared libraries: first.so: cannot open shared object file: No such file or directory [root@prim final]# |
As we discussed just now, a shared library can now be executed by setting the environment.
1 2 3 4 5 |
[root@prim final]# export LD_LIBRARY_PATH=/root/final [root@prim final]# ./a.out Hello! This is a test prgoram. [root@prim final]# |
What is shared_preload_libraries in PostgreSQL?
Now this dynamic library shared object can be loaded into PostgreSQL by below techniques.
- From
shared_preload_libraries
. This loads at postmaster start time, so the extension can register shared memory, locks, hooks etc before we start forking backends. - With
session_preload_libraries
. This loads after fork(), in a backend, so it can’t register shmem etc. - With
local_preload_libraries
. Like, but limited to the plugins directory and settable by normal users. - With the
LOAD
a statement, or implicitly by running a function that refers to aLANGUAGE c
implementation.
The Extensions created in PostgreSQL are mostly created as dynamic library shared objects, so when create extension is executed, the shared object is loaded and called.
Depending on the extension usage, it may need to be added in shared_preload_libraries
in postgresql.conf and few extension can be loaded normally.
For example., pg_stat_statements must be loaded by adding it to shared_preload_libraries in postgresql.conf, because it requires additional shared memory. This means that a server restart is needed to add or remove the module.
How to create a simple function in PostgreSQL
The following function adds 10 to the given value.
1 2 3 4 5 6 7 8 9 10 11 12 |
postgres=# CREATE OR REPLACE FUNCTION addten(sum INTEGER) postgres-# RETURNS INTEGER AS $$ postgres$# DECLARE postgres$# result INTEGER; postgres$# BEGIN postgres$# result=sum+10; postgres$# RETURN result; postgres$# END; $$ postgres-# LANGUAGE plpgsql; CREATE FUNCTION postgres=# postgres=# |
The above function accepts the integer and adds 10 to that and displays it as output.
1 2 3 4 5 |
postgres=# select addten(14); addten -------- 24 (1 row) |
The above is pre-requisite for understanding PostgreSQL Extensions in PostgreSQL.
Please continue to learn PostgreSQL extensions.
Words from postgreshelp
Thank you for giving your valuable time to read the above information. I hope the content served your purpose in reaching out to the blog.
Suggestions for improvement of the blog are highly appreciable. Please contact us for any information/suggestions/feedback.
If you want to be updated with all our articles
please follow us on Facebook | Twitter
Please subscribe to our newsletter.