Puppet Class: postgresql

Summary

postgres installation class

Overview

TODO: log_min_duration_statement HINT: Valid units for this parameter are “kB”, “MB”, “GB”, and “TB”.

Parameters:

  • version (Any) (defaults to: $postgresql::params::version_default)

    version to install

  • datadir (Any) (defaults to: undef)

    datadir to use

  • initdb (Any) (defaults to: true)

    boolean, set it to true to create datadir’s directies. In a standby server with streaming replication you want to set it to false

  • overcommit_memory (Any) (defaults to: '2')

    modes available: undef: do not change it, 0: heuristic overcommit (this is the default), 1: always overcommit, never check, 2: always check, never

  • shmmax (Any) (defaults to: ceiling(sprintf('%f', $::memorysize_mb)*786432))

    maximum size of shared memory segment

  • shmall (Any) (defaults to: ceiling(ceiling(sprintf('%f', $::memorysize_mb)*786432)/$::eyp_postgresql_pagesize))

    total amount of shared memory available

  • manage_service (Any) (defaults to: true)

    set it to true to manage PostgreSQL’s service

  • archive_command_custom (Any) (defaults to: undef)

    custom archive command

  • archive_dir (Any) (defaults to: undef)

    archive dir, if archive_command_custom is undef, it will be: test ! -f $archive_dir/%f && cp %p $archive_dir/%f

  • archive_dir_user (Any) (defaults to: undef)

    archive dir user

  • archive_dir_group (Any) (defaults to: undef)

    archive dir group

  • archive_dir_mode (Any) (defaults to: undef)

    archive dir mode

  • archive_dir_chmod (Any) (defaults to: undef)

    chmod to this mask if using archive_dir

  • install_contrib (Any) (defaults to: false)
  • set_gzip_pglog_cronjob (Any) (defaults to: true)
  • maxdays_gzip_pglog_cronjob (Any) (defaults to: '6')
  • gzip_level_pglog_cronjob (Any) (defaults to: '9')
  • hour_gzip_pglog_cronjob (Any) (defaults to: '1')
  • minute_gzip_pglog_cronjob (Any) (defaults to: '0')
  • month_gzip_pglog_cronjob (Any) (defaults to: undef)
  • monthday_gzip_pglog_cronjob (Any) (defaults to: undef)
  • weekday_gzip_pglog_cronjob (Any) (defaults to: undef)
  • set_purge_pglog_cronjob (Any) (defaults to: true)
  • maxdays_purge_pglog_cronjob (Any) (defaults to: '30')
  • hour_purge_pglog_cronjob (Any) (defaults to: '3')
  • minute_purge_pglog_cronjob (Any) (defaults to: '0')
  • month_purge_pglog_cronjob (Any) (defaults to: undef)
  • monthday_purge_pglog_cronjob (Any) (defaults to: undef)
  • weekday_purge_pglog_cronjob (Any) (defaults to: undef)
  • manage_docker_service (Any) (defaults to: true)
  • ensure (Any) (defaults to: 'running')
  • enable (Any) (defaults to: true)
  • restart_if_needed (Any) (defaults to: true)
  • listen (Any) (defaults to: [ '*' ])
  • port (Any) (defaults to: $postgresql::params::port_default)
  • bonjour (Any) (defaults to: false)
  • bonjour_name (Any) (defaults to: '')
  • max_connections (Any) (defaults to: '100')
  • wal_level (Any) (defaults to: 'hot_standby')
  • max_wal_senders (Any) (defaults to: '10')
  • checkpoint_segments (Any) (defaults to: '16')
  • wal_keep_segments (Any) (defaults to: '0')
  • pidfile (Any) (defaults to: undef)
  • log_directory (Any) (defaults to: $postgresql::params::log_directory_default)
  • log_filename (Any) (defaults to: $postgresql::params::log_filename_default)
  • log_autovacuum_min_duration (Any) (defaults to: '-1')
  • log_timezone (Any) (defaults to: $postgresql::params::timezone_default)
  • log_min_duration_statement (Any) (defaults to: '-1')
  • log_file_mode (Any) (defaults to: '0600')
  • track_activities (Any) (defaults to: true)
  • track_counts (Any) (defaults to: true)
  • effective_io_concurrency (Any) (defaults to: '1')
  • checkpoint_timeout (Any) (defaults to: '5min')
  • checkpoint_completion_target (Any) (defaults to: '0.5')
  • vacuum_cost_limit (Any) (defaults to: '200')
  • autovacuum (Any) (defaults to: true)
  • autovacuum_vacuum_scale_factor (Any) (defaults to: '0.0')
  • autovacuum_vacuum_threshold (Any) (defaults to: '5000')
  • autovacuum_analyze_scale_factor (Any) (defaults to: '0.0')
  • autovacuum_analyze_threshold (Any) (defaults to: '5000')
  • autovacuum_freeze_max_age (Any) (defaults to: undef)
  • autovacuum_naptime (Any) (defaults to: '1min')
  • autovacuum_max_workers (Any) (defaults to: '3')
  • autovacuum_vacuum_cost_limit (Any) (defaults to: '-1')
  • timezone (Any) (defaults to: $postgresql::params::timezone_default)
  • superuser_reserved_connections (Any) (defaults to: '5')
  • archive_mode (Any) (defaults to: true)
  • archive_timeout (Any) (defaults to: '0')
  • archived_wals_retention (Any) (defaults to: '+7')
  • archived_wals_hour (Any) (defaults to: '0')
  • archived_wals_minute (Any) (defaults to: '0')
  • archived_wals_month (Any) (defaults to: '*')
  • archived_wals_monthday (Any) (defaults to: '*')
  • archived_wals_weekday (Any) (defaults to: '*')
  • maintenance_work_mem (Any) (defaults to: '64MB')
  • wal_buffers (Any) (defaults to: '-1')
  • work_mem (Any) (defaults to: '8MB')
  • shared_buffers (Any) (defaults to: sprintf('%dMB',ceiling(sprintf('%f', $::memorysize_mb)/4)))
  • lc_messages (Any) (defaults to: 'C')
  • lc_monetary (Any) (defaults to: 'en_US.UTF-8')
  • lc_numeric (Any) (defaults to: 'en_US.UTF-8')
  • lc_time (Any) (defaults to: 'en_US.UTF-8')
  • default_text_search_config (Any) (defaults to: 'pg_catalog.english')
  • shared_preload_libraries (Any) (defaults to: [])
  • search_path (Any) (defaults to: [ '"$user"', 'public' ])
  • manage_pghba (Any) (defaults to: true)
  • manage_configfile (Any) (defaults to: true)
  • max_replication_slots (Any) (defaults to: '5')
  • effective_cache_size (Any) (defaults to: sprintf('%dMB',ceiling(sprintf('%f', ($::memorysize_mb)/4)*3)))
  • wal_compression (Any) (defaults to: true)
  • log_line_prefix (Any) (defaults to: undef)
  • log_lock_waits (Any) (defaults to: true)
  • deadlock_timeout (Any) (defaults to: '1s')
  • default_transaction_read_only (Any) (defaults to: false)
  • max_worker_processes (Any) (defaults to: $::processorcount)
  • max_parallel_workers (Any) (defaults to: $::processorcount)
  • max_parallel_workers_per_gather (Any) (defaults to: max(2, ceiling(sprintf('%f', ($::processorcount)/2))))
  • hot_standby (Any) (defaults to: false)
  • max_standby_archive_delay (Any) (defaults to: '30s')
  • max_standby_streaming_delay (Any) (defaults to: '30s')
  • add_nagios_checks (Any) (defaults to: true)
  • ensure_nagios_checks (Any) (defaults to: 'present')
  • basedir_nagios_checks (Any) (defaults to: '/usr/local/bin')
  • add_nrpe_sudos_nagios_checks (Any) (defaults to: true)
  • add_hba_default_local_rules (Any) (defaults to: true)
  • add_hba_default_localhost_rules (Any) (defaults to: true)
  • default_local_authmethod (Any) (defaults to: 'trust')
  • pause_replica (Any) (defaults to: undef)


