目录
一、编译安装Postgre
装好系统后,先把需要的包安装一下
[root@bogon ~]# yum install -y zlib-devel
[root@bogon ~]# yum install -y readline-devel
[root@bogon ~]# yum install -y gcc gdb
创建用户
[root@bogon ~]# useradd postgres
[root@bogon ~]# passwd postgres
[root@bogon ~]# su postgres
解压源码包
[postgres@bogon ~]$ tar -zxvf postgresql-12.0
配置安装路径
[postgres@bogon ~]$ cd postgresql-12.0
[postgres@bogon postgresql-12.0]$ ./configure --prefix=/home/postgres/install --with-pgport=5432 --enable-debug
注意:这里一一定要把 --enable-debug 参数加上
编译安装
在编译过程中可以把gcc的参数从O2 改为O0 ,关闭所有优化选项 防止编译器优化,优化后有些代码在调试过程中可能就看不到了
[postgres@bogon postgresql-12.0]$ make
[postgres@bogon postgresql-12.0]$ make clean
[postgres@bogon postgresql-12.0]$ vi src/Makefile.global
[postgres@bogon postgresql-12.0]$ make
[postgres@bogon postgresql-12.0]$ make install
初始化数据库
[postgres@bogon bin]$ cd /home/postgres/install/bin/
[postgres@bogon bin]$ ./initdb -D /home/postgres/install/data/
启动数据库
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ -l logfile start
连接数据库
[postgres@bogon bin]$ ./psql
二、使用GDB调试
准备一些数据
postgres=# create table test(id varchar(10),name varchar(100) );
CREATE TABLE
postgres=# insert into test(id,name) values('1','aaa');
INSERT 0 1
postgres=# select * from test;
id | name
----+------
1 | aaa
(1 row)
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
26239
(1 row)
新开一个窗口会话 调试26239这个进程
[postgres@bogon ~]$ gdb -p 26239
在GetNewTransactionId函数下个断点
(gdb)b GetNewTransactionId
注意:出现函数的地址才算成功,如果下断点后不显示函数地址显示pending,可能是普通用户对该进程没有权限导致,可以换超级用户试试。(如果是在Ubuntu上,可以修改/etc/sysctl.d/10-ptrace.conf 将最后一行改为 kernel.yama.ptrace_scope = 0)
让程序继续运行
(gdb) c
回到数据库,开启一个事务 观察程序运行到GetNewTransactionId函数能否停下
查看GDB窗口是否停在GetNewTransactionId函数,验证结果是GDB可以正常调试,GDB测试调试到此就可以结束了,GDB可以正常使用
到此,回到数据库提交数据 ,gdb也可以退出了
三、修改源码并且编译运行
修改源码
[postgres@bogon ~]$ cd /home/postgres/postgresql-12.0/src/backend/access/transam/
[postgres@bogon transam]$ vi varsup.c
随便添加几行代码
修改完之后重新编译一下
注意:这里我们使用make的方式进行编译,而不是直接用gcc对源文件varsup.c进行编译,如果直接用gcc编译 会报链接库有问题,链接库找了很久编译才过去,但是执行的时候还是有问题,所以还是直接用make,方便好用不容易出问题
[postgres@bogon transam]$ make
将编译好的文件重新复制到bin目录下
注意:这里make install 一定要指定目录,不然文件复制到哪里去了我也没找到~
[postgres@bogon postgresql-12.0]$ cd /home/postgres/postgresql-12.0
[postgres@bogon postgresql-12.0]$ make install DESTDIR=/
可执行文件的修改时间变了,说明是刚复制过来的
重新启动数据库,用GDB调试 ,验证自己修改的代码是否生效
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ start
waiting for server to start....2022-03-20 02:21:57.017 CST [27163] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2022-03-20 02:21:57.019 CST [27163] LOG: listening on IPv6 address "::1", port 5432
2022-03-20 02:21:57.019 CST [27163] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-03-20 02:21:57.020 CST [27163] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-03-20 02:21:57.036 CST [27164] LOG: database system was shut down at 2022-03-20 02:21:52 CST
2022-03-20 02:21:57.038 CST [27163] LOG: database system is ready to accept connections
done
server started
[postgres@bogon bin]$ ./psql
psql (12.0)
Type "help" for help.
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
27172
(1 row)
开启一个事务
去GDB窗口查看修改的代码是否可以生效,结果验证没有问题,源码修改成功
修改的代码都可以正常运行
四、在源文件中添加头文件编译运行
上面这种方式是把函数直接写在源文件中的,实际上我们会有自己的源文件和头文件,下面就是自己写一个源文件和头文件,然后在源码中引用我们的头文件,验证是否可行
随便写一个C文件
[postgres@bogon include]$ cd /home/postgres/postgresql-12.0/src/include
[postgres@bogon include]$ mkdir mycode
[postgres@bogon include]$ cd mycode/
[postgres@bogon mycode]$ vim mycode.h
[postgres@bogon mycode]$ vim mycode.c
C文件:
头文件:
再次修改源码,引用我们写的头文件
[postgres@bogon mycode]$ vi /home/postgres/postgresql-12.0/src/backend/access/transam/varsup.c
重新编译
[postgres@bogon mycode]$ cd /home/postgres/postgresql-12.0/src/backend/access/transam/
[postgres@bogon transam]$ make clean
[postgres@bogon transam]$ make
将编译好的文件复制到bin目录
[postgres@bogon transam]$ cd /home/postgres/postgresql-12.0
[postgres@bogon transam]$ make install DESTDIR=/
重新启动数据库,用GDB调试,验证头文件是否可以有效引用。验证结果一切正常
[postgres@bogon bin]$ cd /home/postgres/install/bin/
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ stop
waiting for server to shut down.... done
server stopped
[postgres@bogon bin]$ ./pg_ctl -D /home/postgres/install/data/ start
waiting for server to start....2022-03-20 03:24:30.138 CST [28438] LOG: starting PostgreSQL 12.0 on x86_64-pc-linux-gnu, compiled by gcc (GCC) 4.8.5 20150623 (Red Hat 4.8.5-44), 64-bit
2022-03-20 03:24:30.139 CST [28438] LOG: listening on IPv6 address "::1", port 5432
2022-03-20 03:24:30.139 CST [28438] LOG: listening on IPv4 address "127.0.0.1", port 5432
2022-03-20 03:24:30.140 CST [28438] LOG: listening on Unix socket "/tmp/.s.PGSQL.5432"
2022-03-20 03:24:30.157 CST [28439] LOG: database system was shut down at 2022-03-20 03:24:23 CST
2022-03-20 03:24:30.158 CST [28438] LOG: database system is ready to accept connections
done
server started
[postgres@bogon bin]$ ./psql
psql (12.0)
Type "help" for help.
postgres=# select pg_backend_pid();
pg_backend_pid
----------------
28447
(1 row)
postgres=# begin;
BEGIN
postgres=# update test set name='mycode' where id ='1';
gnaixgnahz
test c:3
mycode test d=6
UPDATE 1
postgres=# commit;
COMMIT
postgres=# quit;