当前位置:   article > 正文

pg 10 主要参数配置整理_pg 数据库参数

pg 数据库参数

一、 查看参数设置

  • show 参数名
  • select * from pg_settings where name='参数名';
  • 查看 postgresql.conf文件 

 

二、 参数是否可修改

pg中主要可优化参数都位于postgresql.conf文件中,其中参数是否可修改可通过pg_settings的context字段查询。

  1. postgres=# select distinct context from pg_settings;
  2. context
  3. -------------------
  4. postmaster
  5. superuser-backend
  6. user
  7. internal
  8. backend
  9. sighup
  10. superuser
  11. (7 rows)
  12. postgres=# select name,context from pg_settings limit 5;
  13. name | context
  14. -------------------------+------------
  15. allow_system_table_mods | postmaster
  16. application_name | user
  17. archive_command | sighup
  18. archive_mode | postmaster
  19. archive_timeout | sighup
  20. (5 rows)

 

context含义

  • internal:编译期间确定的数据库内部设置,除非重新编译PG否则无法更改
  • postmaster:服务器重启后才生效(所有共享内存设置都属此类)
  • signup:向服务器发送HUP信号会使服务器重新加载postgresql.conf,加载后对参数的设置会立即生效
  • backend:类似signup,但参数的更改只对新连接生效,不影响现有会话
  • superuser:修改后即生效,但只能由拥有超级用户权限的用户修改
  • user:修改仅影响当前会话,类似alter session
  • superuser-backend:可以在postgresql.conf中直接更改,无需重启生效。此值不能在启动会话后进行更改,另外仅超级用户(如postgres用户)可更改这些设置

 

三、 重新加载配置文件

1. pg_reload_conf(超级用户)

select pg_reload_conf();

2. kill命令发送HUP信号

  1. ps -ef|grep "postgres -D" #记下进程号
  2. kill -HUP 进程号

3. pg_ctl命令触发服务器进程SIGHUP信号

  1. pg_ctl reload
  2. #或者
  3. service postgresql reload

4. 查看最后load的时间

select pg_postmaster_start_time();

 

四、 主要参数详解

1. 数据库连接

参数名含义建议值备注

listen_addresses

指定服务器在哪些 TCP/IP 地址上监听客户端连接。'0.0.0.0'或者 '*'即允许从哪些客户端连到该pg数据库
max_connections最大连接数不要超过实际需求值太多过大的值可能浪费内存、加大连接开销
superuser_reserved_connections保留给超级管理员的连接数10 
ssl是否开启ssl连接off 
authentication_timeout客户端认证最长时间默认1分钟 

 

2. 内存参数

参数名

含义

默认值

建议值

备注

shared_buffers

共享内存

128M

os内存的25%

详细参考后文。调整该参数时,适当调整max_wal_size

huge_pages

启用大页

try

try

建议shared_buffer内存大于64G(总内存大于256G)时打开。

temp_buffers

每个连接可用的最大临时缓冲区大小

8M

8M会话的本地缓冲区,只用于访问临时表。

max_prepared_transactions

可以同时处于“prepared”状态的事务的最大数目

0

0

0为禁用,若启用,该值的最小设置为max_connections的大小

work_mem 

用于排序和hash table的内存

4M

4M

排序操作:ORDER BY, DISTINCT,merge joins。

Hash table主要操作:hash joins, hash-based aggregation。

如果该值过小,会使用临时文件。

maintenance_work_mem

用于维护操作的内存

64M

64M

主要操作为:vacuum、create index、alter table add foreign key

该值应该大于work_mem,在vacuuming和restoring database dump时,适当提高该参数有加速作用。同时需要注意和autovacuum_max_workers的配合使用。

replacement_sort_tuples

当要被排序的元组数比这个数字小时,排序将会使用replacement selection而不是quicksort方法来产生其第一个输出

150000

150000默认为150000元组,在内存受限的环境中可能会有用

autovacuum_work_mem

指定每个自动清理worker进程能使用的最大内存量

-1

-1

默认值为 -1,表示转而使用maintenance_work_mem的值做为vacuuming使用的最大内存大小

 

3. 日志记录

参数名 

含义

建议值

备注

where to log

   

logging_collector

打开日志收集

logging_collector = on

打开该参数下面参数才有意义

log_destination

记录日志的格式log_destination = 'stderr,csvlog'

需要logging_collector=on

 

取值范围:

stderr, csvlog, syslog, and eventlog

log_directory

日志写入的目录log_directory = '/data/PRD/postgres/10.7/pg5432/data/pg_log'

