[转帖]PostgreSQL 参数优化设置 32GB内存(推荐) 内存参数 检查点 日志参数 自动初始化参数shell脚本
这个shell脚本可以使用shell的关联数组对
-
#!/bin/bash
-
#command to execute script: su - postgres -c "./create.sh"
-
#default value refer to standard server(cpu:6cores,memory:32G,storage:12T)
-
#value according to the actual situation
-
-
#List of parameters to be modified
-
:<<!
-
enable_seqscan = off
-
enable_indexscan = on
-
enable_bitmapscan = on
-
max_connections = 1000
-
shared_buffers = 16GB
-
effective_cache_size = 24GB
-
work_mem = 32MB
-
temp_buffers = 32MB
-
wal_buffers = 512MB
-
maintenance_work_mem = 2GB
-
autovacuum_max_workers = 3
-
autovacuum_work_mem = 256MB
-
checkpoint_timeout = 30min
-
max_wal_size = 4GB
-
min_wal_size = 1GB
-
checkpoint_completion_target = 0.9
-
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'
-
!
-
-
ERROR="\033[41;37m ERROR \033[0m"
-
INFO="\033[42;37m INFO \033[0m"
-
WARN="\033[43;37m WARN \033[0m"
-
COMMON_ERROR="some error happened, specific information please see console output"
-
-
# Array of parameters
-
declare -A parameter_array
-
parameter_array=([enable_seqscan]=off [enable_indexscan]=on [enable_bitmapscan]=on [max_connections]=1000 [shared_buffers]=16GB
-
[effective_cache_size]=24GB [work_mem]=32MB [temp_buffers]=32MB [wal_buffers]=512MB [maintenance_work_mem]=2GB
-
[autovacuum_max_workers]=3 [autovacuum_work_mem]=256MB [checkpoint_timeout]=30min [max_wal_size]=4GB
-
[min_wal_size]=1GB [checkpoint_completion_target]=0.9 [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]=viid_log_%a.log )
-
-
#default value :
-
pgctl_path=
-
data_directory=
-
memory=
-
-
# check command exit value, 0 is success
-
function check_fun(){
-
status=$?
-
error=${COMMON_ERROR}
-
if [[ 0 -ne ${status} ]] ; then
-
echo -e "${ERROR} ${error}"
-
exit 1
-
fi
-
}
-
-
# prepare conditions
-
function prepare_conditions(){
-
data_directory=$(psql -qtAX -c "show data_directory" | sed 's/[ ]//g')
-
check_fun
-
if [[ ! -d "${data_directory}" ]] ; then
-
echo -e "${ERROR} database's data directory does not exist"
-
exit 1
-
fi
-
# physical machine environment
-
memory=$(grep MemTotal /proc/meminfo | awk '{print $2 / 1024 / 1024}' | sed 's/\.[0-9]*//' | tail -n 1)
-
check_fun
-
# docker environment
-
memory_limit=$(($(awk '{print $1}' /sys/fs/cgroup/memory/memory.limit_in_bytes) / 1024 / 1024 /1024))
-
check_fun
-
# comparing the two, choose the smaller one.
-
if [[ "${memory_limit}" -le "${memory}" ]] ; then
-
memory=${memory_limit}
-
fi
-
}
-
-
# calculate parameters
-
function calculate_parameters(){
-
# 50%*memory
-
parameter_array[shared_buffers]=$((memory * 1024 / 2))"MB"
-
# 75%*memory
-
parameter_array[effective_cache_size]=$((memory * 1024 * 3 / 4 ))"MB"
-
# <1%
-
parameter_array[work_mem]=${memory}"MB"
-
# <1%
-
parameter_array[temp_buffers]=${memory}"MB"
-
# 32GB => 512MB
-
parameter_array[wal_buffers]=$((memory * 16 ))"MB"
-
# 32GB => 2048MB
-
parameter_array[maintenance_work_mem]=$((memory * 64 ))"MB"
-
}
-
-
# modify parameters
-
function modify_parameters(){
-
# modify parameters by modifying file postgresql.conf:
-
for parameter in ${!parameter_array[*]}
-
do
-
#check whether the parameters have been modified
-
# PostgreSQL's default parameter configuration example: enable_seqscan = on
-
# viid's example: enable_seqscan='on'
-
check_out=$(grep "^${parameter}=" "${data_directory}"/postgresql.conf | grep -v '#' | tail -n 1)
-
# process sleep
-
sleep 0.2s
-
if [[ -z "${check_out}" ]] ; then
-
echo "${parameter}='${parameter_array[${parameter}]}'" >> "${data_directory}"/postgresql.conf
-
check_fun
-
echo -e "${INFO} modify ${parameter} successfully"
-
else
-
if [[ "${check_out}" = "${parameter}='${parameter_array[${parameter}]}'" ]] ; then
-
echo -e "${INFO} ${parameter} is already configured, then skip this step"
-
else
-
sed -i s!^"${check_out}"!"${parameter}='${parameter_array[${parameter}]}'"!g "${data_directory}"/postgresql.conf
-
check_fun
-
echo -e "${INFO} modify ${parameter} successfully"
-
fi
-
fi
-
done
-
}
-
-
# create directory(pg_log and pg_arch), and set user postgres permission
-
function create_dir(){
-
directory_array=("pg_arch" "pg_log")
-
for directory in ${directory_array[*]};
-
do
-
# process sleep
-
sleep 0.2s
-
if [[ ! -d "${data_directory}/${directory}" ]] ; then
-
mkdir -p "${data_directory}"/"${directory}"
-
echo -e "${INFO} path ${data_directory}/${directory} create successfully"
-
else
-
echo -e "${INFO} ${data_directory}/${directory} is already exists, then skip this step"
-
fi
-
# set user postgres permission
-
chown postgres:postgres "${data_directory}"/"${directory}"
-
done
-
}
-
-
# because the environment is different, need to find the path of the database restart command 'pg_ctl'
-
function find_cmd(){
-
result=$(find / -name pg_ctl 2> /dev/null | grep bin/pg_ctl$ | tail -n 1 )
-
# check whether the path exists
-
if [[ -z "${result}" ]] ; then
-
echo -e "${ERROR} database restart command 'pg_ctl' not exists"
-
echo -e "${ERROR} please check to see if the database is installed or the command directory does not have permission to access it"
-
exit 1
-
else
-
echo -e "${INFO} database restart command 'pg_ctl' path: ${result}"
-
pgctl_path=${result}
-
fi
-
}
-
-
# user choose whether to restart or not
-
function check_restart(){
-
read -r -p "Is it necessary to restart database immediately?[Enter YES or NO]:" result
-
if [[ "${result,,}" = "yes" ]] ; then
-
echo -e "${INFO} start to restart database"
-
${pgctl_path} restart -D "${data_directory}" >& /dev/null
-
if [[ 0 -ne ${status} ]] ; then
-
echo -e "${ERROR} restart database failed"
-
exit 12
-
fi
-
elif [[ "${result,,}" = "no" ]] ; then
-
echo -e "${WARN} please restart database manually"
-
echo -e "${WARN} if you don't restart, database may not be available"
-
exit 11
-
else
-
echo -e "${ERROR} invalid input,please enter again"
-
check_restart
-
fi
-
}
-
-
# ******* start *******
-
# prepare conditions:memory ,data_directory
-
prepare_conditions
-
# calculate parameters
-
calculate_parameters
-
# modify parameters
-
modify_parameters
-
# create directory(pg_log and pg_arch)
-
create_dir
-
# the path of the database restart command 'pg_ctl'
-
find_cmd
-
# remind user that they need to restart database to take effect
-
# process sleep
-
sleep 0.5s
-
echo -e ""
-
echo -e "*******************************************************************"
-
echo -e "* *"
-
echo -e "* *"
-
echo -e "* restart database to take effect *"
-
echo -e "* *"
-
echo -e "* *"
-
echo -e "*******************************************************************"
-
# process sleep
-
sleep 0.5s
-
# user choose whether to restart or not
-
# check_restart
-
echo -e "${INFO} start to restart database"
-
${pgctl_path} restart -D "${data_directory}" >& /dev/null
-
[转帖]PostgreSQL 参数优化设置 32GB内存(推荐) 内存参数 检查点 日志参数 自动初始化参数shell脚本的更多相关文章
- vm内核参数优化设置
http://www.cnblogs.com/wjoyxt/archive/2014/06/08/3777042.html (1)vm.overcommit_memory 执行grep -i com ...
- ajax 参数data问题 data中的 参数名 参数值为string 提交到后台后,会自动转换参数名相同的 类型 和 js字符串拼接
latlng"14.6005238,100.43635419999998"Cusid"accb5c1b-6aef-4f3b-a4eb-d60ea1ca5f54" ...
- AI人脸识别SDK接入 — 参数优化篇(虹软)
引言 使用了虹软公司免费的人脸识别算法,感觉还是很不错的,当然,如果是初次接触的话会对一些接口的参数有些疑问的.这里分享一下我对一些参数的验证结果(这里以windows版本为例,linux.andro ...
- 虹软AI 人脸识别SDK接入 — 参数优化篇
引言 使用了免费的人脸识别算法,感觉还是很不错的,但是初次接触的话会对一些接口的参数有些疑问的.这里分享一下我对一些参数的验证结果(这里以windows版本为例,linux.android基本一样), ...
- linux——系统内核参数优化
vim /etc/sysctl.conf net.ipv4.tcp_syncookies = 1 fs.file-max = 999999 net.ipv4.tcp_max_tw_buckets = ...
- 使用服务器参数文件(SPFILE)管理初始化参数
传统上,Oracle数据库的初始化参数存储在文本初始化参数文件中.为了更好的可管理性,您可以选择在二进制服务器参数文件中维护初始化参数,该文件在数据库启动和关闭期间保持不变.本节介绍服务器参数文件,并 ...
- Ubuntu 系统服务器初始化配置、安全加固、内核优化和常用软件安装的Shell脚本分享
转载自:https://www.bilibili.com/read/cv13875402?spm_id_from=333.999.0.0 描述: 适用于企业内部 Ubuntu 操作服务器初始化.系统安 ...
- CentOS7 系统服务器初始化配置、安全加固、内核升级优化常用软件安装的Shell脚本分享
转载自:https://www.bilibili.com/read/cv13875630?spm_id_from=333.999.0.0 描述: 适用于企业内部 CentOS7 系列操作服务器初始化. ...
- javaee学习-servlet初始化参数
1.需要定义ServletConfig对象来接收servlet配置的初始化参数. 2.当servlet配置了初始化参数后,web容器在创建servlet实例对象时, 会自动将这些初始化参数封装到Ser ...
- ORACLE初始化参数文件概述
ORACLE初始化参数文件概述 在9i之前,参数文件只有一种,它是文本格式的,称为pfile,在9i及以后的版本中,新增了服务器参数文件,称为spfile,它是二进制格式的.这两种参数文件都是用来存储 ...
随机推荐
- Spring表达式语言(SPEL)学习(01)
算术运算 @Test public void test01() { // 定义解析器 ExpressionParser parser = new SpelExpressionParser(); // ...
- Java异常处理之数字溢出问题
在进行 Java 开发时,经常会遇到数字溢出的问题.这个问题在很多程序中都非常常见,尤其是在进行数值计算的时候.Java 中提供了一种异常机制来处理这种情况,我们可以在代码中使用 try-catch ...
- WinDbg实践--入门篇
WinDbg从字面意思就是Windows+Debug的组合,即Windows平台上的调试工具,可以调试用户模式.内核模式.dump文件等,总之知道它的调试功能非常强大就行了.WinDbg调试命令分 ...
- 如果云是水滴,Kubernetes就是水滴管理平台
摘要:云是由很多小水滴组成的,把每一个计算机想象成小水滴,联合起来就形成了云.一般水滴先出现,然后出现管理水滴的平台(如OpenStack,Kubernetes). 一.云计算–独立宇宙 1.云是由很 ...
- HTTP请求转发那些事:你可能不知道的Hop-by-hop Headers和End-to-end Headers
摘要:不是所有的web容器都能帮助开发者屏蔽hop-by-hop headers,有些容器反而允许开发者自定义hop-by-hop headers来实现更大程度的灵活性. 本文分享自华为云社区< ...
- Kubernetes(K8S) Pod 介绍
Pod 是 k8s 系统中可以创建和管理的最小单元, 是资源对象模型中由用户创建或部署的最小资源对象模型, 也是在 k8s 上运行容器化应用的资源对象, 其他的资源对象都是用来支撑或者扩展 Pod 对 ...
- C++11实用特性1
1 原始字面量 有时候在输出一个路径字符串时,编译器会将其中的部分内容识别成转义字符进行输出,可以用R "xxx(原始字符串)xxx"其中()两边的字符串可以省略.原始字面量R可以 ...
- 系统学Prompt,强烈推荐这3个教程,OpenAI还是最强
系统学Prompt,强烈推荐这3个教程 OpenAI官方Prompt教程 这是最权威.最详细.最值得深入学习的提示词工程学习教程! 教程地址:https://platform.openai.com/d ...
- 什么?你居然没有鸭鸭邮箱?@duck.com邮箱注册与使用
@duck.com 是由专注于隐私的搜索引擎DuckDuckGo提供的面向所有人的匿名邮箱. 注册者可以设置一个自定义前缀,比如 one@duck.com,接着设置接收邮箱(如pete@gmail.c ...
- Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) (个人题解)
Codeforces Round #687 (Div. 2, based on Technocup 2021 Elimination Round 2) A. Prison Break https:// ...