Initializing PostgreSQL
To initialize the PostgreSQL instance it is recommended to run the following command;
initdb -D <data_directory_path> --wal-segsize=64
The wal-segsize determines the size of each segment file in the write-ahead log, the default size of these files is 16MB but we recommend 64MB for overall performance improvement.
OS Level parameters
The following parameters should be added or updated in the/etc/sysctcl.conf file. After making the changes it is usually cleaner to reboot the system, although running the systemctl -p command will also enforce changes.
net.ipv4.tcp_keepalive_time = 240
net.ipv4.tcp_keepalive_intvl = 20
net.ipv4.tcp_keepalive_probes = 5
net.ipv4.tcp_timestamps = 1
net.ipv4.tcp_sack = 1
net.ipv4.tcp_no_metrics_save = 1
vm.overcommit_memory = 2
vm.overcommit_ratio = 100
vm.swappiness = 10
vm.dirty_background_bytes = 134217728
vm.dirty_expire_centisecs = 499
vm.dirty_ratio = 90
vm.nr_hugepages = 0
vm.zone_reclaim_mode = 0
fs.aio-max-nr = 1048576
fs.file-max = 10000000
kernel.numa_balancing = 0
kernel.panic_on_oops = 1
kernel.pid_max = 64512
kernel.sched_autogroup_enabled = 0
kernel.sem = 250 102400 32 4096
net.core.rmem_default = 262144
net.core.rmem_max = 1048576
net.core.wmem_default = 262144
net.core.wmem_max = 1048576
kernel.watchdog_thresh = 20
Filesystem
It is highly recommended to use the XFS to run PostgreSQL clusters.The following parameters are recommended to be specified in the /etc/fstab file when mounting the XFS filesystems:
auto,rw,noatime,nodiratime,nobarrier,inode64,nodev,noquota
We also recommend to create at least four separate XFS mounts as follows;
/opt/pgsql/data – This mount stores the main database and configuration files, the size of the mount would depend on the total size of the database and should be at least 500GB
/opt/pgsql/wal – This mount holds the pg_wal directory and is usually set to at least 50% of the size of the "/opt/pgsql/data" directory
/opt/pgsql/log – This mount holds the PostgreSQL Cluster’s errorlog and should be set to 50GB in size initially. The trace and log parameters will dictate the size.
/opt/pgsql/tablespace/temp – This mount is used to allocate the temp tablespace and should be set to 250GB initially. The usage of this mount can be controlled by the PostgreSQL temp_file_limit parameter if required.
PostgreSQL Configuration parameters
PostgreSQL constantly evolves, the parameters suggested below are for version 13. The following steps are required to update the postgresql.conf and pg_hba.conf files;
a) Update the pg_hba.conf
|
local all postgres peer local all all md5 host replication standby 0.0.0.0/0 md5 host all all 0.0.0.0/0 md5
|
b) Update postgresql.conf
|
# It is recommended to use pgbackrest archive_command = pgbackrest --stanza=<cluster_name> archive-push "%p" archive_mode = 'on' archive_timeout = '900s' autovacuum_analyze_scale_factor = '0.02' autovacuum_vacuum_scale_factor = '0.05' checkpoint_completion_target = '0.9' checkpoint_timeout = '15min' cluster_name = '{hostname no domain or dashes}' # make sure to update per hostname deadlock_timeout: 40s effective_cache_size: 150GB # set it to 80% of total RAM, use free -h to see current host values default_statistics_target=300 effective_io_concurrency: 300 enable_partitionwise_aggregate: true enable_partitionwise_join: true hot_standby = 'on' hot_standby_feedback = 'on' listen_addresses = '0.0.0.0' idle_in_transaction_session_timeout: 1800s lock_timeout: 130min log_autovacuum_min_duration = '0' log_checkpoints = 'on' log_connections = 'on' log_directory = '/opt/pgsql/log' log_disconnections = 'on' log_file_mode = '0640' log_filename = 'postgresql-%Y%m%d.log' log_line_prefix = '%t [%p]: [%l-1] %c %x %d %u %a %h ' log_lock_waits = 'on' log_min_duration_statement = '5000' log_min_error_statement = 'FATAL' log_min_messages = 'LOG' log_rotation_age = '1d' log_statement = 'ddl' log_temp_files = '0' log_truncate_on_rotation = 'on' logging_collector = 'on' maintenance_io_concurrency: 300 maintenance_work_mem: 4GB max_connections = '2000' max_locks_per_transaction = '64' max_parallel_workers: 40 # set to to same value as max_worker_processes max_parallel_worker_per_gather: 8 max_prepared_transactions = '0' max_replication_slots = '10' max_standby_archive_delay = '5min' max_standby_streaming_delay = '5min' max_wal_senders = '10' max_wal_size = '30GB' # or 50% of the /opt/pgsql/wal mount size max_worker_processes = '40' # or total cpu count; use nproc to see the count min_wal_size = '5GB' # or 10% of /opt/pgsql/wal mount no less than 5GB password_encryption = 'scram-sha-256' port = '5000' shared_buffers='16GB' # or 50% of total RAM, use free -h to see RAM values shared_preload_libraries = 'pg_stat_statements' superuser_reserved_connections = '200' statement_timeout: 1440min temp_tablespaces = 'temp' temp_file_limit='50GB' track_activity_query_size = '8192' track_commit_timestamp = 'off' track_functions = 'all' track_io_timing = 'on' unix_socket_directories = '/var/run/postgresql, /tmp' wal_keep_size = '5GB' wal_level = 'replica' wal_buffers: 64MB wal_compression: true wal_log_hints = 'on' hba_file = '/opt/pgsql/data/13/pg_hba.conf' ident_file = '/opt/pgsql/data/13/pg_ident.conf' work_mem='128MB' wal_sync_method: fsync
|