TINY

Patroni TINY模板

Patroni TINY模板主要针对极低配置的虚拟机进行优化,

此模板针对的典型机型是1核/1GB的虚拟机节点。您可以根据自己的实际机型进行调整。

  1. #!/usr/bin/env patroni
  2. #==============================================================#
  3. # File : patroni.yml
  4. # Ctime : 2020-04-08
  5. # Mtime : 2020-12-22
  6. # Desc : patroni cluster definition for {{ pg_cluster }} (tiny)
  7. # Path : /pg/bin/patroni.yml
  8. # Real Path : /pg/conf/{{ pg_instance }}.yml
  9. # Link : /pg/bin/patroni.yml -> /pg/conf/{{ pg_instance}}.yml
  10. # Note : Tiny Database Cluster Template
  11. # Doc : https://patroni.readthedocs.io/en/latest/SETTINGS.html
  12. # Copyright (C) 2018-2021 Ruohang Feng
  13. #==============================================================#
  14. # TINY database are optimized for low-resource situation (e.g 1 Core 1G)
  15. # typical spec: 1 Core | 1-4 GB RAM | Normal SSD 10x GB
  16. ---
  17. #------------------------------------------------------------------------------
  18. # identity
  19. #------------------------------------------------------------------------------
  20. namespace: {{ pg_namespace }}/ # namespace
  21. scope: {{ pg_cluster }} # cluster name
  22. name: {{ pg_instance }} # instance name
  23. #------------------------------------------------------------------------------
  24. # log
  25. #------------------------------------------------------------------------------
  26. log:
  27. level: INFO # NOTEST|DEBUG|INFO|WARNING|ERROR|CRITICAL
  28. dir: /pg/log/ # default log file: /pg/log/patroni.log
  29. file_size: 100000000 # 100MB log triggers a log rotate
  30. # format: '%(asctime)s %(levelname)s: %(message)s'
  31. #------------------------------------------------------------------------------
  32. # dcs
  33. #------------------------------------------------------------------------------
  34. consul:
  35. host: 127.0.0.1:8500
  36. consistency: default # default|consistent|stale
  37. register_service: true
  38. service_check_interval: 15s
  39. service_tags:
  40. - {{ pg_cluster }}
  41. #------------------------------------------------------------------------------
  42. # api
  43. #------------------------------------------------------------------------------
  44. # how to expose patroni service
  45. # listen on all ipv4, connect via public ip, use same credential as dbuser_monitor
  46. restapi:
  47. listen: 0.0.0.0:{{ patroni_port }}
  48. connect_address: {{ inventory_hostname }}:{{ patroni_port }}
  49. authentication:
  50. verify_client: none # none|optional|required
  51. username: {{ pg_monitor_username }}
  52. password: '{{ pg_monitor_password }}'
  53. #------------------------------------------------------------------------------
  54. # ctl
  55. #------------------------------------------------------------------------------
  56. ctl:
  57. optional:
  58. insecure: true
  59. # cacert: '/path/to/ca/cert'
  60. # certfile: '/path/to/cert/file'
  61. # keyfile: '/path/to/key/file'
  62. #------------------------------------------------------------------------------
  63. # tags
  64. #------------------------------------------------------------------------------
  65. tags:
  66. nofailover: false
  67. clonefrom: true
  68. noloadbalance: false
  69. nosync: false
  70. {% if pg_upstream is defined %}
  71. replicatefrom: {{ pg_upstream }} # clone from another replica rather than primary
  72. {% endif %}
  73. #------------------------------------------------------------------------------
  74. # watchdog
  75. #------------------------------------------------------------------------------
  76. # available mode: off|automatic|required
  77. watchdog:
  78. mode: {{ patroni_watchdog_mode }}
  79. device: /dev/watchdog
  80. # safety_margin: 10s
  81. #------------------------------------------------------------------------------
  82. # bootstrap
  83. #------------------------------------------------------------------------------
  84. bootstrap:
  85. #----------------------------------------------------------------------------
  86. # bootstrap method
  87. #----------------------------------------------------------------------------
  88. method: initdb
  89. # add custom bootstrap method here
  90. # default bootstrap method: initdb
  91. initdb:
  92. - locale: C
  93. - encoding: UTF8
  94. - data-checksums # enable data-checksum
  95. #----------------------------------------------------------------------------
  96. # bootstrap users
  97. #---------------------------------------------------------------------------
  98. # additional users which need to be created after initializing new cluster
  99. # replication user and monitor user are required
  100. users:
  101. {{ pg_replication_username }}:
  102. password: '{{ pg_replication_password }}'
  103. {{ pg_monitor_username }}:
  104. password: '{{ pg_monitor_password }}'
  105. # bootstrap hba, allow local and intranet password access & replication
  106. # will be overwritten later
  107. pg_hba:
  108. - local all postgres ident
  109. - local all all md5
  110. - host all all 0.0.0.0/0 md5
  111. - local replication postgres ident
  112. - local replication all md5
  113. - host replication all 0.0.0.0/0 md5
  114. #----------------------------------------------------------------------------
  115. # customization
  116. #---------------------------------------------------------------------------
  117. # post_init: /pg/bin/pg-init
  118. #----------------------------------------------------------------------------
  119. # bootstrap config
  120. #---------------------------------------------------------------------------
  121. # this section will be written to /{{ pg_namespace }}/{{ pg_cluster }}/config
  122. # if will NOT take any effect after cluster bootstrap
  123. dcs:
  124. {% if pg_role == 'primary' and pg_upstream is defined %}
  125. #----------------------------------------------------------------------------
  126. # standby cluster definition
  127. #---------------------------------------------------------------------------
  128. standby_cluster:
  129. host: {{ pg_upstream }}
  130. port: {{ pg_port }}
  131. # primary_slot_name: patroni # must be create manually on upstream server, if specified
  132. create_replica_methods:
  133. - basebackup
  134. {% endif %}
  135. #----------------------------------------------------------------------------
  136. # important parameters
  137. #---------------------------------------------------------------------------
  138. # constraint: ttl >: loop_wait + retry_timeout * 2
  139. # the number of seconds the loop will sleep. Default value: 10
  140. # this is patroni check loop interval
  141. loop_wait: 10
  142. # the TTL to acquire the leader lock (in seconds). Think of it as the length of time before initiation of the automatic failover process. Default value: 30
  143. # config this according to your network condition to avoid false-positive failover
  144. ttl: 30
  145. # timeout for DCS and PostgreSQL operation retries (in seconds). DCS or network issues shorter than this will not cause Patroni to demote the leader. Default value: 10
  146. retry_timeout: 10
  147. # the amount of time a master is allowed to recover from failures before failover is triggered (in seconds)
  148. # Max RTO: 2 loop wait + master_start_timeout
  149. master_start_timeout: 10
  150. # import: candidate will not be promoted if replication lag is higher than this
  151. # maximum RPO: 1MB
  152. maximum_lag_on_failover: 1048576
  153. # The number of seconds Patroni is allowed to wait when stopping Postgres and effective only when synchronous_mode is enabled
  154. master_stop_timeout: 30
  155. # turns on synchronous replication mode. In this mode a replica will be chosen as synchronous and only the latest leader and synchronous replica are able to participate in leader election
  156. # set to true for RPO mode
  157. synchronous_mode: false
  158. # prevents disabling synchronous replication if no synchronous replicas are available, blocking all client writes to the master
  159. synchronous_mode_strict: false
  160. #----------------------------------------------------------------------------
  161. # postgres parameters
  162. #---------------------------------------------------------------------------
  163. postgresql:
  164. use_slots: true
  165. use_pg_rewind: true
  166. remove_data_directory_on_rewind_failure: true
  167. parameters:
  168. #----------------------------------------------------------------------
  169. # IMPORTANT PARAMETERS
  170. #----------------------------------------------------------------------
  171. max_connections: 50 # default 100 -> 50
  172. superuser_reserved_connections: 10 # reserve 10 connection for su
  173. max_locks_per_transaction: 64 # default 64
  174. max_prepared_transactions: 0 # 0 disable 2PC
  175. track_commit_timestamp: on # enabled xact timestamp
  176. max_worker_processes: 1 # default 8 -> 1 (set to cpu core)
  177. wal_level: logical # logical
  178. wal_log_hints: on # wal log hints to support rewind
  179. max_wal_senders: 10 # default 10
  180. max_replication_slots: 10 # default 10
  181. wal_keep_size: 1GB # keep at least 1GB WAL
  182. password_encryption: md5 # use traditional md5 auth
  183. #----------------------------------------------------------------------
  184. # RESOURCE USAGE (except WAL)
  185. #----------------------------------------------------------------------
  186. # memory: shared_buffers and maintenance_work_mem will be dynamically set
  187. shared_buffers: {{ pg_shared_buffers }}
  188. maintenance_work_mem: {{ pg_maintenance_work_mem }}
  189. work_mem: 4MB # default 4MB
  190. huge_pages: try # try huge pages
  191. temp_file_limit: 40GB # 0 -> 40GB (according to your disk)
  192. vacuum_cost_delay: 5ms # wait 5ms per 10000 cost
  193. vacuum_cost_limit: 10000 # 10000 cost each round
  194. bgwriter_delay: 10ms # check dirty page every 10ms
  195. bgwriter_lru_maxpages: 800 # 100 -> 800
  196. bgwriter_lru_multiplier: 5.0 # 2.0 -> 5.0 more cushion buffer
  197. #----------------------------------------------------------------------
  198. # WAL
  199. #----------------------------------------------------------------------
  200. wal_buffers: 16MB # max to 16MB
  201. wal_writer_delay: 20ms # wait period
  202. wal_writer_flush_after: 1MB # max allowed data loss
  203. min_wal_size: 100GB # at least 100GB WAL
  204. max_wal_size: 400GB # at most 400GB WAL
  205. commit_delay: 20 # 200ms -> 20ms, increase speed
  206. commit_siblings: 10 # 5 -> 10
  207. checkpoint_timeout: 15min # checkpoint 5min -> 15min
  208. checkpoint_completion_target: 0.80 # 0.5 -> 0.8
  209. archive_mode: on
  210. archive_command: 'wal_dir=/pg/arcwal; [[ $(date +%H%M) == 1200 ]] && rm -rf ${wal_dir}/$(date -d"yesterday" +%Y%m%d); /bin/mkdir -p ${wal_dir}/$(date +%Y%m%d) && /usr/bin/lz4 -q -z %p > ${wal_dir}/$(date +%Y%m%d)/%f.lz4'
  211. #----------------------------------------------------------------------
  212. # REPLICATION
  213. #----------------------------------------------------------------------
  214. # synchronous_standby_names: ''
  215. vacuum_defer_cleanup_age: 50000 # 0->50000 last 50000 xact changes will not be vacuumed
  216. promote_trigger_file: promote.signal # default promote trigger file path
  217. max_standby_archive_delay: 10min # max delay before canceling queries when reading WAL from archive;
  218. max_standby_streaming_delay: 3min # max delay before canceling queries when reading streaming WAL;
  219. wal_receiver_status_interval: 1s # send replies at least this often
  220. hot_standby_feedback: on # send info from standby to prevent query conflicts
  221. wal_receiver_timeout: 60s # time that receiver waits for
  222. max_logical_replication_workers: 8 # 4 -> 2 (set according to your cpu core)
  223. max_sync_workers_per_subscription: 8 # 4 -> 2
  224. #----------------------------------------------------------------------
  225. # QUERY TUNING
  226. #----------------------------------------------------------------------
  227. # planner
  228. # enable_partitionwise_join: on
  229. random_page_cost: 1.1 # 4 for HDD, 1.1 for SSD
  230. effective_cache_size: 2GB # max mem - shared buffer
  231. default_statistics_target: 200 # stat bucket 100 -> 200
  232. #----------------------------------------------------------------------
  233. # REPORTING AND LOGGING
  234. #----------------------------------------------------------------------
  235. log_destination: csvlog # use standard csv log
  236. logging_collector: on # enable csvlog
  237. log_directory: log # default log dir: /pg/data/log
  238. # log_filename: 'postgresql-%a.log' # weekly auto-recycle
  239. log_filename: 'postgresql-%Y-%m-%d.log' # YYYY-MM-DD full log retention
  240. log_checkpoints: on # log checkpoint info
  241. log_lock_waits: on # log lock wait info
  242. log_replication_commands: on # log replication info
  243. log_statement: ddl # log ddl change
  244. log_min_duration_statement: 100 # log slow query (>100ms)
  245. #----------------------------------------------------------------------
  246. # STATISTICS
  247. #----------------------------------------------------------------------
  248. track_io_timing: on # collect io statistics
  249. track_functions: all # track all functions (none|pl|all)
  250. track_activity_query_size: 8192 # max query length in pg_stat_activity
  251. #----------------------------------------------------------------------
  252. # AUTOVACUUM
  253. #----------------------------------------------------------------------
  254. log_autovacuum_min_duration: 1s # log autovacuum activity take more than 1s
  255. autovacuum_max_workers: 1 # default autovacuum worker 3 -> 1
  256. autovacuum_naptime: 1min # default autovacuum naptime 1min
  257. autovacuum_vacuum_scale_factor: 0.08 # fraction of table size before vacuum 20% -> 8%
  258. autovacuum_analyze_scale_factor: 0.04 # fraction of table size before analyze 10% -> 4%
  259. autovacuum_vacuum_cost_delay: -1 # default vacuum cost delay: same as vacuum_cost_delay
  260. autovacuum_vacuum_cost_limit: -1 # default vacuum cost limit: same as vacuum_cost_limit
  261. autovacuum_freeze_max_age: 100000000 # age > 1 billion triggers force vacuum
  262. #----------------------------------------------------------------------
  263. # CLIENT
  264. #----------------------------------------------------------------------
  265. deadlock_timeout: 50ms # 50ms for deadlock
  266. idle_in_transaction_session_timeout: 10min # 10min timeout for idle in transaction
  267. #----------------------------------------------------------------------
  268. # CUSTOMIZED OPTIONS
  269. #----------------------------------------------------------------------
  270. # extensions
  271. shared_preload_libraries: '{{ pg_shared_libraries | default("pg_stat_statements, auto_explain") }}'
  272. # auto_explain
  273. auto_explain.log_min_duration: 1s # auto explain query slower than 1s
  274. auto_explain.log_analyze: true # explain analyze
  275. auto_explain.log_verbose: true # explain verbose
  276. auto_explain.log_timing: true # explain timing
  277. auto_explain.log_nested_statements: true
  278. # pg_stat_statements
  279. pg_stat_statements.max: 3000 # 5000 -> 3000 queries
  280. pg_stat_statements.track: all # track all statements (all|top|none)
  281. pg_stat_statements.track_utility: off # do not track query other than CRUD
  282. pg_stat_statements.track_planning: off # do not track planning metrics
  283. #------------------------------------------------------------------------------
  284. # postgres
  285. #------------------------------------------------------------------------------
  286. postgresql:
  287. #----------------------------------------------------------------------------
  288. # how to connect to postgres
  289. #----------------------------------------------------------------------------
  290. bin_dir: {{ pg_bin_dir }}
  291. data_dir: {{ pg_data }}
  292. config_dir: {{ pg_data }}
  293. pgpass: {{ pg_dbsu_home }}/.pgpass
  294. listen: {{ pg_listen }}:{{ pg_port }}
  295. connect_address: {{ inventory_hostname }}:{{ pg_port }}
  296. use_unix_socket: true # default: /var/run/postgresql, /tmp
  297. #----------------------------------------------------------------------------
  298. # who to connect to postgres
  299. #----------------------------------------------------------------------------
  300. authentication:
  301. superuser:
  302. username: {{ pg_dbsu }}
  303. replication:
  304. username: {{ pg_replication_username }}
  305. password: '{{ pg_replication_password }}'
  306. rewind:
  307. username: {{ pg_replication_username }}
  308. password: '{{ pg_replication_password }}'
  309. #----------------------------------------------------------------------------
  310. # how to react to database operations
  311. #----------------------------------------------------------------------------
  312. # event callback script log: /pg/log/callback.log
  313. callbacks:
  314. on_start: /pg/bin/pg-failover-callback
  315. on_stop: /pg/bin/pg-failover-callback
  316. on_reload: /pg/bin/pg-failover-callback
  317. on_restart: /pg/bin/pg-failover-callback
  318. on_role_change: /pg/bin/pg-failover-callback
  319. # rewind policy: data checksum should be enabled before using rewind
  320. use_pg_rewind: true
  321. remove_data_directory_on_rewind_failure: true
  322. remove_data_directory_on_diverged_timelines: false
  323. #----------------------------------------------------------------------------
  324. # how to create replica
  325. #----------------------------------------------------------------------------
  326. # create replica method: default pg_basebackup
  327. create_replica_methods:
  328. - basebackup
  329. basebackup:
  330. - max-rate: '1000M'
  331. - checkpoint: fast
  332. - status-interva: 1s
  333. - verbose
  334. - progress
  335. #----------------------------------------------------------------------------
  336. # ad hoc parameters (overwrite with default)
  337. #----------------------------------------------------------------------------
  338. # parameters:
  339. #----------------------------------------------------------------------------
  340. # host based authentication, overwrite default pg_hba.conf
  341. #----------------------------------------------------------------------------
  342. # pg_hba:
  343. # - local all postgres ident
  344. # - local all all md5
  345. # - host all all 0.0.0.0/0 md5
  346. # - local replication postgres ident
  347. # - local replication all md5
  348. # - host replication all 0.0.0.0/0 md5
  349. ...

最后修改 2022-05-27: init commit (1e3e284)