当前位置:   article > 正文

PostgreSQL11 posgres.conf 参数模板_at least 3 parameter(s) provided but only 2 parame

at least 3 parameter(s) provided but only 2 parameter(s) present in query.
  1. #------------------------------------------------------------------------------
  2. # FILE LOCATIONS
  3. #------------------------------------------------------------------------------
  4. # The default values of these variables are driven from the -D command-line
  5. # option or PGDATA environment variable, represented here as ConfigDir.
  6. #data_directory = 'ConfigDir' # use data in another directory
  7. # (change requires restart)
  8. #hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file
  9. # (change requires restart)
  10. #ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file
  11. # (change requires restart)
  12. # If external_pid_file is not explicitly set, no extra PID file is written.
  13. #external_pid_file = '' # write an extra PID file
  14. # (change requires restart)
  15. #------------------------------------------------------------------------------
  16. # CONNECTIONS AND AUTHENTICATION
  17. #------------------------------------------------------------------------------
  18. # - Connection Settings -
  19. # 监听
  20. listen_addresses = '0.0.0.0' # what IP address(es) to listen on;
  21. # comma-separated list of addresses;
  22. # defaults to 'localhost'; use '*' for all
  23. # (change requires restart)
  24. # 根据业务需求设定监听端口
  25. port = 1921 # (change requires restart)
  26. # 比较安全的值:建议不要大于 200 * 四分之一物理内存(GB), 例如四分之一物理内存为16G,则建议不要超过3200.
  27. # (假设平均一个连接耗费5MB。 实际上syscache很大、SQL 使用到WORK_MEM,未使用hugepage并且访问到大量shared buffer page时,可能消耗更多内存)
  28. # 如果业务有更多并发连接,可以使用连接池,例如pgbouncer
  29. # [《PostgreSQL relcache在长连接应用中的内存霸占"坑"》](201607/20160709_01.md)
  30. # max_connections 公式:物理内存(GB)*1000*(1/4)/5
  31. max_connections = 2000 # (change requires restart)
  32. # 为超级用户保留多少个连接
  33. superuser_reserved_connections = 10 # (change requires restart)
  34. # $PGDATA, /tmp中 创建unix socket监听
  35. unix_socket_directories = '., /tmp' # comma-separated list of directories
  36. # (change requires restart)
  37. #unix_socket_group = '' # (change requires restart)
  38. # 除了OWNER和超级用户,其他用户无法从/tmp unix socket连接该实例
  39. unix_socket_permissions = 0700 # begin with 0 to use octal notation
  40. # (change requires restart)
  41. #bonjour = off # advertise server via Bonjour
  42. # (change requires restart)
  43. #bonjour_name = '' # defaults to the computer name
  44. # (change requires restart)
  45. # - TCP Keepalives -
  46. # see "man 7 tcp" for details
  47. # 如果你连接数据库空闲一段时间会端口,可能是网络中存在会话超时的设备,建议可以设置一下这个心跳时间,TCP心跳间隔会缩短到60秒。
  48. tcp_keepalives_idle = 60 # TCP_KEEPIDLE, in seconds;
  49. # 0 selects the system default
  50. tcp_keepalives_interval = 10 # TCP_KEEPINTVL, in seconds;
  51. # 0 selects the system default
  52. tcp_keepalives_count = 10 # TCP_KEEPCNT;
  53. # 0 selects the system default
  54. # - Authentication -
  55. #authentication_timeout = 1min # 1s-600s
  56. # md5 or scram-sha-256 # 如果用户密码的MD5会泄露,建议使用scram-sha-256,但是相互不兼容,请注意。
  57. # [《PostgreSQL 10.0 preview 安全增强 - SASL认证方法 之 scram-sha-256 安全认证机制》](201703/20170309_01.md)
  58. password_encryption = md5 # md5 or scram-sha-256
  59. #db_user_namespace = off
  60. # GSSAPI using Kerberos
  61. #krb_server_keyfile = ''
  62. #krb_caseins_users = off
  63. # - SSL -
  64. #ssl = off
  65. #ssl_ca_file = ''
  66. #ssl_cert_file = 'server.crt'
  67. #ssl_crl_file = ''
  68. #ssl_key_file = 'server.key'
  69. #ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers
  70. #ssl_prefer_server_ciphers = on
  71. #ssl_ecdh_curve = 'prime256v1'
  72. #ssl_dh_params_file = ''
  73. #ssl_passphrase_command = ''
  74. #ssl_passphrase_command_supports_reload = off
  75. #------------------------------------------------------------------------------
  76. # RESOURCE USAGE (except WAL)
  77. #------------------------------------------------------------------------------
  78. # - Memory -
  79. # 1/4 主机内存
  80. # shared_buffers 公式: IF use hugepage: 主机内存*(1/4) ELSE: min(32GB, 主机内存*(1/4))
  81. # [《PostgreSQL Huge Page 使用建议 - 大内存主机、实例注意》](201803/20180325_02.md)
  82. shared_buffers = 24GB # min 128kB
  83. # (change requires restart)
  84. # 建议shared buffer设置超过32GB时 使用大页,页大小 /proc/meminfo Hugepagesize
  85. huge_pages = try # on, off, or try
  86. # (change requires restart)
  87. #temp_buffers = 8MB # min 800kB
  88. # 如果用户需要使用两阶段提交,需要设置为大于0,建议与max_connections一样大
  89. # max_prepared_transactions 公式: max_prepared_transactions=max_connections
  90. max_prepared_transactions = 2000 # zero disables the feature
  91. # (change requires restart)
  92. # Caution: it is not advisable to set max_prepared_transactions nonzero unless
  93. # you actively intend to use prepared transactions.
  94. # 可以在会话中设置,如果有大量JOIN,聚合操作,并且期望使用hash agg或hash join。 (排序,HASH都会用到work_mem)
  95. # 可以设大一些,但是不建议大于 四分之一内存除以最大连接数 .
  96. # (一条QUERY中可以使用多倍WORK_MEM,与执行计划中的NODE有关)
  97. # work_mem 公式: max(min(物理内存/4096, 64MB), 4MB)
  98. work_mem = 8MB # min 64kB
  99. # 创建索引时使用的内存空间。
  100. # maintenance_work_mem 公式: min( 8G, (主机内存*1/8)/max_parallel_maintenance_workers )
  101. maintenance_work_mem = 2GB # min 1MB
  102. # 在对一张表进行垃圾回收时,用于缓存垃圾版本的ctid,
  103. # 如果autovacuum work mem填满了,则VACUUM需要进入索引垃圾回收阶段,扫描索引(因此为了避免索引被多次扫描,需要缓存住一张表的所有垃圾TUPLE的CTID)。
  104. # 一张表(细到单个最底层分区)垃圾回收结束后释放。
  105. # [《PostgreSQL 垃圾回收参数优化之 - maintenance_work_mem , autovacuum_work_mem》](201902/20190226_01.md)
  106. # autovacuum_work_mem 公式: min( 8G, (主机内存*1/8)/autovacuum_max_workers )
  107. autovacuum_work_mem = 1GB # min 1MB, or -1 to use maintenance_work_mem
  108. #max_stack_depth = 2MB # min 100kB
  109. dynamic_shared_memory_type = posix # the default is the first option
  110. # supported by the operating system:
  111. # posix
  112. # sysv
  113. # windows
  114. # mmap
  115. # use none to disable dynamic shared memory
  116. # (change requires restart)
  117. # - Disk -
  118. # 如果需要限制临时文件使用量,可以设置。
  119. # 例如, 防止有异常的递归调用,无限使用临时文件。
  120. #temp_file_limit = -1 # limits per-process temp file space
  121. # in kB, or -1 for no limit
  122. # - Kernel Resources -
  123. ## 如果你的数据库有非常多小文件(比如有几十万以上的表,还有索引等,并且每张表都会被访问到时),
  124. # 建议FD可以设多一些,避免进程需要打开关闭文件。
  125. ## 但是不要大于前面章节系统设置的ulimit -n(open files)
  126. # max_files_per_process=655360
  127. #max_files_per_process = 1000 # min 25
  128. # (change requires restart)
  129. # - Cost-Based Vacuum Delay -
  130. # 如果你的系统IO非常好,则可以关闭vacuum delay , 避免因为垃圾回收任务周期长导致的膨胀。
  131. vacuum_cost_delay = 0 # 0-100 milliseconds
  132. #vacuum_cost_page_hit = 1 # 0-10000 credits
  133. #vacuum_cost_page_miss = 10 # 0-10000 credits
  134. #vacuum_cost_page_dirty = 20 # 0-10000 credits
  135. # io很好,CPU核数很多的机器,设大一些。如果设置了vacuum_cost_delay = 0 ,则这个不需要配置
  136. vacuum_cost_limit = 10000 # 1-10000 credits
  137. # - Background Writer -
  138. bgwriter_delay = 10ms # 10-10000ms between rounds
  139. bgwriter_lru_maxpages = 1000 # max buffers written/round, 0 disables
  140. bgwriter_lru_multiplier = 10.0 # 0-10.0 multiplier on buffers scanned/round
  141. bgwriter_flush_after = 512kB # measured in pages, 0 disables
  142. # - Asynchronous Behavior -
  143. effective_io_concurrency = 0 # 1-1000; 0 disables prefetching
  144. # wal sender, user 动态fork的process, parallel worker等都算作 worker process, 所以你需要设置足够大.
  145. max_worker_processes = 256 # (change requires restart)
  146. # 如果需要使用并行创建索引,设置为大于1 ,不建议超过 主机cores-4
  147. # max_parallel_maintenance_workers 公式: min( max(2, CPU核数/2) , 16 )
  148. max_parallel_maintenance_workers = 6 # taken from max_parallel_workers
  149. # 如果需要使用并行查询,设置为大于1 ,不建议超过 主机cores-4
  150. # max_parallel_workers_per_gather 公式: min( max(2, CPU核数-4) , 24 )
  151. max_parallel_workers_per_gather = 0 # taken from max_parallel_workers
  152. # leader 是否与work process一起参与并行计算,如果ON,则并行度会默认+1
  153. parallel_leader_participation = on
  154. # 如果需要使用并行查询,设置为大于1 ,不建议超过 主机cores-2
  155. # 必须小于 max_worker_processes
  156. # max_parallel_workers 公式: max(2, CPU核数-4)
  157. max_parallel_workers = 32 # maximum number of max_worker_processes that
  158. # can be used in parallel operations
  159. # 是否启用snapshot too old技术,避免长事务导致的膨胀
  160. # 会导致性能一定的下降,约8%
  161. old_snapshot_threshold = 6h # 1min-60d; -1 disables; 0 is immediate
  162. # (change requires restart)
  163. #backend_flush_after = 256 # measured in pages, 0 disables
  164. #------------------------------------------------------------------------------
  165. # WRITE-AHEAD LOG
  166. #------------------------------------------------------------------------------
  167. # - Settings -
  168. # 需要流复制物理备库、归档、时间点恢复时,设置为replica,需要逻辑订阅或逻辑备库则设置为logical
  169. wal_level = replica # minimal, replica, or logical
  170. # (change requires restart)
  171. #fsync = on # flush data to disk for crash safety
  172. # (turning this off can cause
  173. # unrecoverable data corruption)
  174. # 如果双节点,设置为ON,如果是多副本,同步模式,建议设置为remote_write
  175. # 如果磁盘性能很差,并且是OLTP业务。可以考虑设置为off降低COMMIT的RT,提高吞吐(设置为OFF时,可能丢失部分XLOG RECORD)
  176. synchronous_commit = off # synchronization level;
  177. # off, local, remote_write, remote_apply, or on
  178. # 建议使用pg_test_fsync测试后,决定用哪个最快。通常LINUX下open_datasync比较快。
  179. #wal_sync_method = fsync # the default is the first option
  180. # supported by the operating system:
  181. # open_datasync
  182. # fdatasync (default on Linux)
  183. # fsync
  184. # fsync_writethrough
  185. # open_sync
  186. # 如果文件系统支持COW例如ZFS,则建议设置为OFF
  187. # 如果文件系统可以保证datafile block size的原子写,在文件系统与IO系统对齐后也可以设置为OFF
  188. # 如果底层存储能保证IO的原子写,也可以设置为OFF
  189. full_page_writes = on # recover from partial page writes
  190. # 当写FULL PAGE WRITE的io是瓶颈时建议开启
  191. wal_compression = on # enable compression of full-page writes
  192. # 如果要使用pg_rewind,flashback 时间线,需要打开这个功能
  193. # [《PostgreSQL pg_rewind,时间线修复,脑裂修复,flashback - 从库开启读写后,回退为只读从库。异步主从发生角色切换后,主库rewind为新主库的从库》](201901/20190128_02.md)
  194. #wal_log_hints = off # also do full page writes of non-critical updates
  195. # (change requires restart)
  196. # 建议 min( WAL segment size(默认16MB) , shared_buffers/32 )
  197. wal_buffers = 16MB # min 32kB, -1 sets based on shared_buffers
  198. # (change requires restart)
  199. # 如果设置了synchronous_commit = off,建议设置wal_writer_delay
  200. wal_writer_delay = 10ms # 1-10000 milliseconds
  201. wal_writer_flush_after = 1MB # measured in pages, 0 disables
  202. # 如果synchronous_commit=on, 并且已知业务系统为高并发,对数据库有写操作的小事务,则可以设置commit_delay来实现分组提交,合并WAL FSYNCIO 。
  203. # 分组提交
  204. #commit_delay = 10 # range 0-100000, in microseconds
  205. # 同时处于提交状态的事务数超过commit_siblings时,使用分组提交
  206. #commit_siblings = 5 # range 1-1000
  207. # - Checkpoints -
  208. # 不建议频繁做检查点,否则XLOG会产生很多的FULL PAGE WRITE(when full_page_writes=on)。
  209. checkpoint_timeout = 30min # range 30s-1d
  210. # 建议等于SHARED BUFFER,或2倍。
  211. # 同时需要考虑崩溃恢复时间, 越大,检查点可能拉越长导致崩溃恢复耗时越长。但是越小,开启FPW时,WAL日志写入量又越大。 建议采用COW文件系统,关闭FPW。
  212. # max_wal_size 公式: shared_buffers*2
  213. max_wal_size = 48GB
  214. # 建议是SHARED BUFFER的2分之一
  215. # min_wal_size 公式: shared_buffers/2
  216. min_wal_size = 12GB
  217. # 硬盘好(nvme ssd)的情况下,值越小可以让检查点快速结束,恢复时也可以快速达到一致状态。否则建议0.5~0.9
  218. # 如果有hot standby作为HA节点,这个值也可以设置为0.5~0.9 避免写高峰时CHECKPOINT对写带来的冲击。
  219. checkpoint_completion_target = 0.2 # checkpoint target duration, 0.0 - 1.0
  220. # IO很好的机器,不需要考虑平滑调度, 否则建议128~256kB
  221. checkpoint_flush_after = 256kB # measured in pages, 0 disables
  222. #checkpoint_flush_after = 0 # measured in pages, 0 disables
  223. #checkpoint_warning = 30s # 0 disables
  224. # - Archiving -
  225. # 建议默认打开,因为修改它需要重启实例
  226. # 打开后,一个WAL文件写满后,会在pg_wal/archive_status目录中创建xxxxxx.ready的文件,归档命令archive_command正常结束后,会清除这个状态文件。
  227. archive_mode = on # enables archiving; off, on, or always
  228. # (change requires restart)
  229. # 后期再修改,如 'test ! -f /disk1/digoal/arch/%f && cp %p /disk1/digoal/arch/%f'
  230. archive_command = '/bin/date' # command to use to archive a logfile segment
  231. # placeholders: %p = path of file to archive
  232. # %f = file name only
  233. # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f'
  234. #archive_timeout = 0 # force a logfile segment switch after this
  235. # number of seconds; 0 disables
  236. #------------------------------------------------------------------------------
  237. # REPLICATION
  238. #------------------------------------------------------------------------------
  239. # - Sending Servers -
  240. # Set these on the master and on any standby that will send replication data.
  241. # 同时允许几个流复制协议的连接,根据实际需求设定 ,可以设置一个默认值例如64
  242. max_wal_senders = 64 # max number of walsender processes
  243. # (change requires restart)
  244. # 根据实际情况设置保留WAL的数量,主要是防止过早的清除WAL,导致备库因为主库的WAL清除而中断。根据实际情况设定。
  245. #wal_keep_segments = 0 # in logfile segments; 0 disables
  246. #wal_sender_timeout = 60s # in milliseconds; 0 disables
  247. # 根据实际情况设置需要创建多少replication slot
  248. # 使用slot,可以保证流复制下游没有接收的WAL会在当前节点永久保留。所以必须留意下游的接收情况,否则可能导致WAL爆仓
  249. # 建议大于等于max_wal_senders
  250. # max_replication_slots 公式: max_replication_slots=max_wal_senders
  251. max_replication_slots = 64 # max number of replication slots
  252. # (change requires restart)
  253. #track_commit_timestamp = off # collect timestamp of transaction commit
  254. # (change requires restart)
  255. # - Master Server -
  256. # These settings are ignored on a standby server.
  257. # 如果有2个或2个以上的备库,可以考虑使用同步多副本模式。 根据实际情况设置
  258. # [《PostgreSQL 一主多从(多副本,强同步)简明手册 - 配置、压测、监控、切换、防脑裂、修复、0丢失 - 珍藏级》](201803/20180326_01.md)
  259. # [《PostgreSQL 双节点流复制如何同时保证可用性、可靠性(rpo,rto) - (半同步,自动降级方法实践)》](201901/20190127_01.md)
  260. #synchronous_standby_names = '' # standby servers that provide sync rep
  261. # method to choose sync standbys, number of sync standbys,
  262. # and comma-separated list of application_name
  263. # from standby(s); '*' = all
  264. # 注意,当数据库有大量的更新、删除操作时,设置这个值容易导致表膨胀,容易导致VACUUM进程空转,导致IO和CPU飙升。(特别是autovacuum naptime配置很小时)
  265. #vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed
  266. # - Standby Servers -
  267. # These settings are ignored on a master server.
  268. hot_standby = on # "off" disallows queries during recovery
  269. # (change requires restart)
  270. # 当standby的archive replay与standby的用户SQL请求发生冲突时,在打断SQL前,允许用户SQL最长的执行时间. 打断SQL后,需要等STANDBY APPLY所有archive restore command 的WAL文件,APPLY才允许被继续打断。
  271. max_standby_archive_delay = 120s # max delay before canceling queries
  272. # when reading WAL from archive;
  273. # -1 allows indefinite delay
  274. max_standby_streaming_delay = 120s # max delay before canceling queries
  275. # when reading streaming WAL;
  276. # -1 allows indefinite delay
  277. wal_receiver_status_interval = 1s # send replies at least this often
  278. # 0 disables
  279. # 建议关闭,以免备库长事务导致 主库无法回收垃圾而膨胀。
  280. [《PostgreSQL物理"备库"的哪些操作或配置,可能影响"主库"的性能、垃圾回收、IO波动》](201704/20170410_03.md)
  281. hot_standby_feedback = off # send info from standby to prevent
  282. # query conflicts
  283. #wal_receiver_timeout = 60s # time that receiver waits for
  284. # communication from master
  285. # in milliseconds; 0 disables
  286. #wal_retrieve_retry_interval = 5s # time to wait before retrying to
  287. # retrieve WAL after a failed attempt
  288. # - Subscribers -
  289. # 逻辑复制订阅端节点设置:
  290. # These settings are ignored on a publisher.
  291. # [《PostgreSQL 10.0 preview 逻辑订阅 - 原理与最佳实践》](201702/20170227_01.md)
  292. # These settings are ignored on a publisher.
  293. # 必须小于 max_worker_processes
  294. # 包括 apply workers and table synchronization workers
  295. # max_logical_replication_workers 公式: max_logical_replication_workers=max_wal_senders
  296. max_logical_replication_workers = 64 # taken from max_worker_processes
  297. # (change requires restart)
  298. # Maximum number of synchronization workers per subscription. This parameter controls the amount of parallelism of the initial data copy during the subscription initialization or when new tables are added.
  299. # max_sync_workers_per_subscription 公式: min ( 32 , max(2, CPU核数-4) )
  300. max_sync_workers_per_subscription = 8 # taken from max_logical_replication_workers
  301. #------------------------------------------------------------------------------
  302. # QUERY TUNING
  303. #------------------------------------------------------------------------------
  304. # - Planner Method Configuration -
  305. #enable_bitmapscan = on
  306. #enable_hashagg = on
  307. #enable_hashjoin = on
  308. #enable_indexscan = on
  309. #enable_indexonlyscan = on
  310. #enable_material = on
  311. #enable_mergejoin = on
  312. #enable_nestloop = on
  313. #enable_parallel_append = on
  314. #enable_seqscan = on
  315. #enable_sort = on
  316. #enable_tidscan = on
  317. # OLAP业务,建议设置为ON (enable_partitionwise_join, enable_partitionwise_aggregate)
  318. # [《PostgreSQL 并行计算解说 汇总》](201903/20190319_01.md)
  319. enable_partitionwise_join = on
  320. enable_partitionwise_aggregate = on
  321. #enable_parallel_hash = on
  322. #enable_partition_pruning = on
  323. # - Planner Cost Constants -
  324. #seq_page_cost = 1.0 # measured on an arbitrary scale
  325. # 离散IO很好的机器(例如ssd, nvme ssd),不需要考虑离散和顺序扫描的成本差异
  326. random_page_cost = 1.1 # same scale as above
  327. #cpu_tuple_cost = 0.01 # same scale as above
  328. #cpu_index_tuple_cost = 0.005 # same scale as above
  329. #cpu_operator_cost = 0.0025 # same scale as above
  330. #parallel_tuple_cost = 0.1 # same scale as above
  331. #parallel_setup_cost = 1000.0 # same scale as above
  332. #jit_above_cost = 100000 # perform JIT compilation if available
  333. # and query more expensive, -1 disables
  334. #jit_optimize_above_cost = 500000 # optimize JITed functions if query is
  335. # more expensive, -1 disables
  336. #jit_inline_above_cost = 500000 # attempt to inline operators and
  337. # functions if query is more expensive,
  338. # -1 disables
  339. #min_parallel_table_scan_size = 8MB
  340. #min_parallel_index_scan_size = 512kB
  341. # 扣掉会话连接RSS,shared buffer, autovacuum worker, 剩下的都是OS可用的CACHE。
  342. # effective_cache_size 公式: 主机内存/2
  343. effective_cache_size = 80GB
  344. # - Genetic Query Optimizer -
  345. #geqo = on
  346. #geqo_threshold = 12
  347. #geqo_effort = 5 # range 1-10
  348. #geqo_pool_size = 0 # selects default based on effort
  349. #geqo_generations = 0 # selects default based on effort
  350. #geqo_selection_bias = 2.0 # range 1.5-2.0
  351. #geqo_seed = 0.0 # range 0.0-1.0
  352. # - Other Planner Options -
  353. #default_statistics_target = 100 # range 1-10000
  354. #constraint_exclusion = partition # on, off, or partition
  355. #cursor_tuple_fraction = 0.1 # range 0.0-1.0
  356. #from_collapse_limit = 8
  357. #join_collapse_limit = 8 # 1 disables collapsing of explicit
  358. # JOIN clauses
  359. #force_parallel_mode = off
  360. #------------------------------------------------------------------------------
  361. # REPORTING AND LOGGING
  362. #------------------------------------------------------------------------------
  363. # - Where to Log -
  364. log_destination = 'csvlog' # Valid values are combinations of
  365. # stderr, csvlog, syslog, and eventlog,
  366. # depending on platform. csvlog
  367. # requires logging_collector to be on.
  368. # This is used when logging to stderr:
  369. logging_collector = on # Enable capturing of stderr and csvlog
  370. # into log files. Required to be on for
  371. # csvlogs.
  372. # (change requires restart)
  373. # These are only used if logging_collector is on:
  374. log_directory = 'log' # directory where log files are written,
  375. # can be absolute or relative to PGDATA
  376. # 时间格式 http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html
  377. # 设置参考 https://www.postgresql.org/docs/11/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-WHERE
  378. # 日志保留一周例子,每天一个文件
  379. log_filename = 'postgresql-%a.log' # log file name pattern,
  380. # can include strftime() escapes
  381. # 日志保留一个月例子,每天一个文件
  382. # log_filename = 'postgresql-%d.log' # log file name pattern,
  383. # can include strftime() escapes
  384. #log_file_mode = 0600 # creation mode for log files,
  385. # begin with 0 to use octal notation
  386. log_truncate_on_rotation = on # If on, an existing log file with the
  387. # same name as the new log file will be
  388. # truncated rather than appended to.
  389. # But such truncation only occurs on
  390. # time-driven rotation, not on restarts
  391. # or size-driven rotation. Default is
  392. # off, meaning append to existing files
  393. # in all cases.
  394. log_rotation_age = 1d # Automatic rotation of logfiles will
  395. # happen after that time. 0 disables.
  396. # 单个日志文件最大多大时,被截断,可以设置一个上限防止日志无限制增长
  397. log_rotation_size = 1GB
  398. # log_rotation_size = 0 # Automatic rotation of logfiles will
  399. # happen after that much log output.
  400. # 0 disables.
  401. # These are relevant when logging to syslog:
  402. #syslog_facility = 'LOCAL0'
  403. #syslog_ident = 'postgres'
  404. #syslog_sequence_numbers = on
  405. #syslog_split_messages = on
  406. # This is only relevant when logging to eventlog (win32):
  407. # (change requires restart)
  408. #event_source = 'PostgreSQL'
  409. # - When to Log -
  410. #client_min_messages = notice # values in order of decreasing detail:
  411. # debug5
  412. # debug4
  413. # debug3
  414. # debug2
  415. # debug1
  416. # log
  417. # notice
  418. # warning
  419. # error
  420. #log_min_messages = warning # values in order of decreasing detail:
  421. # debug5
  422. # debug4
  423. # debug3
  424. # debug2
  425. # debug1
  426. # info
  427. # notice
  428. # warning
  429. # error
  430. # log
  431. # fatal
  432. # panic
  433. #log_min_error_statement = error # values in order of decreasing detail:
  434. # debug5
  435. # debug4
  436. # debug3
  437. # debug2
  438. # debug1
  439. # info
  440. # notice
  441. # warning
  442. # error
  443. # log
  444. # fatal
  445. # panic (effectively off)
  446. # 根据实际情况设定,例如业务上认为5秒以上是慢SQL,那么就设置为5秒。
  447. log_min_duration_statement = 5s # -1 is disabled, 0 logs all statements
  448. # and their durations, > 0 logs only
  449. # statements running at least this number
  450. # of milliseconds
  451. # - What to Log -
  452. #debug_print_parse = off
  453. #debug_print_rewritten = off
  454. #debug_print_plan = off
  455. #debug_pretty_print = on
  456. # 记录检查点的详细统计信息
  457. log_checkpoints = on
  458. # 如果业务是短连接,建议设置为OFF,否则建议设置为ON
  459. log_connections = off
  460. # 如果业务是短连接,建议设置为OFF,否则建议设置为ON
  461. log_disconnections = off
  462. #log_duration = off
  463. # 记录错误代码的代码位置,是什么代码输出的日志,更好的跟踪问题
  464. log_error_verbosity = verbose # terse, default, or verbose messages
  465. #log_hostname = off
  466. #log_line_prefix = '%m [%p] ' # special values:
  467. # %a = application name
  468. # %u = user name
  469. # %d = database name
  470. # %r = remote host and port
  471. # %h = remote host
  472. # %p = process ID
  473. # %t = timestamp without milliseconds
  474. # %m = timestamp with milliseconds
  475. # %n = timestamp with milliseconds (as a Unix epoch)
  476. # %i = command tag
  477. # %e = SQL state
  478. # %c = session ID
  479. # %l = session line number
  480. # %s = session start timestamp
  481. # %v = virtual transaction ID
  482. # %x = transaction ID (0 if none)
  483. # %q = stop here in non-session
  484. # processes
  485. # %% = '%'
  486. # e.g. '<%u%%%d> '
  487. # 是否打印锁等待事件
  488. log_lock_waits = on # log lock waits >= deadlock_timeout
  489. # 如果需要审计SQL,则可以设置为all
  490. log_statement = 'ddl' # none, ddl, mod, all
  491. #log_replication_commands = off
  492. # 当使用的临时文件超过多大时,打印到日志中,跟踪大SQL。
  493. log_temp_files = 256MB # log temporary files equal or larger
  494. # than the specified size in kilobytes;
  495. # -1 disables, 0 logs all temp files
  496. log_timezone = 'PRC'
  497. #------------------------------------------------------------------------------
  498. # PROCESS TITLE
  499. #------------------------------------------------------------------------------
  500. #cluster_name = '' # added to process titles if nonempty
  501. # (change requires restart)
  502. #update_process_title = on
  503. #------------------------------------------------------------------------------
  504. # STATISTICS
  505. #------------------------------------------------------------------------------
  506. # - Query and Index Statistics Collector -
  507. #track_activities = on
  508. #track_counts = on
  509. # 跟踪IO耗时会带来一定的性能影响,默认是关闭的
  510. # 如果需要统计IO的时间开销,设置为ON
  511. # 建议用pg_test_timing测试一下获取时间的开销,如果开销很大,建议关闭这个时间跟踪。
  512. track_io_timing = on
  513. # 是否需要跟踪函数被调用的次数,耗时
  514. track_functions = pl # none, pl, all
  515. # 单条被跟踪的QUERY最多能存储多少字节,如果有超长SQL,则日志中被截断。 根据需要设置
  516. #track_activity_query_size = 1024 # (change requires restart)
  517. # 相对路径($PGDATA)或绝对路径。用于存储统计信息的临时目录。可以设置为ram based directory,提高性能
  518. # Pointing this at a RAM-based file system will decrease physical I/O requirements and can lead to improved performance.
  519. #stats_temp_directory = 'pg_stat_tmp'
  520. # - Monitoring -
  521. #log_parser_stats = off
  522. #log_planner_stats = off
  523. #log_executor_stats = off
  524. #log_statement_stats = off
  525. #------------------------------------------------------------------------------
  526. # AUTOVACUUM
  527. #------------------------------------------------------------------------------
  528. # 打开自动垃圾回收
  529. autovacuum = on # Enable autovacuum subprocess? 'on'
  530. # requires track_counts to also be on.
  531. log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and
  532. # their durations, > 0 logs only
  533. # actions running at least this number
  534. # of milliseconds.
  535. # CPU核多,并且IO好的情况下,可多点,但是注意最多可能消耗这么多内存:
  536. # autovacuum_max_workers * autovacuum mem(autovacuum_work_mem),
  537. # 会消耗较多内存,所以内存也要有基础。
  538. # 当DELETE\UPDATE非常频繁时,建议设置多一点,防止膨胀严重
  539. # autovacuum_max_workers 公式: max(min( 8 , CPU核数/2 ) , 5)
  540. autovacuum_max_workers = 8 # max number of autovacuum subprocesses
  541. # (change requires restart)
  542. # 建议不要太高频率,否则会因为vacuum产生较多的XLOG。或者在某些垃圾回收不掉的情况下(例如长事务、feed back on,等),导致一直触发vacuum,CPU和IO都会升高
  543. [《PostgreSQL垃圾回收代码分析 - why postgresql cann't reclaim tuple is HEAPTUPLE_RECENTLY_DEAD》](201505/20150503_01.md)
  544. [《PostgreSQL物理"备库"的哪些操作或配置,可能影响"主库"的性能、垃圾回收、IO波动》](201704/20170410_03.md)
  545. #autovacuum_naptime = 1min # time between autovacuum runs
  546. #autovacuum_vacuum_threshold = 50 # min number of row updates before
  547. # vacuum
  548. #autovacuum_analyze_threshold = 50 # min number of row updates before
  549. # analyze
  550. autovacuum_vacuum_scale_factor = 0.02 # fraction of table size before vacuum
  551. autovacuum_analyze_scale_factor = 0.01 # fraction of table size before analyze
  552. # 除了设置较大的FREEZE值。
  553. # 还是需要注意FREEZE风暴 [《PostgreSQL Freeze 风暴预测续 - 珍藏级SQL》](201804/20180411_01.md)
  554. # 表级定制freeze
  555. [《PostgreSQL 10 CLogControlLock 等待事件分析与优化 - hint bit, freeze, autovacuum, 风暴》](201903/20190319_02.md)
  556. autovacuum_freeze_max_age = 1200000000 # maximum XID age before forced vacuum
  557. # (change requires restart)
  558. autovacuum_multixact_freeze_max_age = 1250000000 # maximum multixact age
  559. # before forced vacuum
  560. # (change requires restart)
  561. # 如果数据库UPDATE非常频繁,建议设置为0。并且建议使用SSD
  562. autovacuum_vacuum_cost_delay = 0ms # default vacuum cost delay for
  563. # autovacuum, in milliseconds;
  564. # -1 means use vacuum_cost_delay
  565. #autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for
  566. # autovacuum, -1 means use
  567. # vacuum_cost_limit
  568. #------------------------------------------------------------------------------
  569. # CLIENT CONNECTION DEFAULTS
  570. #------------------------------------------------------------------------------
  571. # - Statement Behavior -
  572. #search_path = '"$user", public' # schema names
  573. #row_security = on
  574. #default_tablespace = '' # a tablespace name, '' uses the default
  575. # 临时表的表空间,可以设置多个,轮询使用。
  576. # 临时表的表空间,建议为SSD目录。速度快。
  577. #temp_tablespaces = '' # a list of tablespace names, '' uses
  578. # only default tablespace
  579. #check_function_bodies = on
  580. #default_transaction_isolation = 'read committed'
  581. #default_transaction_read_only = off
  582. #default_transaction_deferrable = off
  583. #session_replication_role = 'origin'
  584. # 可以用来防止雪崩,但是不建议全局设置
  585. #statement_timeout = 0 # in milliseconds, 0 is disabled
  586. # 执行DDL时,建议加上超时
  587. # 可以用来防止雪崩
  588. #lock_timeout = 0 # in milliseconds, 0 is disabled
  589. # 空闲中事务自动清理,根据业务实际情况设置
  590. idle_in_transaction_session_timeout = '6h' # in milliseconds, 0 is disabled
  591. #[《PostgreSQL 10 CLogControlLock 等待事件分析与优化 - hint bit, freeze, autovacuum, 风暴》](201903/20190319_02.md)
  592. #vacuum_freeze_min_age = 50000000
  593. vacuum_freeze_table_age = 200000000
  594. #vacuum_multixact_freeze_min_age = 5000000
  595. vacuum_multixact_freeze_table_age = 200000000
  596. # [《PostgreSQL 11 内核优化 - 降低vacuum cleanup阶段index scan概率 ( vacuum_cleanup_index_scale_factor , skip index vacuum cleanup stage)》](201902/20190201_03.md)
  597. #vacuum_cleanup_index_scale_factor = 0.1 # fraction of total number of tuples
  598. # before index cleanup, 0 always performs
  599. # index cleanup
  600. #bytea_output = 'hex' # hex, escape
  601. #xmlbinary = 'base64'
  602. #xmloption = 'content'
  603. # 限制GIN扫描的返回结果集大小,在想限制超多匹配的返回时可以设置
  604. #gin_fuzzy_search_limit = 0
  605. # GIN索引pending list的大小
  606. #gin_pending_list_limit = 4MB
  607. # - Locale and Formatting -
  608. datestyle = 'iso, mdy'
  609. #intervalstyle = 'postgres'
  610. timezone = 'PRC'
  611. #timezone_abbreviations = 'Default' # Select the set of available time zone
  612. # abbreviations. Currently, there are
  613. # Default
  614. # Australia (historical usage)
  615. # India
  616. # You can create your own file in
  617. # share/timezonesets/.
  618. # 浮点精度扩展值
  619. [《PostgreSQL 浮点精度调整(extra_float_digits)》](201510/20151020_01.md)
  620. #extra_float_digits = 0 # min -15, max 3
  621. #client_encoding = sql_ascii # actually, defaults to database
  622. # encoding
  623. # These settings are initialized by initdb, but they can be changed.
  624. lc_messages = 'C' # locale for system error message
  625. # strings
  626. lc_monetary = 'C' # locale for monetary formatting
  627. lc_numeric = 'C' # locale for number formatting
  628. lc_time = 'C' # locale for time formatting
  629. # default configuration for text search
  630. default_text_search_config = 'pg_catalog.english'
  631. # - Shared Library Preloading -
  632. # 需要加载什么LIB,预先加载,对于经常访问的库也建议预加载,例如postgis
  633. #shared_preload_libraries = 'pg_jieba,pipelinedb' # (change requires restart)
  634. shared_preload_libraries = 'pg_stat_statements'
  635. #local_preload_libraries = ''
  636. #session_preload_libraries = ''
  637. # - Other Defaults -
  638. #dynamic_library_path = '$libdir'
  639. # 是否已编译JIT特性,如果已编译,设置为ON
  640. #jit = off # allow JIT compilation
  641. #jit_provider = 'llvmjit' # JIT implementation to use
  642. #------------------------------------------------------------------------------
  643. # LOCK MANAGEMENT
  644. #------------------------------------------------------------------------------
  645. deadlock_timeout = 1s
  646. #max_locks_per_transaction = 64 # min 10
  647. # (change requires restart)
  648. #max_pred_locks_per_transaction = 64 # min 10
  649. # (change requires restart)
  650. #max_pred_locks_per_relation = -2 # negative values mean
  651. # (max_pred_locks_per_transaction
  652. # / -max_pred_locks_per_relation) - 1
  653. #max_pred_locks_per_page = 2 # min 0
  654. #------------------------------------------------------------------------------
  655. # VERSION AND PLATFORM COMPATIBILITY
  656. #------------------------------------------------------------------------------
  657. # - Previous PostgreSQL Versions -
  658. #array_nulls = on
  659. #backslash_quote = safe_encoding # on, off, or safe_encoding
  660. #default_with_oids = off
  661. # [《PostgreSQL 转义、UNICODE、与SQL注入》](201704/20170402_01.md)
  662. #escape_string_warning = on
  663. #lo_compat_privileges = off
  664. #operator_precedence_warning = off
  665. #quote_all_identifiers = off
  666. #standard_conforming_strings = on
  667. #synchronize_seqscans = on
  668. # - Other Platforms and Clients -
  669. # 是否启用 exp = null 自动转换为 exp is null
  670. # https://www.postgresql.org/docs/11/runtime-config-compatible.html#RUNTIME-CONFIG-COMPATIBLE-CLIENTS
  671. #transform_null_equals = off
  672. #------------------------------------------------------------------------------
  673. # ERROR HANDLING
  674. #------------------------------------------------------------------------------
  675. #exit_on_error = off # terminate session on any error?
  676. #restart_after_crash = on # reinitialize after backend crash?
  677. #------------------------------------------------------------------------------
  678. # CONFIG FILE INCLUDES
  679. #------------------------------------------------------------------------------
  680. # These options allow settings to be loaded from files other than the
  681. # default postgresql.conf.
  682. #include_dir = 'conf.d' # include files ending in '.conf' from
  683. # directory 'conf.d'
  684. #include_if_exists = 'exists.conf' # include file only if it exists
  685. #include = 'special.conf' # include file
  686. #------------------------------------------------------------------------------
  687. # CUSTOMIZED OPTIONS
  688. #------------------------------------------------------------------------------
  689. # Add settings for extensions here

小结

1、固定参数

  1. listen_addresses = '*'
  2. superuser_reserved_connections = 10
  3. unix_socket_directories = '., /tmp'
  4. unix_socket_permissions = 0700
  5. tcp_keepalives_idle = 60
  6. tcp_keepalives_interval = 10
  7. tcp_keepalives_count = 10
  8. password_encryption = md5 # 初始化时指定后,不建议修改。pg_hba.conf 也请使用相应认证方法。 同时需要客户端支持scram-sha-256认证方法
  9. huge_pages = try
  10. dynamic_shared_memory_type = posix
  11. vacuum_cost_delay = 0
  12. vacuum_cost_limit = 10000
  13. bgwriter_delay = 10ms
  14. bgwriter_lru_maxpages = 1000
  15. bgwriter_lru_multiplier = 10.0
  16. bgwriter_flush_after = 512kB
  17. effective_io_concurrency = 0
  18. max_worker_processes = 256
  19. parallel_leader_participation = on
  20. old_snapshot_threshold = 6h
  21. wal_level = replica
  22. synchronous_commit = off
  23. full_page_writes = on
  24. wal_compression = on
  25. wal_buffers = 16MB
  26. wal_writer_delay = 10ms
  27. wal_writer_flush_after = 1MB
  28. checkpoint_timeout = 30min
  29. checkpoint_completion_target = 0.2
  30. checkpoint_flush_after = 256kB
  31. archive_mode = on
  32. archive_command = '/bin/date'
  33. max_wal_senders = 64
  34. max_replication_slots = 64
  35. hot_standby = on
  36. max_standby_archive_delay = 120s
  37. max_standby_streaming_delay = 120s
  38. wal_receiver_status_interval = 1s
  39. hot_standby_feedback = off
  40. max_logical_replication_workers = 64
  41. enable_partitionwise_join = on
  42. enable_partitionwise_aggregate = on
  43. random_page_cost = 1.1
  44. log_destination = 'csvlog'
  45. logging_collector = on
  46. log_directory = 'log'
  47. log_filename = 'postgresql-%a.log'
  48. log_truncate_on_rotation = on
  49. log_rotation_age = 1d
  50. log_rotation_size = 1GB
  51. log_min_duration_statement = 5s
  52. log_checkpoints = on
  53. log_connections = off
  54. log_disconnections = off
  55. log_error_verbosity = verbose
  56. log_lock_waits = on
  57. log_statement = 'ddl'
  58. log_temp_files = 256MB
  59. track_io_timing = on
  60. track_functions = pl
  61. autovacuum = on
  62. log_autovacuum_min_duration = 0
  63. autovacuum_vacuum_scale_factor = 0.02
  64. autovacuum_analyze_scale_factor = 0.01
  65. autovacuum_freeze_max_age = 1200000000
  66. autovacuum_multixact_freeze_max_age = 1250000000
  67. autovacuum_vacuum_cost_delay = 0ms
  68. idle_in_transaction_session_timeout = '6h'
  69. vacuum_freeze_table_age = 200000000
  70. vacuum_multixact_freeze_table_age = 200000000
  71. default_text_search_config = 'pg_catalog.english'
  72. shared_preload_libraries = 'pg_stat_statements'
  73. deadlock_timeout = 1s

2、环境参数

  1. log_timezone = 'PRC'
  2. datestyle = 'iso, mdy'
  3. timezone = 'PRC'
  4. lc_messages = 'C'
  5. lc_monetary = 'C'
  6. lc_numeric = 'C'
  7. lc_time = 'C'

3、动态参数

  1. port = 1921 # 监听端口
  2. max_connections = 2000 # 物理内存(GB)*1000*(1/4)/5
  3. shared_buffers = 24GB # IF use hugepage: 主机内存*(1/4) ELSE: min(32GB, 主机内存*(1/4))
  4. max_prepared_transactions = 2000 # max_prepared_transactions=max_connections
  5. work_mem = 8MB # max(min(物理内存/4096, 64MB), 4MB)
  6. maintenance_work_mem = 2GB # min( 8G, (主机内存*1/8)/max_parallel_maintenance_workers )
  7. autovacuum_work_mem = 1GB # min( 8G, (主机内存*1/8)/autovacuum_max_workers )
  8. max_parallel_maintenance_workers = 6 # min( max(2, CPU核数/2) , 16 )
  9. max_parallel_workers_per_gather = 0 # min( max(2, CPU核数-4) , 24 )
  10. max_parallel_workers = 32 # max(2, CPU核数-4)
  11. max_wal_size = 48GB # shared_buffers*2
  12. min_wal_size = 12GB # shared_buffers/2
  13. max_sync_workers_per_subscription = 8 # min ( 32 , max(2, CPU核数-4) )
  14. effective_cache_size = 80GB # 主机内存/2
  15. autovacuum_max_workers = 8 # max(min( 8 , CPU核数/2 ) , 5)

配置例子

64G内存,16核,HUGE PAGE=on,SSD机器

  1. port = 1921 # 监听端口
  2. max_connections = 3200 # 物理内存(GB)*1000*(1/4)/5
  3. shared_buffers = 16GB # IF use hugepage: 主机内存*(1/4) ELSE: min(32GB, 主机内存*(1/4))
  4. max_prepared_transactions = 3200 # max_prepared_transactions=max_connections
  5. work_mem = 16MB # max(min(物理内存/4096, 64MB), 4MB)
  6. maintenance_work_mem = 1GB # min( 8G, (主机内存*1/8)/max_parallel_maintenance_workers )
  7. autovacuum_work_mem = 1GB # min( 8G, (主机内存*1/8)/autovacuum_max_workers )
  8. max_parallel_maintenance_workers = 8 # min( max(2, CPU核数/2) , 16 )
  9. max_parallel_workers_per_gather = 12 # min( max(2, CPU核数-4) , 24 )
  10. max_parallel_workers = 12 # max(2, CPU核数-4)
  11. max_wal_size = 32GB # shared_buffers*2
  12. min_wal_size = 8GB # shared_buffers/2
  13. max_sync_workers_per_subscription = 12 # min ( 32 , max(2, CPU核数-4) )
  14. effective_cache_size = 32GB # 主机内存/2
  15. autovacuum_max_workers = 8 # max(min( 8 , CPU核数/2 ) , 5)

其他参数值请拷贝以上固定参数与环境参数内容。

pg_hba.conf 数据库防火墙配置模板

  1. # TYPE DATABASE USER ADDRESS METHOD
  2. # "local" is for Unix domain socket connections only
  3. local all all trust
  4. # IPv4 local connections:
  5. host all all 127.0.0.1/32 trust
  6. # IPv6 local connections:
  7. host all all ::1/128 trust
  8. # Allow replication connections from localhost, by a user with the
  9. # replication privilege.
  10. local replication all trust
  11. host replication all 127.0.0.1/32 trust
  12. host replication all ::1/128 trust
  13. # 禁止超级用户从远程连接
  14. host all postgres 0.0.0.0/0 reject
  15. # 应用连接配置:哪个用户,从哪里来,连接什么数据库。规则为使用何种认证方法,或拒绝?
  16. # TYPE DATABASE USER ADDRESS METHOD
  17. # 如果不想挨个配置,可以使用如下配置,允许所有来源,通过任意用户访问任意数据库
  18. host all all 0.0.0.0/0 md5
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/IT小白/article/detail/256401
推荐阅读
相关标签
  

闽ICP备14008679号