目录
以postgresql-14.6、CentOS7为例
一、官网postgresql 安装包下载
[root@admin /]# yum -y install wget
[root@admin /]# wget https://ftp.postgresql.org/pub/source/v14.6/postgresql-14.6.tar.gz
二、安装所需的依赖包
[root@admin /]# yum install -y perl-ExtUtils-Embed readline-devel zlib-devel pam-devel libxml2-devel libxslt-devel openldap-devel python-devel gcc-c++ openssl-devel cmake
三、 源码编译安装
- 创建文件夹,将postgresql的压缩包移入
[root@admin /]# mkdir /soft
[root@admin /]# mv postgresql-14.6.tar.gz /soft/[root@admin /]# cd /soft/
[root@admin soft]# ls
postgresql-14.6.tar.gz
- 解压压缩包到目录中
[root@admin soft]# tar -zxvf postgresql-14.6.tar.gz
postgresql-14.6/
postgresql-14.6/.dir-locals.el
postgresql-14.6/contrib/
postgresql-14.6/contrib/tcn/
postgresql-14.6/contrib/tcn/tcn.control
postgresql-14.6/contrib/tcn/Makefile
postgresql-14.6/contrib/tcn/tcn.c
...postgresql-14.6/doc/KNOWN_BUGS
postgresql-14.6/doc/Makefile
postgresql-14.6/doc/TODO
postgresql-14.6/doc/MISSING_FEATURES
postgresql-14.6/HISTORY
postgresql-14.6/Makefile
postgresql-14.6/README
postgresql-14.6/COPYRIGHT
postgresql-14.6/GNUmakefile.in
postgresql-14.6/.gitattributes
postgresql-14.6/aclocal.m4
postgresql-14.6/INSTALL
- 进入解压后的文件夹
[root@admin soft]# cd postgresql-14.6
[root@admin postgresql-14.6]# ls
aclocal.m4 config configure configure.ac contrib COPYRIGHT doc GNUmakefile.in HISTORY INSTALL Makefile README src
- 创建postgresql的目录,编译postgresql源码
[root@admin postgresql-14.6]# mkdir -p /opt/pgsql/postgresql
[root@localhost postgresql-14.6]# ./configure --prefix=/opt/pgsql/postgresqlchecking build system type... x86_64-pc-linux-gnu
checking host system type... x86_64-pc-linux-gnu
checking which template to use... linux
checking whether NLS is wanted... no
checking for default port number... 5432
checking for block size... 8kB
checking for segment size... 1GB
checking for WAL block size... 8kB
checking for gcc... gcc
...configure: creating ./config.status
config.status: creating GNUmakefile
config.status: creating src/Makefile.global
config.status: creating src/include/pg_config.h
config.status: creating src/include/pg_config_ext.h
config.status: creating src/interfaces/ecpg/include/ecpg_config.h
config.status: linking src/backend/port/tas/dummy.s to src/backend/port/tas.s
config.status: linking src/backend/port/posix_sema.c to src/backend/port/pg_sema.c
config.status: linking src/backend/port/sysv_shmem.c to src/backend/port/pg_shmem.c
config.status: linking src/include/port/linux.h to src/include/pg_config_os.h
config.status: linking src/makefiles/Makefile.linux to src/Makefile.port
- 源码编译安装
[root@admin postgresql-14.6]# make && make install
make -C ./src/backend generated-headers
make[1]: Entering directory `/soft/postgresql-14.6/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/soft/postgresql-14.6/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
prereqdir=`cd './' >/dev/null && pwd` && \...
make[2]: Leaving directory `/soft/postgresql-14.6/src/test/isolation'
make -C test/perl all
make[2]: Entering directory `/soft/postgresql-14.6/src/test/perl'
make[2]: Nothing to be done for `all'.
make[2]: Leaving directory `/soft/postgresql-14.6/src/test/perl'
make[1]: Leaving directory `/soft/postgresql-14.6/src'
make -C config all
make[1]: Entering directory `/soft/postgresql-14.6/config'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/soft/postgresql-14.6/config'[root@localhost postgresql-14.6]# make install
make -C ./src/backend generated-headers
make[1]: Entering directory `/soft/postgresql-14.6/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/soft/postgresql-14.6/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/soft/postgresql-14.6/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/soft/postgresql-14.6/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/soft/postgresql-14.6/src/backend/utils'
make[1]: Leaving directory `/soft/postgresql-14.6/src/backend'...
/usr/bin/install -c -m 644 ./nls-global.mk '/opt/pgsql/postgresql/lib/pgxs/src/nls-global.mk'
make[1]: Leaving directory `/soft/postgresql-14.6/src'
make -C config install
make[1]: Entering directory `/soft/postgresql-14.6/config'
/usr/bin/mkdir -p '/opt/pgsql/postgresql/lib/pgxs/config'
/usr/bin/install -c -m 755 ./install-sh '/opt/pgsql/postgresql/lib/pgxs/config/install-sh'
/usr/bin/install -c -m 755 ./missing '/opt/pgsql/postgresql/lib/pgxs/config/missing'
make[1]: Leaving directory `/soft/postgresql-14.6/config'
- 完成postgreql的安装。检查。
[root@admin postgresql-14.6]# cd /opt/pgsql/postgresql/
[root@admin postgresql]# pwd
/opt/pgsql/postgresql[root@admin postgresql]# ls
bin include lib share
四、创建用户和用户组
- 创建用户组postgres、用户postgres
[root@admin postgresql]# groupadd postgres
[root@admin postgresql]# useradd -g postgres postgres
[root@admin postgresql]# id postgres
uid=1002(postgres) gid=1002(postgres) groups=1002(postgres)
五、创建数据主目录
- 创建postgresql数据库的数据主目录并修改文件所有者
- 数据库主目录是随实际情况而不同,这里的主目录是在/pgsql/postgresql/data目录下
[root@admin postgresql]# cd /opt/pgsql/postgresql
[root@admin postgresql]# mkdir data
[root@admin postgresql]# chown postgres:postgres data[root@admin postgresql]# ls -la
total 20
drwxr-xr-x. 7 root root 68 Jul 29 10:42 .
drwxr-xr-x. 3 root root 24 Jul 29 09:34 ..
drwxr-xr-x. 2 root root 4096 Jul 29 10:42 bin
drwx------. 19 postgres postgres 4096 Jul 31 08:38 data
drwxr-xr-x. 6 root root 4096 Jul 29 10:42 include
drwxr-xr-x. 4 root root 4096 Jul 29 10:42 lib
drwxr-xr-x. 6 root root 4096 Jul 29 10:42 share
六、配置环境变量
- 进入home/postgres目录可以看到.bash_profile文件。
[root@admin postgresql]# cd /home/postgres/
[root@admin postgres]# ls -latotal 24
drwx------. 5 postgres postgres 165 Jul 29 12:23 .
drwxr-xr-x. 5 root root 51 Jul 29 09:41 ..
-rw-------. 1 postgres postgres 120 Jul 29 12:23 .bash_history
-rw-r--r--. 1 postgres postgres 18 Apr 1 2020 .bash_logout
-rw-r--r--. 1 postgres postgres 297 Jul 29 10:45 .bash_profile
-rw-r--r--. 1 postgres postgres 231 Apr 1 2020 .bashrc
drwxrwxr-x. 3 postgres postgres 18 Jul 29 10:46 .cache
drwxrwxr-x. 3 postgres postgres 18 Jul 29 10:46 .config
drwxr-xr-x. 4 postgres postgres 39 Jul 11 08:28 .mozilla
-rw-------. 1 postgres postgres 6 Jul 29 12:23 .psql_history
-rw-------. 1 postgres postgres 1184 Jul 29 10:57 .viminfo
- 修改.bash_profile文件。
[root@admin postgres]# vi .bash_profile
[root@admin postgres]# cat .bash_profile
# .bash_profile# Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi# User specific environment and startup programs
PATH=$PATH:$HOME/.local/bin:$HOME/bin
export PATH
export PGHOME=/opt/pgsql/postgresql
export PGDATA=$PGHOME/data
export PATH=$PATH:$HOME/bin:$PGHOME/bin
- 保存,退出vi。执行以下命令,使环境变量生效
[root@admin postgres]# source .bash_profile
七、initdb初使化数据库
- 切换用户到postgres并使用initdb初使化数据库
[root@admin postgres]# su - postgres
[postgres@admin ~]$ initdb
The files belonging to this database system will be owned by user "postgres".
This user must also own the server process.The database cluster will be initialized with locale "en_US.UTF-8".
The default database encoding has accordingly been set to "UTF8".
The default text search configuration will be set to "english".Data page checksums are disabled.
fixing permissions on existing directory /opt/pgsql/postgresql/data ... ok
creating subdirectories ... ok
selecting dynamic shared memory implementation ... posix
selecting default max_connections ... 100
selecting default shared_buffers ... 128MB
selecting default time zone ... PRC
creating configuration files ... ok
running bootstrap script ... ok
performing post-bootstrap initialization ... ok
syncing data to disk ... okinitdb: warning: enabling "trust" authentication for local connections
You can change this by editing pg_hba.conf or using the option -A, or
--auth-local and --auth-host, the next time you run initdb.Success. You can now start the database server using:
pg_ctl -D /opt/pgsql/postgresql/data -l logfile start
- 查看/pgsql/postgresql/data目录,看是否初始化成功
[postgres@admin ~]$ ls -lh $PGDATA
total 68K
drwx------. 5 postgres postgres 41 Jul 29 10:46 base
drwx------. 2 postgres postgres 4.0K Jul 31 08:35 global
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_commit_ts
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_dynshmem
-rw-------. 1 postgres postgres 4.8K Jul 29 10:57 pg_hba.conf
-rw-------. 1 postgres postgres 1.6K Jul 29 10:46 pg_ident.conf
drwx------. 4 postgres postgres 68 Jul 31 08:43 pg_logical
drwx------. 4 postgres postgres 36 Jul 29 10:46 pg_multixact
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_notify
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_replslot
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_serial
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_snapshots
drwx------. 2 postgres postgres 6 Jul 31 08:38 pg_stat
drwx------. 2 postgres postgres 25 Jul 31 09:22 pg_stat_tmp
drwx------. 2 postgres postgres 18 Jul 29 10:46 pg_subtrans
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_tblspc
drwx------. 2 postgres postgres 6 Jul 29 10:46 pg_twophase
-rw-------. 1 postgres postgres 3 Jul 29 10:46 PG_VERSION
drwx------. 3 postgres postgres 60 Jul 29 10:46 pg_wal
drwx------. 2 postgres postgres 18 Jul 29 10:46 pg_xact
-rw-------. 1 postgres postgres 88 Jul 29 10:46 postgresql.auto.conf
-rw-------. 1 postgres postgres 29K Jul 29 10:56 postgresql.conf
-rw-------. 1 postgres postgres 69 Jul 31 08:38 postmaster.opts
-rw-------. 1 postgres postgres 84 Jul 31 08:38 postmaster.pid
-rw-rw-r--. 1 postgres postgres 2.6K Jul 31 08:38 serverlog
八、配置服务
- 修改/opt/pgsql/postgresql/data目录下postgresql.conf文件,该文件配置PostgreSQL数据库服务器的相应的参数。
[postgres@admin ~]$ cd /opt/pgsql/postgresql/data
[postgres@admin data]$ vi postgresql.conf
[postgres@admin data]$ cat postgresql.conf...
#------------------------------------------------------------------------------
# CONNECTIONS AND AUTHENTICATION
#------------------------------------------------------------------------------# - Connection Settings -
listen_addresses = '*' # what IP address(es) to listen on;
# comma-separated list of addresses;
# defaults to 'localhost'; use '*' for all
# (change requires restart)
port = 5432 # (change requires restart)
max_connections = 100 # (change requires restart)
...
- 修改/opt/pgsql/postgresql/data目录下pg_hba.conf 文件,该文件 配置对数据库的访问权限。
[postgres@admin data]$ vi pg_hba.conf
[postgres@admin data]$ cat pg_hba.conf...
# 添加下面这一行
# IPv4 local connections:
host all all 0.0.0.0/0 trust
host all all 127.0.0.1/32 trust...
九、设置开机自启动
- PostgreSQL的开机自启动脚本位于PostgreSQL源码目录的contrib/start-scripts路径下。linux文件即为linux系统上的启动脚本。需切换为root用户。
[postgres@admin start-scripts]$ su root
[root@admin data]# cd /soft/postgresql-14.6/contrib/start-scripts
[root@admin start-scripts]# ls
freebsd linux macos
- 切换为root用户,修改linux文件属性,添加X属性
[root@admin start-scripts]# chmod a+x linux
- 复制linux文件到/etc/init.d目录下,更名为postgresql
[root@admin start-scripts]# cp linux /etc/init.d/postgresql
- 修改/etc/init.d/postgresql文件的两个变量
[root@admin start-scripts]# vi /etc/init.d/postgresql
[root@admin start-scripts]# cat /etc/init.d/postgresql
...
## EDIT FROM HERE
# Installation prefix
#prefix=/usr/local/pgsql
prefix=/opt/pgsql/postgresql# Data directory
#PGDATA="/usr/local/pgsql/data"
PGDATA="/opt/pgsql/postgresql/data"...
- 设置postgresql服务开机自启动
[root@admin start-scripts]# chkconfig --add postgresql
[root@admin start-scripts]# chkconfig
Note: This output shows SysV services only and does not include native
systemd services. SysV configuration data might be overridden by native
systemd configuration.If you want to list systemd services use 'systemctl list-unit-files'.
To see services enabled on particular target use
'systemctl list-dependencies [target]'.netconsole 0:off 1:off 2:off 3:off 4:off 5:off 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
postgresql 0:off 1:off 2:on 3:on 4:on 5:on 6:off
十、设置防火墙
- 安装依赖包
[root@admin /]# yum -y install firewalld
- 设置防火墙
[root@admin start-scripts]# systemctl start firewalld
[root@admin start-scripts]# systemctl enable firewalld
[root@admin start-scripts]# systemctl status firewalld● firewalld.service - firewalld - dynamic firewall daemon
Loaded: loaded (/usr/lib/systemd/system/firewalld.service; enabled; vendor preset: enabled)
Active: active (running) since Wed 2024-07-31 08:35:51 CST; 56min ago
Docs: man:firewalld(1)
Main PID: 670 (firewalld)
Tasks: 2
CGroup: /system.slice/firewalld.service
└─670 /usr/bin/python2 -Es /usr/sbin/firewalld --nofork --nopid
- 添加postgresql服务到防火墙,并加载
[root@admin start-scripts]# firewall-cmd --permanent --zone=public --add-service=postgresql
success
[root@admin start-scripts]# firewall-cmd --reload
success
- 查看端口是否开放
[root@admin start-scripts]# /sbin/iptables -L -n
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0 ctstate RELATED,ESTABLISHED
ACCEPT all -- 0.0.0.0/0 0.0.0.0/0
INPUT_direct all -- 0.0.0.0/0 0.0.0.0/0
INPUT_ZONES_SOURCE all -- 0.0.0.0/0 0.0.0.0/0
INPUT_ZONES all -- 0.0.0.0/0 0.0.0.0/0...
Chain IN_public_deny (1 references)
target prot opt source destinationChain IN_public_log (1 references)
target prot opt source destinationChain OUTPUT_direct (1 references)
target prot opt source destination
十一、启动数据库服务
- 启动PostgreSQL服务
[root@admin start-scripts]# service postgresql start
Starting PostgreSQL: ok
- 查看PostgreSQL服务,确认是否启动成功
[root@admin start-scripts]# ps -ef | grep postgres
postgres 6258 1 0 08:40 ? 00:00:00 /opt/pgsql/postgresql/bin/postmaster -D /opt/pgsql/postgresql/data
postgres 6260 6258 0 08:40 ? 00:00:00 postgres: checkpointer
postgres 6261 6258 0 08:40 ? 00:00:00 postgres: background writer
postgres 6262 6258 0 08:40 ? 00:00:00 postgres: walwriter
postgres 6263 6258 0 08:40 ? 00:00:00 postgres: autovacuum launcher
postgres 6264 6258 0 08:40 ? 00:00:00 postgres: stats collector
postgres 6265 6258 0 08:40 ? 00:00:00 postgres: logical replication launcher
root 7327 6014 0 08:40 pts/0 00:00:00 su - postgres
postgres 7328 7327 0 08:40 pts/0 00:00:00 -bash
postgres 102365 7328 0 09:36 pts/0 00:00:00 ps -ef
postgres 102366 7328 0 09:36 pts/0 00:00:00 grep --color=auto postgres
十二、测试本地连接
- 切换设置的postgres用户
[root@admin postgres]# su - postgres
Last login: Wed Jul 31 08:39:17 CST 2024 on pts/0
[postgres@admin ~]$ psql
psql (14.6)
Type "help" for help.postgres=# \l
List of databases
Name | Owner | Encoding | Collate | Ctype | Access privileges
-----------+----------+----------+-------------+-------------+-----------------------
postgres | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 |
template0 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
template1 | postgres | UTF8 | en_US.UTF-8 | en_US.UTF-8 | =c/postgres +
| | | | | postgres=CTc/postgres
(3 rows)
十三、回归测试文件
- \q或者ctrl+d退出psql
- PostgreSQL的主代码测试文件在 /soft/postgresql-14.6/src/test/regress目录中。
- 这个目录的结构如下:
[postgres@admin test]$ cd /soft/postgresql-14.6/src/test/regress
[postgres@admin regress]$ ls -la
total 472
drwxrwxrwx. 7 1107 1107 4096 Jul 29 10:42 .
drwxrwxrwx. 15 1107 1107 232 Nov 8 2022 ..
-rwxr-xr-x. 1 root root 13000 Jul 29 09:38 autoinc.so
drwxrwxrwx. 2 1107 1107 4096 Nov 8 2022 data
drwxrwxrwx. 2 1107 1107 8192 Nov 8 2022 expected
-rw-r--r--. 1 1107 1107 209 Nov 8 2022 .gitignore
-rw-r--r--. 1 1107 1107 5236 Nov 8 2022 GNUmakefile
drwxrwxrwx. 2 1107 1107 217 Nov 8 2022 input
-rw-r--r--. 1 1107 1107 778 Nov 8 2022 Makefile
drwxrwxrwx. 2 1107 1107 245 Nov 8 2022 output
-rw-r--r--. 1 1107 1107 5105 Nov 8 2022 parallel_schedule
-rwxr-xr-x. 1 root root 82952 Jul 29 10:42 pg_regress
-rw-r--r--. 1 1107 1107 69114 Nov 8 2022 pg_regress.c
-rw-r--r--. 1 1107 1107 1915 Nov 8 2022 pg_regress.h
-rw-r--r--. 1 1107 1107 3349 Nov 8 2022 pg_regress_main.c
-rw-r--r--. 1 root root 5056 Jul 29 09:38 pg_regress_main.o
-rw-r--r--. 1 root root 65792 Jul 29 10:42 pg_regress.o
-rw-r--r--. 1 1107 1107 159 Nov 8 2022 README
-rwxr-xr-x. 1 root root 18048 Jul 29 09:38 refint.so
-rw-r--r--. 1 1107 1107 31982 Nov 8 2022 regress.c
-rw-r--r--. 1 root root 50696 Jul 29 09:38 regress.o
-rwxr-xr-x. 1 1107 1107 4434 Nov 8 2022 regressplans.sh
-rwxr-xr-x. 1 root root 38664 Jul 29 09:38 regress.so
-rw-r--r--. 1 1107 1107 165 Nov 8 2022 resultmap
drwxrwxrwx. 2 1107 1107 8192 Nov 8 2022 sql
-rw-r--r--. 1 1107 1107 579 Nov 8 2022 standby_schedule
十四、 pg_regress
工具
1. 基础用法
pg_regress
工具用于自动化运行回归测试,确保对 PostgreSQL 源码的更改不会引入新的错误,并且现有的功能依然按照预期工作。回归测试框架主要通过执行一系列预定义的 SQL 脚本,并将其输出与预期结果进行比较来实现。这些测试验证了数据库系统的功能完整性和稳定性。- 运行
pg_regress
可以使用以下命令:
cd src/test/regress
src/test/regress/pg_regress [OPTIONS] [EXTRA-TEST]...
2. 常用选项
- --config-auth=DATADIR:更新 DATADIR 中的身份验证设置。
- --create-role=ROLE:在测试前创建指定的角色。
- --dbname=DB:使用指定的数据库 DB(默认是 "regression")。
- --debug:开启调试模式。
- --inputdir=DIR:从指定目录 DIR 中获取输入文件(默认是当前目录 ".")。
- --launcher=CMD:使用指定的命令 CMD 启动 psql。
- --load-extension=EXT:在运行测试前加载指定的扩展,可以多次出现以加载多个扩展。
- --load-language=LANG:在运行测试前加载指定的语言,可以多次出现以加载多种语言。
- --max-connections=N:设置最大并发连接数(默认是 0,表示无限制)。
- --outputdir=DIR:将输出文件放在指定目录 DIR(默认是当前目录 ".")。
- --schedule=FILE:使用 FILE 中的测试顺序安排,可以多次出现以连接多个文件。
- --temp-install=DIR:在 DIR 中创建临时安装。
- --use-existing:使用现有安装。
3. 临时安装模式选项
- --extra-install=DIR:安装附加目录(如 contrib)。
- --no-locale:使用 C 语言环境。
- --port=PORT:在指定端口 PORT 启动 postmaster。
- --temp-config=FILE:将 FILE 的内容附加到临时配置中。
- --top-builddir=DIR:相对于顶级构建目录的路径。
4. 使用现有安装的选项
- --host=HOST:使用运行在 HOST 上的 postmaster。
- --port=PORT:使用在 PORT 上运行的 postmaster。
- --user=USER:以 USER 连接。
- --psqldir=DIR:使用 DIR 中的 psql(默认是配置的绑定目录)。
十五、运行内置回归测试
- 进入回归测试目录并运行测试:
- 这将会编译 PostgreSQL 并运行所有的回归测试。测试结果将会显示在控制台中,并且生成的日志文件会被保存在
src/test/regress/results
目录中。
[postgres@admin regress]$ make check
make -C ../../../src/backend generated-headers
make[1]: Entering directory `/soft/postgresql-14.6/src/backend'
make -C catalog distprep generated-header-symlinks
make[2]: Entering directory `/soft/postgresql-14.6/src/backend/catalog'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/soft/postgresql-14.6/src/backend/catalog'
make -C utils distprep generated-header-symlinks
make[2]: Entering directory `/soft/postgresql-14.6/src/backend/utils'
make[2]: Nothing to be done for `distprep'.
make[2]: Nothing to be done for `generated-header-symlinks'.
make[2]: Leaving directory `/soft/postgresql-14.6/src/backend/utils'
make[1]: Leaving directory `/soft/postgresql-14.6/src/backend'
make -C ../../../src/port all
make[1]: Entering directory `/soft/postgresql-14.6/src/port'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/soft/postgresql-14.6/src/port'
make -C ../../../src/common all
make[1]: Entering directory `/soft/postgresql-14.6/src/common'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/soft/postgresql-14.6/src/common'
make -C ../../../contrib/spi
make[1]: Entering directory `/soft/postgresql-14.6/contrib/spi'
make[1]: Nothing to be done for `all'.
make[1]: Leaving directory `/soft/postgresql-14.6/contrib/spi'
rm -rf '/soft/postgresql-14.6'/tmp_install
/bin/mkdir -p '/soft/postgresql-14.6'/tmp_install/log
make -C '../../..' DESTDIR='/soft/postgresql-14.6'/tmp_install install >'/soft/postgresql-14.6'/tmp_install/log/install.log 2>&1
make -j1 checkprep >>'/soft/postgresql-14.6'/tmp_install/log/install.log 2>&1
PATH="/soft/postgresql-14.6/tmp_install/opt/pgsql/postgresql/bin:/soft/postgresql-14.6/src/test/regress:$PATH" LD_LIBRARY_PATH="/soft/postgresql-14.6/tmp_install/opt/pgsql/postgresql/lib" ../../../src/test/regress/pg_regress --temp-instance=./tmp_check --inputdir=. --bindir= --dlpath=. --max-concurrent-tests=20 --make-testtablespace-dir --schedule=./parallel_schedule
============== creating temporary instance ==============
============== initializing database system ==============
============== starting postmaster ==============
running on port 58086 with PID 39605
============== creating database "regression" ==============
CREATE DATABASE
ALTER DATABASE
============== running regression test queries ==============
test tablespace ... ok 339 ms
parallel group (20 tests): boolean char name varchar text int2 oid int4 regproc txid int8 pg_lsn money float4 uuid float8 bit enum numeric rangetypes
boolean ... ok 124 ms
char ... ok 139 ms
name ... ok 205 ms
varchar ... ok 208 ms
text ... ok 211 ms
int2 ... ok 195 ms
int4 ... ok 255 ms
int8 ... ok 319 ms
oid ... ok 207 ms
float4 ... ok 335 ms
float8 ... ok 396 ms
bit ... ok 421 ms
numeric ... ok 705 ms
txid ... ok 283 ms
uuid ... ok 343 ms
enum ... ok 451 ms
money ... ok 323 ms
rangetypes ... ok 782 ms
pg_lsn ... ok 306 ms
regproc ... ok 272 ms
parallel group (20 tests): line circle lseg timetz macaddr8 time path macaddr point numerology interval create_function_0 date inet timestamp strings timestamptz multirangetypes box polygon
strings ... ok 747 ms
numerology ... ok 386 ms
point ... ok 366 ms
lseg ... ok 291 ms
line ... ok 279 ms
box ... ok 1025 ms
path ... ok 352 ms
polygon ... ok 1052 ms
circle ... ok 281 ms
date ... ok 451 ms
time ... ok 346 ms
timetz ... ok 298 ms
timestamp ... ok 707 ms
timestamptz ... ok 796 ms
interval ... ok 432 ms
inet ... ok 471 ms
macaddr ... ok 355 ms
macaddr8 ... ok 320 ms
multirangetypes ... ok 1009 ms
create_function_0 ... ok 440 ms
parallel group (12 tests): unicode comments xid tstypes misc_sanity expressions horology mvcc type_sanity geometry opr_sanity regex
geometry ... ok 558 ms
horology ... ok 390 ms
tstypes ... ok 270 ms
regex ... ok 1226 ms
type_sanity ... ok 532 ms
opr_sanity ... ok 1094 ms
misc_sanity ... ok 316 ms
comments ... ok 140 ms
expressions ... ok 368 ms
unicode ... ok 126 ms
xid ... ok 233 ms
mvcc ... ok 400 ms
test create_function_1 ... ok 17 ms
test create_type ... ok 27 ms
test create_table ... ok 277 ms
test create_function_2 ... ok 11 ms
parallel group (5 tests): copyselect copydml insert_conflict insert copy
copy ... ok 701 ms
copyselect ... ok 89 ms
copydml ... ok 105 ms
insert ... ok 580 ms
insert_conflict ... ok 313 ms
parallel group (3 tests): create_operator create_procedure create_misc
create_misc ... ok 137 ms
create_operator ... ok 50 ms
create_procedure ... ok 101 ms
parallel group (5 tests): index_including create_view index_including_gist create_index_spgist create_index
create_index ... ok 1477 ms
create_index_spgist ... ok 1218 ms
create_view ... ok 814 ms
index_including ... ok 619 ms
index_including_gist ... ok 1088 ms
parallel group (16 tests): create_cast errors roleattributes hash_func create_aggregate drop_if_exists select typed_table create_am create_function_3 constraints vacuum infinite_recurse inherit updatable_views triggers
create_aggregate ... ok 322 ms
create_function_3 ... ok 791 ms
create_cast ... ok 165 ms
constraints ... ok 1043 ms
triggers ... ok 2391 ms
select ... ok 617 ms
inherit ... ok 2165 ms
typed_table ... ok 704 ms
vacuum ... ok 1862 ms
drop_if_exists ... ok 515 ms
updatable_views ... ok 2229 ms
roleattributes ... ok 244 ms
create_am ... ok 710 ms
hash_func ... ok 287 ms
errors ... ok 198 ms
infinite_recurse ... ok 1915 ms
test sanity_check ... ok 173 ms
parallel group (20 tests): select_distinct_on select_having delete random namespace select_implicit case select_into prepared_xacts transactions portals select_distinct union subselect arrays hash_index update join aggregates btree_index
select_into ... ok 594 ms
select_distinct ... ok 971 ms
select_distinct_on ... ok 169 ms
select_implicit ... ok 385 ms
select_having ... ok 223 ms
subselect ... ok 1329 ms
union ... ok 1248 ms
case ... ok 448 ms
join ... ok 2229 ms
aggregates ... ok 2252 ms
transactions ... ok 823 ms
random ... ok 262 ms
portals ... ok 951 ms
arrays ... ok 1409 ms
btree_index ... ok 2692 ms
hash_index ... ok 1538 ms
update ... ok 1802 ms
delete ... ok 224 ms
namespace ... ok 314 ms
prepared_xacts ... ok 781 ms
parallel group (20 tests): init_privs drop_operator security_label lock tablesample object_address collate replica_identity password identity groupingsets matview generated rowsecurity spgist gin gist join_hash brin privileges
brin ... ok 4668 ms
gin ... ok 3615 ms
gist ... ok 4239 ms
spgist ... ok 3344 ms
privileges ... ok 4929 ms
init_privs ... ok 161 ms
security_label ... ok 296 ms
collate ... ok 1253 ms
matview ... ok 2410 ms
lock ... ok 500 ms
replica_identity ... ok 1307 ms
rowsecurity ... ok 3026 ms
object_address ... ok 819 ms
tablesample ... ok 649 ms
groupingsets ... ok 2192 ms
drop_operator ... ok 259 ms
password ... ok 1840 ms
identity ... ok 1852 ms
generated ... ok 2659 ms
join_hash ... ok 4649 ms
parallel group (2 tests): brin_bloom brin_multi
brin_bloom ... ok 166 ms
brin_multi ... ok 248 ms
parallel group (14 tests): collate.icu.utf8 async dbsize alter_operator tidrangescan tsrf tidscan tid sysviews misc_functions alter_generic misc incremental_sort create_table_like
create_table_like ... ok 694 ms
alter_generic ... ok 522 ms
alter_operator ... ok 180 ms
misc ... ok 570 ms
async ... ok 113 ms
dbsize ... ok 132 ms
misc_functions ... ok 478 ms
sysviews ... ok 473 ms
tsrf ... ok 272 ms
tid ... ok 323 ms
tidscan ... ok 311 ms
tidrangescan ... ok 249 ms
collate.icu.utf8 ... ok 78 ms
incremental_sort ... ok 621 ms
parallel group (6 tests): amutils psql_crosstab collate.linux.utf8 psql rules stats_ext
rules ... ok 871 ms
psql ... ok 832 ms
psql_crosstab ... ok 124 ms
amutils ... ok 87 ms
stats_ext ... ok 1926 ms
collate.linux.utf8 ... ok 433 ms
test select_parallel ... ok 1339 ms
test write_parallel ... ok 199 ms
test vacuum_parallel ... ok 95 ms
parallel group (2 tests): subscription publication
publication ... ok 82 ms
subscription ... ok 59 ms
parallel group (17 tests): portals_p2 advisory_lock combocid xmlmap guc functional_deps tsdicts equivclass dependency select_views window bitmapops cluster tsearch indirect_toast foreign_data foreign_key
select_views ... ok 944 ms
portals_p2 ... ok 212 ms
foreign_key ... ok 2079 ms
cluster ... ok 1448 ms
dependency ... ok 734 ms
guc ... ok 517 ms
bitmapops ... ok 1419 ms
combocid ... ok 263 ms
tsearch ... ok 1463 ms
tsdicts ... ok 562 ms
foreign_data ... ok 1794 ms
window ... ok 1251 ms
xmlmap ... ok 320 ms
functional_deps ... ok 543 ms
advisory_lock ... ok 223 ms
indirect_toast ... ok 1540 ms
equivclass ... ok 568 ms
parallel group (6 tests): jsonpath_encoding json_encoding jsonpath jsonb_jsonpath json jsonb
json ... ok 370 ms
jsonb ... ok 492 ms
json_encoding ... ok 126 ms
jsonpath ... ok 200 ms
jsonpath_encoding ... ok 73 ms
jsonb_jsonpath ... ok 307 ms
parallel group (18 tests): prepare returning xml limit plancache conversion copy2 sequence temp polymorphism rowtypes with truncate rangefuncs domain largeobject plpgsql alter_table
plancache ... ok 854 ms
limit ... ok 833 ms
plpgsql ... ok 2201 ms
copy2 ... ok 1095 ms
temp ... ok 1307 ms
domain ... ok 1661 ms
rangefuncs ... ok 1607 ms
prepare ... ok 390 ms
conversion ... ok 876 ms
truncate ... ok 1564 ms
alter_table ... ok 2642 ms
sequence ... ok 1268 ms
polymorphism ... ok 1375 ms
rowtypes ... ok 1400 ms
returning ... ok 519 ms
largeobject ... ok 1670 ms
with ... ok 1464 ms
xml ... ok 725 ms
parallel group (11 tests): hash_part reloptions explain partition_info memoize compression indexing partition_join partition_aggregate partition_prune tuplesort
partition_join ... ok 3211 ms
partition_prune ... ok 3597 ms
reloptions ... ok 349 ms
hash_part ... ok 207 ms
indexing ... ok 2869 ms
partition_aggregate ... ok 3264 ms
partition_info ... ok 375 ms
tuplesort ... ok 3811 ms
explain ... ok 366 ms
compression ... ok 736 ms
memoize ... ok 611 ms
parallel group (2 tests): event_trigger oidjoins
event_trigger ... ok 182 ms
oidjoins ... ok 250 ms
test fast_default ... ok 249 ms
test stats ... ok 193 ms
============== shutting down postmaster ==============
============== removing temporary instance =====================================
All 210 tests passed.
=======================
十六、编写自定义回归测试
示例1、为一个简单的函数add_numbers编写回归测试
1. 定义函数
postgres=# CREATE OR REPLACE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$ BEGIN RETURN a + b; END; $$ LANGUAGE plpgsql;
CREATE FUNCTION
CREATE OR REPLACE FUNCTION add_numbers(a INT, b INT) RETURNS INT AS $$
BEGIN
RETURN a + b;
END;
$$ LANGUAGE plpgsql;
2. 创建测试文件
- 在
src/test/regress/sql
目录下创建 SQL 测试脚本 (add_numbers.sql
):
[postgres@admin sql]$ vi add_numbers.sql
[postgres@admin sql]$ cat add_numbers.sql
-- add_numbers.sql
SELECT add_numbers(3, 4);
SELECT add_numbers(-1, 5);
-- add_numbers.sql
SELECT add_numbers(3, 4);
SELECT add_numbers(-1, 5);
- 在
src/test/regress/expected
目录下创建预期输出文件 (add_numbers.out
):
[postgres@admin expected]$ vi add_numbers.out
[postgres@admin expected]$ cat add_numbers.out
-- add_numbers.out
add_numbers
--------------
7
4
(2 rows)
-- add_numbers.out
add_numbers
--------------
7
4
(2 rows)
3. 更新测试计划
- 编辑
src/test/regress/parallel_schedule
文件,添加新的测试条目:
test: add_numbers
[postgres@admin regress]$ cat parallel_schedule
# ----------
# src/test/regress/parallel_schedule...
# run stats by itself because its delay may be insufficient under heavy load
test: statstese: add_numbers(有错误)
4. 运行测试
进入回归测试目录并运行测试:
cd src/test/regress
make check
5. 分析测试结果
pg_regress
将生成一个测试报告,显示每个测试的结果。如果所有测试通过,报告将显示all tests passed
。如果有测试失败,报告将显示失败的测试文件和差异,需要开发者进一步检查和修正代码。(如下面出现拼写错误,“tese” 应该改为 “test”。)
test fast_default ... ok 137 ms
test stats ... ok 183 mssyntax error in schedule file "./parallel_schedule" line 135: tese: add_numbers
- 在
psql
中,使用以下命令查看当前连接的数据库:\conninfo
- 确认函数是否已成功创建:
\df add_numbers