CRIT
Patroni CRIT模板,针对金融场景、不允许数据丢失错漏的场景进行优化。
Patroni CRIT模板主要针对RPO进行优化,采用同步复制,发生故障时确保不会有数据丢失。
此模板针对的机型是Dell R740 64核/400GB内存,使用PCI-E SSD的节点。用户可以根据自己的实际机型进行调整。
#!/usr/bin/env patroni#==============================================================## File : patroni.yml# Ctime : 2020-04-08# Mtime : 2020-12-22# Desc : patroni cluster definition for {{ pg_cluster }} (crit)# Path : /pg/bin/patroni.yml# Real Path : /pg/conf/{{ pg_instance }}.yml# Link : /pg/bin/patroni.yml -> /pg/conf/{{ pg_instance}}.yml# Note : Critical Database Cluster Template# Doc : https://patroni.readthedocs.io/en/latest/SETTINGS.html# Copyright (C) 2018-2021 Ruohang Feng#==============================================================## CRIT database are optimized for security, integrity, RPO# typical spec: 64 Core | 400 GB RAM | PCI-E SSD xTB---#------------------------------------------------------------------------------# identity#------------------------------------------------------------------------------namespace: {{ pg_namespace }}/ # namespacescope: {{ pg_cluster }} # cluster namename: {{ pg_instance }} # instance name#------------------------------------------------------------------------------# log#------------------------------------------------------------------------------log:level: INFO # NOTEST|DEBUG|INFO|WARNING|ERROR|CRITICALdir: /pg/log/ # default log file: /pg/log/patroni.logfile_size: 100000000 # 100MB log triggers a log rotate# format: '%(asctime)s %(levelname)s: %(message)s'#------------------------------------------------------------------------------# dcs#------------------------------------------------------------------------------consul:host: 127.0.0.1:8500consistency: default # default|consistent|staleregister_service: trueservice_check_interval: 15sservice_tags:- {{ pg_cluster }}#------------------------------------------------------------------------------# api#------------------------------------------------------------------------------# how to expose patroni service# listen on all ipv4, connect via public ip, use same credential as dbuser_monitorrestapi:listen: 0.0.0.0:{{ patroni_port }}connect_address: {{ inventory_hostname }}:{{ patroni_port }}authentication:verify_client: none # none|optional|requiredusername: {{ pg_monitor_username }}password: '{{ pg_monitor_password }}'#------------------------------------------------------------------------------# ctl#------------------------------------------------------------------------------ctl:optional:insecure: true# cacert: '/path/to/ca/cert'# certfile: '/path/to/cert/file'# keyfile: '/path/to/key/file'#------------------------------------------------------------------------------# tags#------------------------------------------------------------------------------tags:nofailover: falseclonefrom: truenoloadbalance: falsenosync: false{% if pg_upstream is defined %}replicatefrom: {{ pg_upstream }} # clone from another replica rather than primary{% endif %}#------------------------------------------------------------------------------# watchdog#------------------------------------------------------------------------------# available mode: off|automatic|requiredwatchdog:mode: {{ patroni_watchdog_mode }}device: /dev/watchdog# safety_margin: 10s#------------------------------------------------------------------------------# bootstrap#------------------------------------------------------------------------------bootstrap:#----------------------------------------------------------------------------# bootstrap method#----------------------------------------------------------------------------method: initdb# add custom bootstrap method here# default bootstrap method: initdbinitdb:- locale: C- encoding: UTF8# - data-checksums # enable data-checksum#----------------------------------------------------------------------------# bootstrap users#---------------------------------------------------------------------------# additional users which need to be created after initializing new cluster# replication user and monitor user are requiredusers:{{ pg_replication_username }}:password: '{{ pg_replication_password }}'{{ pg_monitor_username }}:password: '{{ pg_monitor_password }}'{{ pg_admin_username }}:password: '{{ pg_admin_password }}'# bootstrap hba, allow local and intranet password access & replication# will be overwritten laterpg_hba:- local all postgres ident- local all all md5- host all all 0.0.0.0/0 md5- local replication postgres ident- local replication all md5- host replication all 0.0.0.0/0 md5#----------------------------------------------------------------------------# template#---------------------------------------------------------------------------# post_init: /pg/bin/pg-init#----------------------------------------------------------------------------# bootstrap config#---------------------------------------------------------------------------# this section will be written to /{{ pg_namespace }}/{{ pg_cluster }}/config# if will NOT take any effect after cluster bootstrapdcs:{% if pg_role == 'primary' and pg_upstream is defined %}#----------------------------------------------------------------------------# standby cluster definition#---------------------------------------------------------------------------standby_cluster:host: {{ pg_upstream }}port: {{ pg_port }}# primary_slot_name: patroni # must be create manually on upstream server, if specifiedcreate_replica_methods:- basebackup{% endif %}#----------------------------------------------------------------------------# important parameters#---------------------------------------------------------------------------# constraint: ttl >: loop_wait + retry_timeout * 2# the number of seconds the loop will sleep. Default value: 10# this is patroni check loop intervalloop_wait: 10# 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# config this according to your network condition to avoid false-positive failoverttl: 30# 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: 10retry_timeout: 10# the amount of time a master is allowed to recover from failures before failover is triggered (in seconds)# Max RTO: 2 loop wait + master_start_timeoutmaster_start_timeout: 120 # more patient on critical database# import: candidate will not be promoted if replication lag is higher than this# maximum RPO: 0 for critical databasemaximum_lag_on_failover: 1# The number of seconds Patroni is allowed to wait when stopping Postgres and effective only when synchronous_mode is enabledmaster_stop_timeout: 10 # more patient on critical database# 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# set to true for RPO modesynchronous_mode: true # use sync replication on critical database# prevents disabling synchronous replication if no synchronous replicas are available, blocking all client writes to the mastersynchronous_mode_strict: false#----------------------------------------------------------------------------# postgres parameters#---------------------------------------------------------------------------postgresql:use_slots: trueuse_pg_rewind: trueremove_data_directory_on_rewind_failure: trueparameters:#----------------------------------------------------------------------# IMPORTANT PARAMETERS#----------------------------------------------------------------------max_connections: 400 # 100 -> 400superuser_reserved_connections: 10 # reserve 10 connection for sumax_locks_per_transaction: 128 # 64 -> 128max_prepared_transactions: 0 # 0 disable 2PCtrack_commit_timestamp: on # enabled xact timestampmax_worker_processes: 8 # default 8, set to cpu corewal_level: logical # logicalwal_log_hints: on # wal log hints to support rewindmax_wal_senders: 16 # 10 -> 16max_replication_slots: 16 # 10 -> 16wal_keep_size: 100GB # keep at least 100GB WALpassword_encryption: md5 # use traditional md5 auth#----------------------------------------------------------------------# RESOURCE USAGE (except WAL)#----------------------------------------------------------------------# memory: shared_buffers and maintenance_work_mem will be dynamically setshared_buffers: {{ pg_shared_buffers }}maintenance_work_mem: {{ pg_maintenance_work_mem }}work_mem: 32MB # 4MB -> 32MBhuge_pages: try # try huge pagestemp_file_limit: 100GB # 0 -> 100GBvacuum_cost_delay: 2ms # wait 2ms per 10000 costvacuum_cost_limit: 10000 # 10000 cost each roundbgwriter_delay: 10ms # check dirty page every 10msbgwriter_lru_maxpages: 800 # 100 -> 800bgwriter_lru_multiplier: 5.0 # 2.0 -> 5.0 more cushion buffer#----------------------------------------------------------------------# WAL#----------------------------------------------------------------------wal_buffers: 16MB # max to 16MBwal_writer_delay: 20ms # wait periodwal_writer_flush_after: 1MB # max allowed data lossmin_wal_size: 100GB # at least 100GB WALmax_wal_size: 400GB # at most 400GB WALcommit_delay: 20 # 200ms -> 20ms, increase speedcommit_siblings: 10 # 5 -> 10checkpoint_timeout: 60min # checkpoint 5min -> 1hcheckpoint_completion_target: 0.95 # 0.5 -> 0.95archive_mode: onarchive_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'#----------------------------------------------------------------------# REPLICATION#----------------------------------------------------------------------# synchronous_standby_names: ''vacuum_defer_cleanup_age: 50000 # 0->50000 last 50000 xact changes will not be vacuumedpromote_trigger_file: promote.signal # default promote trigger file pathmax_standby_archive_delay: 10min # max delay before canceling queries when reading WAL from archive;max_standby_streaming_delay: 3min # max delay before canceling queries when reading streaming WAL;wal_receiver_status_interval: 1s # send replies at least this oftenhot_standby_feedback: on # send info from standby to prevent query conflictswal_receiver_timeout: 60s # time that receiver waits formax_logical_replication_workers: 8 # 4 -> 8max_sync_workers_per_subscription: 8 # 4 -> 8#----------------------------------------------------------------------# QUERY TUNING#----------------------------------------------------------------------# planner# enable_partitionwise_join: onrandom_page_cost: 1.1 # 4 for HDD, 1.1 for SSDeffective_cache_size: 320GB # max mem - shared bufferdefault_statistics_target: 1000 # stat bucket 100 -> 1000#----------------------------------------------------------------------# REPORTING AND LOGGING#----------------------------------------------------------------------log_destination: csvlog # use standard csv loglogging_collector: on # enable csvloglog_directory: log # default log dir: /pg/data/log# log_filename: 'postgresql-%a.log' # weekly auto-recyclelog_filename: 'postgresql-%Y-%m-%d.log' # YYYY-MM-DD full log retentionlog_checkpoints: on # log checkpoint infolog_lock_waits: on # log lock wait infolog_replication_commands: on # log replication infolog_statement: ddl # log ddl changelog_min_duration_statement: 100 # log slow query (>100ms)#----------------------------------------------------------------------# STATISTICS#----------------------------------------------------------------------track_io_timing: on # collect io statisticstrack_functions: all # track all functions (none|pl|all)track_activity_query_size: 32768 # show full query on critical database#----------------------------------------------------------------------# AUTOVACUUM#----------------------------------------------------------------------log_autovacuum_min_duration: 1s # log autovacuum activity take more than 1sautovacuum_max_workers: 3 # default autovacuum worker 3autovacuum_naptime: 1min # default autovacuum naptime 1minautovacuum_vacuum_scale_factor: 0.08 # fraction of table size before vacuum 20% -> 8%autovacuum_analyze_scale_factor: 0.04 # fraction of table size before analyze 10% -> 4%autovacuum_vacuum_cost_delay: -1 # default vacuum cost delay: same as vacuum_cost_delayautovacuum_vacuum_cost_limit: -1 # default vacuum cost limit: same as vacuum_cost_limitautovacuum_freeze_max_age: 100000000 # age > 1 billion triggers force vacuum#----------------------------------------------------------------------# CLIENT#----------------------------------------------------------------------deadlock_timeout: 50ms # 50ms for deadlockidle_in_transaction_session_timeout: 1min # 1min timeout for idle in transaction in critical database#----------------------------------------------------------------------# CUSTOMIZED OPTIONS#----------------------------------------------------------------------# extensionsshared_preload_libraries: '{{ pg_shared_libraries | default("pg_stat_statements, auto_explain") }}'# auto_explainauto_explain.log_min_duration: 1s # auto explain query slower than 1sauto_explain.log_analyze: true # explain analyzeauto_explain.log_verbose: true # explain verboseauto_explain.log_timing: true # explain timingauto_explain.log_nested_statements: true# pg_stat_statementspg_stat_statements.max: 10000 # 5000 -> 10000 queriespg_stat_statements.track: all # track all statements (all|top|none)pg_stat_statements.track_utility: on # TRACK all queries on critical databasepg_stat_statements.track_planning: off # do not track planning metrics#------------------------------------------------------------------------------# postgres#------------------------------------------------------------------------------postgresql:#----------------------------------------------------------------------------# how to connect to postgres#----------------------------------------------------------------------------bin_dir: {{ pg_bin_dir }}data_dir: {{ pg_data }}config_dir: {{ pg_data }}pgpass: {{ pg_dbsu_home }}/.pgpasslisten: {{ pg_listen }}:{{ pg_port }}connect_address: {{ inventory_hostname }}:{{ pg_port }}use_unix_socket: true # default: /var/run/postgresql, /tmp#----------------------------------------------------------------------------# who to connect to postgres#----------------------------------------------------------------------------authentication:superuser:username: {{ pg_dbsu }}replication:username: {{ pg_replication_username }}password: '{{ pg_replication_password }}'rewind:username: {{ pg_replication_username }}password: '{{ pg_replication_password }}'#----------------------------------------------------------------------------# how to react to database operations#----------------------------------------------------------------------------# event callback script log: /pg/log/callback.logcallbacks:on_start: /pg/bin/pg-failover-callbackon_stop: /pg/bin/pg-failover-callbackon_reload: /pg/bin/pg-failover-callbackon_restart: /pg/bin/pg-failover-callbackon_role_change: /pg/bin/pg-failover-callback# rewind policy: data checksum should be enabled before using rewinduse_pg_rewind: trueremove_data_directory_on_rewind_failure: trueremove_data_directory_on_diverged_timelines: false#----------------------------------------------------------------------------# how to create replica#----------------------------------------------------------------------------# create replica method: default pg_basebackupcreate_replica_methods:- basebackupbasebackup:- max-rate: '1000M'- checkpoint: fast- status-interva: 1s- verbose- progress#----------------------------------------------------------------------------# ad hoc parameters (overwrite with default)#----------------------------------------------------------------------------# parameters:#----------------------------------------------------------------------------# host based authentication, overwrite default pg_hba.conf#----------------------------------------------------------------------------# pg_hba:# - local all postgres ident# - local all all md5# - host all all 0.0.0.0/0 md5# - local replication postgres ident# - local replication all md5# - host replication all 0.0.0.0/0 md5...
Last modified 2022-05-27: init commit (1e3e284)