1.修改参数列表
(1)执行计划
enable_nestloop = off #默认为on
enable_seqscan = off #默认为on
enable_indexscan = on
enable_bitmapscan = on
max_connections = 1000 #默认为100
 
(2)内存相关
shared_buffers = 16GB # 默认为128MB
effective_cache_size = 24GB #默认为4GB
work_mem = 32MB
temp_buffers = 32MB
wal_buffers = 512MB
maintenance_work_mem = 2GB
 
(3)autovacuum
autovacuum = on
autovacuum_max_workers = 3 #默认为3
autovacuum_work_mem = 256MB
autovacuum_vacuum_scale_factor = 0.05 #默认为0.2
 
(4)检查点
max_wal_size = 4GB #默认为1GB
min_wal_size = 1GB #默认为80MB
checkpoint_completion_target = 0.9 #默认为0.5
checkpoint_timeout = 30min #默认为5min
 
(5)日志
log_destination = csvlog
log_directory = pg_log
logging_collector = on
log_min_duration_statement = 800
log_rotation_size = 1024MB
log_truncate_on_rotation = on
log_filename = 'xl_log_%a.log'
 
2.参数设置公式

这个shell脚本可以使用shell的关联数组对


  1. #!/bin/bash
  2. #command to execute script: su - postgres -c "./create.sh"
  3. #default value refer to standard server(cpu:6cores,memory:32G,storage:12T)
  4. #value according to the actual situation
  5. #List of parameters to be modified
  6. :<<!
  7. enable_seqscan = off
  8. enable_indexscan = on
  9. enable_bitmapscan = on
  10. max_connections = 1000
  11. shared_buffers = 16GB
  12. effective_cache_size = 24GB
  13. work_mem = 32MB
  14. temp_buffers = 32MB
  15. wal_buffers = 512MB
  16. maintenance_work_mem = 2GB
  17. autovacuum_max_workers = 3
  18. autovacuum_work_mem = 256MB
  19. checkpoint_timeout = 30min
  20. max_wal_size = 4GB
  21. min_wal_size = 1GB
  22. checkpoint_completion_target = 0.9
  23. log_destination = csvlog
  24. log_directory = pg_log
  25. logging_collector = on
  26. log_min_duration_statement = 800
  27. log_rotation_size = 1024MB
  28. log_truncate_on_rotation = on
  29. log_filename = 'xl_log_%a.log'
  30. !
  31. ERROR="\033[41;37m ERROR \033[0m"
  32. INFO="\033[42;37m INFO \033[0m"
  33. WARN="\033[43;37m WARN \033[0m"
  34. COMMON_ERROR="some error happened, specific information please see console output"
  35. # Array of parameters
  36. declare -A parameter_array
  37. parameter_array=([enable_seqscan]=off [enable_indexscan]=on [enable_bitmapscan]=on [max_connections]=1000 [shared_buffers]=16GB
  38. [effective_cache_size]=24GB [work_mem]=32MB [temp_buffers]=32MB [wal_buffers]=512MB [maintenance_work_mem]=2GB
  39. [autovacuum_max_workers]=3 [autovacuum_work_mem]=256MB [checkpoint_timeout]=30min [max_wal_size]=4GB
  40. [min_wal_size]=1GB [checkpoint_completion_target]=0.9 [log_destination]=csvlog [log_directory]=pg_log
  41. [logging_collector]=on [log_min_duration_statement]=800 [log_rotation_size]=1024MB [log_truncate_on_rotation]=on [log_filename]=viid_log_%a.log )
  42. #default value :
  43. pgctl_path=
  44. data_directory=
  45. memory=
  46. # check command exit value, 0 is success
  47. function check_fun(){
  48. status=$?
  49. error=${COMMON_ERROR}
  50. if [[ 0 -ne ${status} ]] ; then
  51. echo -e "${ERROR} ${error}"
  52. exit 1
  53. fi
  54. }
  55. # prepare conditions
  56. function prepare_conditions(){
  57. data_directory=$(psql -qtAX -c "show data_directory" | sed 's/[ ]//g')
  58. check_fun
  59. if [[ ! -d "${data_directory}" ]] ; then
  60. echo -e "${ERROR} database's data directory does not exist"
  61. exit 1
  62. fi
  63. # physical machine environment
  64. memory=$(grep MemTotal /proc/meminfo | awk '{print $2 / 1024 / 1024}' | sed 's/\.[0-9]*//' | tail -n 1)
  65. check_fun
  66. # docker environment
  67. memory_limit=$(($(awk '{print $1}' /sys/fs/cgroup/memory/memory.limit_in_bytes) / 1024 / 1024 /1024))
  68. check_fun
  69. # comparing the two, choose the smaller one.
  70. if [[ "${memory_limit}" -le "${memory}" ]] ; then
  71. memory=${memory_limit}
  72. fi
  73. }
  74. # calculate parameters
  75. function calculate_parameters(){
  76. # 50%*memory
  77. parameter_array[shared_buffers]=$((memory * 1024 / 2))"MB"
  78. # 75%*memory
  79. parameter_array[effective_cache_size]=$((memory * 1024 * 3 / 4 ))"MB"
  80. # <1%
  81. parameter_array[work_mem]=${memory}"MB"
  82. # <1%
  83. parameter_array[temp_buffers]=${memory}"MB"
  84. # 32GB => 512MB
  85. parameter_array[wal_buffers]=$((memory * 16 ))"MB"
  86. # 32GB => 2048MB
  87. parameter_array[maintenance_work_mem]=$((memory * 64 ))"MB"
  88. }
  89. # modify parameters
  90. function modify_parameters(){
  91. # modify parameters by modifying file postgresql.conf:
  92. for parameter in ${!parameter_array[*]}
  93. do
  94. #check whether the parameters have been modified
  95. # PostgreSQL's default parameter configuration example: enable_seqscan = on
  96. # viid's example: enable_seqscan='on'
  97. check_out=$(grep "^${parameter}=" "${data_directory}"/postgresql.conf | grep -v '#' | tail -n 1)
  98. # process sleep
  99. sleep 0.2s
  100. if [[ -z "${check_out}" ]] ; then
  101. echo "${parameter}='${parameter_array[${parameter}]}'" >> "${data_directory}"/postgresql.conf
  102. check_fun
  103. echo -e "${INFO} modify ${parameter} successfully"
  104. else
  105. if [[ "${check_out}" = "${parameter}='${parameter_array[${parameter}]}'" ]] ; then
  106. echo -e "${INFO} ${parameter} is already configured, then skip this step"
  107. else
  108. sed -i s!^"${check_out}"!"${parameter}='${parameter_array[${parameter}]}'"!g "${data_directory}"/postgresql.conf
  109. check_fun
  110. echo -e "${INFO} modify ${parameter} successfully"
  111. fi
  112. fi
  113. done
  114. }
  115. # create directory(pg_log and pg_arch), and set user postgres permission
  116. function create_dir(){
  117. directory_array=("pg_arch" "pg_log")
  118. for directory in ${directory_array[*]};
  119. do
  120. # process sleep
  121. sleep 0.2s
  122. if [[ ! -d "${data_directory}/${directory}" ]] ; then
  123. mkdir -p "${data_directory}"/"${directory}"
  124. echo -e "${INFO} path ${data_directory}/${directory} create successfully"
  125. else
  126. echo -e "${INFO} ${data_directory}/${directory} is already exists, then skip this step"
  127. fi
  128. # set user postgres permission
  129. chown postgres:postgres "${data_directory}"/"${directory}"
  130. done
  131. }
  132. # because the environment is different, need to find the path of the database restart command 'pg_ctl'
  133. function find_cmd(){
  134. result=$(find / -name pg_ctl 2> /dev/null | grep bin/pg_ctl$ | tail -n 1 )
  135. # check whether the path exists
  136. if [[ -z "${result}" ]] ; then
  137. echo -e "${ERROR} database restart command 'pg_ctl' not exists"
  138. echo -e "${ERROR} please check to see if the database is installed or the command directory does not have permission to access it"
  139. exit 1
  140. else
  141. echo -e "${INFO} database restart command 'pg_ctl' path: ${result}"
  142. pgctl_path=${result}
  143. fi
  144. }
  145. # user choose whether to restart or not
  146. function check_restart(){
  147. read -r -p "Is it necessary to restart database immediately?[Enter YES or NO]:" result
  148. if [[ "${result,,}" = "yes" ]] ; then
  149. echo -e "${INFO} start to restart database"
  150. ${pgctl_path} restart -D "${data_directory}" >& /dev/null
  151. if [[ 0 -ne ${status} ]] ; then
  152. echo -e "${ERROR} restart database failed"
  153. exit 12
  154. fi
  155. elif [[ "${result,,}" = "no" ]] ; then
  156. echo -e "${WARN} please restart database manually"
  157. echo -e "${WARN} if you don't restart, database may not be available"
  158. exit 11
  159. else
  160. echo -e "${ERROR} invalid input,please enter again"
  161. check_restart
  162. fi
  163. }
  164. # ******* start *******
  165. # prepare conditions:memory ,data_directory
  166. prepare_conditions
  167. # calculate parameters
  168. calculate_parameters
  169. # modify parameters
  170. modify_parameters
  171. # create directory(pg_log and pg_arch)
  172. create_dir
  173. # the path of the database restart command 'pg_ctl'
  174. find_cmd
  175. # remind user that they need to restart database to take effect
  176. # process sleep
  177. sleep 0.5s
  178. echo -e ""
  179. echo -e "*******************************************************************"
  180. echo -e "* *"
  181. echo -e "* *"
  182. echo -e "* restart database to take effect *"
  183. echo -e "* *"
  184. echo -e "* *"
  185. echo -e "*******************************************************************"
  186. # process sleep
  187. sleep 0.5s
  188. # user choose whether to restart or not
  189. # check_restart
  190. echo -e "${INFO} start to restart database"
  191. ${pgctl_path} restart -D "${data_directory}" >& /dev/null
