1.配置参数的更改
1.1.增加参数
| 参数名称 | 描述 | 默认值 |
|---|---|---|
| enable_gathermerge | Enable execution plan Gather Merge (user) | on |
| max_parallel_workers | Maximum number of parallel worker process (user) | 8 |
| max_sync_workers_per_subscription | Maximum number of synchronous workers for SUBSCRIPTION (sighup) | 2 |
| wal_consistency_checking | Check the consistency of WAL on the standby instance (superuser) | - |
| max_logical_replication_workers | Maximum number of Logical Replication worker process (postmaster) | 4 |
| max_pred_locks_per_relation | Maximum number of pages that can be Predicate-Lock before locking the entire relation (sighup) | -2 |
| max_pred_locks_per_page | Maximum number of records that can be Predicate-Lock before locking the entire page (sighup) | 2 |
| min_parallel_table_scan_size | Minimum table size at which Parallel table scan are considered (user) | 8MB |
| min_parallel_index_scan_size | Minimum table size at which Parallel index scan are considered (user) | 512kB |
- max_parallel_workers
指定可以在实例中并发运行的并行查询工作进程的最大数量。默认值为 8。在旧版本中,max_worker_processes 参数是上限。如果此值设置为 0,则并行查询无效。
- max_logical_replication_workers
指定要为每个 SUBSCRIPTION 启动的 Logical Replication Worker 进程的最大值。即使该参数的值小于要求的值,CREATE SUBSCRIPTION 语句也不会失败。复制开始时会定期输出以下日志。
WARNING: out of logical replication worker slots
HINT: You might need to increase max_logical_replication_workers.
- wal_consistency_checking
此参数用于复制环境中 WAL 重新执行程序的错误检查。对于参数,请使用逗号 (,) 分隔符指定要检查的对象类型。以下值可用:all、hash、heap、heap 2、btree、gin、gist、sequence、spgist、brin、generic。
- max_pred_locks_per_page
指定要转换为页锁的最大 Tuples 锁数。
- max_pred_locks_per_relation
指定要转换为关系锁的最大页锁数。
1.2.调整参数
| 参数名称 | 调整 |
|---|---|
| ssl | The value of the context column has been changed to sighup |
| ssl_ca_file | The value of the context column has been changed to sighup |
| ssl_cert_file | The value of the context column has been changed to sighup |
| ssl_ciphers | The value of the context column has been changed to sighup |
| ssl_crl_file | The value of the context column has been changed to sighup |
| ssl_ecdh_curve | The value of the context column has been changed to sighup |
| ssl_key_file | The value of the context column has been changed to sighup |
| ssl_prefer_server_ciphers | The value of the context column has been changed to sighup |
| bgwriter_lru_maxpages | The value of max_val column was changed to INT_MAX / 2 |
| archive_timeout | The value of the short_desc column has changed |
| server_version_num | The value of max_val / min_val column was changed to 100021 |
| password_encryption | The value of the vartype was changed to enum. “md5” or “scram-sha-256” can specified. “on” is a alias for “md5” |
| max_wal_size | The value of the unit column has been changed to 1MB |
| min_wal_size | The value of the unit column has been changed to 1MB |
1.3.参数默认值改变
| 参数名称 | PostgreSQL 9.6 | PostgreSQL 10 |
|---|---|---|
| hot_standby | off | on |
| log_line_prefix | ‘’ | %m [%p] |
| max_parallel_workers_per_gather | 0 | 2 |
| max_replication_slots | 0 | 10 |
| max_wal_senders | 0 | 10 |
| password_encryption | on | md5 |
| server_version | 9.6.3 | 10.21 |
| server_version_num | 90603 | 100021 |
| wal_level | minimal | replica |
| log_directory | pg_log | log |
1.4.移除的参数
| 参数名称 | 替代值 |
|---|---|
| min_parallel_relation_size | Changed to min_parallel_table_scan_size |
| sql_inheritance | None (same as ‘on’) |
1.5.认证方式新功能
对 pg_hba.conf 文件进行了以下更改。
- 指定 RADIUS 服务器
RADIUS 身份验证所需的 RADIUS 服务器规格从radiusserver 改为 radiusservers。可以指定多个服务器,以逗号分隔。
- 添加了 SCRAM 身份验证
可以在 pg_hba.conf 中为身份验证方法指定 Scram-sha-256。这是一个RFC 5802 和 7677 中指定的 SCRAM-SHA-256 的实施。Scram-sha-256 也可以是为配置参数 password_encryption 指定。
1.6.本地认证默认值
pg_hba.conf 文件中包含的与复制相关的默认值已更改。默认情况下,本地连接设置为 “trust” 设置。
# Allow replication connections from localhost, by a user with the
# replication privilege.
local replication all trust
host replication all 127.0.0.1/32 trust
host replication all ::1/128 trust
1.7.其他参数调整
与 “Point In Time Recovery” 相关的参数 recovery_target_lsn 已添加到 recovery.conf 文件中。对于此参数,请指定恢复完成 LSN。
2.实用程序改变
2.1.psql
- \d command
更改了 d 命令输出的表信息格式。通常使用的 “Modifier” 列已分为 Collation、Nullable 和 Default。
PostgreSQL 9.6
postgres=> \d data1
Table "public.data1"
Column | Type | Modifiers
--------+-----------------------+-----------
c1 | numeric | default 1
c2 | character varying(10) | not null
PostgreSQL 10
postgres=> \d data1
Table "public.data1"
Column | Type | Collation | Nullable | Default
--------+-----------------------+-----------+----------+---------
c1 | numeric | default 1| | | 1
c2 | character varying(10) | | not null |
- \timing 命令的其他信息
timing 命令控制 SQL 语句执行时间的输出。在新版本中,执行时间输出中添加了易于理解的时间格式。当 SQL 执行时间小于 1 秒时,输出格式与旧版本相同。
postgres=> \timing
Timing is on.
postgres=> INSERT INTO data1 values (generate_series(1, 10000000)) ;
INSERT 0 10000000
Time: 61086.012 ms (01:01.086)
- \gx command
\gx 命令以扩展格式重新运行最近执行的 SQL 语句。
postgres=> SELECT * FROM data1 ;
c1 | c2 ----+------
1 | data
(1 row)
postgres=> \gx -[ RECORD 1 ]
c1 | 1
c2 | data
- \set command
postgres=# \set
AUTOCOMMIT = 'on'
COMP_KEYWORD_CASE = 'preserve-upper'
DBNAME = 'postgres'
ECHO = 'none'
ECHO_HIDDEN = 'off'
...
- \if, \elif, \else, \endif command
现在可以在 psql 命令中执行条件分支。可以在 if、else 和 endif 之间执行条件分支,并且两者之间的命令被视为一个块。
对于 If command 和 elif command,必须指定可以确定 True 或 False 的参数。也可以嵌套条件语句。
postgres=# create table customer (customer_id int);
CREATE TABLE
postgres=# create table employee (employee_id int);
CREATE TABLE
postgres=# insert into customer values(123);
INSERT 0 1
postgres=# SELECT
EXISTS(SELECT 1 FROM customer WHERE customer_id=123) AS is_customer,
EXISTS(SELECT 1 FROM employee WHERE employee_id=456) AS is_employee \gset
\if :is_customer
SELECT * FROM customer WHERE customer_id = 123 ;
\elif :is_employee
SELECT * FROM employee WHERE employee_id = 456 ;
\endif
2.2.pg_ctl
pg_ctl 命令中新增了以下函数。
- promote等待
pg_ctl 命令可以指定选项 ‘-w’ 以等待备用实例被提升。之前版本需要参考 trigger 文件来确认 promote 完成。
- 为选项添加了别名
--wait 和 --no-wait 可以用作选项 -w 和 -W 的别名。此外,--options 可以是
用于 “-o” 指定选项。
- 等待启动 (-w) 默认
默认情况下,所有操作都已变为等待操作完成 (--wait)。之前版本实例启动和提升处理的默认行为未等待操作完成。
2.3.pg_basebackup
- 更改默认模式
备份 WAL 的默认方式是 Stream。说明默认备份的时候同时进行WAL的备份。
- 停止使用 -x 选项
-x 选项(--xlog 选项)已弃用。
- 更改 -X 选项
对于 -X 选项,值 “none” 表示事务日志不包含在备份中。长选项名称已从 --xlog-method更改为 --wal-method。
- 更改 --xlogdir 选项
选项名称已从 --xlogdir 更改为 --waldir。
- -Ft 选项和 -Xstream 选项组合
将备份数据输出到 tar 文件 -Ft 的选项和 -Xstream 选项现在可以同时使用
时间。在这种情况下pg_wal.tar,事务日志存储在
output 的 -D 选项。
PostgreSQL 9.6
$ pg_basebackup -D backup -v --format=t ---xlog-method=stream
pg_basebackup: WAL streaming can only be used in plain mode
Try "pg_basebackup --help" for more information.
PostgreSQL 10
$ pg_basebackup -D backup -v -Ft -Xstream
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
…
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
$ ls backup/
base.tar pg_wal.tar
$ tar tvf back1/pg_wal.tar -rw------- postgres/postgres
$ -rw------- postgres/postgres 16777216 2024-12-02 16:36 0000001000000000000002F
0 2024-12-02 16:36
archive_status/000000010000000000000
- 临时复制槽
如果未指定槽名称 (-S)(并且未指定 --no-slot),则使用临时复制槽。以下是log_replication_commands参数设置为 “on” 时的日志。临时槽已创建以 pg_basebackup_ 开头的名称。
LOG: received replication command: IDENTIFY_SYSTEM
LOG: received replication command: BASE_BACKUP LABEL 'pg_basebackup
base backup'
NOWAIT
LOG: received replication command: IDENTIFY_SYSTEM
LOG: received replication command: CREATE_REPLICATION_SLOT
"pg_basebackup_12889" TEMPORARY PHYSICAL RESERVE_WAL
LOG: received replication command: START_REPLICATION SLOT
"pg_basebackup_12889" 0/49000000 TIMELINE 1
如果复制槽已满,则创建复制槽将失败pg_basebackup因此命令将失败。请检查参数max_replication_slots 是否有可用空间。
$ pg_basebackup -D backup2/
pg_basebackup: could not create temporary replication slot "pg_basebackup_64": ERROR: all replication slots are in use
HINT: Free one or increase max_replication_slots.
pg_basebackup: child process exited with exit code 1
pg_basebackup: removing contents of data directory "backup2/"
$ echo $?
1
- 出错时清理
当 pg_basebackup 命令期间发生错误或接收到信号时,将删除 -D 参数指定目录中的文件。如果不需要删除操作,请可以指定参数 --no-clean(或 -n)。
- --verbose 模式
指定参数 --verbose(或 -v)时,将显示更详细的信息。
$ pg_basebackup -D backup2 --verbose
pg_basebackup: initiating base backup, waiting for checkpoint to complete
pg_basebackup: checkpoint completed
pg_basebackup: write-ahead log start point: 0/35000028 on timeline 1
pg_basebackup: starting background WAL receiver
pg_basebackup: write-ahead log end point: 0/35000130
$
pg_basebackup: waiting for background process to finish streaming ...
pg_basebackup: base backup completed
2.4.pg_dump
新增了下面的option
- -B (--no-blobs)
Exclude large objects
- --no-subscriptions
Exclude SUBSCRIPTION objects used for Logical Replication
- --no-publications
Exclude PUBLICATION objects used for Logical Replication
- --no-sync
Does not execute sync system call after writing file. By default, the sync call is executed to
ensure a reliable write operation.
2.5.pg_dumpall
新增了下面的option
- --no-sync
Does not execute sync system call after writing file. By default, the sync call is executed to
ensure a reliable write operation.
- --no-role-passwords
Does not dump role’s password.
- --no-subscriptions
Exclude SUBSCRIPTION objects used for Logical Replication
- --no-publications
Exclude PUBLICATION objects used for Logical Replication
2.6.pg_recvlogical
添加了 -E 选项(--endpos 选项),用于在收到指定的 LSN 后终止程序
$ pg_recvlogical -d postgres --endpos=0/3016A20 --slot slot1 --start --file=output_file
2.7.pgbench
添加了 “--log-prefix” 参数来更改日志文件的前缀字符串。默认值与以前的版本一样为 “pgbench_log”。除了上述内容外,还有一些新功能是提供给 pgbench 命令,但尚未进行验证。
$ pgbench --log-prefix benchmark_2024_test --log --client=10 --transactions=1000 -s 10 postgres
scale option ignored, using count from pgbench_branches table (10)
starting vacuum...end.
transaction type: <builtin: TPC-B (sort of)>
scaling factor: 10
query mode: simple
number of clients: 10
number of threads: 1
number of transactions per client: 1000
number of transactions actually processed: 10000/10000
latency average = 3.237 ms
tps = 3088.974257 (including connections establishing)
tps = 3091.521662 (excluding connections establishing)
$ ls
benchmark_2024_test.102
$ cat benchmark_2024_test.102
1 0 5895 0 1733294707 112264
2 0 6199 0 1733294707 112587
1 1 1586 0 1733294707 113849
0 0 10027 0 1733294707 116361
...
2.8.initdb
“--noclean” 和 “--nosync” 选项已更改为 “--no-clean” 和 “--no-sync” 选项。
2.9.pg_receivexlog
命令的名称已更改为 pg_receivewal。现在可以指定 --compress 参数来压缩输出 WAL 文件。压缩比可以指定为 0 到 9。为了使用此功能,必须在安装了 libz 库的环境中构建。
$ pg_receivewal -D backup2/ --compress=9 &
2.10.pg_restore
- -N (–exclude-schema)
To specify the name of the schema not to be restored has been added.
- --no-subscriptions
Exclude SUBSCRIPTION objects used for Logical Replication
- --no-publications
Exclude PUBLICATION objects used for Logical Replication
2.11.pg_upgrade
在内部,它将表和序列视为单独的对象。
2.12.createuser
--unencrypted" option (-N option) has been deprecated.
2.13.createlang / droplang
createlang 命令、droplang 命令已弃用。
3.contrib 模块
本节描述了 contrib 模块的新功能。
3.1.postgres_fdw
- 聚合操作可以下推到源端
PostgreSQL 9.6
postgres=# explain analyze select count(*) from table2 ;
QUERY PLAN
----------------------------------------------------------------------------------
Aggregate (cost=220.92..220.93 rows=1 width=0) (actual time=13778.312..13778.313 rows=1
loops=1)-> Foreign Scan on table2 (cost=100.00..212.39 rows=3413 width=0) (actual
time=2.424..13329.284 rows=1000000 loops=1)
Planning time: 0.106 ms
Execution time: 13780.712 ms。
(4 行记录)
PostgreSQL 10
postgres=# explain analyze select count(*) from table2 ;
QUERY PLAN
-------------------------------------------------------------------------------------------------
Foreign Scan (cost=108.53..152.69 rows=1 width=8) (actual time=112.222..112.225 rows=1
loops=1)
Relations: Aggregate on (public.table2)
Planning time: 0.153 ms
Execution time: 115.023 ms
(4 行记录)
上述 SQL 语句在 FOREIGN SERVER 处转换为以下 SQL 语句。
参数设置为 log_statement = 'all'
statement: START TRANSACTION ISOLATION LEVEL REPEATABLE READ
execute <unnamed>: DECLARE c1 CURSOR FOR
select count(*) from table2
statement: FETCH 100 FROM c1
statement: CLOSE c1
statement: COMMIT TRANSACTION
- FULL JOIN下推源端
postgres=> CREATE FOREIGN TABLE remote_employees (
id INT,
name VARCHAR(100),
salary NUMERIC
)
SERVER remote_server
OPTIONS (schema_name 'public', table_name 'employees');
postgres=> CREATE FOREIGN TABLE remote_employees1 (
id INT,
name VARCHAR(100),
salary NUMERIC
)
SERVER remote_server
OPTIONS (schema_name 'public', table_name 'employees1');
postgres=> select * from remote_employees1;
id | name | salary
----+-------------+--------
1 | John Doe | 55000
2 | Jane Smith | 60000
3 | Alice Brown | 48000
(3 rows)
postgres=> select * from remote_employees;
id | name | salary
----+-------------+--------
1 | John Doe | 55000
2 | Jane Smith | 60000
3 | Alice Brown | 48000
4 | John Doe | 55000
5 | Jane Smith | 60000
6 | Alice Brown | 48000
7 | John Doe | 55000
8 | Jane Smith | 60000
9 | Alice Brown | 48000
10 | John Doe | 55000
11 | Jane Smith | 60000
12 | Alice Brown | 48000
(12 rows)
postgres=> explain (analyze,verbose) SELECT * FROM (SELECT * FROM remote_employees1 WHERE
id < 3) r1 FULL JOIN (SELECT * FROM remote_employees WHERE id < 9) r2 ON (TRUE) LIMIT 10 ;
QUERY PLAN
--------------------------------------------------------------------------------------------------------------------------------------------------
Limit (cost=100.00..100.23 rows=10 width=508) (actual time=1.398..1.400 rows=10 loops=1)
Output: remote_employees1.id, remote_employees1.name, remote_employees1.salary, remote_employees.id, remote_employees.name, remote_employees.sa
lary
-> Foreign Scan (cost=100.00..319.43 rows=9604 width=508) (actual time=1.397..1.398 rows=10 loops=1)
Output: remote_employees1.id, remote_employees1.name, remote_employees1.salary, remote_employees.id, remote_employees.name, remote_employ
ees.salary
Relations: (public.remote_employees1) FULL JOIN (public.remote_employees)
Remote SQL: SELECT s4.c1, s4.c2, s4.c3, s5.c1, s5.c2, s5.c3 FROM ((SELECT id, name, salary FROM public.employees1 WHERE ((id < 3))) s4(c1
, c2, c3) FULL JOIN (SELECT id, name, salary FROM public.employees WHERE ((id < 9))) s5(c1, c2, c3) ON (TRUE))
Planning time: 0.126 ms
Execution time: 2.733 ms
(8 rows)
3.2.file_fdw
添加了用于运行应用程序的 “program” 选项。改为指定 program 选项的 filename 选项中指定文件名。当 FOREIGN TABLE 的 SELECT 语句执行时,将自动执行指定的程序。执行程序的内容标准输出的输出将作为元组返回给应用程序。
postgres=# echo 'echo "1,a"'>1.sh && chmod +x 1.sh
postgres=# CREATE EXTENSION file_fdw ;
CREATE EXTENSION
postgres=# CREATE SERVER fs FOREIGN DATA WRAPPER file_fdw ;
CREATE SERVER
--程序 1.sh 的执行结果可以返回到查询结果中
postgres=# CREATE FOREIGN TABLE tfile1 (id NUMERIC, val VARCHAR(10)) SERVER fs OPTIONS (program '/var/lib/postgresql/1.sh', delimiter ',') ;
CREATE FOREIGN TABLE
postgres=#
postgres=# select * from tfile1;
id | val
----+-----
1 | a
(1 row)
3.3.amcheck
用于检查 BTree 索引一致性的 amcheck 模块已添加到 contrib 中模块。此模块中添加了以下函数。
- bt_index_check (索引 regclass)
检查指定 BTree 索引的完整性
- bt_index_parent_check (索引 regclass)
检查父子索引的一致性
在下面的示例中,对索引 (idx1_check1) 执行 bt_index_check 函数,其中某些数据已损坏。
postgres=# CREATE EXTENSION amcheck ;
CREATE EXTENSION
postgres=# SELECT bt_index_check('idx1_check1') ;
ERROR: item order invariant violated for index "idx1_check1"
DETAIL: Lower index tid=(1,2) (points to heap tid=(0,2)) higher index
tid=(1,3) (points to heap tid=(0,3)) page lsn=0/7EFE4638.
3.4.pageinspect
新增了以下与 Hash Index 对应的函数。
• hash_page_type
• hash_page_stats
• hash_page_items
• hash_metapage_info
• page_checksum
• bt_page_items(IN page bytea)
postgres=# \d test_hash_table
Table "public.test_hash_table"
Column | Type | Collation | Nullable | Default
--------+------------------------+-----------+----------+---------------------------------------------
id | integer | | not null | nextval('test_hash_table_id_seq'::regclass)
name | character varying(100) | | |
Indexes:
"test_hash_table_pkey" PRIMARY KEY, btree (id)
"test_hash_index" hash (id)
postgres=#
postgres=# select hash_page_type(get_raw_page('test_hash_index',0));
hash_page_type
----------------
metapage
(1 row)
postgres=# select hash_page_type(get_raw_page('test_hash_index',1));
hash_page_type
----------------
bucket
(1 row)
postgres=# select hash_page_type(get_raw_page('test_hash_index',2));
hash_page_type
----------------
bucket
(1 row)
postgres=# select hash_page_type(get_raw_page('test_hash_index',3));
hash_page_type
----------------
bitmap
(1 row)
postgres=# select live_items, dead_items from hash_page_stats(get_raw_page('test_hash_index',1));
live_items | dead_items
------------+------------
3 | 0
(1 row)
postgres=# select * from hash_page_items(get_raw_page('test_hash_index',0));
ERROR: page is not a hash bucket or overflow page
postgres=# select * from hash_page_items(get_raw_page('test_hash_index',1));
itemoffset | ctid | data
------------+-------+------------
1 | (0,2) | 1134484726
2 | (0,1) | 2389907270
3 | (0,5) | 2964702588
(3 rows)
postgres=# select * from hash_page_items(get_raw_page('test_hash_index',2));
itemoffset | ctid | data
------------+-------+------------
1 | (0,4) | 3283889963
2 | (0,3) | 4266872727
(2 rows)
postgres=# select * from hash_page_items(get_raw_page('test_hash_index',3));
ERROR: page is not a hash bucket or overflow page
3.5.pgstattuple
• pgstathashindex
提供有关 Hash Index 的信息
postgres=# create extension pgstattuple;
CREATE EXTENSION
postgres=#
postgres=# SELECT * FROM pgstathashindex('test_hash_index') ;
version | bucket_pages | overflow_pages | bitmap_pages | unused_pages | live_items | dead_items | free_percent
---------+--------------+----------------+--------------+--------------+------------+------------+------------------
4 | 2 | 0 | 1 | 0 | 5 | 0 | 99.3866535819431
(1 row)
3.6.btree_gist / btree_gin
现在可以在 UUID 类型列和 ENUM 类型列中创建 GiST 索引。GIN 索引可以在 ENUM 类型的列中创建。
postgres=# CREATE EXTENSION btree_gist ;
CREATE EXTENSION
postgres=# CREATE TYPE type1 AS ENUM ('typ1', 'typ2', 'typ3') ;
CREATE TYPE
postgres=# CREATE TABLE gist1(c1 UUID, c2 type1) ;
CREATE TABLE
postgres=# CREATE INDEX idx1_gist1 ON gist1 USING gist (c1) ;
CREATE INDEX
postgres=# CREATE INDEX idx2_gist1 ON gist1 USING gist (c2) ;
CREATE INDEX
postgres=# select * from gist1;
c1 | c2
----+----
(0 rows)
postgres=# -- 插入示例数据
postgres=# INSERT INTO gist1 (c1, c2) VALUES
postgres-# ('a0eeb238-d3c8-11eb-b8bc-0242ac130003', 'typ1'),
postgres-# ('b1eeed59-d3c8-11eb-b8bc-0242ac130003', 'typ2'),
postgres-# ('c2eeed59-d3c8-11eb-b8bc-0242ac130003', 'typ3');
INSERT 0 3
postgres=# SELECT * FROM gist1;
c1 | c2
--------------------------------------+------
a0eeb238-d3c8-11eb-b8bc-0242ac130003 | typ1
b1eeed59-d3c8-11eb-b8bc-0242ac130003 | typ2
c2eeed59-d3c8-11eb-b8bc-0242ac130003 | typ3
(3 rows)
3.7.pg_stat_statements
SQL 语句存储在 pg_stat_statements 视图中的格式已经发生了变化。之前,WHERE 子句中的字面值通常以问号(?)的形式输出,但现在已被更改为 ${N} 的格式(其中 N = 1, 2, …)
postgres=# create extension pg_stat_statements;
CREATE EXTENSION
postgres=# select queryid,query from pg_stat_statements limit 2;
queryid | query
------------+-------------------------------------------
287764235 | drop extension pg_stat_statements
1675270703 | select * from pg_stat_statements limit $1
(2 rows)
3.7.tsearch2
tsearch2 模块已删除。
参考
《PostgreSQL_10_New_Features_en_20170522-1》
《快速掌握PostgreSQL版本新特性》




