PostgreSQL ——如何在Linux中编译和运行C程序

PostgreSQL是用C编写的,因此安装PostgreSQL软件本质上不过是编译和运行源代码中所有的C程序。
想要使用PostgreSQL内部组件,强烈建议学习C编程的基础知识。

1、如何在Linux上创建和编译C程序

首先我们创建一个名为first.c的C程序:

[root@prim final]# cat first.c
#include<stdio.h>
int main(void)
{
printf("Hello! This is a test prgoram.\n");
}

接下来编译该程序:

[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]#

执行完上面的命令后会编译first.c程序并创建一个名为first.o的可执行文件。

接下来想要运行first.o,只需执行./first.o

[root@prim final]# ./first.o
Hello! This is a test prgoram.
[root@prim final]#

2、什么是shared library

在编程中,库(library)指定是可以在程序中重用的各种预编译代码段。

例如,如果你想要构建写一段执行数学运算的程序,则不需要为此创建新的数学函数,直接使用该编程语言库中的现有函数即可。

libc(标准C库)是Linux生态系统上的一种此类标准库。

Linux支持两类库,即:

  • Static libraries :在编译时静态绑定到程序。
  • Dynamic or shared libraries:在程序启动时加载并加载到内存中,并且在运行时进行绑定。

共享库文件可以由gcc编译器创建,其扩展名为.so。

2.1、在Linux中查找shared library

程序可以使用其库名或文件名来调用库,也可以通过在文件系统中找到库的目录来调用库。默认情况下,库位于/ usr / local / lib,/ usr / local / lib64,/ usr / lib和/ usr / lib64中;系统启动库位于/ lib和/ lib64中。但是,程序员可以在自定义位置安装库。

PostgreSQL共享库/共享对象的默认位置是install_directory / lib。例如在pg10中,它的路径就是/ usr / pgsql-10 / lib。

[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]$

想要临时设置它,可以在命令行中使用export LD_LIBRARY_PATH来修改环境变量。
如果想要永久保留更改,请在外壳程序初始化文件/ etc / profile(全局)或〜/ .profile(用户特定)中添加此行。

export LD_LIBRARY_PATH=/path/to/library/file

3、如何在Linux中创建shared library

现在我们了解了库文件的原理,接下来我们将使用以下命令为我们前面编写的C程序创建一个共享库。
gcc -shared -o first.so -fPIC first.c

[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]#

现在,就创建好了一个名为first.so的共享对象文件。

要运行此文件,请执行以下命令

[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

可以看到,它创建了一个a.out文件,我们可以运行./a.out来获取输出。

[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]#

正如我们刚才讨论的那样,现在可以通过设置环境变量来执行共享库。

[root@prim final]#  export LD_LIBRARY_PATH=/root/final     [root@prim final]# ./a.out  
Hello! This is a test prgoram.  
[root@prim final]#

4、PostgreSQL中的shared_preload_libraries是什么

现在,我们可以通过下面的方式将此动态库共享对象加载到PostgreSQL中。

  • 通过shared_preload_libraries:这会在postmaster启动时加载,因此扩展程序可以在启动一个backends进程之前注册共享内存,锁,hooks等。
  • 通过session_preload_libraries:它将在fork()之后加载到backends进程中,因此无法注册shmem等。
  • 通过local_preload_libraries:类似于,但仅限于plugins目录,普通用户可以设置。
  • 通过load语句,通过运行引用C函数实现的函数隐式地实现。

在PostgreSQL中创建的扩展大部分是作为动态库共享对象创建的,因此当执行create extension时,将加载并调用共享对象。

在添加新的扩展来使用时,有少数扩展可能需要我们在postgresql.conf的shared_preload_libraries中添加它,来它们的正常加载。

例如,pg_stat_statements必须要将其添加到postgresql.conf的shared_preload_libraries中才能加载,因为它需要额外的共享内存。这意味着需要重新启动数据库服务才能添加或删除该扩展模块。

5、如何在PostgreSQL中创建一个简单的函数

下面的函数将实现给定值加10

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=#  

上面的函数作用是将接受到的整数加10,并将其显示为输出。

postgres=# select addten(14);
 addten
--------
     24
(1 row)

猜你喜欢

转载自blog.csdn.net/weixin_39540651/article/details/109032405