日志存放路径,可以写绝对路径,或者相对路径(相对于PGDATA)

log_filename

日志文件名格式

log_filename = 'postgresql-%Y-%m-%d.log'

 
log_truncate_on_rotation若为on,当日志名重复时会覆盖掉原文件,否则为追加log_truncate_on_rotation = on 

log_file_mode

日志文件权限log_file_mode = 0640 

log_rotation_age

日志切分时间间隔

log_rotation_age = 1d

当未达log_rotation_size大小时,一天生成一个

log_rotation_size

日志最大大小,大于该值会切分log_rotation_size = 100MB

 

    

when to log

   

log_min_error_statement

日志记录级别

log_min_error_statement = waring

可选值:debug5,debug4,debug3,debug2,debug1,

info,notice,warning,error,log,fatal,panic (effectively off)

log_min_duration_statement

记录慢查询log_min_duration_statement = 60000

单位为ms,-1不记录,0记录所有

    

what to log

   

debug_print_parse

打印sql解析树off 

debug_print_rewritten

打印重写的sqloff 

debug_print_plan

打印sql执行计划off 

debug_pretty_print

缩进以上三种日志,使其更易读off 

log_checkpoints

记录Checkpoint

on

 

log_connections

记录客户端连接

on

 

log_disconnections

记录客户端断开连接off 

log_duration

记录所有语句的执行时间off

 

log_hostname

是否记录客户端主机名off

默认只记录客户端IP,不记录hostname

log_line_prefix

日志记录格式,将指定信息打印到日志每一行log_line_prefix = '%t:%r:%u@%d:[%p]: '

%a = application name
%u = user name
%d = database name
%r = remote host and port
%h = remote host
%p = process ID
%t = timestamp without milliseconds
%m = timestamp with milliseconds
%n = timestamp with milliseconds (as a Unix epoch)
%i = command tag
%e = SQL state
%c = session ID
%l = session line number
%s = session start timestamp
%v = virtual transaction ID
%x = transaction ID (0 if none)
%q = stop here in non-session processes
%% = '%'

log_lock_waits

session等待锁时间超过deadlock_timeout时,记录在日志中

on

 

log_statement

日志记录的语句类型log_statement = 'ddl'

none:不记录任何语句信息

ddl:只记录ddl

mod:记录ddl+dml和COPY FROM、PREPARE、EXECUTE、EXPLAIN ANALYZE语句

all:记录所有语句

log_replication_commands

是否记录replication commandoff

 

log_temp_files

记录tempflie名字及大小。

(-1,0,num)

-1不记录,0记录temp文件删除,>0 表示记录tempfile size大于这个值(KB)

log_timezone

日志显示时区

log_timezone = 'Asia/Shanghai'

 

 

4. vacuum

参数名 

含义

建议值

备注

autovacuum是否启用自动清理子进程on需要track_counts=on
log_autovacuum_min_duration -1 
autovacuum_max_workers 3 
autovacuum_naptime 1min 
autovacuum_vacuum_threshold 50 
autovacuum_analyze_threshold 50 
autovacuum_vacuum_scale_factor 0.2 
autovacuum_analyze_scale_factor 0.1 
autovacuum_freeze_max_age 200000000 
autovacuum_multixact_freeze_max_age 400000000 
autovacuum_vacuum_cost_delay 20ms 
autovacuum_vacuum_cost_limit -1 

vacuum_cost_delay

 

0

vacuuming达到cost值时的休眠时间。如果vacuum压力比较大,可以设置成10ms(20ms),避免对系统造成太大影响。

vacuum_cost_page_hit

 1 

vacuum_cost_page_miss

 10 

vacuum_cost_page_dirty

 20 

vacuum_cost_limit

 200 

 

5. 检查点

 

checkpoint触发条件

   

checkpoint所做操作

 

1,执行checkpoint命令。
2,执行pg_start_backup,create database,pg_ctl stop|restart。
3,达到checkpoint_timeout时间。
4,达到max_wal_size。

   

1,在shared buffer中确认所有的dirty blocks。
2,将所用脏块写入磁盘(或者文件系统缓存)
3,调用fsync()

 

参数名称

含义

默认值

建议值

备注

关键参数

checkpoint_timeout

自上次检查点后,经过checkpoint_timeout指定时间时,自动执行检查点

5min

15-30 min,1h也可

单位为秒,有效值为30秒到1天

 

max_wal_size

WAL日志大小达到max_wal_size时,自动执行检查点

1GB

 

