-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcount-table-rows.sh
executable file
·61 lines (52 loc) · 1.28 KB
/
count-table-rows.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
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
#!/usr/bin/env bash
# count-table-rows.sh
# by Jon Jensen <jon@endpointdev.com>
# 2024-03-12
# This is free and unencumbered software released into the public domain. See Unlicense: https://unlicense.org/
set -euo pipefail
shopt -s expand_aliases
trap 'exit 1' INT
#export PGUSER=postgres
alias psql="psql -XqtA"
alias timestamp="date -u -Iseconds"
echo -n "Host: "
hostname -f
echo -n "Started at: "
timestamp
echo
odd_names=$(psql -c "
SELECT quote_ident(datname)
FROM pg_database
WHERE datallowconn
AND datname <> quote_ident(datname)
ORDER BY 1
")
if [[ -n "$odd_names" ]]; then
echo "Skipping these databases whose names cause trouble for the shell:"
echo "$odd_names"
echo
fi
psql -c "
SELECT datname
FROM pg_database
WHERE datallowconn
AND datname = quote_ident(datname)
ORDER BY 1
" | while read db
do
echo "Working on database $db"
export PGDATABASE=$db
psql -c "
SELECT format('%I.%I', schemaname, tablename)
FROM pg_tables
WHERE schemaname NOT IN ('information_schema', 'pg_catalog')
ORDER BY schemaname, tablename
" | while read table
do
echo -n "Rows in table $table = "
psql -c "SELECT count(*) FROM $table"
done
echo
done
echo -n "Ended at: "
timestamp