PostgreSQL 9.4 支持 background workers 后台进程动态注册,启动,停止, 这块工作是并行查询的基础, 从而实现大查询可以分布到多 cpu 上; 但是目前并不支持并行查询,只是支持动态注册,启动,停止后台进程,动态的意思是可以在数据库启动以后动态加载,不需要重启,下面简单演示。
手册中的说明
Allow background workers to be dynamically registered, started and terminated
创建 Worker_Spi 模块
1 |
[pg94@db1 ~]$ psql francs |
备注: worker_spi 模块现在可以以 extension 方式创建了, 创建后会生成 worker_spi_launch 函数,此函数用来动态注册后台进程。
注册后台进程
1 |
francs=# select worker_spi_launch(1); |
备注:函数返回的是后台进程号,可以从下面的输出验证。
验证后台进程
查看后台进程:
1 |
[pg94@db1 pg_root]$ ps -ef | grep bgworker |
备注: 现在可以看到两个 bgworker 进程,想试下是否支持并行查询,编写一个代码断,让它不停地运行。
测试代码断
1 |
do $$ |
备注:开启一个会话执行上面的代码断.
观察数据库 cpu 使用情况,top 输出如下:
备注:结果还是在用一个 cpu 核, 而没有将负载分担到其它 cpu 核上,这里纯属 yy。
调高主库的 max_worker_processes 设置后,从库的日志如下
1 |
2014-05-22 12:58:33.176 PDT,,,6547,,537e569c.1993,7,,2014-05-22 12:57:16 PDT,1/0,0,FATAL,22023,"hot standby is not possible because max_worker_processes = 10 is a lower setting than on the master server (its value was 12)",,,,,"xlog redo parameter change: max_connections=500 max_worker_processes=12 max_prepared_xacts=0 max_locks_per_xact=64 wal_level=hot_standby",,,,"" |
备注:将主库的 max_worker_processes 调整成 12 并重启主库,之后从库宕机并报以上错误,解决方法: 这时可能需要重做从库了。
调低从库的 max_worker_processes 设置后,从库重启的日志如下
1 |
2014-05-22 12:51:22.027 PDT,,,6443,,537e553a.192b,3,,2014-05-22 12:51:22 PDT,,0,FATAL,22023,"hot standby is not possible because max_worker_proces |
备注: 将从库的 max_worker_processes调整成 9 并重启从库,这时从库日志报以上错误; 解决方法: 只需要将从库的这个参数值调成比主库大或者相等,之后重启从库即可。
注意事项
- 后台进程的最大数由参数 max_worker_processes 限制,默认值为 8,修改后需重启生效。
- 流复制环境从库的 max_worker_processes 设置值必须大于或等于主库设置的值,否则从库不可查询,下面分别针对两种情形做了测试: 初始状态主库和从库的 max_worker_processes 参数值设置成 10.。
关于 max_worker_processes (integer) 参数
Sets the maximum number of background processes that the system can support. This parameter can only be set at server start. When running a standby server, you must set this parameter to the same or higher value than on the master server. Otherwise, queries will not be allowed in the standby server.
参考
原创文章,作者:3628473679,如若转载,请注明出处:https://blog.ytso.com/tech/bigdata/238061.html