25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
# File 'manifests/init.pp', line 25

class postgresql(
                  #general
                  $version                         = $postgresql::params::version_default,
                  $datadir                         = undef,
                  # install
                  $install_contrib                 = false,
                  $initdb                          = true,
                  $overcommit_memory               = '2',
                  $shmmax                          = ceiling(sprintf('%f', $::memorysize_mb)*786432),
                  $shmall                          = ceiling(ceiling(sprintf('%f', $::memorysize_mb)*786432)/$::eyp_postgresql_pagesize),
                  $set_gzip_pglog_cronjob          = true,
                  $maxdays_gzip_pglog_cronjob      = '6',
                  $gzip_level_pglog_cronjob        = '9',
                  $hour_gzip_pglog_cronjob         = '1',
                  $minute_gzip_pglog_cronjob       = '0',
                  $month_gzip_pglog_cronjob        = undef,
                  $monthday_gzip_pglog_cronjob     = undef,
                  $weekday_gzip_pglog_cronjob      = undef,
                  $set_purge_pglog_cronjob         = true,
                  $maxdays_purge_pglog_cronjob     = '30',
                  $hour_purge_pglog_cronjob        = '3',
                  $minute_purge_pglog_cronjob      = '0',
                  $month_purge_pglog_cronjob       = undef,
                  $monthday_purge_pglog_cronjob    = undef,
                  $weekday_purge_pglog_cronjob     = undef,
                  # service
                  $manage_service                  = true,
                  $manage_docker_service           = true,
                  $ensure                          = 'running',
                  $enable                          = true,
                  $restart_if_needed               = true,
                  # config
                  $listen                          = [ '*' ],
                  $port                            = $postgresql::params::port_default,
                  $bonjour                         = false,
                  $bonjour_name                    = '',
                  $max_connections                 = '100',
                  $wal_level                       = 'hot_standby',
                  $max_wal_senders                 = '10',
                  $checkpoint_segments             = '16',
                  $wal_keep_segments               = '0',
                  $pidfile                         = undef,
                  $log_directory                   = $postgresql::params::log_directory_default,
                  $log_filename                    = $postgresql::params::log_filename_default,
                  $log_autovacuum_min_duration     = '-1',
                  $log_timezone                    = $postgresql::params::timezone_default,
                  $log_min_duration_statement      = '-1',
                  $log_file_mode                   = '0600',
                  $track_activities                = true,
                  $track_counts                    = true,
                  $effective_io_concurrency        = '1',
                  $checkpoint_timeout              = '5min',
                  $checkpoint_completion_target    = '0.5',
                  $vacuum_cost_limit               = '200',
                  $autovacuum                      = true,
                  $autovacuum_vacuum_scale_factor  = '0.0',
                  $autovacuum_vacuum_threshold     = '5000',
                  $autovacuum_analyze_scale_factor = '0.0',
                  $autovacuum_analyze_threshold    = '5000',
                  $autovacuum_freeze_max_age       = undef,
                  $autovacuum_naptime              = '1min',
                  $autovacuum_max_workers          = '3',
                  $autovacuum_vacuum_cost_limit    = '-1',
                  $timezone                        = $postgresql::params::timezone_default,
                  $superuser_reserved_connections  = '5',
                  $archive_mode                    = true,
                  $archive_command_custom          = undef,
                  $archive_dir                     = undef,
                  $archive_dir_user                = undef,
                  $archive_dir_group               = undef,
                  $archive_dir_mode                = undef,
                  $archive_dir_chmod               = undef,
                  $archive_timeout                 = '0',
                  $archived_wals_retention         = '+7',
                  $archived_wals_hour              = '0',
                  $archived_wals_minute            = '0',
                  $archived_wals_month             = '*',
                  $archived_wals_monthday          = '*',
                  $archived_wals_weekday           = '*',
                  $maintenance_work_mem            = '64MB',
                  $wal_buffers                     = '-1',
                  $work_mem                        = '8MB',
                  $shared_buffers                  = sprintf('%dMB',ceiling(sprintf('%f', $::memorysize_mb)/4)),
                  $lc_messages                     = 'C',
                  $lc_monetary                     = 'en_US.UTF-8',
                  $lc_numeric                      = 'en_US.UTF-8',
                  $lc_time                         = 'en_US.UTF-8',
                  $default_text_search_config      = 'pg_catalog.english',
                  $shared_preload_libraries        = [],
                  $search_path                     = [ '"$user"', 'public' ],
                  $manage_pghba                    = true,
                  $manage_configfile               = true,
                  $max_replication_slots           = '5',
                  $effective_cache_size            = sprintf('%dMB',ceiling(sprintf('%f', ($::memorysize_mb)/4)*3)),
                  $wal_compression                 = true,
                  $log_line_prefix                 = undef,
                  $log_lock_waits                  = true,
                  $deadlock_timeout                = '1s',
                  $default_transaction_read_only   = false,
                  $max_worker_processes            = $::processorcount,
                  $max_parallel_workers            = $::processorcount,
                  $max_parallel_workers_per_gather = max(2, ceiling(sprintf('%f', ($::processorcount)/2))),
                  $hot_standby                     = false,
                  $max_standby_archive_delay       = '30s',
                  $max_standby_streaming_delay     = '30s',
                  $add_nagios_checks               = true,
                  $ensure_nagios_checks            = 'present',
                  $basedir_nagios_checks           = '/usr/local/bin',
                  $add_nrpe_sudos_nagios_checks    = true,
                  $add_hba_default_local_rules     = true,
                  $add_hba_default_localhost_rules = true,
                  $default_local_authmethod        = 'trust',
                  $pause_replica                   = undef,
                ) inherits postgresql::params {

  Exec {
    path => '/usr/sbin:/usr/bin:/sbin:/bin',
  }

  if($pidfile==undef)
  {
    if($postgresql::params::repoprovider=='raspbian10')
    {
      $pidfilename='/run/postgresql/11-main.pid'
    }
    else
    {
      $pidfilename=$postgresql::params::pidfile[$version]
    }
  }
  else
  {
    $pidfilename=$pidfile
  }

  if($datadir==undef)
  {
    $datadir_path=$postgresql::params::datadir_default[$version]
  }
  else
  {
    $datadir_path = $datadir
  }

  if($archive_dir!=undef)
  {
    #tenim un munt de muntatge local, per exemple un NFS pels arxivats
    # validate_absolute_path($archive_dir)

    exec { "mkdir -p ${archive_dir} postgres archive command ${version} ${datadir_path}":
      command => "mkdir -p ${archive_dir}",
      creates => $archive_dir,
      require => Class['::postgresql::install'],
      tag     => 'post-recoveryconf',
    }

    file { $archive_dir:
      ensure => 'directory',
      owner  => $archive_dir_user,
      group  => $archive_dir_group,
      mode   => $archive_dir_mode,
      before => Class['::postgresql::service'],
      tag    => 'post-recoveryconf',
    }

    if($archive_dir!=undef and $archive_command_custom==undef)
    {
      #si no tenim un archive_command_custom definit, fem el default
      if($archive_dir_chmod==undef)
      {
        $archive_command="test ! -f ${archive_dir}/%f && cp --no-preserve=mode,ownership,timestamps ${datadir_path}/%p ${archive_dir}/%f"
      }
      else
      {
        $archive_command="test ! -f ${archive_dir}/%f && cp --no-preserve=mode,ownership,timestamps ${datadir_path}/%p ${archive_dir}/%f && chmod ${archive_dir_chmod} ${archive_dir}/*"
      }
    }
    else
    {
      #sino, el que ens pasin
      $archive_command=$archive_command_custom
    }

    if($archived_wals_retention!=undef)
    {
      #si no definim com undef la retencio, configurem un cron per purgar
      cron { "cronjob purge walls ${$archived_wals_retention} postgres ${archive_dir}":
        ensure   => 'present',
        command  => "find ${archive_dir} -type f -mtime ${archived_wals_retention} -delete",
        user     => 'root',
        hour     => $archived_wals_hour,
        minute   => $archived_wals_minute,
        month    => $archived_wals_month,
        monthday => $archived_wals_monthday,
        weekday  => $archived_wals_weekday,
      }
    }
  }
  else
  {
    #si els arxivats no son locals
    if($archive_command_custom!=undef)
    {
      # pasem el que toqui
      $archive_command=$archive_command_custom
    }
    else
    {
      # segurament es un: i have no idea what i'm doing o son proves, deixem un cd .
      $archive_command='cd .'
    }
  }

  if($add_nagios_checks)
  {
    class { '::postgresql::checks':
      ensure         => $ensure_nagios_checks,
      basedir        => $basedir_nagios_checks,
      add_nrpe_sudos => $add_nrpe_sudos_nagios_checks,
    }
  }

  class { '::postgresql::install': }

  class { '::postgresql::config':
    require => Class['::postgresql::install'],
    notify  => Class['::postgresql::config::reload'],
  }

  class { '::postgresql::hba::config':
    require => Class['::postgresql::install'],
    notify  => Class['::postgresql::config::reload'],
  }

  class { '::postgresql::config::reload':
    require => Class['::postgresql::config'],
  }

  class { '::postgresql::service':
    before => Class['::postgresql::config::reload'],
  }

  if($pause_replica!=undef)
  {
    if(versioncmp($version, '10')>0)
    {
      if($pause_replica)
      {
        postgresql_psql { 'SELECT pg_wal_replay_pause()':
          unless  => 'SELECT pg_is_wal_replay_paused()',
          require => Class['::postgresql::service'],
        }
      }
      else
      {
        postgresql_psql { 'SELECT pg_wal_replay_resume()':
          onlyif  => 'SELECT pg_is_wal_replay_paused()',
          require => Class['::postgresql::service'],
        }
      }
    }
    else
    {
      if($pause_replica)
      {
        postgresql_psql { 'SELECT pg_xlog_replay_pause()':
          unless  => 'SELECT pg_is_xlog_replay_paused()',
          require => Class['::postgresql::service'],
        }
      }
      else
      {
        postgresql_psql { 'SELECT pg_xlog_replay_resume()':
          onlyif  => 'SELECT pg_is_xlog_replay_paused()',
          require => Class['::postgresql::service'],
        }
      }
    }
  }

}