暂无图片
暂无图片
暂无图片
暂无图片
暂无图片

安装postgresqlt自建扩展(练手项目)

SmallDB 2025-04-11
91

 

源码位置

postgresql/pg_hello_world · smalldb/utilitiy - 码云 - 开源中国

这个代码是别人写的,我也是参考,从一个例子出发学习如何开始一个扩展,我认为是最好的学习方式

前期准备工作

切换用户加入环境变量

su - postgres
[postgres@vbox ~]$ vim ~/.bashrc

加入环境变量信息(编译时需要)

export PGUSER=postgres
export PGHOME=/usr/local/pgsqldebug
export LD_LIBRARY_PATH=$PGHOME/lib:$LD_LIBRARY_PATH
export PATH=$HOME/bin:$PGHOME/bin:$PATH

复制扩展到postgres目录

[root@vbox postgresql]# cp -r pg_hello_world home/postgres/
[root@vbox postgresql]# chown postgres:postgres /home/postgres/pg_hello_world/ -R
[root@vbox postgresql]#

正式开始

Makefile
EXTENSION = pg_hello_world
EXTVERSION = 1.0
DATA = pg_hello_world--1.0.sql
MODULE_big = pg_hello_world
OBJS = pg_hello_world.o
USE_PGXS = 1
PG_CONFIG = pg_config
PGXS := $(shell $(PG_CONFIG) --pgxs)
include $(PGXS)

日志
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -O2 -fPIC -I. -I./ -I/usr/local/pgsqldebug/include/server -I/usr/local/pgsqldebug/include/internal  -D_GNU_SOURCE   -c -o pg_hello_world.o pg_hello_world.c
gcc -Wall -Wmissing-prototypes -Wpointer-arith -Wdeclaration-after-statement -Werror=vla -Wendif-labels -Wmissing-format-attribute -Wimplicit-fallthrough=3 -Wcast-function-type -Wformat-security -fno-strict-aliasing -fwrapv -fexcess-precision=standard -Wno-format-truncation -Wno-stringop-truncation -g -O2 -fPIC -shared -o pg_hello_world.so pg_hello_world.o -L/usr/local/pgsqldebug/lib    -Wl,--as-needed -Wl,-rpath,'/usr/local/pgsqldebug/lib',--enable-new-dtags

出错解决(权限问题)
[postgres@vbox pg_hello_world]$ make install
/usr/bin/mkdir -p '/usr/local/pgsqldebug/lib'
/usr/bin/mkdir -p '/usr/local/pgsqldebug/share/extension'
/usr/bin/mkdir -p '/usr/local/pgsqldebug/share/extension'
/usr/bin/install -c -m 755  pg_hello_world.so '/usr/local/pgsqldebug/lib/pg_hello_world.so'
/usr/bin/install: cannot create regular file '/usr/local/pgsqldebug/lib/pg_hello_world.so': Permission denied
make: *** [/usr/local/pgsqldebug/lib/pgxs/src/makefiles/../../src/Makefile.shlib:456: install-lib-shared] Error 1
[postgres@vbox pg_hello_world]$ exit

解决
[root@vbox postgresql]# cd /usr/local/
[root@vbox local]# chown postgres:postgres pgsqldebug/ -R

重新编译
[root@vbox local]# su - postgres
[postgres@vbox ~]$ cd pg_hello_world/
[postgres@vbox pg_hello_world]$ make
make: Nothing to be done for 'all'.
[postgres@vbox pg_hello_world]$ ls
Makefile  pg_hello_world--1.0.sql  pg_hello_world.c  pg_hello_world.control  pg_hello_world.o  pg_hello_world.so
[postgres@vbox pg_hello_world]$ ll
total 80
-rw-r--r--. 1 postgres postgres   774 Apr 11 10:52 Makefile
-rw-r--r--. 1 postgres postgres   516 Apr 11 10:35 pg_hello_world--1.0.sql
-rw-r--r--. 1 postgres postgres   644 Apr 11 10:35 pg_hello_world.c
-rw-r--r--. 1 postgres postgres   441 Apr 11 10:35 pg_hello_world.control
-rw-r--r--. 1 postgres postgres 32336 Apr 11 10:52 pg_hello_world.o
-rwxr-xr-x. 1 postgres postgres 30808 Apr 11 10:52 pg_hello_world.so
[postgres@vbox pg_hello_world]$ make install
/usr/bin/mkdir -p '/usr/local/pgsqldebug/lib'
/usr/bin/mkdir -p '/usr/local/pgsqldebug/share/extension'
/usr/bin/mkdir -p '/usr/local/pgsqldebug/share/extension'
/usr/bin/install -c -m 755  pg_hello_world.so '/usr/local/pgsqldebug/lib/pg_hello_world.so'
/usr/bin/install -c -m 644 .//pg_hello_world.control '/usr/local/pgsqldebug/share/extension/'
/usr/bin/install -c -m 644 .//pg_hello_world--1.0.sql  '/usr/local/pgsqldebug/share/extension/'
[postgres@vbox pg_hello_world]$

使用

[postgres@vbox pg_hello_world]$ /usr/local/pgsqldebug/bin/psql -p 5432
psql (14.7)
Type "help" for help.

postgres=# create extension pg_hello_world;
CREATE EXTENSION
postgres=# \dx
                      List of installed extensions
      Name      | Version |   Schema   |          Description
----------------+---------+------------+--------------------------------
 pg_hello_world | 1.0     | public     | A PostgreSQL Extension Example
 plpgsql        | 1.0     | pg_catalog | PL/pgSQL procedural language
(2 rows)

postgres=# SELECT pg_hello_world();
 pg_hello_world
----------------
 Hello world!
(1 row)

postgres=#

说明

PGXS 是什么?

PostgreSQL 所提供的一个内置 makefile 文件,它可以结合由 pg_config 程序所提供的信息,让拓展能够编译、部署到 PostgreSQL 所处的位置去。
所以所有采用 PGXS 进行 makefile 设计的拓展都需要 pg_config 程序能够提供出正确的信息来(当然,对于正常部署的 PostgreSQL,它的工作结果都是正确的)。

Datum、PG_FUNCTION_ARGS 是什么?

他们都是 PostgreSQL 所提供的数据传输桥梁,因为拓展程序并非同 PostgreSQL 一同编译而产生,而是需要用动态加载的方式进入内核,因此自然而然需要用一定的手段,告知 PostgreSQL 我们所传输的数据内容,以及需要借助一定的手段,让我们能够获知来自于 PostgreSQL 的数据。    Datum 与 PG_FUNCTION_ARGS 便是为这种工作而设计的内容,其中前者是对于“数据类型”的抽象,用于代指其它所有的数据(正如 void* 在C语言指针中的位置一样),而后者则是对于用户调用函数时传入的参数的描述(拓展以后的内容非常复杂)。

参考

编写简单的 PostgreSQL 拓展-以 Hello world 程序为例
PostgreSQL用C完成存储过程例子
Writing PostgreSQL Extensions is Fun – C Language
动手编写PostgreSQL插件(2)

 


文章转载自SmallDB,如果涉嫌侵权,请发送邮件至:contact@modb.pro进行举报,并提供相关证据,一经查实,墨天轮将立刻删除相关内容。

评论