1,select pg_current_wal_insert_lsn();
2,alter 5 min
3, select pg_current_wal_insert_lsn('',''); =1.8GB
4,如果timeout=30min,约产生10G左右wal。Max_wal_size一般为这个值的2-3倍。max_wal_size=30GB
或者checkpoint_segments * (2 + checkpoint_completion_target) = max_wal_size

 

min_wal_size

只要WAL磁盘使用率低于这个设置,旧的WAL文件总数被回收,以供将来检查点使用。而不是删除。 这可以用来确保预留足够的WAL空间处理WAL使用中的峰值,比如当运行大批量工作时。

80MB

 

最小checkpoint wal size大小。

 

checkpoint_completion_target

每次checkpoint的数据会在多长时间内写完

0.5

0.9

时间为 checkpoint_completion_target * checkpoint_timeout
 

checkpoint_warning

系统默认值是30秒,如果checkpoints的实际发生间隔小于该参数,将会在server log中写入写入一条相关信息。如果出现该告警,可能需要提高max_wal_size。  

 

 

6. wal

参数名

默认值

建议值

备注

wal_level

replica

 

minial:只记录数据库crash,immediate shutdown恢复所需要的日志。
replica:9.6对应 archive and hot_standby
logical:用于新增的逻辑复制

fsync

on

on

确保更新实际写入磁盘,关闭fsync(同时关闭full_page_writes)可以得到性能的提升,但是不安全。

synchronous_commit

onon提交事务是否需等待其把wal写入磁盘后再返回成功

wal_sync_method

Linux:fdatasync 指定向磁盘强制更新wal日志数据的方法,可选值为open_datasync/fdatasync/fsync/fsync_writethrough/open_sync

full_page_writes

on

on

全页写,解决页断裂问题。

wal_compression

off

on

wal日志压缩,节省磁盘空间

wal_buffers

16M

 

shared memory的一部分,用来缓存wal data未写入磁盘部分。

wal_writer_delay

200ms

 

WAL writer进程的睡眠时间,进程每次在完成写事务日志的任务后,会等待wal_writer_delay时间,然后将新产生的事务日志从缓冲区写到WAL文件中。

如果时间过长可能造成WAL buffer的内存不足,数据丢失的危险;过短则WAL会不断写入,对磁盘IO也是很大考验

wal_writer_flush_after 

1M

 wal write的字节数超过配置的阈值时,触发fsync,设为0表示关闭

commit_delay

0(无延迟)

 

至少有commit_siblings个并发事务时,该事务提交后,wal日志将延迟commit_delay时间后再写入磁盘。


非0值的影响:
    减少IO,提高性能:事务执行commit后不会立即写入磁盘,而存放在WAL buffer中
    崩溃数据面临着丢失的危险
    可能引起WAL buffer内存不足,尤其是提交事务较多的高峰期

commit_siblings

5 延迟提交wal日志的最小并发事务数,决定参数commit_delay是否生效。
假设值是5,表示数据库中正在执行的事务数大于或等于5,该事务提交后,wal日志将会存入wal buffer中,延迟commit_delay时间后再写入磁盘。
如果数据库中正在执行的事务数小于5,这个事务提交后将wal日志直接写入磁盘。

 

