Skip to content

Commit e4495c4

Browse files
committed
Version 1.0.0
1 parent b8d3abb commit e4495c4

File tree

6 files changed

+19
-30
lines changed

6 files changed

+19
-30
lines changed

README.md

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,17 @@
22

33
A collection of shell scripts for PostgreSQL database administrator (DBA). Tested on PostgreSQL 12-13 under CentOS 7.
44

5-
- [scripts/pg_database_activity.sh](#pg_database_activity). PostgreSQL monitoring script, all information is displayed on one page. Displays PostgreSQL version and status (Master / Replica), hostname and IP address, CPU and Disks load. Shows the sizes of the main PostgreSQL directories, archived logs and free disk space, swap usage. It also displays memory consumption by PostgreSQL processes, statistics on databases, waits and locks, archive and replication statuses. When activities occur in PostgreSQL, the progress of operations is displayed: vacuum, vacuum full or cluster, index creation, analyze, pg_basebackup. At the end, the last entries of the PostgreSQL log file are displayed. For ease of perception, information is displayed in color.
6-
- [scripts/pg_database_activity_refresh.sh](#pg_database_activity). Fast refresh of the **pg_database_activity.sh** script every 5 seconds.
5+
[scripts/pg_database_activity.sh](#pg_database_activity). PostgreSQL monitoring script, all information is displayed on one page.
6+
- Displays PostgreSQL version and status (Master / Replica), hostname and IP address, CPU and Disks load.
7+
- Shows the sizes of the main PostgreSQL directories, archived logs and free disk space, swap usage.
8+
- Displays memory consumption by PostgreSQL processes, statistics on databases, waits and locks, archive and replication statuses.
9+
- When activities occur in PostgreSQL, the progress of operations is displayed: vacuum, vacuum full or cluster, index creation, analyze, pg_basebackup.
10+
- At the end, the last entries of the PostgreSQL log file are displayed.
11+
- For ease of perception, information is displayed in color.
12+
13+
[scripts/pg_database_activity_refresh.sh](#pg_database_activity). Fast refresh of the **pg_database_activity.sh** script every 5 seconds.
14+
15+
Small scripts to manage PostgreSQL:
716
- [scripts/pg_database_hugepages.sh](#pg_database_hugepages). Shows current usage of HugePages and recommended settings for PostgreSQL.
817
- [scripts/pg_database_start.sh](#pg_database_start). Start PostgreSQL, confirmation is required.
918
- [scripts/pg_database_stop.sh](#pg_database_stop). Stop PostgreSQL, confirmation is required.
@@ -53,14 +62,14 @@ PG_LOG_LINES=15 # PostgreSQL log lines to show. 0 - disable output
5362

5463
**Examples of work:**
5564

56-
Running pgbench, 10 backend connections. WALSync wait and Row Exclusive locks. WalSender background process consumes 6.7% CPU. Total CPU load 4.44 %.
65+
Running pgbench, 10 backend connections. WALSync wait and Row Exclusive locks. WalSender background processes consumes 19.3% and 24.2% CPU. Total CPU load 6.34 %.
5766

58-
![pg_database_activity1](media/dbactivity1.png)
67+
![pg_database_activity1](media/db_activity_pgbench.png)
5968

6069

61-
Vacuum full command executing with Access Exclusive lock. Replication lag appeared (total_lag 3), archiving are not lagging behind (diff 1). The size of Archive logs increased to 609 MB. WalSender background process consumes 11.6% CPU, overall Disk load 14.76 %. The number of PostgreSQL log file entries has automatically decreased.
70+
Vacuum full command executing with Access Exclusive lock. Replication lag appeared (total_lag 2721) on streaming and logical replication, archiving are not lagging behind (arc_diff 1). The size of Archive logs increased to 1.3 GB. WalSender background processes consumes 13.2% and 16.5% CPU, overall Disk load 9.73 %. The number of PostgreSQL log file entries has automatically decreased.
6271

63-
![pg_database_activity2](media/dbactivity2.png)
72+
![pg_database_activity2](media/db_activity_vacuumfull.png)
6473

6574

6675
---

media/db_activity_pgbench.png

107 KB
Loading

media/db_activity_vacuumfull.png

93 KB
Loading

media/dbactivity1.png

-99.9 KB
Binary file not shown.

media/dbactivity2.png

-97.8 KB
Binary file not shown.

scripts/pg_database_activity.sh

Lines changed: 4 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -16,18 +16,12 @@ PG_LOG_FILENAME=`ls -t $PG_LOG_DIR/postgresql-*.log | head -n1` # newest Postgre
1616

1717
# Backend Processes (Client connections). Uncomment to show
1818
pid_clients=`$PG_BIN/psql -t -c "SELECT pid FROM pg_stat_activity where backend_type='client backend' and pid<>pg_backend_pid();"`
19-
#echo "PID| Database| Username| Application name| Client address| Backend type| Wait event type| Wait event| Memory (KB)| CPU% " > pg_database_activity_tmp.txt
2019

2120
total_clients_mem=0
2221
total_clients_count=0
2322

2423
for pids in $pid_clients ; do
2524
mem=`ps -q $pids -eo rss | sed 1d`
26-
cpu=`ps -q $pids -eo pcpu | sed 1d`
27-
28-
# pid_client_info=`$PG_BIN/psql -t -c "SELECT datname as database, usename as username, application_name, client_addr, backend_type, wait_event_type, wait_event FROM pg_stat_activity where pid=$pids;"`
29-
# echo "$pids|$pid_client_info|$mem| $cpu" >> pg_database_activity_tmp.txt
30-
3125
total_clients_mem=$((total_clients_mem+mem))
3226
((total_clients_count++))
3327
done
@@ -47,7 +41,6 @@ for pids in $pid_server ; do
4741

4842
mem=`ps -q $pids -eo rss | sed 1d`
4943
cpu=`ps -q $pids -eo pcpu | sed 1d`
50-
#mem_cpu=`ps -q $pids -eo rss,pcpu | sed 1d | sed 's/ /|/'`
5144

5245
pid_client_info=`$PG_BIN/psql -t -c "SELECT datname as database, usename as username, application_name, client_addr, backend_type, wait_event_type, wait_event FROM pg_stat_activity where pid=$pids;"`
5346
echo "$pids|$pid_client_info|$mem| $cpu" >> pg_database_activity_tmp.txt
@@ -78,17 +71,14 @@ DB_STATUS=`$PG_BIN/psql -t -c "select pg_is_in_recovery();"`
7871
if [[ $DB_STATUS == " f" ]]; then
7972
STATUS="${GREENLIGHT}[$HOST ($HOSTIP) / PostgreSQL $POSTGRES_VER / Master]${YELLOW}"
8073
else
81-
STATUS="${PURPLE}[$HOST ($HOSTIP) / PostgreSQL $POSTGRES_VER / Replica]${YELLOW}"
74+
STATUS="${PURPLELIGHT}[$HOST ($HOSTIP) / PostgreSQL $POSTGRES_VER / Replica]${YELLOW}"
8275
fi
8376

8477
echo -e "${YELLOW}[$DATE] $STATUS [CPU load (1/5/15 min): $UPTIME] [Disk load: util $IOSTAT_UTIL %, await $IOSTAT_AWAIT ms] ${NC}"
8578

8679

8780

8881
# Title (2nd line). Disk usage & free
89-
#PG_DATA=/postgres/13/data # Main data directory
90-
#PG_ARC=/postgres/13/archive # Archive logs directory
91-
9282
DIR_DATA_FREE=`df -h $PG_DATA | sed 1d | grep -v used | awk '{ print $4 "\t" }' | tr -d '\t'` # free disk space for PG_DATA
9383
DIR_ARC_FREE=`df -h $PG_ARC | sed 1d | grep -v used | awk '{ print $4 "\t" }' | tr -d '\t'` # free disk space for PG_ARC
9484
DIR_BASE_SIZE=`du -sh $PG_DATA/base | awk '{print $1}'` # Base folder size
@@ -113,16 +103,6 @@ echo
113103

114104
# ------------------------------------------------
115105

116-
# Background Processes (Client connections). Uncomment to show
117-
# echo
118-
# echo -e "${GREENLIGHT}Clients connections ($total_clients_count) memory consumption: $total_clients_mem_mb MB${NC}"
119-
# echo "--------------------------------------------------------------------------------------------------------------------------------------------"
120-
# sort -t '|' -k9 -n pg_database_activity_tmp.txt | column -t -s '|' -o ' |'
121-
# echo "--------------------------------------------------------------------------------------------------------------------------------------------"
122-
# echo
123-
124-
125-
126106
# Background Processes (Server connections)
127107
echo -e "${GREENLIGHT}Background processes ($total_server_count) memory consumption: $total_server_mem_mb MB${NC}"
128108
echo "---------------------------------------------------------------------------------------------------------------------------------------------------------------"
@@ -169,7 +149,7 @@ if [[ ${#archiving_status} >0 ]]; then
169149
('x'||substring(pg_walfile_name(pg_current_wal_lsn()),9,8))::bit(32)::int*256 +
170150
('x'||substring(pg_walfile_name(pg_current_wal_lsn()),17))::bit(32)::int -
171151
('x'||substring(last_archived_wal,9,8))::bit(32)::int*256 -
172-
('x'||substring(last_archived_wal,17))::bit(32)::int as diff
152+
('x'||substring(last_archived_wal,17))::bit(32)::int as arc_diff
173153
--TO_CHAR(stats_reset, 'dd.mm.yyyy') as stats_reset
174154
from pg_stat_archiver;" | grep -v row
175155
else
@@ -185,11 +165,11 @@ fi
185165

186166

187167

188-
# Replication status
168+
# Replication status (Master)
189169
replication_status=`$PG_BIN/psql -t -c "select * from pg_stat_replication;"`
190170
if [[ ${#replication_status} >0 ]]; then
191171

192-
echo -e "${GREENLIGHT}Replication status:${NC}"
172+
echo -e "${GREENLIGHT}Replication status (Master):${NC}"
193173
$PG_BIN/psql -c "
194174
SELECT r.client_addr AS client_addr, r.usename AS username, r.application_name AS app_name, r.pid, s.slot_name, s.slot_type, r.state, r.sync_state AS MODE,
195175
(pg_wal_lsn_diff(pg_current_wal_lsn(), r.sent_lsn) / 1024)::int AS send_lag, -- sending_lag (network problems)

0 commit comments

Comments
 (0)