|
| 1 | +[Unit] |
| 2 | +Description=PostgreSQL pg_receivewal service |
| 3 | +Documentation=https://www.postgresql.org/docs/current/app-pgreceivewal.html |
| 4 | +After=postgresql-%i.service |
| 5 | + |
| 6 | +[Service] |
| 7 | +Type=simple |
| 8 | +User=postgres |
| 9 | +Group=postgres |
| 10 | + |
| 11 | +# архивировать WAL файлы будем только с мастера |
| 12 | +ExecCondition=echo "pg_receivewal: check if PostgreSQL is primary" |
| 13 | +ExecCondition=/bin/bash -c "test f = $(psql --user=bkp_replicator --dbname=postgres --quiet --no-psqlrc --pset=null=¤ --tuples-only --no-align --command='select pg_is_in_recovery()')" |
| 14 | + |
| 15 | +# создаём слот, если его ещё не было (select * from pg_replication_slots;) |
| 16 | +ExecStartPre=/usr/pgsql-%i/bin/pg_receivewal --verbose --user=bkp_replicator --slot=pg_receivewal --create-slot --if-not-exists |
| 17 | + |
| 18 | +# архивируем WAL файлы в реальном времени, появится ещё одна асинхронная реплика, которая никогда не догонит мастер (select * from pg_stat_replication;) |
| 19 | +ExecStart=/usr/pgsql-%i/bin/pg_receivewal --verbose --user=bkp_replicator --slot=pg_receivewal --directory=/mnt/backup_db/archive_wal --synchronous |
| 20 | + |
| 21 | +# удаляем слот репликации, если он существует и неактивен (не используется); активный слот удалить нельзя, будет ошибка |
| 22 | +ExecStopPost=echo "pg_receivewal: drop unused replication slot" |
| 23 | +ExecStopPost=psql --user=bkp_replicator --dbname=postgres --quiet --no-psqlrc --pset=null=¤ --tuples-only --no-align \ |
| 24 | + --command="select pg_drop_replication_slot(slot_name) from pg_replication_slots where slot_name='pg_receivewal' and not active" |
| 25 | + |
| 26 | +[Install] |
| 27 | +WantedBy=multi-user.target |
0 commit comments