四、 实际参数文件内容

  1. # -----------------------------
  2. # PostgreSQL configuration file
  3. # -----------------------------
  4. #------------------------------------------------------------------------------
  5. # FILE LOCATIONS
  6. #------------------------------------------------------------------------------
  7. # The default values of these variables are driven from the -D command-line
  8. # option or PGDATA environment variable, represented here as ConfigDir.
  9. data_directory = '/data/PRD/postgres/10.7/pg5432/data'          # use data in another directory
  10.                                         # (change requires restart)
  11. hba_file = '/data/PRD/postgres/10.7/pg5432/data/pg_hba.conf'    # host-based authentication file
  12.                                         # (change requires restart)
  13. ident_file = '/data/PRD/postgres/10.7/pg5432/data/pg_ident.conf'        # ident configuration file
  14.                                         # (change requires restart)
  15. # If external_pid_file is not explicitly set, no extra PID file is written.
  16. #external_pid_file = ''                 # write an extra PID file
  17.                                         # (change requires restart)
  18. #------------------------------------------------------------------------------
  19. # CONNECTIONS AND AUTHENTICATION
  20. #------------------------------------------------------------------------------
  21. # - Connection Settings -
  22. listen_addresses = '0.0.0.0'            # what IP address(es) to listen on;
  23.                                         # comma-separated list of addresses;
  24.                                         # defaults to 'localhost'; use '*' for all
  25.                                         # (change requires restart)
  26. #port = 5432                            # (change requires restart)
  27. max_connections = 1000                  # (change requires restart)
  28. superuser_reserved_connections = 10     # (change requires restart)
  29. unix_socket_directories = '/tmp'        # comma-separated list of directories
  30.                                         # (change requires restart)
  31. #unix_socket_group = ''                 # (change requires restart)
  32. unix_socket_permissions = 0770          # begin with 0 to use octal notation
  33. # - Security and Authentication -
  34. authentication_timeout = 1min           # 1s-600s
  35. ssl = off
  36. tcp_keepalives_idle = 60                # TCP_KEEPIDLE, in seconds;
  37. #------------------------------------------------------------------------------
  38. # RESOURCE USAGE (except WAL)
  39. #------------------------------------------------------------------------------
  40. # - Memory -
  41. shared_buffers = 128MB                  # min 128kB
  42.                                         # (change requires restart)
  43. #huge_pages = try                       # on, off, or try
  44.                                         # (change requires restart)
  45. #temp_buffers = 8MB                     # min 800kB
  46. #max_prepared_transactions = 0          # zero disables the feature
  47.                                         # (change requires restart)
  48. # Caution: it is not advisable to set max_prepared_transactions nonzero unless
  49. # you actively intend to use prepared transactions.
  50. #work_mem = 4MB                         # min 64kB
  51. #maintenance_work_mem = 64MB            # min 1MB
  52. #replacement_sort_tuples = 150000       # limits use of replacement selection sort
  53. #autovacuum_work_mem = -1               # min 1MB, or -1 to use maintenance_work_mem
  54. #max_stack_depth = 2MB                  # min 100kB
  55. dynamic_shared_memory_type = posix      # the default is the first option
  56.                                         # supported by the operating system:
  57.                                         #   posix
  58.                                         #   sysv
  59.                                         #   windows
  60.                                         #   mmap
  61.                                         # use none to disable dynamic shared memory
  62.                                         # (change requires restart)
  63. # - Disk -
  64. #temp_file_limit = -1                   # limits per-process temp file space
  65.                                         # in kB, or -1 for no limit
  66. # - Kernel Resource Usage -
  67. #max_files_per_process = 1000           # min 25
  68.                                         # (change requires restart)
  69. shared_preload_libraries = 'pg_stat_statements,auto_explain'            # (change requires restart)
  70. #------------------------------------------------------------------------------
  71. # WRITE AHEAD LOG
  72. #------------------------------------------------------------------------------
  73. # - Settings -
  74. #wal_level = replica                    # minimal, replica, or logical
  75.                                         # (change requires restart)
  76. #fsync = on                             # flush data to disk for crash safety
  77.                                         # (turning this off can cause
  78.                                         # unrecoverable data corruption)
  79. synchronous_commit = local              # synchronization level;
  80.                                         # off, local, remote_write, remote_apply, or on
  81. #wal_sync_method = fsync                # the default is the first option
  82.                                         # supported by the operating system:
  83.                                         #   open_datasync
  84.                                         #   fdatasync (default on Linux)
  85.                                         #   fsync
  86.                                         # (change requires restart)
  87. #wal_buffers = -1                       # min 32kB, -1 sets based on shared_buffers
  88.                                         # (change requires restart)
  89. # - Checkpoints -
  90. #checkpoint_timeout = 5min              # range 30s-1d
  91. #max_wal_size = 1GB
  92. #min_wal_size = 80MB
  93. #checkpoint_completion_target = 0.5     # checkpoint target duration, 0.0 - 1.0
  94. #checkpoint_flush_after = 256kB         # measured in pages, 0 disables
  95. checkpoint_warning = 10800s             # 0 disables
  96. # - Archiving -
  97. archive_mode = on               # enables archiving; off, on, or always
  98.                                 # (change requires restart)
  99. archive_command = 'test ! -f /data/PRD/postgres/10.7/pg5432/pg_archlog/%f && cp %p /data/PRD/postgres/10.7/pg5432/pg_archlog/%f'               # command to use to archive a logfile segment
  100.                                 # placeholders: %p = path of file to archive
  101.                                 #               %f = file name only
  102.                                 # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
  103. #archive_timeout = 0            # force a logfile segment switch after this
  104.                                 # number of seconds; 0 disables
  105. #------------------------------------------------------------------------------
  106. # REPLICATION
  107. #------------------------------------------------------------------------------
  108. # - Sending Server(s) -
  109. # Set these on the master and on any standby that will send replication data.
  110. max_wal_senders = 5             # max number of walsender processes
  111.                                 # (change requires restart)
  112. wal_keep_segments = 64          # in logfile segments, 16MB each; 0 disables
  113. #wal_sender_timeout = 60s       # in milliseconds; 0 disables
  114. #max_replication_slots = 10     # max number of replication slots
  115.                                 # (change requires restart)
  116. #track_commit_timestamp = off   # collect timestamp of transaction commit
  117.                                 # (change requires restart)
  118. # - Standby Servers -
  119. # These settings are ignored on a master server.
  120. hot_standby = on                        # "off" disallows queries during recovery
  121.                                         # (change requires restart)
  122. #------------------------------------------------------------------------------
  123. # ERROR REPORTING AND LOGGING
  124. #------------------------------------------------------------------------------
  125. # - Where to Log -
  126. log_destination = 'stderr,csvlog'               # csvlog是pg_rman需要
  127. # This is used when logging to stderr:
  128. logging_collector = on          # Enable capturing of stderr and csvlog
  129.                                         # into log files. Required to be on for
  130.                                         # csvlogs.
  131.                                         # (change requires restart)
  132. # These are only used if logging_collector is on:
  133. log_directory = '/data/PRD/postgres/10.7/pg5432/data/pg_log'                    # directory where log files are written,
  134.                                         # can be absolute or relative to PGDATA
  135. log_filename = 'postgresql-%d.log'      # log file name pattern,
  136.                                         # can include strftime() escapes
  137. log_file_mode = 0640                    # creation mode for log files,
  138.                                         # begin with 0 to use octal notation
  139. log_truncate_on_rotation = on           # If on, an existing log file with the
  140.                                         # same name as the new log file will be
  141.                                         # truncated rather than appended to.
  142.                                         # But such truncation only occurs on
  143.                                         # time-driven rotation, not on restarts
  144.                                         # or size-driven rotation.  Default is
  145.                                         # off, meaning append to existing files
  146.                                         # in all cases.
  147. #log_rotation_age = 1d                  # Automatic rotation of logfiles will
  148.                                         # happen after that time.  0 disables.
  149. log_rotation_size = 100MB               # Automatic rotation of logfiles will
  150.                                         # happen after that much log output.
  151.                                         # 0 disables.
  152. log_min_duration_statement = 60000      # -1 is disabled, 0 logs all statements
  153.                                         # and their durations, > 0 logs only
  154.                                         # statements running at least this number
  155.                                         # of milliseconds
  156. # - What to Log -
  157. log_checkpoints = on
  158. log_connections = on
  159. log_line_prefix = '%t:%r:%u@%d:[%p]: '          
  160. log_lock_waits = on                     # log lock waits >= deadlock_timeout
  161. log_timezone = 'Asia/Shanghai'
  162. #------------------------------------------------------------------------------
  163. # AUTOVACUUM PARAMETERS
  164. #------------------------------------------------------------------------------
  165. autovacuum = on
  166. autovacuum_work_mem = 3GB
  167. autovacuum_max_workers = 10
  168. autovacuum_naptime = 1s
  169. autovacuum_vacuum_threshold = 50
  170. autovacuum_analyze_threshold = 50
  171. autovacuum_vacuum_scale_factor = 0.002
  172. autovacuum_analyze_scale_factor = 0.001
  173. autovacuum_vacuum_cost_delay = 0
  174. #------------------------------------------------------------------------------
  175. # CLIENT CONNECTION DEFAULTS
  176. #------------------------------------------------------------------------------
  177. # - Locale and Formatting -
  178. datestyle = 'iso, mdy'
  179. #intervalstyle = 'postgres'
  180. timezone = 'PRC'
  181. #------------------------------------------------------------------------------
  182. # CUSTOMIZED OPTIONS
  183. #------------------------------------------------------------------------------
  184. # Add settings for extensions here
  185. # - pg_stat_statements auto_explain -
  186. pg_stat_statements.max = 10000
  187. pg_stat_statements.track = all
  188. pg_stat_statements.track_utility = true
  189. pg_stat_statements.save = true
  190. auto_explain.log_min_duration = 30s
  191. # - AUDIT DDL -
  192. log_statement=ddl

 

参考

https://blog.csdn.net/liyingke112/article/details/78805350

本文内容由网友自发贡献,转载请注明出处:【wpsshop博客】
推荐阅读
相关标签
  

闽ICP备14008679号