2.idle_in_transaction_session_timeout:
用来控制事务执行时长,自动查杀超过指定时间的idle in transaction空闲事务连接,用于清理应用代码中忘记关闭已开启的事务,或者系统中存在僵死进程等,单位是ms,默认值是0,不开启。注意:该参数对idle正常连接无影响。
查看:
postgres=# SELECT name, setting FROM pg_settings WHERE name LIKE '%tcp%';
name | setting
-------------------------+---------
tcp_keepalives_count | 9
tcp_keepalives_idle | 7200
tcp_keepalives_interval | 75
tcp_user_timeout | 0
(4 rows)
在PostgreSQL中,也有相关的几个参数用于释放无法继续通讯的空闲连接,默认配置取决于操作系统
postgres=# select name,setting from pg_settings where name like 'tcp%';
name | setting
-------------------------+---------
tcp_keepalives_count | 0
tcp_keepalives_idle | 0
tcp_keepalives_interval | 0
tcp_user_timeout | 0
(4 rows)
如果通信一方突然崩溃,无法通知对方,此时另一方能做的就只有傻傻等待了,所以引入了keepalive机制,快速感知失败。
但是如果服务端正在忙着处理一个长查询,并不会立马注意到死连接直至查询结束,并且尝试给客户端发送执行结果
在14中引入了client_connection_check_interval参数来解决这个问题,每隔一段时间检查客户端是否已离线,如果离线则快速结束未完成的查询,防止客户端已离线而数据库继续运行未完成的查询。
unix和tcp/ip区别
现象:
$ tail postgresql.auto.conf
Do not edit this file manually!
It will be overwritten by the ALTER SYSTEM command.
port =15432
listen_addresses = '*'
logging_collector = on
archive_command = 'cp %p /home/arch/%f'
shared_preload_libraries = 'pg stat_statements'
tcp_keepalives_idle= 3
tcp_keepalives_interval= 1
tcp_keepalives_count= 1
$ psql
psq1(14.4)
Type "help" for help.
postgres=# show tcp_keepalives idle;
tcp_keepalives_idle
--------------------
0
postgres=i show tcp_keepalives_interval;
tcp_keepalives_interval
--------------------
0
postgres=t show tcp_keepalives_count;
tcp_keepalives_count
--------------------
0
两种方式要以上面的查询值为准:
[postgres@jeromebase ~]$ psql -h 127.0.0.1
psql (16.6)
Type "help" for help.
postgres=# SELECT name, setting FROM pg_settings WHERE name LIKE '%tcp%';
name | setting
-------------------------+---------
tcp_keepalives_count | 9
tcp_keepalives_idle | 7200
tcp_keepalives_interval | 75
tcp_user_timeout | 0
(4 rows)
[postgres@jeromebase ~]$ psql
psql (16.6)
Type "help" for help.
postgres=# SELECT name, setting FROM pg_settings WHERE name LIKE '%tcp%';
name | setting
-------------------------+---------
tcp_keepalives_count | 0
tcp_keepalives_idle | 0
tcp_keepalives_interval | 0
tcp_user_timeout | 0
(4 rows)
为什么数据库里显式为0?官网说明:
[postgres@Nick ~]$ psql -h 127.0.0.1 -U testuser -d postgres
psql (18beta1)
Type "help" for help.
postgres=> set client_min_messages to log;
SET
postgres=> set tcp_keepalives_idle to 32768;
LOG: setsockopt(TCP_KEEPIDLE) failed: Invalid argument
SET
postgres=> set tcp_keepalives_interval to 32768;
LOG: setsockopt(TCP_KEEPINTVL) failed: Invalid argument
SET
postgres=> set tcp_keepalives_count to 128;
LOG: setsockopt(TCP_KEEPCNT) failed: Invalid argument
SET
postgres=> set tcp_keepalives_idle to 32767;
SET
postgres=> set tcp_keepalives_interval to 32767;
SET
postgres=> set tcp_keepalives_count to 127;
SET
postgres=>
但是PG官方给定的这几个keepalive参数最大值为2147483647(int类型),这是为什么?
postgres=> select name,min_val,max_val from pg_settings where name like '%keepalives%';
name | min_val | max_val
-------------------------+---------+------------
tcp_keepalives_count | 0 | 2147483647
tcp_keepalives_idle | 0 | 2147483647
tcp_keepalives_interval | 0 | 2147483647
总结
parameter min_val max_val
tcp_keepalives_count 0 127
tcp_keepalives_idle 0 32767
tcp_keepalives_interval 0 32767