Skip to content

Commit

Permalink
Version 1.1.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Azmodey committed Jan 12, 2021
1 parent d2a2bbf commit 1a070b0
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 24 deletions.
56 changes: 36 additions & 20 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,18 @@ A collection of shell scripts for PostgreSQL database administrator (DBA). Teste

## Installation

Copy the scripts to a separate **postgres** user directory (for example **~scripts/**) and grant the necessary execution rights:
As user **postgres**, download the latest version of the scripts collection (see [Releases](https://github.com/Azmodey/pg_dba_scripts/releases) page):
```
$ chmod 700 *.sh
$ chmod 600 settings.txt
# sudo su - postgres
$ wget https://github.com/Azmodey/pg_dba_scripts/archive/1.1.0.tar.gz
```

Extract script files to separate directory (for example **~scripts/**) and grant the necessary execution rights:
```
$ tar xvzf 1.1.0.tar.gz
$ mv pg_dba_scripts-1.1.0/scripts ~/scripts
$ chmod 700 ~/scripts/*.sh
$ chmod 600 ~/scripts/settings.txt
```


Expand Down Expand Up @@ -72,6 +80,16 @@ Vacuum full command executing with Access Exclusive lock. Replication lag appear
![pg_database_activity2](media/db_activity_vacuumfull.png)


Streaming replication to replica server. The server status is highlighted in a separate color, the type of replication (streaming), the slot used (node1_slot) and the lag in replication are shown.

![pg_database_activity3](media/db_activity_repstream.png)


Logical replication to the master PostgreSQL server 13.1. Subscription name (appdbsub) and replication lag (subscription_lag) are shown. Logical replication worker consumes 1.3% CPU.

![pg_database_activity4](media/db_activity_replogical.png)


---
### pg_database_hugepages

Expand Down Expand Up @@ -133,27 +151,25 @@ PostgreSQL status. Additionally, PostgreSQL processes and replication services a
```
PostgreSQL processes:
UID PID PPID C STIME TTY TIME CMD
postgres 16719 1 0 13:00 ? 00:00:04 /usr/pgsql-12/bin/postgres -D /var/lib/pgsql/12/data
postgres 16724 16719 0 13:00 ? 00:00:00 \_ postgres: logger
postgres 16728 16719 0 13:00 ? 00:00:01 \_ postgres: checkpointer
postgres 16729 16719 0 13:00 ? 00:00:00 \_ postgres: background writer
postgres 16730 16719 0 13:00 ? 00:00:02 \_ postgres: walwriter
postgres 16731 16719 0 13:00 ? 00:00:00 \_ postgres: autovacuum launcher
postgres 16732 16719 0 13:00 ? 00:00:00 \_ postgres: archiver last was 0000000600000005000000A7
postgres 16733 16719 0 13:00 ? 00:00:04 \_ postgres: stats collector
postgres 16734 16719 0 13:00 ? 00:00:00 \_ postgres: logical replication launcher
postgres 16736 16719 2 13:00 ? 00:00:41 \_ postgres: walsender postgres 192.168.1.198(34354) idle
postgres 16871 16719 1 13:00 ? 00:00:33 \_ postgres: walsender rep_user 192.168.1.197(34146) streaming 5/A8E437B8
postgres 23665 16719 0 13:03 ? 00:00:00 \_ postgres: postgres postgres 192.168.1.10(2053) idle
postgres 23666 16719 0 13:04 ? 00:00:10 \_ postgres: postgres appdb 192.168.1.10(2057) idle
postgres 985 1 0 11:05 ? 00:00:01 /usr/pgsql-12/bin/postmaster -D /var/lib/pgsql/12/data/
postgres 1121 985 0 11:05 ? 00:00:00 \_ postgres: logger
postgres 1138 985 0 11:05 ? 00:00:00 \_ postgres: checkpointer
postgres 1139 985 0 11:05 ? 00:00:00 \_ postgres: background writer
postgres 1140 985 0 11:05 ? 00:00:00 \_ postgres: walwriter
postgres 1141 985 0 11:05 ? 00:00:00 \_ postgres: autovacuum launcher
postgres 1142 985 0 11:05 ? 00:00:00 \_ postgres: archiver last was 0000000600000005000000E9
postgres 1143 985 0 11:05 ? 00:00:00 \_ postgres: stats collector
postgres 1146 985 0 11:05 ? 00:00:00 \_ postgres: logical replication launcher
postgres 1257 985 1 11:05 ? 00:00:40 \_ postgres: walsender postgres 192.168.1.198(59920) idle
postgres 1258 985 0 11:05 ? 00:00:33 \_ postgres: walsender rep_user 192.168.1.197(33130) streaming 5/EA8884B0
PostgreSQL status:
pg_ctl: server is running (PID: 16719)
/usr/pgsql-12/bin/postgres "-D" "/var/lib/pgsql/12/data"
pg_ctl: server is running (PID: 985)
/usr/pgsql-12/bin/postgres "-D" "/var/lib/pgsql/12/data/"
PostgreSQL replication service (sender). Works on Master server:
16736 ? Ss 0:41 postgres: walsender postgres 192.168.1.198(34354) idle
16871 ? Ss 0:33 postgres: walsender rep_user 192.168.1.197(34146) streaming 5/A8E437B8
postgres 1257 985 1 11:05 ? 00:00:40 postgres: walsender postgres 192.168.1.198(59920) idle
postgres 1258 985 0 11:05 ? 00:00:33 postgres: walsender rep_user 192.168.1.197(33130) streaming 5/EA8884B0
PostgreSQL replication service (receiver). Works on Replica server:
Expand Down
Binary file added media/db_activity_replogical.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added media/db_activity_repstream.png
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
35 changes: 35 additions & 0 deletions scripts/pg_database_activity.sh
Original file line number Diff line number Diff line change
Expand Up @@ -186,6 +186,41 @@ fi



# Replication status (Replica)
if [[ $DB_STATUS != " f" ]]; then

echo -e "${GREENLIGHT}Replication status (Replica):${NC}"
$PG_BIN/psql -c "
SELECT sender_host, sender_port, pid, slot_name, status, received_lsn, received_tli,
CASE WHEN pg_last_wal_receive_lsn() = pg_last_wal_replay_lsn() THEN 0
ELSE EXTRACT (EPOCH FROM now() - pg_last_xact_replay_timestamp())
END AS log_delay
FROM pg_stat_wal_receiver;" | grep -v row

PG_LOG_LINES=$((PG_LOG_LINES-5))

fi



# Logical Replication status (Replica)
logical_replication=`$PG_BIN/psql -t -c "select * from pg_stat_subscription;"`
if [[ ${#logical_replication} >0 ]]; then

echo -e "${GREENLIGHT}Logical Replication status (Replica):${NC}"

$PG_BIN/psql -c "
SELECT subid, subname, pid, relid, received_lsn, TO_CHAR(last_msg_send_time, 'dd.mm.yyyy HH24:MI:SS') as last_msg_send_time,
TO_CHAR(last_msg_receipt_time, 'dd.mm.yyyy HH24:MI:SS') as last_msg_receipt_time, latest_end_lsn, TO_CHAR(latest_end_time, 'dd.mm.yyyy HH24:MI:SS') as latest_end_time,
(pg_wal_lsn_diff(received_lsn, latest_end_lsn) / 1024)::int AS subscription_lag
FROM pg_stat_subscription;" | grep -v row

PG_LOG_LINES=$((PG_LOG_LINES-5))

fi



# PostgreSQL system process activity progress

# PostgreSQL 9.6 and higher
Expand Down
8 changes: 4 additions & 4 deletions scripts/pg_database_status.sh
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,20 @@
source ./settings.txt

echo "PostgreSQL processes:"
ps -afH --forest -u postgres | grep -v sshd | grep -v bash | grep -v 'ps -afH' | grep -v '/usr/bin/mc' | grep -v '/sbin/agetty'
ps -afH --forest -u postgres | grep -v sshd | grep -v bash | grep -v 'su - postgres' | grep -v 'ps -afH' | grep -v '/usr/bin/mc' | grep -v '/sbin/agetty'
echo

echo "PostgreSQL status:"
$PG_BIN/pg_ctl -D $PG_DATA status
echo

echo "PostgreSQL replication service (sender). Works on Master server:"
ps ax | grep -v grep | grep sender
ps -aef | grep -v grep | grep sender
echo

echo "PostgreSQL replication service (receiver). Works on Replica server:"
ps ax | grep -v grep | grep receiver
ps -aef | grep -v grep | grep receiver
echo

echo "PostgreSQL logical replication service (worker). Works on Replica server:"
ps ax | grep -v grep | grep "logical replication worker"
ps -aef | grep -v grep | grep "logical replication worker"

0 comments on commit 1a070b0

Please sign in to comment.