文章知识点与官方知识档案匹配,可进一步学习相关知识
PostgreSQL技能树首页概览6104 人正在系统学习中

[转帖]PostgreSQL 参数优化设置 32GB内存(推荐) 内存参数 检查点 日志参数 自动初始化参数shell脚本的更多相关文章

  1. vm内核参数优化设置

     http://www.cnblogs.com/wjoyxt/archive/2014/06/08/3777042.html (1)vm.overcommit_memory 执行grep -i com ...

  2. ajax 参数data问题 data中的 参数名 参数值为string 提交到后台后,会自动转换参数名相同的 类型 和 js字符串拼接

    latlng"14.6005238,100.43635419999998"Cusid"accb5c1b-6aef-4f3b-a4eb-d60ea1ca5f54" ...

  3. AI人脸识别SDK接入 — 参数优化篇(虹软)

    引言 使用了虹软公司免费的人脸识别算法,感觉还是很不错的,当然,如果是初次接触的话会对一些接口的参数有些疑问的.这里分享一下我对一些参数的验证结果(这里以windows版本为例,linux.andro ...

  4. 虹软AI 人脸识别SDK接入 — 参数优化篇

    引言 使用了免费的人脸识别算法,感觉还是很不错的,但是初次接触的话会对一些接口的参数有些疑问的.这里分享一下我对一些参数的验证结果(这里以windows版本为例,linux.android基本一样), ...

  5. linux——系统内核参数优化

    vim /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 fs.file-max = 999999 net.ipv4.tcp_max_tw_buckets = ...

  6. 使用服务器参数文件(SPFILE)管理初始化参数

    传统上,Oracle数据库的初始化参数存储在文本初始化参数文件中.为了更好的可管理性,您可以选择在二进制服务器参数文件中维护初始化参数,该文件在数据库启动和关闭期间保持不变.本节介绍服务器参数文件,并 ...

  7. Ubuntu 系统服务器初始化配置、安全加固、内核优化和常用软件安装的Shell脚本分享

    转载自:https://www.bilibili.com/read/cv13875402?spm_id_from=333.999.0.0 描述: 适用于企业内部 Ubuntu 操作服务器初始化.系统安 ...

  8. CentOS7 系统服务器初始化配置、安全加固、内核升级优化常用软件安装的Shell脚本分享

    转载自:https://www.bilibili.com/read/cv13875630?spm_id_from=333.999.0.0 描述: 适用于企业内部 CentOS7 系列操作服务器初始化. ...

  9. javaee学习-servlet初始化参数

    1.需要定义ServletConfig对象来接收servlet配置的初始化参数. 2.当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 会自动将这些初始化参数封装到Ser ...

  10. ORACLE初始化参数文件概述

    ORACLE初始化参数文件概述 在9i之前,参数文件只有一种,它是文本格式的,称为pfile,在9i及以后的版本中,新增了服务器参数文件,称为spfile,它是二进制格式的.这两种参数文件都是用来存储 ...

随机推荐

  1. 标注工具合集(点云&图片)

    有什么问题欢迎留言交流,发现好用的会持续更新-- 图片类 1. labelimg:https://github.com/tzutalin/labelImg --- 只能拉框 2. labelme:ht ...

  2. Luogu1419 区间问题 二分 单调优化

    原题链接 题意 给定一段长度为1e5的序列A,并且给我们一个范围 \([S, T]\), 要求我们求出一段长度在这个范围内的连续子序列,并且要使这个连续子序列的平均值最大,输出这个平均值. 思路 一开 ...

  3. Langchain-Chatchat项目:3-Langchain计算器工具Agent思路和实现

      本文主要讨论Langchain-Chatchat项目中自定义Agent问答的思路和实现.以"计算器工具"为例,简单理解就是通过LLM识别应该使用的工具类型,然后交给相应的工具( ...

  4. 火山引擎 DataTester 应用故事:一个 A/B 测试,将产品 DAU 提升了数十万

      更多技术交流.求职机会,欢迎关注字节跳动数据平台微信公众号,回复[1]进入官方交流群 疫情让线下的需求大量转移到线上,催生出了远程办公.网络授课.线上健身等新的生态现象.如何更好地为用户服务,提升 ...

  5. .Net Core NLog 配置

    using NLog; private static Logger logger = LogManager.GetCurrentClassLogger(); //初始化日志类 NLog.config ...

  6. 阿里OSS文件访问变成下载

    将 ECS 挂载 OSS 多Bucket ,进行文件存储后,发现PDF.图片在浏览器中访问URL,变成了下载,页不是预览. 1. 解决办法,文件类型 application/octet-stream  ...

  7. 远程桌面CredSSP 加密数据库修正

    如图所示: 远程桌面连接,出现身份验证错误,要求的函数不受支持,这可能是由于 CredSSP 加密数据库修正

  8. Flink异步IO

    本文讲解 Flink 用于访问外部数据存储的异步 I/O API. 对于不熟悉异步或者事件驱动编程的用户,建议先储备一些关于 Future 和事件驱动编程的知识. 对于异步 I/O 操作的需求 在与外 ...

  9. print('Hello World!')的新玩法

    相信很多同学入门Python的第一行代码都是print('Hello World!') print是初学者最先接触的Python函数,但是很多人可能到现在也不完全清楚它的用法. print(*obje ...

  10. 华东交通大学2019年ACM 双基 程序设计竞赛 个人题解(A - K)

    目前先放几道题面,等晚上做完实验补 Update:A ~ D,更新剩余的题面(题面复制会有链接水印,懒得一一去除.直接截图) A.签到 真·签到题 输出祝贺祖国成立70周年!即可 B.欧涛的烦恼 思路 ...