Puppet Class: postgresql::slave

Defined in:
modules/postgresql/manifests/slave.pp

Summary

Overview

This class installs the server in a slave configuration It will create the replication user

Actions:
  Install/configure postgresql in a slave configuration

Examples:

class {'postgresql::slave':
     master_server => 'mserver',
     replication_pass => 'mypass',
}

Parameters:

  • master_server (Stdlib::Host)

    The FQDN of the master server to connect to

  • replication_pass (String)

    The password the replication user should use

  • pgversion (Optional[Numeric]) (defaults to: undef)

    Defaults to 9.6 in Debian Stretch and 11 in Buster

  • ensure (String) (defaults to: 'present')

    Defaults to present

  • max_wal_senders (Integer) (defaults to: 5)

    the max wal senders

  • root_dir (Stdlib::Unixpath) (defaults to: '/var/lib/postgresql')

    $postgresql::server::root_dir

  • use_ssl (Boolean) (defaults to: false)

    Enable ssl for both clients and replication

  • includes (Array[String]) (defaults to: [])

    addtional include files

  • rep_app (Optional[String]) (defaults to: undef)

    The replication label to use for this host

  • ssldir (Optional[Stdlib::Unixpath]) (defaults to: undef)

    the ssl dir

  • log_min_duration_statement (Optional[Integer[250]]) (defaults to: undef)

    log statments that take longer then this (seconds)

  • prom_lag_critical (Integer) (defaults to: 16777216)

    critical level of replica lag for prometheus (bytes)

  • prom_lag_warning (Integer) (defaults to: 1048576)

    warning level of replica lag for prometheus (bytes)

  • replication_slot_name (Optional[String[1]]) (defaults to: undef)

    the name of the replication slot to use



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
# File 'modules/postgresql/manifests/slave.pp', line 27

class postgresql::slave(
    Stdlib::Host               $master_server,
    String                     $replication_pass,
    String                     $ensure                      = 'present',
    Integer                    $max_wal_senders             = 5,
    Stdlib::Unixpath           $root_dir                    = '/var/lib/postgresql',
    Boolean                    $use_ssl                     = false,
    Integer                    $prom_lag_critical           = 16777216, # 16Mb
    Integer                    $prom_lag_warning            = 1048576, # 1Mb
    Array[String]              $includes                    = [],
    Optional[String]           $rep_app                     = undef,
    Optional[Numeric]          $pgversion                   = undef,
    Optional[Stdlib::Unixpath] $ssldir                      = undef,
    Optional[Integer[250]]     $log_min_duration_statement  = undef,
    Optional[String[1]]        $replication_slot_name       = undef,
) {

    $_pgversion = $pgversion ? {
        undef   => debian::codename() ? {
            'buster'   => 11,
            'bullseye' => 13,
            'bookworm' => 15,
            default    => fail("${title} not supported by: ${debian::codename()})")
        },
        default => $pgversion,
    }
    $data_dir = "${root_dir}/${_pgversion}/main"

    class { 'postgresql::server':
        ensure                     => $ensure,
        pgversion                  => $_pgversion,
        includes                   => $includes + ['slave.conf'],
        root_dir                   => $root_dir,
        use_ssl                    => $use_ssl,
        ssldir                     => $ssldir,
        log_min_duration_statement => $log_min_duration_statement,
    }

    file { '/usr/local/bin/pg-resync-replica':
        ensure => $ensure,
        owner  => 'root',
        group  => 'root',
        mode   => '0755',
        source => 'puppet:///modules/postgresql/resync-replica.sh',
    }

    file { '/usr/local/bin/resync_replica':
        ensure => link,
        target => '/usr/local/bin/pg-resync-replica',
    }

    file { "/etc/postgresql/${_pgversion}/main/slave.conf":
        ensure  => $ensure,
        owner   => 'root',
        group   => 'root',
        mode    => '0444',
        content => template('postgresql/slave.conf.erb'),
        require => Package["postgresql-${_pgversion}"],
    }
    if $facts['postgres_replica_initialised'] {
        # postgresql 12+ no longer uses the recovery.conf file
        # https://www.postgresql.org/docs/current/recovery-config.html
        if $_pgversion >= 12 {
            file { "${data_dir}/standby.signal":
                ensure => file,
                owner  => 'postgres',
                mode   => '0600',
                before => Service[$postgresql::server::service_name],
            }
            # postgress 12+ fails to start of this is present
            file { "${data_dir}/recovery.conf":
                ensure => 'absent',
                before => Service[$postgresql::server::service_name],
            }
        } else {
            file { "${data_dir}/recovery.conf":
                ensure  => $ensure,
                owner   => 'postgres',
                group   => 'root',
                mode    => '0644',
                content => template('postgresql/recovery.conf.erb'),
                before  => Service[$postgresql::server::service_name],
            }
        }
    } else {
        notify {'Replication not initialised please run: pg-resync-replica': }
    }

    # Having this file here helps perform slave initialization.
    # This file should not be deleted when performing slave init.
    file { "/etc/postgresql/${_pgversion}/main/.pgpass":
        ensure  => $ensure,
        owner   => 'postgres',
        group   => 'postgres',
        mode    => '0600',
        content => template('postgresql/.pgpass.erb'),
        require => Package["postgresql-${_pgversion}"],
    }

    file { '/usr/bin/prometheus_postgresql_replication_lag':
        owner   => 'root',
        group   => 'root',
        mode    => '0755',
        content => template('postgresql/prometheus/postgresql_replication_lag.sh.erb'),
    }
}