先說一下配置文档
postgresql.conf配置解釋
# ----------------------------- # PostgreSQL configuration file # ----------------------------- # # This file consists of lines of the form: # # name = value # # (The "=" is optional.) Whitespace may be used. Comments are introduced with # "#" anywhere on a line. The complete list of parameter names and allowed # values can be found in the PostgreSQL documentation. # # The commented-out settings shown in this file represent the default values. # Re-commenting a setting is NOT sufficient to revert it to the default value; # you need to reload the server. # # This file is read on server startup and when the server receives a SIGHUP # signal. If you edit the file on a running system, you have to SIGHUP the # server for the changes to take effect, run "pg_ctl reload", or execute # "SELECT pg_reload_conf()". Some parameters, which are marked below, # require a server shutdown and restart to take effect. # # Any parameter can also be given as a command-line option to the server, e.g., # "postgres -c log_connections=on". Some parameters can be changed at run time # with the "SET" SQL command. # # Memory units: kB = kilobytes Time units: ms = milliseconds # MB = megabytes s = seconds # GB = gigabytes min = minutes # TB = terabytes h = hours # d = days #------------------------------------------------------------------------------ # FILE LOCATIONS #------------------------------------------------------------------------------ # The default values of these variables are driven from the -D command-line # option or PGDATA environment variable, represented here as ConfigDir. #data_directory = 'ConfigDir' # use data in another directory # (change requires restart) #hba_file = 'ConfigDir/pg_hba.conf' # host-based authentication file # (change requires restart) #ident_file = 'ConfigDir/pg_ident.conf' # ident configuration file # (change requires restart) # If external_pid_file is not explicitly set, no extra PID file is written. #external_pid_file = '' # write an extra PID file # (change requires restart) #------------------------------------------------------------------------------ # CONNECTIONS AND AUTHENTICATION #------------------------------------------------------------------------------ # - Connection Settings - # 監聽 listen_addresses = '0.0.0.0' # what IP address(es) to listen on; # comma-separated list of addresses; # defaults to 'localhost'; use '*' for all # (change requires restart) # 根據業務需求設定監聽耑口 port = 1921 # (change requires restart) # 比較安全的值:建議不要大於 200 * 四分之一物理内存(GB), 例如四分之一物理内存爲16G,則建議不要超過3200. # (假設平均一個連接耗費5MB。 實際上syscache很大、SQL 使用到WORK_MEM,未使用hugepage並且訪問到大量shared buffer page時,可能消耗更多内存) # 如果業務有更多並發連接,可以使用連接池,例如pgbouncer # [《PostgreSQL relcache在長連接應用中的内存霸佔"坑"》](201607/20160709_01.md) # max_connections 公式:物理内存(GB)*1000*(1/4)/5 max_connections = 2000 # (change requires restart) # 爲超級用戶保留多少個連接 superuser_reserved_connections = 10 # (change requires restart) # $PGDATA, /tmp中 創建unix socket監聽 unix_socket_directories = '., /tmp' # comma-separated list of directories # (change requires restart) #unix_socket_group = '' # (change requires restart) # 除了OWNER和超級用戶,其他用戶無法從/tmp unix socket連接該實例 unix_socket_permissions = 0700 # begin with 0 to use octal notation # (change requires restart) #bonjour = off # advertise server via Bonjour # (change requires restart) #bonjour_name = '' # defaults to the computer name # (change requires restart) # - TCP Keepalives - # see "man 7 tcp" for details # 如果你連接數據庫空閑一段時間會耑口,可能是網路中存在會話超時的設備,建議可以設置一下這個心跳時間,TCP心跳間隔會縮短到60秒。 tcp_keepalives_idle = 60 # TCP_KEEPIDLE, in seconds; # 0 selects the system default tcp_keepalives_interval = 10 # TCP_KEEPINTVL, in seconds; # 0 selects the system default tcp_keepalives_count = 10 # TCP_KEEPCNT; # 0 selects the system default # - Authentication - #authentication_timeout = 1min # 1s-600s # md5 or scram-sha-256 # 如果用戶密碼的MD5會泄露,建議使用scram-sha-256,但是相互不兼容,請注意。 # [《PostgreSQL 10.0 preview 安全增強 - SASL認证方法 之 scram-sha-256 安全認证機制》](201703/20170309_01.md) password_encryption = md5 # md5 or scram-sha-256 #db_user_namespace = off # GSSAPI using Kerberos #krb_server_keyfile = '' #krb_caseins_users = off # - SSL - #ssl = off #ssl_ca_file = '' #ssl_cert_file = 'server.crt' #ssl_crl_file = '' #ssl_key_file = 'server.key' #ssl_ciphers = 'HIGH:MEDIUM:+3DES:!aNULL' # allowed SSL ciphers #ssl_prefer_server_ciphers = on #ssl_ecdh_curve = 'prime256v1' #ssl_dh_params_file = '' #ssl_passphrase_command = '' #ssl_passphrase_command_supports_reload = off #------------------------------------------------------------------------------ # RESOURCE USAGE (except WAL) #------------------------------------------------------------------------------ # - Memory - # 1/4 主機内存 # shared_buffers 公式: IF use hugepage: 主機内存*(1/4) ELSE: min(32GB, 主機内存*(1/4)) # [《PostgreSQL Huge Page 使用建議 - 大内存主機、實例注意》](201803/20180325_02.md) shared_buffers = 24GB # min 128kB # (change requires restart) # 建議shared buffer設置超過32GB時 使用大頁,頁大小 /proc/meminfo Hugepagesize huge_pages = try # on, off, or try # (change requires restart) #temp_buffers = 8MB # min 800kB # 如果用戶需要使用兩階段提交,需要設置爲大於0,建議與max_connections一樣大 # max_prepared_transactions 公式: max_prepared_transactions=max_connections max_prepared_transactions = 2000 # zero disables the feature # (change requires restart) # Caution: it is not advisable to set max_prepared_transactions nonzero unless # you actively intend to use prepared transactions. # 可以在會話中設置,如果有大量JOIN,聚合操作,並且期望使用hash agg或hash join。 (排序,HASH都會用到work_mem) # 可以設大一些,但是不建議大於 四分之一内存除以最大連接數 . # (一條QUERY中可以使用多倍WORK_MEM,與執行計劃中的NODE有關) # work_mem 公式: max(min(物理内存/4096, 64MB), 4MB) work_mem = 8MB # min 64kB # 創建索引時使用的内存空間。 # maintenance_work_mem 公式: min( 8G, (主機内存*1/8)/max_parallel_maintenance_workers ) maintenance_work_mem = 2GB # min 1MB # 在對一張表進行垃圾回收時,用於緩存垃圾版本的ctid, # 如果autovacuum work mem填滿了,則VACUUM需要進入索引垃圾回收階段,掃描索引(因此爲了避免索引被多次掃描,需要緩存住一張表的所有垃圾TUPLE的CTID)。 # 一張表(細到單個最底層分區)垃圾回收結束後釋放。 # [《PostgreSQL 垃圾回收參數優化之 - maintenance_work_mem , autovacuum_work_mem》](201902/20190226_01.md) # autovacuum_work_mem 公式: min( 8G, (主機内存*1/8)/autovacuum_max_workers ) autovacuum_work_mem = 1GB # min 1MB, or -1 to use maintenance_work_mem #max_stack_depth = 2MB # min 100kB dynamic_shared_memory_type = posix # the default is the first option # supported by the operating system: # posix # sysv # windows # mmap # use none to disable dynamic shared memory # (change requires restart) # - Disk - # 如果需要限制臨時文档使用量,可以設置。 # 例如, 防止有異常的遞歸調用,無限使用臨時文档。 # temp_file_limit = -1 # limits per-process temp file space # in kB, or -1 for no limit # - Kernel Resources - ## 如果你的數據庫有非常多小文档(比如有幾十萬以上的表,還有索引等,並且每張表都會被訪問到時), # 建議FD可以設多一些,避免進程需要打開關閉文档。 ## 但是不要大於前面章節系統設置的ulimit -n(open files) # max_files_per_process=655360 #max_files_per_process = 1000 # min 25 # (change requires restart) # - Cost-Based Vacuum Delay - # 如果你的系統IO非常好,則可以關閉vacuum delay , 避免因爲垃圾回收任務周期長導致的膨脹。 vacuum_cost_delay = 0 # 0-100 milliseconds #vacuum_cost_page_hit = 1 # 0-10000 credits #vacuum_cost_page_miss = 10 # 0-10000 credits #vacuum_cost_page_dirty = 20 # 0-10000 credits # io很好,CPU核數很多的機器,設大一些。如果設置了vacuum_cost_delay = 0 ,則這個不需要配置 vacuum_cost_limit = 10000 # 1-10000 credits # - Background Writer - bgwriter_delay = 10ms # 10-10000ms between rounds bgwriter_lru_maxpages = 1000 # max buffers written/round, 0 disables bgwriter_lru_multiplier = 10.0 # 0-10.0 multiplier on buffers scanned/round bgwriter_flush_after = 512kB # measured in pages, 0 disables # - Asynchronous Behavior - effective_io_concurrency = 0 # 1-1000; 0 disables prefetching # wal sender, user 動態fork的process, parallel worker等都算作 worker process, 所以你需要設置足夠大. max_worker_processes = 256 # (change requires restart) # 如果需要使用並行創建索引,設置爲大於1 ,不建議超過 主機cores-4 # max_parallel_maintenance_workers 公式: min( max(2, CPU核數/2) , 16 ) max_parallel_maintenance_workers = 6 # taken from max_parallel_workers # 如果需要使用並行查詢,設置爲大於1 ,不建議超過 主機cores-4 # max_parallel_workers_per_gather 公式: min( max(2, CPU核數-4) , 24 ) max_parallel_workers_per_gather = 0 # taken from max_parallel_workers # leader 是否與work process一起參與並行計算,如果ON,則並行度會默認+1。 parallel_leader_participation = on # 如果需要使用並行查詢,設置爲大於1 ,不建議超過 主機cores-2 # 必須小於 max_worker_processes # max_parallel_workers 公式: max(2, CPU核數-4) max_parallel_workers = 32 # maximum number of max_worker_processes that # can be used in parallel operations # 是否啓用snapshot too old技術,避免長事務導致的膨脹 # 會導致性能一定的下降,約8% old_snapshot_threshold = 6h # 1min-60d; -1 disables; 0 is immediate # (change requires restart) #backend_flush_after = 256 # measured in pages, 0 disables #------------------------------------------------------------------------------ # WRITE-AHEAD LOG #------------------------------------------------------------------------------ # - Settings - # 需要流複制物理備庫、歸档、時間點恢複時,設置爲replica,需要邏輯訂閲或邏輯備庫則設置爲logical wal_level = replica # minimal, replica, or logical # (change requires restart) #fsync = on # flush data to disk for crash safety # (turning this off can cause # unrecoverable data corruption) # 如果雙節點,設置爲ON,如果是多副本,同步模式,建議設置爲remote_write。 # 如果磁盤性能很差,並且是OLTP業務。可以考慮設置爲off降低COMMIT的RT,提高吞吐(設置爲OFF時,可能丟失部分XLOG RECORD) synchronous_commit = off # synchronization level; # off, local, remote_write, remote_apply, or on # 建議使用pg_test_fsync測試後,決定用哪個最快。通常LINUX下open_datasync比較快。 #wal_sync_method = fsync # the default is the first option # supported by the operating system: # open_datasync # fdatasync (default on Linux) # fsync # fsync_writethrough # open_sync # 如果文档系統支持COW例如ZFS,則建議設置爲OFF。 # 如果文档系統可以保证datafile block size的原子寫,在文档系統與IO系統對齊後也可以設置爲OFF。 # 如果底層存儲能保证IO的原子寫,也可以設置爲OFF。 full_page_writes = on # recover from partial page writes # 當寫FULL PAGE WRITE的io是瓶頸時建議開啓 wal_compression = on # enable compression of full-page writes # 如果要使用pg_rewind,flashback 時間線,需要打開這個功能 # [《PostgreSQL pg_rewind,時間線修複,腦裂修複,flashback - 從庫開啓讀寫後,回退爲只讀從庫。異步主從發生角色切換後,主庫rewind爲新主庫的從庫》](201901/20190128_02.md) #wal_log_hints = off # also do full page writes of non-critical updates # (change requires restart) # 建議 min( WAL segment size(默認16MB) , shared_buffers/32 ) wal_buffers = 16MB # min 32kB, -1 sets based on shared_buffers # (change requires restart) # 如果設置了synchronous_commit = off,建議設置wal_writer_delay wal_writer_delay = 10ms # 1-10000 milliseconds wal_writer_flush_after = 1MB # measured in pages, 0 disables # 如果synchronous_commit=on, 並且已知業務系統爲高並發,對數據庫有寫操作的小事務,則可以設置commit_delay來實現分組提交,合並WAL FSYNCIO 。 # 分組提交 #commit_delay = 10 # range 0-100000, in microseconds # 同時處於提交狀態的事務數超過commit_siblings時,使用分組提交 #commit_siblings = 5 # range 1-1000 # - Checkpoints - # 不建議頻繁做檢查點,否則XLOG會産生很多的FULL PAGE WRITE(when full_page_writes=on)。 checkpoint_timeout = 30min # range 30s-1d # 建議等於SHARED BUFFER,或2倍。 # 同時需要考慮崩潰恢複時間, 越大,檢查點可能拉越長導致崩潰恢複耗時越長。但是越小,開啓FPW時,WAL日志寫入量又越大。 建議採用COW文档系統,關閉FPW。 # max_wal_size 公式: # min(shared_buffers*2 , 用戶存儲空間/10) max_wal_size = 48GB # 建議是SHARED BUFFER的2分之一 # min_wal_size 公式: # min(shared_buffers/2 , 用戶存儲空間/10) min_wal_size = 12GB # 硬盤好(nvme ssd)的情況下,值越小可以讓檢查點快速結束,恢複時也可以快速達到一致狀態。否則建議0.5~0.9 # 如果有hot standby作爲HA節點,這個值也可以設置爲0.5~0.9 避免寫高峰時CHECKPOINT對寫帶來的沖擊。 checkpoint_completion_target = 0.2 # checkpoint target duration, 0.0 - 1.0 # IO很好的機器,不需要考慮平滑調度, 否則建議128~256kB checkpoint_flush_after = 256kB # measured in pages, 0 disables #checkpoint_flush_after = 0 # measured in pages, 0 disables #checkpoint_warning = 30s # 0 disables # - Archiving - # 建議默認打開,因爲修改它需要重啓實例 # 打開後,一個WAL文档寫滿後,會在pg_wal/archive_status目錄中創建xxxxxx.ready的文档,歸档命令archive_command正常結束後,會清除這個狀態文档。 archive_mode = on # enables archiving; off, on, or always # (change requires restart) # 後期再修改,如 'test ! -f /disk1/digoal/arch/%f && cp %p /disk1/digoal/arch/%f' archive_command = '/bin/date' # command to use to archive a logfile segment # placeholders: %p = path of file to archive # %f = file name only # e.g. 'test ! -f /mnt/server/archivedir/%f && cp %p /mnt/server/archivedir/%f' #archive_timeout = 0 # force a logfile segment switch after this # number of seconds; 0 disables #------------------------------------------------------------------------------ # REPLICATION #------------------------------------------------------------------------------ # - Sending Servers - # Set these on the master and on any standby that will send replication data. # 同時允許幾個流複制協議的連接,根據實際需求設定 ,可以設置一個默認值例如64 max_wal_senders = 64 # max number of walsender processes # (change requires restart) # 根據實際情況設置保留WAL的數量,主要是防止過早的清除WAL,導致備庫因爲主庫的WAL清除而中斷。根據實際情況設定。 #wal_keep_segments = 0 # in logfile segments; 0 disables #wal_sender_timeout = 60s # in milliseconds; 0 disables # 根據實際情況設置需要創建多少replication slot # 使用slot,可以保证流複制下遊沒有接收的WAL會在當前節點永久保留。所以必須留意下遊的接收情況,否則可能導致WAL爆倉 # 建議大於等於max_wal_senders # max_replication_slots 公式: max_replication_slots=max_wal_senders max_replication_slots = 64 # max number of replication slots # (change requires restart) #track_commit_timestamp = off # collect timestamp of transaction commit # (change requires restart) # - Master Server - # These settings are ignored on a standby server. # 如果有2個或2個以上的備庫,可以考慮使用同步多副本模式。 根據實際情況設置 # [《PostgreSQL 一主多從(多副本,強同步)簡明手冊 - 配置、壓測、監控、切換、防腦裂、修複、0丟失 - 珍藏級》](201803/20180326_01.md) # [《PostgreSQL 雙節點流複制如何同時保证可用性、可靠性(rpo,rto) - (半同步,自動降級方法實踐)》](201901/20190127_01.md) #synchronous_standby_names = '' # standby servers that provide sync rep # method to choose sync standbys, number of sync standbys, # and comma-separated list of application_name # from standby(s); '*' = all # 注意,當數據庫有大量的更新、刪除操作時,設置這個值容易導致表膨脹,容易導致VACUUM進程空轉,導致IO和CPU飆升。(特別是autovacuum naptime配置很小時) #vacuum_defer_cleanup_age = 0 # number of xacts by which cleanup is delayed # - Standby Servers - # These settings are ignored on a master server. hot_standby = on # "off" disallows queries during recovery # (change requires restart) # 當standby的archive replay與standby的用戶SQL請求發生沖突時,在打斷SQL前,允許用戶SQL最長的執行時間. 打斷SQL後,需要等STANDBY APPLY所有archive restore command 的WAL文档,APPLY才允許被繼續打斷。 max_standby_archive_delay = 120s # max delay before canceling queries # when reading WAL from archive; # -1 allows indefinite delay max_standby_streaming_delay = 120s # max delay before canceling queries # when reading streaming WAL; # -1 allows indefinite delay wal_receiver_status_interval = 1s # send replies at least this often # 0 disables # 建議關閉,以免備庫長事務導致 主庫無法回收垃圾而膨脹。 [《PostgreSQL物理"備庫"的哪些操作或配置,可能影響"主庫"的性能、垃圾回收、IO波動》](201704/20170410_03.md) hot_standby_feedback = off # send info from standby to prevent # query conflicts #wal_receiver_timeout = 60s # time that receiver waits for # communication from master # in milliseconds; 0 disables #wal_retrieve_retry_interval = 5s # time to wait before retrying to # retrieve WAL after a failed attempt # - Subscribers - # 邏輯複制訂閲耑節點設置: # These settings are ignored on a publisher. # [《PostgreSQL 10.0 preview 邏輯訂閲 - 原理與最佳實踐》](201702/20170227_01.md) # These settings are ignored on a publisher. # 必須小於 max_worker_processes # 包括 apply workers and table synchronization workers # max_logical_replication_workers 公式: max_logical_replication_workers=max_wal_senders max_logical_replication_workers = 64 # taken from max_worker_processes # (change requires restart) # 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. # max_sync_workers_per_subscription 公式: min ( 32 , max(2, CPU核數-4) ) max_sync_workers_per_subscription = 8 # taken from max_logical_replication_workers #------------------------------------------------------------------------------ # QUERY TUNING #------------------------------------------------------------------------------ # - Planner Method Configuration - #enable_bitmapscan = on #enable_hashagg = on #enable_hashjoin = on #enable_indexscan = on #enable_indexonlyscan = on #enable_material = on #enable_mergejoin = on #enable_nestloop = on #enable_parallel_append = on #enable_seqscan = on #enable_sort = on #enable_tidscan = on # OLAP業務,建議設置爲ON (enable_partitionwise_join, enable_partitionwise_aggregate) # [《PostgreSQL 並行計算解說 滙總》](201903/20190319_01.md) enable_partitionwise_join = on enable_partitionwise_aggregate = on #enable_parallel_hash = on #enable_partition_pruning = on # - Planner Cost Constants - #seq_page_cost = 1.0 # measured on an arbitrary scale # 離散IO很好的機器(例如ssd, nvme ssd),不需要考慮離散和順序掃描的成本差異 random_page_cost = 1.1 # same scale as above #cpu_tuple_cost = 0.01 # same scale as above #cpu_index_tuple_cost = 0.005 # same scale as above #cpu_operator_cost = 0.0025 # same scale as above #parallel_tuple_cost = 0.1 # same scale as above #parallel_setup_cost = 1000.0 # same scale as above #jit_above_cost = 100000 # perform JIT compilation if available # and query more expensive, -1 disables #jit_optimize_above_cost = 500000 # optimize JITed functions if query is # more expensive, -1 disables #jit_inline_above_cost = 500000 # attempt to inline operators and # functions if query is more expensive, # -1 disables #min_parallel_table_scan_size = 8MB #min_parallel_index_scan_size = 512kB # 釦掉會話連接RSS,shared buffer, autovacuum worker, 剩下的都是OS可用的CACHE。 # effective_cache_size 公式: 主機内存*0.75 effective_cache_size = 80GB # - Genetic Query Optimizer - #geqo = on #geqo_threshold = 12 #geqo_effort = 5 # range 1-10 #geqo_pool_size = 0 # selects default based on effort #geqo_generations = 0 # selects default based on effort #geqo_selection_bias = 2.0 # range 1.5-2.0 #geqo_seed = 0.0 # range 0.0-1.0 # - Other Planner Options - #default_statistics_target = 100 # range 1-10000 #constraint_exclusion = partition # on, off, or partition #cursor_tuple_fraction = 0.1 # range 0.0-1.0 #from_collapse_limit = 8 #join_collapse_limit = 8 # 1 disables collapsing of explicit # JOIN clauses #force_parallel_mode = off #------------------------------------------------------------------------------ # REPORTING AND LOGGING #------------------------------------------------------------------------------ # - Where to Log - log_destination = 'csvlog' # Valid values are combinations of # stderr, csvlog, syslog, and eventlog, # depending on platform. csvlog # requires logging_collector to be on. # This is used when logging to stderr: logging_collector = on # Enable capturing of stderr and csvlog # into log files. Required to be on for # csvlogs. # (change requires restart) # These are only used if logging_collector is on: log_directory = 'log' # directory where log files are written, # can be absolute or relative to PGDATA # 時間格式 http://pubs.opengroup.org/onlinepubs/009695399/functions/strftime.html # 設置參考 https://www.postgresql.org/docs/11/runtime-config-logging.html#RUNTIME-CONFIG-LOGGING-WHERE # 日志保留一天,每個小時一個文档取決於log_rotation_age 每小時切換一下 log_filename = 'postgresql-%H.log' # log file name pattern, # can include strftime() escapes # 日志保留一個月例子,每天一個文档取決於log_rotation_age 每天切換一下 # log_filename = 'postgresql-%d.log' # log file name pattern, # can include strftime() escapes # 日志保留一周例子,每天一個文档取決於log_rotation_age 每天切換一下 # log_filename = 'postgresql-%a.log' # log file name pattern, # can include strftime() escapes #log_file_mode = 0600 # creation mode for log files, # begin with 0 to use octal notation log_truncate_on_rotation = on # If on, an existing log file with the # same name as the new log file will be # truncated rather than appended to. # But such truncation only occurs on # time-driven rotation, not on restarts # or size-driven rotation. Default is # off, meaning append to existing files # in all cases. # 配合log_filename設置,建議與%後面的名字周期一致 log_rotation_age = 1h # Automatic rotation of logfiles will # happen after that time. 0 disables. # 單個日志文档最大多大時,被截斷,可以設置一個上限防止日志無限制增長 log_rotation_size = 1GB # log_rotation_size = 0 # Automatic rotation of logfiles will # happen after that much log output. # 0 disables. # These are relevant when logging to syslog: #syslog_facility = 'LOCAL0' #syslog_ident = 'postgres' #syslog_sequence_numbers = on #syslog_split_messages = on # This is only relevant when logging to eventlog (win32): # (change requires restart) #event_source = 'PostgreSQL' # - When to Log - #client_min_messages = notice # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # log # notice # warning # error #log_min_messages = warning # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # info # notice # warning # error # log # fatal # panic #log_min_error_statement = error # values in order of decreasing detail: # debug5 # debug4 # debug3 # debug2 # debug1 # info # notice # warning # error # log # fatal # panic (effectively off) # 根據實際情況設定,例如業務上認爲5秒以上是慢SQL,那麽就設置爲5秒。 log_min_duration_statement = 5s # -1 is disabled, 0 logs all statements # and their durations, > 0 logs only # statements running at least this number # of milliseconds # - What to Log - #debug_print_parse = off #debug_print_rewritten = off #debug_print_plan = off #debug_pretty_print = on # 記錄檢查點的詳細統計信息 log_checkpoints = on # 如果業務是短連接,建議設置爲OFF,否則建議設置爲ON log_connections = off # 如果業務是短連接,建議設置爲OFF,否則建議設置爲ON log_disconnections = off #log_duration = off # 記錄錯誤代碼的代碼位置,是什麽代碼輸出的日志,更好的跟蹤問題 log_error_verbosity = verbose # terse, default, or verbose messages #log_hostname = off #log_line_prefix = '%m [%p] ' # special values: # %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 # %% = '%' # e.g. '<%u%%%d> ' # 是否列印鎖等待事件 log_lock_waits = on # log lock waits >= deadlock_timeout # 如果需要審計SQL,則可以設置爲all log_statement = 'ddl' # none, ddl, mod, all #log_replication_commands = off # 當使用的臨時文档超過多大時,列印到日志中,跟蹤大SQL。 log_temp_files = 256MB # log temporary files equal or larger # than the specified size in kilobytes; # -1 disables, 0 logs all temp files log_timezone = 'PRC' #------------------------------------------------------------------------------ # PROCESS TITLE #------------------------------------------------------------------------------ #cluster_name = '' # added to process titles if nonempty # (change requires restart) #update_process_title = on #------------------------------------------------------------------------------ # STATISTICS #------------------------------------------------------------------------------ # - Query and Index Statistics Collector - #track_activities = on #track_counts = on # 跟蹤IO耗時會帶來一定的性能影響,默認是關閉的 # 如果需要統計IO的時間開銷,設置爲ON # 建議用pg_test_timing測試一下獲取時間的開銷,如果開銷很大,建議關閉這個時間跟蹤。 track_io_timing = on # 是否需要跟蹤函數被調用的次數,耗時 track_functions = pl # none, pl, all # 單條被跟蹤的QUERY最多能存儲多少字節,如果有超長SQL,則日志中被截斷。 根據需要設置 #track_activity_query_size = 1024 # (change requires restart) # 相對路徑($PGDATA)或絕對路徑。用於存儲統計信息的臨時目錄。可以設置爲ram based directory,提高性能 # Pointing this at a RAM-based file system will decrease physical I/O requirements and can lead to improved performance. #stats_temp_directory = 'pg_stat_tmp' # - Monitoring - #log_parser_stats = off #log_planner_stats = off #log_executor_stats = off #log_statement_stats = off #------------------------------------------------------------------------------ # AUTOVACUUM #------------------------------------------------------------------------------ # 打開自動垃圾回收 autovacuum = on # Enable autovacuum subprocess? 'on' # requires track_counts to also be on. log_autovacuum_min_duration = 0 # -1 disables, 0 logs all actions and # their durations, > 0 logs only # actions running at least this number # of milliseconds. # CPU核多,並且IO好的情況下,可多點,但是注意最多可能消耗這麽多内存: # autovacuum_max_workers * autovacuum mem(autovacuum_work_mem), # 會消耗較多内存,所以内存也要有基礎。 # 當DELETE\UPDATE非常頻繁時,建議設置多一點,防止膨脹嚴重 # autovacuum_max_workers 公式: max(min( 8 , CPU核數/2 ) , 5) autovacuum_max_workers = 8 # max number of autovacuum subprocesses # (change requires restart) # 建議不要太高頻率,否則會因爲vacuum産生較多的XLOG。或者在某些垃圾回收不掉的情況下(例如長事務、feed back on,等),導致一直觸發vacuum,CPU和IO都會升高 [《PostgreSQL垃圾回收代碼分析 - why postgresql cann't reclaim tuple is HEAPTUPLE_RECENTLY_DEAD》](201505/20150503_01.md) [《PostgreSQL物理"備庫"的哪些操作或配置,可能影響"主庫"的性能、垃圾回收、IO波動》](201704/20170410_03.md) #autovacuum_naptime = 1min # time between autovacuum runs #autovacuum_vacuum_threshold = 50 # min number of row updates before # vacuum #autovacuum_analyze_threshold = 50 # min number of row updates before # analyze autovacuum_vacuum_scale_factor = 0.02 # fraction of table size before vacuum autovacuum_analyze_scale_factor = 0.01 # fraction of table size before analyze # 除了設置較大的FREEZE值。 # 還是需要注意FREEZE風暴 [《PostgreSQL Freeze 風暴預測續 - 珍藏級SQL》](201804/20180411_01.md) # 表級定制freeze [《PostgreSQL 10 CLogControlLock 等待事件分析與優化 - hint bit, freeze, autovacuum, 風暴》](201903/20190319_02.md) autovacuum_freeze_max_age = 1200000000 # maximum XID age before forced vacuum # (change requires restart) autovacuum_multixact_freeze_max_age = 1250000000 # maximum multixact age # before forced vacuum # (change requires restart) # 如果數據庫UPDATE非常頻繁,建議設置爲0。並且建議使用SSD autovacuum_vacuum_cost_delay = 0ms # default vacuum cost delay for # autovacuum, in milliseconds; # -1 means use vacuum_cost_delay #autovacuum_vacuum_cost_limit = -1 # default vacuum cost limit for # autovacuum, -1 means use # vacuum_cost_limit #------------------------------------------------------------------------------ # CLIENT CONNECTION DEFAULTS #------------------------------------------------------------------------------ # - Statement Behavior - #search_path = '"$user", public' # schema names #row_security = on #default_tablespace = '' # a tablespace name, '' uses the default # 臨時表的表空間,可以設置多個,輪詢使用。 # 臨時表的表空間,建議爲SSD目錄。速度快。 #temp_tablespaces = '' # a list of tablespace names, '' uses # only default tablespace #check_function_bodies = on #default_transaction_isolation = 'read committed' #default_transaction_read_only = off #default_transaction_deferrable = off #session_replication_role = 'origin' # 可以用來防止雪崩,但是不建議全局設置 #statement_timeout = 0 # in milliseconds, 0 is disabled # 執行DDL時,建議加上超時 # 可以用來防止雪崩 #lock_timeout = 0 # in milliseconds, 0 is disabled # 空閑中事務自動清理,根據業務實際情況設置 idle_in_transaction_session_timeout = '6h' # in milliseconds, 0 is disabled #[《PostgreSQL 10 CLogControlLock 等待事件分析與優化 - hint bit, freeze, autovacuum, 風暴》](201903/20190319_02.md) #vacuum_freeze_min_age = 50000000 vacuum_freeze_table_age = 200000000 #vacuum_multixact_freeze_min_age = 5000000 vacuum_multixact_freeze_table_age = 200000000 # [《PostgreSQL 11 内核優化 - 降低vacuum cleanup階段index scan概率 ( vacuum_cleanup_index_scale_factor , skip index vacuum cleanup stage)》](201902/20190201_03.md) #vacuum_cleanup_index_scale_factor = 0.1 # fraction of total number of tuples # before index cleanup, 0 always performs # index cleanup #bytea_output = 'hex' # hex, escape #xmlbinary = 'base64' #xmloption = 'content' # 限制GIN掃描的返回結果集大小,在想限制超多匹配的返回時可以設置 #gin_fuzzy_search_limit = 0 # GIN索引pending list的大小 #gin_pending_list_limit = 4MB # - Locale and Formatting - datestyle = 'iso, mdy' #intervalstyle = 'postgres' timezone = 'PRC' #timezone_abbreviations = 'Default' # Select the set of available time zone # abbreviations. Currently, there are # Default # Australia (historical usage) # India # You can create your own file in # share/timezonesets/. # 浮點精度擴展值 [《PostgreSQL 浮點精度調整(extra_float_digits)》](201510/20151020_01.md) #extra_float_digits = 0 # min -15, max 3 #client_encoding = sql_ascii # actually, defaults to database # encoding # These settings are initialized by initdb, but they can be changed. lc_messages = 'C' # locale for system error message # strings lc_monetary = 'C' # locale for monetary formatting lc_numeric = 'C' # locale for number formatting lc_time = 'C' # locale for time formatting # default configuration for text search default_text_search_config = 'pg_catalog.english' # - Shared Library Preloading - # 需要加載什麽LIB,預先加載,對於經常訪問的庫也建議預加載,例如postgis #shared_preload_libraries = 'pg_jieba,pipelinedb' # (change requires restart) shared_preload_libraries = 'pg_stat_statements' #local_preload_libraries = '' #session_preload_libraries = '' # - Other Defaults - #dynamic_library_path = '$libdir' # 是否已編譯JIT特性,如果已編譯,設置爲ON #jit = off # allow JIT compilation #jit_provider = 'llvmjit' # JIT implementation to use #------------------------------------------------------------------------------ # LOCK MANAGEMENT #------------------------------------------------------------------------------ deadlock_timeout = 1s #max_locks_per_transaction = 64 # min 10 # (change requires restart) #max_pred_locks_per_transaction = 64 # min 10 # (change requires restart) #max_pred_locks_per_relation = -2 # negative values mean # (max_pred_locks_per_transaction # / -max_pred_locks_per_relation) - 1 #max_pred_locks_per_page = 2 # min 0 #------------------------------------------------------------------------------ # VERSION AND PLATFORM COMPATIBILITY #------------------------------------------------------------------------------ # - Previous PostgreSQL Versions - #array_nulls = on #backslash_quote = safe_encoding # on, off, or safe_encoding #default_with_oids = off # [《PostgreSQL 轉義、UNICODE、與SQL注入》](201704/20170402_01.md) #escape_string_warning = on #lo_compat_privileges = off #operator_precedence_warning = off #quote_all_identifiers = off #standard_conforming_strings = on #synchronize_seqscans = on # - Other Platforms and Clients - # 是否啓用 exp = null 自動轉換爲 exp is null # https://www.postgresql.org/docs/11/runtime-config-compatible.html#RUNTIME-CONFIG-COMPATIBLE-CLIENTS #transform_null_equals = off #------------------------------------------------------------------------------ # ERROR HANDLING #------------------------------------------------------------------------------ #exit_on_error = off # terminate session on any error? #restart_after_crash = on # reinitialize after backend crash? #------------------------------------------------------------------------------ # CONFIG FILE INCLUDES #------------------------------------------------------------------------------ # These options allow settings to be loaded from files other than the # default postgresql.conf. #include_dir = 'conf.d' # include files ending in '.conf' from # directory 'conf.d' #include_if_exists = 'exists.conf' # include file only if it exists #include = 'special.conf' # include file #------------------------------------------------------------------------------ # CUSTOMIZED OPTIONS #------------------------------------------------------------------------------ # Add settings for extensions here
固定參數
listen_addresses='*' superuser_reserved_connections=100 tcp_keepalives_idle=45 tcp_keepalives_interval=10 tcp_keepalives_count=10 vacuum_cost_delay=0 vacuum_cost_limit=10000 bgwriter_delay=10ms bgwriter_lru_maxpages=1000 bgwriter_lru_multiplier=10.0 effective_io_concurrency=0 max_worker_processes=256 old_snapshot_threshold = -1 wal_level = replica wal_compression = on wal_buffers=16MB wal_writer_delay=10ms checkpoint_timeout = 25min checkpoint_completion_target = 0.4 max_wal_senders = 64 max_replication_slots = 56 max_logical_replication_workers = 56 hot_standby = on max_standby_archive_delay = 300s max_standby_streaming_delay = 300s wal_receiver_status_interval = 1s hot_standby_feedback = off wal_receiver_timeout = 30s random_page_cost=1.1 logging_collector=on log_truncate_on_rotation=on log_min_duration_statement=5s log_checkpoints=on log_lock_waits=on log_statement='ddl' log_temp_files=128MB track_io_timing=on track_functions=pl autovacuum = on log_autovacuum_min_duration=0 autovacuum_analyze_scale_factor = 0.05 autovacuum_freeze_max_age = 1200000000 autovacuum_multixact_freeze_max_age = 1400000000 autovacuum_vacuum_cost_delay=0 statement_timeout = 0 lock_timeout = 0 idle_in_transaction_session_timeout = '1h' vacuum_freeze_table_age = 200000000 vacuum_multixact_freeze_table_age = 200000000 deadlock_timeout = 1s auth_delay.milliseconds=3s enable_partitionwise_join = on enable_partitionwise_aggregate = on pg_stat_statements.max=5000 pg_stat_statements.save=off pg_stat_statements.track=top pg_stat_statements.track_utility=off track_activity_query_size = 1024 unix_socket_directories = '.' unix_socket_permissions = 0700 jit = off # allow JIT compilation jit_provider = 'llvmjit' # JIT implementation to use log_timezone='UTC' huge_pages=try log_error_verbosity=verbose log_rotation_age=1h log_rotation_size = 100MB log_filename = 'postgresql-%H.log' autovacuum_vacuum_scale_factor = 0.02 # 0.005~ 0.15 log_destination = 'csvlog' data_sync_retry = off full_page_writes = on
環境參數
log_timezone = 'PRC' datestyle = 'iso, mdy' timezone = 'PRC' lc_messages = 'C' lc_monetary = 'C' lc_numeric = 'C' lc_time = 'C'
動態參數
port = 1921 # 監聽耑口 max_connections= # 槼格内存(GB)*1000*(1/4)/10 + superuser_reserved_connections shared_buffers= # IF use hugepage: 槼格内存*(1/4) ELSE: min(32GB, 槼格内存*(1/4)) max_prepared_transactions # max_prepared_transactions=max_connections work_mem # max(min(槼格内存/4096, 64MB), 4MB) maintenance_work_mem # min( 8G, (主機内存*1/8)/max_parallel_maintenance_workers ) autovacuum_work_mem # min( 8G, (槼格内存*1/8)/autovacuum_max_workers ) max_parallel_maintenance_workers # min( max(2, CPU核數/2) , 16 ) max_parallel_workers_per_gather # min( max(2, CPU核數-4) , 24 ) max_parallel_workers # min(max(2, CPU核數-4) ,32) max_wal_size # min(shared_buffers*2, 用戶存儲空間/10) min_wal_size # min(shared_buffers/2 , 用戶存儲空間/10) max_sync_workers_per_subscription # min ( 32 , max(2, CPU核數-4) ) effective_cache_size # 槼格内存*0.75 autovacuum_max_workers # max(min( 8 , CPU核數/2 ) , 5) synchronous_commit = off # 當高並發寫事務遇到了WAL瓶頸時,優先考慮提高磁盤IOPS能力,如果需要立即提升性能可以使用異步提交,或開啓分組提交
根據實際情況開啓歸档
archive_mode = on archive_timeout = '5min' archive_command='test ! -f /disk1/digoal/arch/%f && cp %p /disk1/digoal/arch/%f'
# 也可以先設置爲如下,槼劃好歸档目錄後再設置。
# archive_command = '/bin/date' # 開啓歸档,但是不COPY歸档文档,
根據實際情況設置預加載的SO庫
shared_preload_libraries='pg_stat_statements,auth_delay,auto_explain'
根據實際情況,設置SSL鏈路
ssl=on ssl_cert_file = 'server.crt' ssl_key_file = 'server.key'
建議只允許用戶修改如下配置,默認值以及允許用戶修改的範圍:
enable_partitionwise_join = on # on, off enable_partitionwise_aggregate = on # on, off jit = off # on, off temp_file_limit= 槼格内存大小 # -1, 16MB ~ 1024000MB wal_level=replica # replica , logical wal_keep_segments=0 # 0 ~ 1024000 track_commit_timestamp=off # off, on vacuum_defer_cleanup_age=0 # 0 ~ 5000000 log_min_duration_statement=5s # -1 , 1s ~ 600s log_connections=off # on, off log_disconnections=off # on, off log_duration=off # on, off log_statement='ddl' # ddl, mod, all log_temp_files=128MB # 0, -1, 16MB ~ 1024MB default_transaction_deferrable=off # on, off statement_timeout = 0 # 0, 3s ~ 3600s lock_timeout = 0 # 0, 3s ~ 600s idle_in_transaction_session_timeout = '1h' # 0, 3s ~ 36000s extra_float_digits = 0 # -15~3, int old_snapshot_threshold=-1 # -1, 1min ~ 2880min autovacuum_vacuum_cost_delay = 0 # 0~50 autovacuum_vacuum_cost_limit = 10000 # 5000 ~ 10000 shared_buffers=min(32GB, 槼格内存*(1/4)) # min(32GB, 槼格内存*(1/4)) ,槼格内存*(1/4) autovacuum_vacuum_scale_factor = 0.02 # 0.005~ 0.15
如果加了LOG審計的採樣插件,再加上對應參數,允許用戶修改,以及允許用戶可以修改的範圍。
參考資料 https://www.pgxn.org/dist/pg_sampletolog/
配置例子
64G内存,16核,HUGE PAGE=on,SSD機器
port = 1921 # 監聽耑口 max_connections=1700 # 槼格内存(GB)*1000*(1/4)/10 + superuser_reserved_connections shared_buffers=16GB # IF use hugepage: 槼格内存*(1/4) ELSE: min(32GB, 槼格内存*(1/4)) max_prepared_transactions=1700 # max_prepared_transactions=max_connections work_mem =16MB # max(min(槼格内存/4096, 64MB), 4MB) maintenance_work_mem = 1GB # min( 8G, (主機内存*1/8)/max_parallel_maintenance_workers ) autovacuum_work_mem = 1GB # min( 8G, (槼格内存*1/8)/autovacuum_max_workers ) max_parallel_maintenance_workers = 8 # min( max(2, CPU核數/2) , 16 ) max_parallel_workers_per_gather = 12 # min( max(2, CPU核數-4) , 24 ) max_parallel_workers =12 # min(max(2, CPU核數-4) ,32) max_wal_size = 32GB # min(shared_buffers*2 , 用戶存儲空間/10) min_wal_size =8GB # min(shared_buffers/2 , 用戶存儲空間/10) max_sync_workers_per_subscription = 12 # min ( 32 , max(2, CPU核數-4) ) effective_cache_size = 48GB # 槼格内存*0.75 autovacuum_max_workers = 8 # max(min( 8 , CPU核數/2 ) , 5)
其他參數值請拷貝以上固定參數與環境參數内容。
pg_hba.conf 數據庫防火牆配置模板
# TYPE DATABASE USER ADDRESS METHOD # "local" is for Unix domain socket connections only local all all trust # IPv4 local connections: host all all 127.0.0.1/32 trust # IPv6 local connections: host all all ::1/128 trust # Allow replication connections from localhost, by a user with the # replication privilege. local replication all trust host replication all 127.0.0.1/32 trust host replication all ::1/128 trust # 禁止超級用戶從遠程連接 host all postgres 0.0.0.0/0 reject # 應用連接配置:哪個用戶,從哪裡來,連接什麽數據庫。槼則爲使用何種認证方法,或拒絕? # TYPE DATABASE USER ADDRESS METHOD # 如果不想挨個配置,可以使用如下配置,允許所有來源,通過任意用戶訪問任意數據庫 host all all 0.0.0.0/0 md5