May 17, 2026, 11:40 PM
This commit is contained in:
@@ -0,0 +1,367 @@
|
||||
Notatka sporządzona na podstawie informacji ze SLack instrukcję przygotował [@MichalBulawa]
|
||||
|
||||
GSNI -> Postgres 13 + ETCD + Patroni + HAproxy + confd + keepalived
|
||||
|
||||
|
||||
# Informacje ogólne
|
||||
|
||||
Postgres - wiadomo
|
||||
etcd -> klaster do przechowywania kluczy dla rozwiązania
|
||||
patroni -> coś co utrzymuje całość przy życiu ( manual później )
|
||||
haproxy -> wiadomo / używane do komunikacji z “Leaderem klastra”, oraz do umożliwienia load-balancingu na replikach
|
||||
confd -> odpowiedzialny za pilnowanie konfiguracji plików ( nie pozwala wprowadzić samemu zmian
|
||||
keepalived -> usługa do utrzymywania VIP-a
|
||||
|
||||
( GSNI TO: plpa2ps01srrm, plpa2ps02srrm, plwa2ps03srrm )
|
||||
|
||||
1. Prereq
|
||||
|
||||
/etc/hosts ( na każdym serwerze wyglądają podobnie )
|
||||
-sh-4.4$ cat /etc/hosts
|
||||
127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4::1 localhost localhost.localdomain localhost6 localhost6.localdomain6
|
||||
158.98.154.129 plpa2ps01srrm.plf.esni.ibm.com plpa2ps01srrm pgsql-01
|
||||
158.98.154.130 plpa2ps02srrm.plf.esni.ibm.com plpa2ps02srrm pgsql-02
|
||||
158.98.154.131 plwa2ps03srrm.plf.esni.ibm.com plwa2ps03srrm pgsql-03
|
||||
158.98.154.122 plpa2ps00srrm.plf.esni.ibm.com plpa2ps00srrm pgsql-vip
|
||||
-sh-4.4$
|
||||
2. Postgres ( pre-install )
|
||||
|
||||
-sh-4.4$ ls -l /etc/yum.repos.d/pgdg-redhat-all.repo
|
||||
-rw-r--r--. 1 root root 11072 Sep 30 16:28 /etc/yum.repos.d/pgdg-redhat-all.repo
|
||||
-sh-4.4$ head /etc/yum.repos.d/pgdg-redhat-all.repo -n 15
|
||||
#######################################################
|
||||
# PGDG Red Hat Enterprise Linux / CentOS repositories #
|
||||
#######################################################
|
||||
|
||||
# PGDG Red Hat Enterprise Linux / CentOS stable common repository for all PostgreSQL versions
|
||||
|
||||
[pgdg-common]
|
||||
name=PostgreSQL common RPMs for RHEL/CentOS $releasever - $basearch
|
||||
baseurl=https://download.postgresql.org/pub/repos/yum/common/redhat/rhel-$releasever-$basearch
|
||||
enabled=1
|
||||
gpgcheck=1
|
||||
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-PGDG
|
||||
repo_gpgcheck = 1
|
||||
proxy=http://172.25.1.10:8080 <--------------- taka sztuczka jak dodać repozytorium postgresowe na gsni by działało
|
||||
|
||||
-sh-4.4$
|
||||
3. squid ( opcja za firewallem więc proxy potrzebne )
|
||||
|
||||
[root@plpa4gp01psrm acls]# cat pgsql.services
|
||||
postgresql.org
|
||||
github.com
|
||||
pypi.org
|
||||
python.org
|
||||
pythonhosted.org
|
||||
[root@plpa4gp01psrm acls]#
|
||||
4. sysctl
|
||||
|
||||
sysctl svm.swappiness=1
|
||||
sysctl vm.dirty_vm.min_free_kbytes=102400
|
||||
sysctl vm.dirty_expire_centisecs=1000
|
||||
sysctl vm.dirty_background_bytes=67108864
|
||||
sysctl vm.dirty_bytes=536870912
|
||||
sysctl vm.nr_hugepages=9510
|
||||
sysctl vm.zone_reclaim_mode=0
|
||||
sysctl kernel.numa_balancing=0
|
||||
sysctl kernel.sched_migration_cost_ns=5000000
|
||||
sysctl kernel.sched_autogroup_enabled=0
|
||||
sysctl net.ipv4.ip_nonlocal_bind=1
|
||||
sysctl net.ipv4.ip_forward=1
|
||||
sysctl net.core.netdev_max_backlog=10000
|
||||
sysctl net.ipv4.tcp_max_syn_backlog=8192
|
||||
sysctl net.core.somaxconn=65535
|
||||
sysctl net.ipv4.tcp_tw_reuse=1
|
||||
5. yum install && pip3 update
|
||||
|
||||
yum install -y git
|
||||
yum install -y keepalived
|
||||
yum install -y libyaml
|
||||
yum install -y python3-psycopg2
|
||||
yum install -y python2-psycopg2
|
||||
yum install -y python3
|
||||
yum install -y python2
|
||||
yum install -y haproxy
|
||||
HTTPS_PROXY=http://172.25.1.10:8080 pip3 install --upgrade pip
|
||||
6. SSL
|
||||
Sprawdzenie czy są zaimportowane w CA
|
||||
7. System HDD + Postgres
|
||||
|
||||
[root@plpa2ps01srrm ~]# lsblk
|
||||
NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT
|
||||
sda 8:0 0 50G 0 disk
|
||||
├─sda1 8:1 0 512M 0 part /boot
|
||||
└─sda2 8:2 0 49.5G 0 part
|
||||
└─rootvg-backup_lv 253:12 0 10G 0 lvm /backup
|
||||
sdb 8:16 0 100G 0 disk
|
||||
└─databasevg-data_lv 253:13 0 100G 0 lvm /database
|
||||
sr0 11:0 1 1024M 0 rom
|
||||
[root@plpa2ps01srrm ~]#
|
||||
8. Postgres install
|
||||
<https://www.postgresql.org/download/linux/redhat/> <– tutaj też mamy dodanie repozytorium / a je ( o ile trzeba ) edytujemy dodając proxy no i jak podaje manual na stronie
|
||||
|
||||
dnf -qy module disable postgresql
|
||||
dnf install -y postgresql13-server
|
||||
dnf install -y pgaudit15_13.x86_64
|
||||
dnf install -y postgresql13-contrib
|
||||
|
||||
taka instalacja powoduje dodanie użytkownika w systemie o nazwie postgres oraz binarki które lądują w:
|
||||
|
||||
-bash-4.4$ ls -l /usr/pgsql-13/bin/
|
||||
total 11104
|
||||
-rwxr-xr-x. 1 root root 73192 Aug 12 12:04 clusterdb
|
||||
|
||||
9. Pre-Konfiguracja Porsgres-a
|
||||
Pre-Konfiguracja Porsgres-a
|
||||
<https://www.pgconfig.org/> —> generuje kawałek pliku postgres.conf w zależności od tego co mamy za sprzęt na GSNI to: RHEL8 x86-64, NS-SAN, 8cpu, 16mem, max<sub>con</sub> 100, ver.13, ERP or long… ( zapotrzebowanie dla Zabbix )
|
||||
10. Uruchomienie bazy
|
||||
|
||||
[root@plpa2ps01srrm ~]# cd /database/
|
||||
[root@plpa2ps01srrm database]# mkdir data
|
||||
[root@plpa2ps01srrm database]# chown postgres:postgres data/
|
||||
[root@plpa2ps01srrm database]# su - postgres
|
||||
Last login: Thu Oct 7 10:59:49 CEST 2021 on pts/1
|
||||
-bash-4.4$ /usr/pgsql-13/bin/initdb --pgdata=/database/data
|
||||
The files belonging to this database system will be owned by user "postgres".
|
||||
This user must also own the server process.
|
||||
|
||||
The database cluster will be initialized with locale "en_US.UTF-8".
|
||||
The default database encoding has accordingly been set to "UTF8".
|
||||
The default text search configuration will be set to "english".
|
||||
|
||||
Data page checksums are disabled.
|
||||
|
||||
fixing permissions on existing directory /database/data ... ok
|
||||
creating subdirectories ... ok
|
||||
selecting dynamic shared memory implementation ... posix
|
||||
selecting default max_connections ... 100
|
||||
selecting default shared_buffers ... 128MB
|
||||
selecting default time zone ... Europe/Warsaw
|
||||
creating configuration files ... ok
|
||||
running bootstrap script ... ok
|
||||
performing post-bootstrap initialization ... ok
|
||||
syncing data to disk ... ok
|
||||
|
||||
initdb: warning: enabling "trust" authentication for local connections
|
||||
You can change this by editing pg_hba.conf or using the option -A, or
|
||||
--auth-local and --auth-host, the next time you run initdb.
|
||||
|
||||
Success. You can now start the database server using:
|
||||
|
||||
/usr/pgsql-13/bin/pg_ctl -D /database/data -l logfile start
|
||||
|
||||
-bash-4.4$ cd /database/data/
|
||||
-bash-4.4$ ls
|
||||
base pg_hba.conf pg_notify pg_stat pg_twophase postgresql.auto.conf
|
||||
global pg_ident.conf pg_replslot pg_stat_tmp PG_VERSION postgresql.conf
|
||||
pg_commit_ts pg_logical pg_serial pg_subtrans pg_wal
|
||||
pg_dynshmem pg_multixact pg_snapshots pg_tblspc pg_xact
|
||||
-bash-4.4$ /usr/pgsql-13/bin/pg_ctl -D /database/data -l logfile start
|
||||
waiting for server to start.... done
|
||||
server started
|
||||
-bash-4.4$ psql
|
||||
psql (13.4)
|
||||
Type "help" for help.
|
||||
|
||||
postgres=#
|
||||
11. Hasło dla użytkownika postgres oraz na potrzeby replikacji, użytkownik do replikacji z hasłem
|
||||
|
||||
postgres=# ALTER USER postgres PASSWORD 'bo zupa była za słona'; // :D
|
||||
ALTER ROLE
|
||||
postgres=# \q
|
||||
-bash-4.4$ createuser -U postgres replicator -P -c 5 --replication
|
||||
Enter password for new role: 'bo zupa była za słona' // :D
|
||||
Enter it again: 'bo zupa była za słona' // :D
|
||||
-bash-4.4$
|
||||
12. wyłączamy bazę -> bo już nam narazie nie będzie potrzebna
|
||||
|
||||
-bash-4.4$ /usr/pgsql-13/bin/pg_ctl stop -D /database/data
|
||||
waiting for server to shut down.... done
|
||||
server stopped
|
||||
-bash-4.4$
|
||||
13. ETCD
|
||||
|
||||
wget https://github.com/etcd-io/etcd/releases/download/v3.5.0/etcd-v3.5.0-linux-amd64.tar.gz
|
||||
tar -xzf etcd-v3.5.0-linux-amd64.tar.gz
|
||||
cp etcd-v3.5.0-linux-amd64/etcd etcd-v3.5.0-linux-amd64/etcdctl etcd-v3.5.0-linux-amd64/etcdutl /usr/bin/
|
||||
|
||||
[root@plwa2ps03srrm ~]# /usr/bin/etcd --version
|
||||
etcd Version: 3.5.0
|
||||
Git SHA: 946a5a6f2
|
||||
Go Version: go1.16.3
|
||||
Go OS/Arch: linux/amd64
|
||||
[root@plwa2ps03srrm ~]# /usr/bin/etcdctl version
|
||||
etcdctl version: 3.5.0
|
||||
API version: 3.5
|
||||
[root@plwa2ps03srrm ~]# /usr/bin/etcdutl version
|
||||
etcdutl version: 3.5.0
|
||||
API version: 3.5
|
||||
[root@plwa2ps03srrm ~]#
|
||||
14. konfiguracja ( serwis uruchamiany jako root / więc uprawnienia aż tak nie są ważne )
|
||||
|
||||
vim /usr/lib/systemd/system/etcd.service
|
||||
vim /etc/etcd/etcd.conf
|
||||
mkdir /etc/etcd
|
||||
mkdir /var/lib/etcd
|
||||
vim /etc/etcd/etcd.conf
|
||||
systemctl enable etcd.service
|
||||
systemctl start etcd.service
|
||||
15. Patroni
|
||||
|
||||
vim requirements.txt
|
||||
pip3 install -r requirements.txt
|
||||
pip3 install patroni[etcd]
|
||||
mkdir -p /etc/patroni/
|
||||
chmod go-rwx /etc/patroni/
|
||||
chown postgres:postgres /etc/patroni/
|
||||
vim /etc/patroni/patroni.yml
|
||||
vim /usr/lib/systemd/system/patroni.service
|
||||
systemctl daemon-reload
|
||||
echo "export PATRONICTL_CONFIG_FILE=/etc/patroni/patroni.yml" >> /etc/profile
|
||||
|
||||
cat requirements
|
||||
|
||||
urllib3>=1.19.1,!=1.21
|
||||
ipaddress; python<sub>version</sub>==“2.7”
|
||||
boto
|
||||
PyYAML
|
||||
six >= 1.7
|
||||
kazoo>=1.3.1
|
||||
python-etcd>=0.4.3,<0.5
|
||||
python-consul>=0.7.1
|
||||
click>=4.1
|
||||
prettytable>=0.7
|
||||
python-dateutil
|
||||
pysyncobj>=0.3.8
|
||||
cryptography>=1.4
|
||||
psutil>=2.0.0
|
||||
ydiff>=1.2.0
|
||||
|
||||
[root@plpa2ps01srrm ~]# patronictl version
|
||||
patronictl version 2.1.1
|
||||
[root@plpa2ps01srrm ~]#
|
||||
16. Keepalived
|
||||
zainstalowany był wcześniej, konfiguracja w plikach na serverze:
|
||||
|
||||
vim /etc/keepalived/keepalived.conf
|
||||
vim /usr/libexec/keepalived/leader<sub>check.sh</sub>
|
||||
chmod u+x /usr/libexec/keepalived/leader<sub>check.sh</sub>
|
||||
|
||||
standardowe rozwiązanie pgSQL+etcd+Patroni+HAProxy wykorzystuje do VIP-y keepalived i opiera go o działający proces haproxy natomiast moim zdaniem lepszym rozwiązaniem jest oprzeć go o inny mechanizm sprawdzający / o sam klaster patroni, co też zastosowane jest w GSNI ( skrypt leader<sub>check.sh</sub> )
|
||||
|
||||
17. HAProxy
|
||||
rozwiązanie zakłada że:
|
||||
|
||||
- połączenie do vip:5000 - łączy nas do lidera i daje prawo zapisu i odczytu
|
||||
- połączenie do vip:5001 - łączy nas do jednej z replik z prawem tylko do odczytu
|
||||
|
||||
moja modyfikacja daje możliwość podłączenia do vip:5432 - łączy do lidera i daje prawo do odczytu i zapisu (różnice pomiędzy podłączeniem via 5000 a 5432 są w działaniu pg<sub>hba.conf</sub>)
|
||||
|
||||
vim /etc/haproxy/haproxy.conf
|
||||
|
||||
[root@plpa2ps01srrm ~]# systemctl start patroni.service
|
||||
[root@plpa2ps01srrm ~]# patronictl list
|
||||
+---------------+----------------+--------+---------+----+-----------+
|
||||
| Member | Host | Role | State | TL | Lag in MB |
|
||||
+ Cluster: PLPA2PS00SRRM (7016247082177975663) -----+----+-----------+
|
||||
| plpa2ps01srrm | 158.98.154.129 | Leader | running | 2 | |
|
||||
+---------------+----------------+--------+---------+----+-----------+
|
||||
[root@plpa2ps01srrm ~]#
|
||||
|
||||
18. Stworzenie / edycja / wyświetlanie konfiguracji w klastrze:
|
||||
defakto stworzony to on już jest, bo część z niego jest brana z pliku patroni.yml
|
||||
|
||||
patronictl edit-config / patronictl show-config
|
||||
|
||||
19. Uruchomienie pozostałych nodów klastra:
|
||||
jak już odpowiednio wyedytujemy hosty dla replikacji danych to po odpaleniu na 2 nodzie patroni mamy efekt:
|
||||
|
||||
[root@plpa2ps02srrm patroni]# patronictl list
|
||||
+---------------+----------------+---------+---------+----+-----------+
|
||||
| Member | Host | Role | State | TL | Lag in MB |
|
||||
+ Cluster: PLPA2PS00SRRM (7016247082177975663) ------+----+-----------+
|
||||
| plpa2ps01srrm | 158.98.154.129 | Leader | running | 3 | |
|
||||
| plpa2ps02srrm | 158.98.154.130 | Replica | running | 3 | 0 |
|
||||
+---------------+----------------+---------+---------+----+-----------+
|
||||
[root@plpa2ps02srrm patroni]#
|
||||
|
||||
i ostatni
|
||||
|
||||
[root@plwa2ps03srrm patroni]# patronictl list
|
||||
+---------------+----------------+---------+---------+----+-----------+
|
||||
| Member | Host | Role | State | TL | Lag in MB |
|
||||
+ Cluster: PLPA2PS00SRRM (7016247082177975663) ------+----+-----------+
|
||||
| plpa2ps01srrm | 158.98.154.129 | Leader | running | 3 | |
|
||||
| plpa2ps02srrm | 158.98.154.130 | Replica | running | 3 | 16 |
|
||||
| plwa2ps03srrm | 158.98.154.131 | Replica | running | 3 | 0 |
|
||||
+---------------+----------------+---------+---------+----+-----------+
|
||||
[root@plwa2ps03srrm patroni]#
|
||||
|
||||
20. Start usługi keepalived
|
||||
|
||||
[root@plpa2ps01srrm ~]# systemctl start keepalived.service
|
||||
[root@plpa2ps01srrm ~]# systemctl status keepalived.service
|
||||
● keepalived.service - LVS and VRRP High Availability Monitor
|
||||
Loaded: loaded (/usr/lib/systemd/system/keepalived.service; disabled; vendor preset: disabled)
|
||||
Active: active (running) since Mon 2021-10-11 13:02:39 CEST; 5s ago
|
||||
Process: 3677958 ExecStart=/usr/sbin/keepalived $KEEPALIVED_OPTIONS (code=exited, status=0/SUCCESS)
|
||||
Main PID: 3677959 (keepalived)
|
||||
Tasks: 2 (limit: 102175)
|
||||
Memory: 4.4M
|
||||
CGroup: /system.slice/keepalived.service
|
||||
├─3677959 /usr/sbin/keepalived -D
|
||||
└─3677960 /usr/sbin/keepalived -D
|
||||
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: Registering Kernel netlink command channel
|
||||
Oct 11 13:02:39 plpa2ps01srrm systemd[1]: Started LVS and VRRP High Availability Monitor.
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: Opening file '/etc/keepalived/keepalived.conf>
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: Assigned address 158.98.154.129 for interface>
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: Registering gratuitous ARP shared channel
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: (VI_1) removing VIPs.
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: (VI_1) Entering BACKUP STATE (init)
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: VRRP sockpool: [ifindex( 2), family(IPv4), p>
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: VRRP_Script(haproxy_check) succeeded
|
||||
Oct 11 13:02:39 plpa2ps01srrm Keepalived_vrrp[3677960]: (VI_1) Changing effective priority from 100 t>
|
||||
[root@plpa2ps01srrm ~]# ip a
|
||||
1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
|
||||
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
|
||||
inet 127.0.0.1/8 scope host lo
|
||||
valid_lft forever preferred_lft forever
|
||||
2: ens192: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
|
||||
link/ether 00:50:56:8e:3a:3f brd ff:ff:ff:ff:ff:ff
|
||||
inet 158.98.154.129/24 brd 158.98.154.255 scope global noprefixroute ens192
|
||||
valid_lft forever preferred_lft forever
|
||||
inet 158.98.154.122/32 scope global ens192
|
||||
valid_lft forever preferred_lft forever
|
||||
[root@plpa2ps01srrm ~]#
|
||||
|
||||
21. start haproxy
|
||||
|
||||
[root@plwa2ps03srrm ~]# systemctl enable haproxy.service && systemctl status haproxy
|
||||
Created symlink /etc/systemd/system/multi-user.target.wants/haproxy.service → /usr/lib/systemd/system/haproxy.service.
|
||||
● haproxy.service - HAProxy Load Balancer
|
||||
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
|
||||
Active: inactive (dead)
|
||||
[root@plwa2ps03srrm ~]# systemctl start haproxy
|
||||
[root@plwa2ps03srrm ~]# systemctl status haproxy.service
|
||||
● haproxy.service - HAProxy Load Balancer
|
||||
Loaded: loaded (/usr/lib/systemd/system/haproxy.service; enabled; vendor preset: disabled)
|
||||
Active: active (running) since Mon 2021-10-11 13:15:52 CEST; 54s ago
|
||||
Process: 3365621 ExecStartPre=/usr/sbin/haproxy -f $CONFIG -c -q $OPTIONS (code=exited, status=0/SU>
|
||||
Main PID: 3365624 (haproxy)
|
||||
Tasks: 2 (limit: 102175)
|
||||
Memory: 14.4M
|
||||
CGroup: /system.slice/haproxy.service
|
||||
├─3365624 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
|
||||
└─3365627 /usr/sbin/haproxy -Ws -f /etc/haproxy/haproxy.cfg -p /run/haproxy.pid
|
||||
|
||||
Oct 11 13:15:52 plwa2ps03srrm systemd[1]: Starting HAProxy Load Balancer...
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy stats started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy stats started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy master started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy master started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy replicas started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm haproxy[3365624]: Proxy replicas started.
|
||||
Oct 11 13:15:52 plwa2ps03srrm systemd[1]: Started HAProxy Load Balancer.
|
||||
[root@plwa2ps03srrm ~]#
|
||||
|
||||
@@ -0,0 +1,2 @@
|
||||
# PostgreSQL
|
||||
Wszystko co znajdę na temat bazy danych PostgreSQL
|
||||
@@ -0,0 +1,15 @@
|
||||
-- export specific column data to text file:
|
||||
|
||||
copy EMPLOYEE( EMP_NAME,EMP_ID) to '/tmp/emp.txt';
|
||||
|
||||
-- export complete table data to text file:
|
||||
|
||||
copy EMPLOYEE to '/tmp/emp.txt';
|
||||
|
||||
-- export table data to csv file:
|
||||
|
||||
copy EMPLOYEE to '/tmp/emp.csv' with csv headers;
|
||||
|
||||
-- export specific query output to csv file:
|
||||
|
||||
copy ( select ename,depname from emp where depname='HR') to '/tmp/emp.csv' with csv headers;
|
||||
@@ -0,0 +1,37 @@
|
||||
How to connect to postgres db:
|
||||
|
||||
set PATH if not done:
|
||||
|
||||
postgres$ export PATH=/Library/PostgreSQL/10/bin:$PATH
|
||||
postgres$ which psql
|
||||
/Library/PostgreSQL/10/bin/psql
|
||||
|
||||
connect to db using SYNTAX - psql -d -U
|
||||
|
||||
postgres$ psql -d edb -U postgres
|
||||
Password for user postgres:
|
||||
psql (10.13)
|
||||
Type "help" for help.
|
||||
|
||||
postgres=#
|
||||
|
||||
Find current connection info:
|
||||
|
||||
postgres=# \conninfo
|
||||
You are connected to database "edb" as user "postgres" via socket in "/tmp" at port "5432".
|
||||
|
||||
|
||||
|
||||
postgres=# select current_schema,current_user,session_user,current_database();
|
||||
current_schema | current_user | session_user | current_database
|
||||
----------------+--------------+--------------+------------------
|
||||
public | postgres. | postgres | edb
|
||||
|
||||
|
||||
|
||||
Switch to another database:
|
||||
|
||||
postgres-# \c dbaclass
|
||||
You are now connected to database "dbaclass" as user "postgres".
|
||||
|
||||
|
||||
@@ -0,0 +1,18 @@
|
||||
DATA_DIRECTORY - > Specifies the directory to use for data storage.
|
||||
|
||||
dbaclass=# show data_directory;
|
||||
|
||||
data_directory
|
||||
-----------------------------
|
||||
/Library/PostgreSQL/10/data
|
||||
(1 row)
|
||||
|
||||
dbaclass=# select setting from pg_settings where name = 'data_directory';
|
||||
setting
|
||||
-----------------------------
|
||||
/Library/PostgreSQL/10/data
|
||||
(1 row)
|
||||
|
||||
-- This will show location of important files in postgres
|
||||
|
||||
dbaclass=# SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
|
||||
@@ -0,0 +1,21 @@
|
||||
- Drop database from psql
|
||||
Note - while dropping a database, you need to connect to a database other than the db you are trying to drop.
|
||||
|
||||
postgres=# \conninfo
|
||||
You are connected to database "postgres" as user "postgres" on host "localhost" at port "5432".
|
||||
|
||||
postgres#drop database "DBACLASS";
|
||||
|
||||
-- Drop database using dropdb os utility
|
||||
|
||||
postgres$ pwd
|
||||
/Library/PostgreSQL/10/bin
|
||||
|
||||
postgres$ ./dropdb -e "DBACLASS"
|
||||
Password:
|
||||
SELECT pg_catalog.set_config('search_path', '', false)
|
||||
DROP DATABASE "DBACLASS";
|
||||
|
||||
|
||||
|
||||
For complete article visit - > https://dbaclass.com/article/drop-database-postgres/
|
||||
@@ -0,0 +1,19 @@
|
||||
1. Create directory for archiving:
|
||||
|
||||
mkdir -p /Library/PostgreSQL/10/data/archive/
|
||||
|
||||
2. Update the postgres.conf file with below values
|
||||
|
||||
wal_level = replica
|
||||
archive_mode = on
|
||||
max_wal_senders=1
|
||||
archive_command= 'test ! -f /Library/PostgreSQL/10/data/archive/%f && cp %p /Library/PostgreSQL/10/data/archive/%f'
|
||||
|
||||
3. Restart the postgres servers
|
||||
|
||||
export PGDATA=/Library/PostgreSQL/10/data
|
||||
pg_ctl stop
|
||||
pg_ctl start
|
||||
|
||||
3. Check archive status:
|
||||
postgres=# select name,setting from pg_settings where name like 'archive%';
|
||||
@@ -0,0 +1,15 @@
|
||||
-- Find list of installed extension:
|
||||
|
||||
psql# \dx
|
||||
|
||||
(or)
|
||||
|
||||
psql#\dx+
|
||||
|
||||
(or)
|
||||
|
||||
psql#SELECT * FROM pg_extension;
|
||||
|
||||
-- For finding available extension in server:
|
||||
|
||||
psql# SELECT * FROM pg_available_extensions;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- QUERY TO FIND BLOCKING SESSION DETAILS
|
||||
|
||||
dbaclass#select pid as blocked_pid, usename, pg_blocking_pids(pid) as "blocked_by(pid)", query as blocked_query from pg_stat_activity where cardinality(pg_blocking_pids(pid)) > 0;
|
||||
@@ -0,0 +1,32 @@
|
||||
Find location of postgres related conf files
|
||||
|
||||
dbaclass=# SELECT name, setting FROM pg_settings WHERE category = 'File Locations';
|
||||
name | setting
|
||||
-------------------+---------------------------------------------
|
||||
config_file | /Library/PostgreSQL/10/data/postgresql.conf
|
||||
data_directory | /Library/PostgreSQL/10/data
|
||||
external_pid_file |
|
||||
hba_file | /Library/PostgreSQL/10/data/pg_hba.conf
|
||||
ident_file | /Library/PostgreSQL/10/data/pg_ident.conf
|
||||
(5 rows)
|
||||
|
||||
alternatively:
|
||||
|
||||
postgres=# show config_file;
|
||||
|
||||
config_file
|
||||
--------------------------------------------
|
||||
|
||||
/Library/PostgreSQL/10/data/postgresql.conf
|
||||
|
||||
(1 row)
|
||||
|
||||
postgres=# show hba_file;
|
||||
|
||||
hba_file
|
||||
-----------------------------------------
|
||||
/Library/PostgreSQL/10/data/pg_hba.conf
|
||||
|
||||
(1 row)
|
||||
|
||||
postgres=# show ident_file;
|
||||
@@ -0,0 +1 @@
|
||||
select t.relname,l.locktype,page,virtualtransaction,pid,mode,granted from pg_locks l, pg_stat_all_tables t where l.relation=t.relid order by relation asc;
|
||||
@@ -0,0 +1,25 @@
|
||||
Find sessions in the postgres:
|
||||
|
||||
select pid as process_id,
|
||||
usename as username,
|
||||
datname as database_name,
|
||||
client_addr as client_address,
|
||||
application_name,
|
||||
backend_start,
|
||||
state,
|
||||
state_change,query
|
||||
from pg_stat_activity;
|
||||
|
||||
|
||||
|
||||
-- For specific database:
|
||||
|
||||
select pid as process_id,
|
||||
usename as username,
|
||||
datname as database_name,
|
||||
client_addr as client_address,
|
||||
application_name,
|
||||
backend_start,
|
||||
state,
|
||||
state_change,query
|
||||
from pg_stat_activity where datname='dbaclass';
|
||||
@@ -0,0 +1,5 @@
|
||||
postgres=# SELECT pg_database.datname as "database_name", pg_size_pretty(pg_database_size(pg_database.datname)) AS size_in_mb FROM pg_database ORDER by size_in_mb DESC;
|
||||
|
||||
(or)
|
||||
|
||||
postgres=# \l+
|
||||
@@ -0,0 +1,7 @@
|
||||
-- First find the pid of the session:
|
||||
|
||||
dbaclass#SELECT datname as database, pid as pid, usename as username, application_name , client_addr , query FROM pg_stat_activity;
|
||||
|
||||
<< Lets say the pid=1124, in the below query pass the pid value to kill that particular session..>>
|
||||
|
||||
dbaclass#select pg_terminate_backend(pid) from pg_stat_activity where pid='1123';
|
||||
@@ -0,0 +1,8 @@
|
||||
-- Here we want to kill all session of the user postgres
|
||||
|
||||
-- List all the session of that user.
|
||||
dbaclass#select datname as database, pid as pid, usename as username, application_name , client_addr, query FROM pg_stat_activity where username='postgres';
|
||||
|
||||
-- Kill all the session of user postgres.
|
||||
|
||||
dbaclass#select pg_terminate_backend(pid) from pg_stat_activity where usename='postgres';
|
||||
@@ -0,0 +1,17 @@
|
||||
-- Last pg config reload time
|
||||
|
||||
postgres=# select pg_conf_load_time() ;
|
||||
pg_conf_load_time
|
||||
----------------------------------
|
||||
2020-07-06 13:20:18.048689+05:30
|
||||
(1 row)
|
||||
|
||||
-- Reload again and see whether reload time changed or not
|
||||
|
||||
postgres=# select pg_reload_conf();
|
||||
pg_reload_conf
|
||||
----------------
|
||||
t
|
||||
(1 row)
|
||||
|
||||
postgres=# select pg_conf_load_time() ;
|
||||
@@ -0,0 +1,13 @@
|
||||
postgres=# \list+
|
||||
List of databases
|
||||
Name | Owner | Encoding | Collate | Ctype | Access privileges | Size | Tablespace | Description
|
||||
-----------+----------+----------+---------+-------+-----------------------+---------+------------+--------------------------------------------
|
||||
dbaclass | postgres | UTF8 | C | C | | 2268 MB | pg_default |
|
||||
postgres | postgres | UTF8 | C | C | | 4132 MB | pg_default | default administrative connection database
|
||||
template0 | postgres | UTF8 | C | C | =c/postgres +| 7601 kB | pg_default | unmodifiable empty database
|
||||
| | | | | postgres=CTc/postgres | | |
|
||||
template1 | postgres | UTF8 | C | C | =c/postgres +| 7601 kB | pg_default | default template for new databases
|
||||
| | | | | postgres=CTc/postgres | | |
|
||||
(4 rows)
|
||||
|
||||
postgres=# select datname from pg_database;
|
||||
@@ -0,0 +1,3 @@
|
||||
-- Below command signals the log-file manager to switch to a new output file immediately.it is just like an alert log
|
||||
|
||||
select pg_rotate_logfile() ;
|
||||
@@ -0,0 +1,21 @@
|
||||
dbaclass=# show server_version;
|
||||
server_version
|
||||
----------------
|
||||
10.13
|
||||
(1 row)
|
||||
|
||||
dbaclass=# select version ();
|
||||
version
|
||||
--------------------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
PostgreSQL 10.13 on x86_64-apple-darwin, compiled by i686-apple-darwin11-llvm-gcc-4.2 (GCC) 4.2.1 (Based on Apple Inc. build 5658) (LLVM build 2336.11.00), 64-bit
|
||||
(1 row)
|
||||
|
||||
postgres$ cd /Library/PostgreSQL/10/bin
|
||||
|
||||
postgres$ ./postgres -V
|
||||
postgres (PostgreSQL) 10.13
|
||||
|
||||
-- PG_VERSION file is under data directory
|
||||
postgres$ cd /Library/PostgreSQL/10/data
|
||||
|
||||
postgres$ cat PG_VERSION
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Monitor query execution time
|
||||
|
||||
select substr(query,1,100) query,calls,min_time/1000 "min_time(in sec)" , max_time/1000 "max_time(in sec)", mean_time/1000 "avg_time(in sec)", rows from pg_stat_statements order by mean_time desc;
|
||||
|
||||
-------------------------------------------------------------------------------------------------------------------------------------------------------
|
||||
|
||||
NOTE:
|
||||
|
||||
If pg_stat_statements is not available in your database, then activate using below:
|
||||
-- Add below parameters in postgres.conf file and restart the postgres cluster
|
||||
|
||||
shared_preload_libraries = 'pg_stat_statements'
|
||||
pg_stat_statements.track = all
|
||||
|
||||
sudo service postgresql restart
|
||||
|
||||
-- Now create extension:
|
||||
|
||||
dbaclass=# create extension pg_stat_statements;
|
||||
@@ -0,0 +1,11 @@
|
||||
-- Uptime of server
|
||||
|
||||
postgres# SELECT now() - pg_postmaster_start_time() "uptime";
|
||||
uptime
|
||||
------------------------
|
||||
9 days 04:54:56.774981
|
||||
(1 row)
|
||||
|
||||
-- Server startup time:
|
||||
|
||||
postgres# SELECT pg_postmaster_start_time();
|
||||
@@ -0,0 +1 @@
|
||||
select pg_switch_wal();
|
||||
@@ -0,0 +1,34 @@
|
||||
-- Below commands can be used to find postgres db date/timestamp
|
||||
|
||||
postgres=# SELECT CURRENT_TIMESTAMP;
|
||||
current_timestamp
|
||||
----------------------------------
|
||||
2020-07-06 17:45:24.929293+05:30
|
||||
(1 row)
|
||||
|
||||
postgres=# select current_date;
|
||||
current_date
|
||||
--------------
|
||||
2020-07-06
|
||||
(1 row)
|
||||
|
||||
postgres=# select statement_timestamp() ;
|
||||
statement_timestamp
|
||||
----------------------------------
|
||||
2020-07-06 17:46:16.825492+05:30
|
||||
(1 row)
|
||||
|
||||
postgres=# select timeofday() ;
|
||||
timeofday
|
||||
-------------------------------------
|
||||
Mon Jul 06 17:46:23.861551 2020 IST
|
||||
(1 row)
|
||||
|
||||
postgres=# select localtime(0);
|
||||
localtime
|
||||
-----------
|
||||
17:52:38
|
||||
(1 row)
|
||||
|
||||
postgres=# select localtimestamp(0);
|
||||
localtimestamp
|
||||
@@ -0,0 +1,12 @@
|
||||
dbaclass=# show timezone
|
||||
TimeZone
|
||||
--------------
|
||||
Asia/Kolkata
|
||||
(1 row)
|
||||
|
||||
dbaclass=# SELECT current_setting('TIMEZONE');
|
||||
current_setting
|
||||
-----------------
|
||||
Asia/Kolkata
|
||||
|
||||
dbaclass=# select name,setting,short_desc,boot_val from pg_settings where name='TimeZone';
|
||||
@@ -0,0 +1 @@
|
||||
postgres# select * from pg_stat_archiver;
|
||||
@@ -0,0 +1,21 @@
|
||||
postgres=# create database DBATEST;
|
||||
CREATE DATABASE
|
||||
|
||||
postgres=# create database DBATEST with tablespace ts_postgres;
|
||||
CREATE DATABASE
|
||||
|
||||
postgres#CREATE DATABASE "DBATEST"
|
||||
WITH TABLESPACE ts_postgres
|
||||
OWNER "postgres"
|
||||
ENCODING 'UTF8'
|
||||
LC_COLLATE = 'en_US.UTF-8'
|
||||
LC_CTYPE = 'en_US.UTF-8'
|
||||
TEMPLATE template0;
|
||||
|
||||
|
||||
|
||||
-- View database information:
|
||||
|
||||
postgres=# \l
|
||||
|
||||
postgres# select * from pg_database;
|
||||
@@ -0,0 +1,49 @@
|
||||
These scripts should work on all versions of PostgreSQL up to 9.4
|
||||
Most have options to specify host and port.
|
||||
./<script> -h shows all options
|
||||
|
||||
cache_hit_ratio.sh -> Shows the cache hit ration for all databases
|
||||
cancel_all_queries.sh -> Terminates all queries in progress
|
||||
check_postgresql_log.sh -> greps the postgres log file for ERROR, WARNING and ref's to pg_hba. You will probably need to edit and adjust the directory path and suffix for your cluster.
|
||||
columns_of_type_x -> Shows all tables and columns with specific character type
|
||||
connect_count.sh -> Shows connection count for each database
|
||||
current_locks.sh -> Shows status for all current locks
|
||||
current_queries.sh -> Shows information on all current queries, will loop continuously unless -x option is specified. -s option specifies sleeptime, default is 5 secs. -i will ignore <IDLE> queries
|
||||
database_sizes.sh -> shows size of all databases
|
||||
filename_tables.sh -> shows physical directory, filename, owner & size for all tables in specified database
|
||||
|
||||
flip_database_connect.sh -> Flips state of datallowconn for database
|
||||
|
||||
get_trans_min_cnt.sh -> shows transaction count per minute for cluster
|
||||
groups_users.sh -> shows all groups & members in cluster and if group or user is a superuser
|
||||
index_bloat.sh -> shows index ration to table size for all indexes of all or specified table in database.
|
||||
index_comments.sh -> shows comments on all or specified index for database
|
||||
|
||||
logins_block.sh -> prevents all non-superusers from future logins. MUST be run as user postgres
|
||||
logins_allow.sh -> re-allows login access to all normal users. MUST be run as user postgres
|
||||
|
||||
pg_backup.sh -> generic backup script for postgres database. You will need to edit and adjust for your needs.
|
||||
pg_runtime.sh -> shows startup and continuous runtime for the cluster
|
||||
pg_stat_all_indexes.sh -> shows stats and status of all indexes or just for specified table
|
||||
pg_stat_all_tables.sh -> shows stats for all or specified table
|
||||
pg_vacuum_time.sh -> Shows total time to vacuum a database
|
||||
remove_ctrl.sh -> removes control characters from a script. Useful if script was created on a windows PC and then copied to Linux
|
||||
role_members.sh -> Shows roles and all members
|
||||
slony_status.sh -> shows status and lag time of slony cluster. Should be run on master node.
|
||||
table_column_stats.sh -> shows stats for all columns of specified table
|
||||
table_comments.sh -> shows comments for all or specified table
|
||||
table_dependents.sh -> shows foreign key constraints for all or specified table
|
||||
table_in_functions.sh -> shows all functions that reference specified table
|
||||
table_in_trogger.sh -> shows all triggerss that reference specified table
|
||||
tables_and_owners.sh -> shows owners and permits on all tables
|
||||
tables-filenames.sh -> shows physical directory, filename, owner & size for all tables in specified database
|
||||
table_sizes.sh -> shows tuple count and size for all or specified table
|
||||
tablespace_use.sh -> shows tablespace used by all or specified table/index
|
||||
tables_with_column_name.sh -> shows all tables that have a column name LIKE specified column name
|
||||
tables_with_FKS.sh -> shows all tables taht have a foreign key constraint
|
||||
tables_with_no_public_grants.sh -> shows tables that have no public grants (restricted table)
|
||||
triggers_and_constraints.sh -> shows all triggers and called functions for all or specified table
|
||||
user_table_access_detail.sh -> shows table access for specified user
|
||||
user_table_access.sh -> shows permit and position in permit for specified user
|
||||
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
# Shows cache hit ratio for all databases
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
DBNAME="postgres"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -h <host> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql $USER $HOST $PORT $DBNAME <<_EOF_
|
||||
SELECT pg_stat_database.datname,
|
||||
pg_stat_database.blks_read,
|
||||
pg_stat_database.blks_hit,
|
||||
round((pg_stat_database.blks_hit::double precision
|
||||
/ (pg_stat_database.blks_read
|
||||
+ pg_stat_database.blks_hit
|
||||
+1)::double precision * 100::double precision)::numeric, 2) AS cachehitratio
|
||||
FROM pg_stat_database
|
||||
WHERE pg_stat_database.datname !~ '^(template(0|1)|postgres)$'::text
|
||||
ORDER BY round((pg_stat_database.blks_hit::double precision
|
||||
/ (pg_stat_database.blks_read
|
||||
+ pg_stat_database.blks_hit
|
||||
+ 1)::double precision * 100::double precision)::numeric, 2) DESC;
|
||||
_EOF_
|
||||
@@ -0,0 +1,52 @@
|
||||
#!/bin/bash
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-h <host> -p <port>]"
|
||||
echo "-p will default to 5432"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "h:p:" OPT;
|
||||
do case "${OPT}" in
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $HOST $PORT -U postgres <<_QRYVER_
|
||||
SELECT substring(version() from 12 for 1);
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
cancel_pids()
|
||||
{
|
||||
psql -t $HOST $PORT -U postgres <<_QRYVER_
|
||||
--SELECT pg_cancel_backend($PID)
|
||||
SELECT pg_terminate_backend($PID)
|
||||
FROM pg_stat_activity WHERE pg_backend_pid() <> $PID);
|
||||
_QRYVER_
|
||||
|
||||
}
|
||||
|
||||
##### MAIN #####
|
||||
|
||||
getveri
|
||||
declare -i VER
|
||||
if [ "$VER" -lt 9 ]
|
||||
then
|
||||
PID="procpid"
|
||||
else
|
||||
PID="pid"
|
||||
fi
|
||||
cancel_pids
|
||||
|
||||
exit 0;
|
||||
|
||||
@@ -0,0 +1,35 @@
|
||||
#!/bin/bash
|
||||
|
||||
# if arguments > 0, set count option
|
||||
|
||||
if [ $# -eq 0 ]
|
||||
then
|
||||
cntflg='-c'
|
||||
else
|
||||
cntflg=''
|
||||
fi
|
||||
|
||||
set $(date '+%m%d')
|
||||
MMDD=$1
|
||||
|
||||
#set $(date)
|
||||
## if day number less than 10, prefix a '0'
|
||||
#if [ $3 -le 9 ]
|
||||
# then
|
||||
# DD=0${3}
|
||||
# else
|
||||
# DD=${3}
|
||||
#fi
|
||||
|
||||
set $(date +%m)
|
||||
MM=${1}
|
||||
logdir=/var/log/postgresql/
|
||||
|
||||
echo "Current error log is: ${logdir}postgresql.${MMDD}"
|
||||
echo
|
||||
echo -n "ERROR: "
|
||||
grep ${cntflg} -i ERROR ${logdir}postgresql.${MMDD}
|
||||
echo -n "WARNING: "
|
||||
grep ${cntflg} -i WARNING ${logdir}postgresql.${MMDD}
|
||||
echo -n "pg_hba: "
|
||||
grep ${cntflg} -i pg_hba ${logdir}postgresql.${MMDD}
|
||||
@@ -0,0 +1,77 @@
|
||||
#!/bin/bash
|
||||
# Shows tables and columns that are of type specified
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
COLTYPE="?"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -t <coltype> [-h <host> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:t:h:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) COLTYPE="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="postgres"
|
||||
fi
|
||||
|
||||
choose_qry ()
|
||||
{
|
||||
|
||||
if [ "$COLTYPE" = "?" ]
|
||||
then
|
||||
echo "Usage: $0 -d <dbname> -t <coltype> [-h <host> -U <user> -p <port>]"
|
||||
echo
|
||||
echo "Available Data Types"
|
||||
QRY="SELECT DISTINCT (c.data_type)
|
||||
FROM information_schema.columns c
|
||||
ORDER BY c.data_type;"
|
||||
else
|
||||
QRY="SELECT quote_ident(c.table_schema) as schema,
|
||||
quote_ident(c.table_name) as table,
|
||||
quote_ident(c.column_name) as column,
|
||||
c.data_type as type,
|
||||
case when c.data_type LIKE '%char%'
|
||||
then COALESCE(character_maximum_length::text, 'N/A')
|
||||
when c.data_type LIKE '%numeric%'
|
||||
then '(' || c.numeric_precision::text || ', ' || c.numeric_scale::text || ')'
|
||||
when c.data_type LIKE '%int%'
|
||||
then c.numeric_precision::text
|
||||
else COALESCE(character_maximum_length::text, 'N/A')
|
||||
end as size
|
||||
FROM information_schema.columns c
|
||||
WHERE c.table_schema NOT LIKE 'pg_%'
|
||||
AND c.table_schema NOT LIKE 'information%'
|
||||
AND c.table_name NOT LIKE 'sql_%'
|
||||
AND c.data_type LIKE '%$COLTYPE%'
|
||||
AND c.is_updatable = 'YES'
|
||||
ORDER BY 1, 2, 3;"
|
||||
fi
|
||||
|
||||
psql $HOST $USER $PORT $DBNAME <<_CODE_
|
||||
|
||||
${QRY}
|
||||
|
||||
_CODE_
|
||||
|
||||
}
|
||||
|
||||
choose_qry
|
||||
@@ -0,0 +1,131 @@
|
||||
#!/bin/bash
|
||||
|
||||
DBNAME="postgres"
|
||||
USER="-U postgres"
|
||||
PORT="-p 5432"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -h <host> -U <user> -p <port> u]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
set $(date '+%m%d')
|
||||
MMDD=$1
|
||||
|
||||
LOGDIR='/var/log/postgresql'
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
SELECT version();
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
#VER=$(cat ${PGDATA}/PG_VERSION)
|
||||
#use next line only if PGDATA has version in path
|
||||
#VER=$(echo $PGDATA | cut -d / -f 5)
|
||||
}
|
||||
|
||||
clear
|
||||
|
||||
getver
|
||||
echo "PostgreSQL Version is $VER"
|
||||
|
||||
connect_cnts() {
|
||||
DBLIST=$(psql -q -t $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT d.datname
|
||||
FROM pg_database d
|
||||
WHERE datallowconn = TRUE
|
||||
AND datistemplate = FALSE
|
||||
ORDER BY 1;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
|
||||
}
|
||||
|
||||
get_cnt() {
|
||||
|
||||
|
||||
CNT=$(psql -t $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
/*
|
||||
SELECT count(*)
|
||||
FROM pg_stat_activity
|
||||
WHERE datname = '$DB';
|
||||
*/
|
||||
SELECT usename,
|
||||
count(*)
|
||||
FROM pg_stat_activity
|
||||
WHERE datname = '$DB'
|
||||
GROUP BY usename;
|
||||
|
||||
|
||||
_QUERY_
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
all_count() {
|
||||
CNT=$(psql -t $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT count(*)
|
||||
FROM pg_stat_activity;
|
||||
|
||||
_QUERY_
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
###############################################################
|
||||
# MAIN #
|
||||
###############################################################
|
||||
|
||||
connect_cnts
|
||||
|
||||
for DB in $DBLIST;
|
||||
do
|
||||
echo -n "$DB "
|
||||
get_cnt
|
||||
# grep -E -c "$DB.*connection auth" #LOGDIR/postgresql.${MMDD}
|
||||
echo -e "\t$CNT"
|
||||
done
|
||||
|
||||
all_count
|
||||
echo
|
||||
echo -e "TOTAL \t\t$CNT"
|
||||
|
||||
#exit 0
|
||||
|
||||
#echo "$(hostname -f) CONNECTION COUNTS"
|
||||
#echo "Current log file is: $logdir/postgresql.$MMDD"
|
||||
#echo
|
||||
|
||||
#for DB in $DBLIST
|
||||
# do
|
||||
# echo -n "$DB: "
|
||||
# grep 'connection authorized' $LOGDIR/postgresql.${MMDD} | grep -c "$DB"
|
||||
#done
|
||||
#exit 0
|
||||
@@ -0,0 +1,109 @@
|
||||
#!/bin/bash
|
||||
|
||||
USER="-U postgres"
|
||||
PORT="-p 5432"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-h <host> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="postgres"
|
||||
fi
|
||||
|
||||
#Column names changed from procpid $ current_query to pid & query from 9.2 up
|
||||
gettstver()
|
||||
{
|
||||
TSTVER=$(psql -q -t $HOST $PORT $USER $DBNAME <<_QRY_
|
||||
SELECT CASE WHEN SUBSTRING(version() FROM 12 FOR 3) < '9.2'
|
||||
THEN 'procpid'
|
||||
ELSE 'pid'
|
||||
END;
|
||||
_QRY_
|
||||
|
||||
)
|
||||
|
||||
TSTVER=${TSTVER:1:7}
|
||||
|
||||
}
|
||||
|
||||
gettstver
|
||||
|
||||
if [ "${TSTVER}" = "procpid" ]
|
||||
then
|
||||
COL="procpid"
|
||||
QRY="current_query"
|
||||
else
|
||||
COL="pid"
|
||||
QRY="query"
|
||||
fi
|
||||
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $HOST $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
getver
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
current_locks() {
|
||||
LCKLIST=$(psql -q $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT l.database,
|
||||
d.datname,
|
||||
l.relation,
|
||||
n.nspname,
|
||||
c.relname,
|
||||
l.pid,
|
||||
a.usename,
|
||||
l.locktype,
|
||||
l.mode,
|
||||
l.granted,
|
||||
l.tuple
|
||||
FROM pg_locks l
|
||||
JOIN pg_class c ON (c.oid = l.relation)
|
||||
JOIN pg_database d ON (CASE WHEN l.database = 0 THEN d.oid = 0 ELSE d.oid = l.database END)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_stat_activity a ON (a.$COL = l.pid)
|
||||
ORDER BY l.database,
|
||||
l.relation,
|
||||
l.pid;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
current_locks
|
||||
echo "${LCKLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,127 @@
|
||||
#!/bin/bash
|
||||
|
||||
DBNAME="postgres"
|
||||
USER="-U postgres"
|
||||
PORT="-p 5432"
|
||||
SLEEPTIME=5
|
||||
CANCEL="F"
|
||||
NOIDLE=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-s <sleeptime> -d <dbname> -h <host> -U <user> -p <port> -x -i]"
|
||||
echo "-x will exit after 1 iteration"
|
||||
echo "-i will exclude <IDLE> queries"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:s:h:p:uU:xi" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
s) SLEEPTIME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
x) CANCEL="T"
|
||||
;;
|
||||
i) NOIDLE="AND current_query <> '<IDLE>'"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
getver() {
|
||||
VER=$(psql -q -t $HOST $PORT $USER $DBNAME <<_VER_
|
||||
|
||||
SELECT SUBSTR(version(), 1, 16);
|
||||
|
||||
_VER_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
#Column names changed from procpid $ current_query to pid & query from 9.2 up
|
||||
gettstver()
|
||||
{
|
||||
TSTVER=$(psql -q -t $HOST $PORT $USER $DBNAME <<_QRY_
|
||||
SELECT CASE WHEN SUBSTRING(version() FROM 12 FOR 3) < '9.2'
|
||||
THEN 'procpid'
|
||||
ELSE 'pid'
|
||||
END;
|
||||
_QRY_
|
||||
|
||||
)
|
||||
|
||||
TSTVER=${TSTVER:1:7}
|
||||
|
||||
}
|
||||
|
||||
gettstver
|
||||
|
||||
if [ "${TSTVER}" = "procpid" ]
|
||||
then
|
||||
COL="procpid"
|
||||
QRY="current_query"
|
||||
else
|
||||
COL="pid"
|
||||
QRY="query"
|
||||
fi
|
||||
|
||||
getver
|
||||
echo "$VER"
|
||||
sleep 2
|
||||
|
||||
current_qry() {
|
||||
QRYLIST=$(psql -q $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT datname,
|
||||
$COL as pid,
|
||||
-- procpid as pid,
|
||||
client_addr,
|
||||
usename as user,
|
||||
-- regexp_replace(current_query, ' (..)', ' ', 'g') as query,
|
||||
regexp_replace($QRY, ' (..)', ' ', 'g') as query,
|
||||
waiting,
|
||||
query_start,
|
||||
clock_timestamp() - query_start as duration
|
||||
FROM pg_stat_activity
|
||||
--WHERE procpid != pg_backend_pid()
|
||||
WHERE $COL != pg_backend_pid()
|
||||
$NOIDLE
|
||||
ORDER BY datname,
|
||||
query_start;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
#### MAIN ####
|
||||
|
||||
while [ 1 -ge 0 ]
|
||||
do
|
||||
clear
|
||||
current_qry
|
||||
echo "${QRYLIST}"
|
||||
if [ "$CANCEL" = "T" ]
|
||||
then
|
||||
exit 0
|
||||
fi
|
||||
echo "Sleeping $SLEEPTIME"
|
||||
sleep $SLEEPTIME
|
||||
done
|
||||
|
||||
@@ -0,0 +1,46 @@
|
||||
#!/bin/bash
|
||||
# Reports sizes for all or selected database
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
DBNAME="%"
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
psql $PORT $USER postgres <<_EOF_
|
||||
SELECT datname,
|
||||
rolname as owner,
|
||||
pg_size_pretty(pg_database_size(datname) )as size_pretty,
|
||||
pg_database_size(datname) as size,
|
||||
(SELECT pg_size_pretty (SUM( pg_database_size(datname))::bigint)
|
||||
FROM pg_database) AS total,
|
||||
((pg_database_size(datname) / (SELECT SUM( pg_database_size(datname))
|
||||
FROM pg_database) ) * 100)::numeric(6,3) AS pct
|
||||
FROM pg_database d
|
||||
JOIN pg_authid a ON a.oid = datdba
|
||||
WHERE datname LIKE '%$DBNAME%'
|
||||
ORDER BY datname;
|
||||
_EOF_
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND SIZES FOR ALL TABLES
|
||||
# SHOULD BE RUN AS a SUPERUSER
|
||||
|
||||
HOST=""
|
||||
PORT=""
|
||||
USER=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-h <host> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
psql $HOST $PORT $USER $DBNAME <<_CODE_
|
||||
|
||||
SELECT d.oid as directory,
|
||||
c.relfilenode as filename,
|
||||
n.nspname as schema,
|
||||
c.relname as table,
|
||||
a.rolname as owner,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(c.relname))) as size
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner ),
|
||||
pg_database d
|
||||
WHERE d.datname = current_database()
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind = 'r'
|
||||
ORDER BY relfilenode;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,63 @@
|
||||
#!/bin/bash
|
||||
# Disables or Enables connections to specified database
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-p <port> -U <user>]"
|
||||
echo Database name to disable/enable is required
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
if [ $DBNAME == "postgres" ]
|
||||
then
|
||||
echo Disable of postgres is prohibited
|
||||
exit
|
||||
fi
|
||||
|
||||
echo changing $DBNAME
|
||||
|
||||
psql $PORT $USER -v ON_ERROR_STOP=on postgres <<_EOF_
|
||||
SELECT datname, datallowconn
|
||||
FROM pg_database
|
||||
WHERE datname = '$DBNAME' ;
|
||||
|
||||
UPDATE pg_database
|
||||
SET datallowconn = CASE
|
||||
WHEN datallowconn = TRUE THEN
|
||||
FALSE
|
||||
ELSE
|
||||
TRUE
|
||||
END
|
||||
WHERE datname = '$DBNAME' ;
|
||||
|
||||
SELECT datname, datallowconn
|
||||
FROM pg_database
|
||||
WHERE datname = '$DBNAME' ;
|
||||
|
||||
\echo Connect status for $DBNAME has been reversed
|
||||
|
||||
_EOF_
|
||||
|
||||
@@ -0,0 +1,48 @@
|
||||
#!/bin/bash
|
||||
# Counts transaction per minute
|
||||
# for postgresql server
|
||||
# Note: transactions are actual / not potential
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
DBNAME="postgres"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME="$OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
get_procnum() {
|
||||
PROCNUM=$(psql $PORT -q -t $USER $DBNAME <<_QUERY_
|
||||
SELECT SUM(xact_commit + xact_rollback)
|
||||
FROM pg_stat_database;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
}
|
||||
|
||||
get_procnum
|
||||
echo "$PROCNUM"
|
||||
FIRST_CNT=$PROCNUM
|
||||
sleep 60
|
||||
get_procnum
|
||||
LAST_CNT=$PROCNUM
|
||||
echo "$PROCNUM"
|
||||
(( TOT_QRY = $LAST_CNT - $FIRST_CNT ))
|
||||
echo "Total queries = $TOT_QRY"
|
||||
|
||||
@@ -0,0 +1,19 @@
|
||||
#!/bin/bash
|
||||
|
||||
psql -U postgres postgres <<_EOF_
|
||||
SELECT g.rolname as group,
|
||||
u.rolname as user,
|
||||
r.admin_option,
|
||||
CASE WHEN g.rolsuper = TRUE
|
||||
THEN 'SUPERUSER'
|
||||
ELSE 'f'
|
||||
END as g_super,
|
||||
CASE WHEN u.rolsuper = TRUE
|
||||
THEN 'SUPERUSER'
|
||||
ELSE 'f'
|
||||
END as u_super
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
ORDER BY 1;
|
||||
_EOF_
|
||||
@@ -0,0 +1,56 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND SIZES FOR ALL TABLES
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:t:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
|
||||
SELECT quote_ident(N.nspname),
|
||||
quote_ident(T.relname) AS "table",
|
||||
quote_ident(C.relname) AS idx_name,
|
||||
round(100 * pg_relation_size(indexrelid) / pg_relation_size(indrelid) ) / 100 AS "index_ratio %",
|
||||
pg_size_pretty(pg_relation_size(indexrelid) ) AS index_size,
|
||||
pg_size_pretty(pg_relation_size(indrelid) ) AS table_size_size
|
||||
FROM pg_index I
|
||||
LEFT JOIN pg_class C ON (c.oid = I.indexrelid)
|
||||
LEFT JOIN pg_class T ON (T.oid = I.indrelid)
|
||||
LEFT JOIN pg_namespace N ON (N.oid = C.relnamespace)
|
||||
WHERE nspname NOT IN ('pg_catalog', 'information_schema', 'pg_toast')
|
||||
AND c.relkind = 'i'
|
||||
AND pg_relation_size(indrelid) > 0
|
||||
AND C.relname LIKE '%$TBL%'
|
||||
ORDER BY 4 DESC, 2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,75 @@
|
||||
#!/bin/bash
|
||||
|
||||
#Shows commnets on all or selected index
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-i <index> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:i:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
i) IDX=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $PORT -t $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
QRY="SELECT quote_ident(n.nspname) as schema,
|
||||
quote_ident(c.relname),
|
||||
d.description as comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_description d ON (d.objoid = c.oid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relname LIKE '%$IDX%'
|
||||
AND relkind = 'i'
|
||||
AND d.description IS NOT NULL
|
||||
ORDER BY 2;"
|
||||
|
||||
current_stats() {
|
||||
STATLIST=$(psql $PORT -q $USER $DBNAME <<_QUERY_
|
||||
|
||||
${QRY}
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
current_stats
|
||||
echo "${STATLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,78 @@
|
||||
#!/bin/bash
|
||||
|
||||
DBNAME="postgres"
|
||||
USER="-U postgres"
|
||||
PORT="-p 5432"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -h <host> -U <user> -p <port> u]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
allow_logins()
|
||||
{
|
||||
|
||||
CNT=$(psql $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
UPDATE pg_authid au
|
||||
SET rolcanlogin = TRUE
|
||||
FROM pg_lockout lo
|
||||
WHERE au.rolname = lo.rolname;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
current_user()
|
||||
{
|
||||
|
||||
CUSER=$(psql -t $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT TRIM(BOTH FROM current_user);
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
|
||||
###############################################################
|
||||
# MAIN #
|
||||
###############################################################
|
||||
|
||||
current_user
|
||||
|
||||
if [ $CUSER != "postgres" ]
|
||||
then
|
||||
echo "You must be postgres to execute this script."
|
||||
echo "Aborting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
allow_logins
|
||||
|
||||
echo "Users allowed to login = $CNT"
|
||||
|
||||
exit 0
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
#!/bin/bash
|
||||
|
||||
DBNAME="postgres"
|
||||
USER="-U postgres"
|
||||
PORT="-p 5432"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -U <user> -p <port> u]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
lockout()
|
||||
{
|
||||
|
||||
CNT=$(psql $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
DROP TABLE IF EXISTS pg_lockout;
|
||||
CREATE TABLE pg_lockout
|
||||
AS SELECT rolname,
|
||||
rolsuper,
|
||||
rolcanlogin
|
||||
FROM pg_authid
|
||||
WHERE rolsuper IS FALSE;
|
||||
|
||||
ALTER TABLE pg_lockout
|
||||
ADD CONSTRAINT pg_lockout_pkey PRIMARY KEY (rolname);
|
||||
|
||||
UPDATE pg_authid au
|
||||
SET rolcanlogin = FALSE
|
||||
FROM pg_lockout lo
|
||||
WHERE lo.rolcanlogin = TRUE
|
||||
AND au.rolname = lo.rolname;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
current_user()
|
||||
{
|
||||
|
||||
CUSER=$(psql -t $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT TRIM(BOTH FROM current_user);
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
|
||||
###############################################################
|
||||
# MAIN #
|
||||
###############################################################
|
||||
|
||||
current_user
|
||||
|
||||
echo "current user is $CUSER"
|
||||
|
||||
if [ $CUSER != "postgres" ]
|
||||
then
|
||||
echo "You must be postgres to execute this script."
|
||||
echo "Aborting."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
|
||||
lockout
|
||||
|
||||
echo "Users locked out = $CNT"
|
||||
|
||||
exit 0
|
||||
|
||||
@@ -0,0 +1,3 @@
|
||||
BACKFILE=pgbackup.`date +%C%y%m%d%H%M`
|
||||
pg_dump -f /home/pgsql/backup/$BACKFILE -F c postgres
|
||||
|
||||
@@ -0,0 +1,54 @@
|
||||
#!/bin/bash
|
||||
# Show start and runtime for postgresql server
|
||||
|
||||
DBNAME="postgres"
|
||||
PORT=""
|
||||
USER=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME="$OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
getver
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
show_runtime() {
|
||||
RUNTIME=$(psql -q $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT pg_postmaster_start_time() as pg_start,
|
||||
current_timestamp - pg_postmaster_start_time() as runtime;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
show_runtime
|
||||
echo "${RUNTIME}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,140 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Shows statistics for all indexes on all selected table
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL="%"
|
||||
COLS=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-U <user> -p <port> -t <table> -s ]"
|
||||
echo "dbname REQUIRED"
|
||||
echo "-s will show columns used in index"
|
||||
exit 1
|
||||
}
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
set_qry_cols() {
|
||||
|
||||
QRY="SELECT n.nspname as schema,
|
||||
i.relname as table,
|
||||
i.indexrelname as index,
|
||||
i.idx_scan,
|
||||
i.idx_tup_read,
|
||||
i.idx_tup_fetch,
|
||||
CASE WHEN idx.indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN idx.indisunique
|
||||
THEN 'uidx'
|
||||
ELSE 'idx'
|
||||
END AS type,
|
||||
pg_get_indexdef(idx.indexrelid, 1, FALSE) as idxcol1,
|
||||
pg_get_indexdef(idx.indexrelid, 2, FALSE) as idxcol2,
|
||||
pg_get_indexdef(idx.indexrelid, 3, FALSE) as idxcol3,
|
||||
pg_get_indexdef(idx.indexrelid, 4, FALSE) as idxcol4,
|
||||
CASE WHEN idx.indisvalid
|
||||
THEN 'valid'
|
||||
ELSE 'INVALID'
|
||||
END as status,
|
||||
pg_relation_size( quote_ident(n.nspname) || '.' || quote_ident(i.relname) ) as size_in_bytes,
|
||||
pg_size_pretty(pg_relation_size(quote_ident(n.nspname)|| '.' || quote_ident(i.relname) )) as size
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE i.relname LIKE '%$TBL%'
|
||||
AND n.nspname NOT LIKE 'pg_%'
|
||||
ORDER BY 1, 2, 3;"
|
||||
|
||||
}
|
||||
|
||||
set_qry_nocols() {
|
||||
|
||||
QRY="SELECT n.nspname as schema,
|
||||
i.relname as table,
|
||||
i.indexrelname as index,
|
||||
i.idx_scan,
|
||||
i.idx_tup_read,
|
||||
i.idx_tup_fetch,
|
||||
CASE WHEN idx.indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN idx.indisunique
|
||||
THEN 'uidx'
|
||||
ELSE 'idx'
|
||||
END AS type,
|
||||
CASE WHEN idx.indisvalid
|
||||
THEN 'valid'
|
||||
ELSE 'INVALID'
|
||||
END as status,
|
||||
pg_relation_size(quote_ident(n.nspname) || '.' || quote_ident(i.relname) ) as size_in_bytes
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE i.relname LIKE '%$TBL%'
|
||||
AND n.nspname NOT LIKE 'pg_%'
|
||||
ORDER BY 1, 2, 3;"
|
||||
|
||||
}
|
||||
|
||||
current_stats() {
|
||||
STATLIST=$(psql -q -U postgres $DBNAME <<_QUERY_
|
||||
|
||||
$QRY
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
###########################################################
|
||||
# MAIN #
|
||||
###########################################################
|
||||
|
||||
while getopts "d:p:st:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
s) COLS="TRUE"
|
||||
;;
|
||||
t) TBL="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
if [ "$COLS" = "TRUE" ]
|
||||
then
|
||||
set_qry_cols
|
||||
else
|
||||
set_qry_nocols
|
||||
fi
|
||||
|
||||
current_stats
|
||||
echo "${STATLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,100 @@
|
||||
#!/bin/bash
|
||||
# Reports statistics for all tables
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:t:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
if [ "${VER:11:3}" \< "8.3" ]
|
||||
then
|
||||
QRY="SELECT n.nspname as schema,
|
||||
s.relname as table,
|
||||
c.reltuples::bigint,
|
||||
n_tup_ins,
|
||||
n_tup_upd,
|
||||
n_tup_del,
|
||||
date_trunc('second', last_vacuum) as last_vacuum,
|
||||
date_trunc('second', last_autovacuum) as last_autovacuum,
|
||||
date_trunc('second', last_analyze) as last_analyze,
|
||||
date_trunc('second', last_autoanalyze) as last_autoanalyze
|
||||
FROM pg_stat_all_tables s
|
||||
JOIN pg_class c ON c.oid = s.relid
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE s.relname NOT LIKE 'pg_%'
|
||||
AND s.relname NOT LIKE 'sql_%'
|
||||
AND s.relname LIKE '%$TBL%'
|
||||
ORDER by 1, 2;"
|
||||
else
|
||||
QRY="SELECT n.nspname as schema,
|
||||
s.relname as table,
|
||||
c.reltuples::bigint,
|
||||
n_live_tup,
|
||||
n_tup_ins,
|
||||
n_tup_upd,
|
||||
n_tup_del,
|
||||
date_trunc('second', last_vacuum) as last_vacuum,
|
||||
date_trunc('second', last_autovacuum) as last_autovacuum,
|
||||
date_trunc('second', last_analyze) as last_analyze,
|
||||
date_trunc('second', last_autoanalyze) as last_autoanalyze
|
||||
FROM pg_stat_all_tables s
|
||||
JOIN pg_class c ON c.oid = s.relid
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE s.relname NOT LIKE 'pg_%'
|
||||
AND s.relname NOT LIKE 'sql_%'
|
||||
AND s.relname LIKE '%$TBL%'
|
||||
ORDER by 1, 2;"
|
||||
fi
|
||||
|
||||
current_stats() {
|
||||
STATLIST=$(psql -q $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
${QRY}
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
current_stats
|
||||
echo "${STATLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,66 @@
|
||||
#!/bin/bash
|
||||
# Shows vacuum time for all or selected table.
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:t:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
t) TBL="$OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $PORT -t $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
vacuum_time() {
|
||||
TIMEINFO=$(psql -q $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT max(last_vacuum) - min(last_vacuum) as vacuum_time
|
||||
FROM pg_stat_all_tables s
|
||||
WHERE s.relname NOT LIKE 'pg_%'
|
||||
AND s.relname NOT LIKE 'sql_%'
|
||||
AND s.relname LIKE '%$TBL%'
|
||||
ORDER by 1;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
vacuum_time
|
||||
echo "${TIMEINFO}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,24 @@
|
||||
#!/bin/bash
|
||||
|
||||
#!/bin/bash
|
||||
|
||||
OLDFILE=$1
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 oldfile"
|
||||
exit 1
|
||||
}
|
||||
|
||||
if [ -z "$OLDFILE" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
OLDFILE=$1
|
||||
NEWFILE=x$1
|
||||
|
||||
col -bp < $OLDFILE >> $NEWFILE
|
||||
chmod +x $NEWFILE
|
||||
rm $OLDFILE
|
||||
mv $NEWFILE $OLDFILE
|
||||
@@ -0,0 +1,64 @@
|
||||
#!/bin/bash
|
||||
#Shows roles and members
|
||||
|
||||
PORT=""
|
||||
USER="-U postgres"
|
||||
DBNAME="postgres"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-d <dbname> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
getver
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
show_members() {
|
||||
MEMBERS=$(psql -q $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT a.rolname as role,
|
||||
u.rolname as member,
|
||||
CASE
|
||||
WHEN m.admin_option THEN
|
||||
'YES'
|
||||
ELSE
|
||||
'no'
|
||||
END as admin
|
||||
FROM pg_auth_members m
|
||||
JOIN pg_authid a ON (a.oid = m.roleid)
|
||||
JOIN pg_authid u ON (u.oid = m.member)
|
||||
ORDER BY 1, 2
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
show_members
|
||||
echo "${MEMBERS}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 [-p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "p:u" OPT;
|
||||
do case "${OPT}" in
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $PORT -t -U postgres postgres <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
slony_status() {
|
||||
STATUS=$(psql $PORT -q <<_QUERY_
|
||||
|
||||
BEGIN;
|
||||
|
||||
SELECT st_origin as org,
|
||||
st_last_event as last_event,
|
||||
st_lag_num_events as lag_events,
|
||||
st_lag_time
|
||||
FROM _slony.sl_status;
|
||||
|
||||
COMMIT;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
slony_status
|
||||
echo "${STATUS}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,83 @@
|
||||
#!/bin/bash
|
||||
# Reports statistics for all tables
|
||||
|
||||
HOST=""
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -t <table> [-h <host> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:h:p:t:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
h) HOST="-h $OPTARG"
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="playmaker"
|
||||
fi
|
||||
|
||||
if [ "$TBL" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $HOST $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
getver
|
||||
|
||||
current_stats() {
|
||||
STATLIST=$(psql -q $HOST $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT schemaname,
|
||||
tablename,
|
||||
attname As colname,
|
||||
n_distinct,
|
||||
array_to_string(most_common_vals, E'\n') AS common_vals,
|
||||
array_to_string(most_common_freqs, E'\n') As dist_freq
|
||||
FROM pg_stats
|
||||
WHERE tablename = '$TBL'
|
||||
ORDER BY schemaname,
|
||||
tablename,
|
||||
attname;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
##### MAIN #####
|
||||
echo "HOST=$HOST"
|
||||
echo "PORT=$PORT"
|
||||
echo "USER=$USER"
|
||||
echo "TBL=$TBL"
|
||||
echo "DBNAME=$DBNAME"
|
||||
|
||||
current_stats
|
||||
echo "${STATLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,101 @@
|
||||
#!/bin/bash
|
||||
# Shows comments for all or specified table
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:t:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $PORT -t $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
QRY="SELECT DISTINCT ON (c.relname)
|
||||
n.nspname as schema,
|
||||
c.relname,
|
||||
a.rolname as owner,
|
||||
0 as col_seq,
|
||||
'' as column,
|
||||
d.description as comment
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
LEFT JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = 0)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relname LIKE '%$TBL%'
|
||||
AND relkind = 'r'
|
||||
-- AND d.description IS NOT NULL
|
||||
UNION
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
'' as owner,
|
||||
col.attnum as col_seq,
|
||||
col.attname as column,
|
||||
d.description
|
||||
FROM pg_class c
|
||||
JOIN pg_attribute col ON (col.attrelid = c.oid)
|
||||
LEFT JOIN pg_description d ON (d.objoid = col.attrelid AND d.objsubid = col.attnum)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE n.nspname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relname LIKE '%$TBL%'
|
||||
AND relkind = 'r'
|
||||
-- AND d.description IS NOT NULL
|
||||
AND col.attnum >= 0
|
||||
ORDER BY 1, 2, 4;"
|
||||
|
||||
current_stats() {
|
||||
STATLIST=$(psql $USER $PORT -q $DBNAME <<_QUERY_
|
||||
|
||||
${QRY}
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
current_stats
|
||||
echo "${STATLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,70 @@
|
||||
#!/bin/bash
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:t:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql $PORT -t $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
get_dependents() {
|
||||
DEPLIST=$(psql $PORT -q $USER $DBNAME <<_QUERY_
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
pa.relname as parent,
|
||||
ch.relname as dependent,
|
||||
cn.conname as constraint_name
|
||||
FROM pg_constraint cn
|
||||
JOIN pg_class pa ON (pa.oid = cn.confrelid)
|
||||
JOIN pg_class ch ON (ch.oid = cn.conrelid)
|
||||
JOIN pg_namespace n ON (n.oid = pa.relnamespace)
|
||||
WHERE pa.relname LIKE '%$TBL%'
|
||||
AND contype = 'f'
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
get_dependents
|
||||
echo "${DEPLIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Shows functions that specified table occurs in
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -t <table> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:t:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="enf"
|
||||
fi
|
||||
|
||||
if [ "$TBL" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "$TBL"
|
||||
|
||||
psql $USER $PORT $DBNAME <<_CODE_
|
||||
SELECT p.proname,
|
||||
CASE WHEN p.proretset = TRUE
|
||||
THEN 'SET OF '
|
||||
ELSE ''
|
||||
END ||
|
||||
UPPER( (SELECT typname
|
||||
FROM pg_type
|
||||
WHERE oid = p.prorettype) ) AS return_type,
|
||||
proargnames as parameters
|
||||
FROM pg_proc p
|
||||
WHERE prosrc LIKE '%$TBL%'
|
||||
ORDER BY p.proname;
|
||||
|
||||
_CODE_
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,53 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Shows trigger functions that specified table occurs in
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -t <table> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:t:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="enf"
|
||||
fi
|
||||
|
||||
if [ "$TBL" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "$TBL"
|
||||
|
||||
psql $USER $PORT $DBNAME <<_CODE_
|
||||
SELECT c.relname,
|
||||
t.tgname,
|
||||
p.proname
|
||||
FROM pg_proc p
|
||||
JOIN pg_trigger t ON (t.tgfoid = p.oid)
|
||||
JOIN pg_class c ON (c.oid = t.tgrelid)
|
||||
WHERE prosrc LIKE ('%$TBL%')
|
||||
ORDER BY c.relname;
|
||||
_CODE_
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,69 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND SIZES FOR ALL TABLES
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:t:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
a.rolname as owner,
|
||||
d.oid as directory,
|
||||
c.relfilenode as filename,
|
||||
c.reltuples::integer,
|
||||
pg_size_pretty(pg_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) )) as size,
|
||||
pg_size_pretty(pg_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) )
|
||||
+ pg_relation_size( i.indexrelid ) ) as total_size,
|
||||
pg_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) ) as size_bytes,
|
||||
pg_total_relation_size( quote_ident( n.nspname ) || '.' || quote_ident( c.relname ) ) as total_size_bytes,
|
||||
CASE WHEN c.reltablespace = 0
|
||||
THEN 'pg_default'
|
||||
ELSE (SELECT t.spcname
|
||||
FROM pg_tablespace t WHERE (t.oid = c.reltablespace) )
|
||||
END as tablespace
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index i ON (i.indrelid = c.oid )
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner ),
|
||||
pg_database d
|
||||
WHERE d.datname = current_database()
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relname LIKE '%$TBL%'
|
||||
AND relkind = 'r'
|
||||
ORDER BY 9 DESC, 1, 2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND OWNERS FOR ALL TABLES
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
PORT=""
|
||||
USER="-U postgres"
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
a.rolname as owner,
|
||||
c.relacl as permits
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON ( n.oid = c.relnamespace )
|
||||
JOIN pg_authid a ON ( a.OID = c.relowner )
|
||||
WHERE relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind = 'r'
|
||||
-- AND position('useradmin' in ARRAY_TO_STRING(c.relacl, '') )> 0
|
||||
ORDER BY n.nspname,
|
||||
c.relname;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,55 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND SIZES FOR ALL TABLES
|
||||
# SHOULD BE RUN AS postgres user
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-p <port> -U <user>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:U:u" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
c.relname as table,
|
||||
-- c.relkind,
|
||||
a.rolname as owner,
|
||||
d.oid as directory,
|
||||
c.relfilenode as filename,
|
||||
pg_size_pretty(pg_relation_size( quote_ident(n.nspname ) || '.' || quote_ident( c.relname ) )) as size
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner ),
|
||||
pg_database d
|
||||
WHERE d.datname = current_database()
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND relkind = 'r'
|
||||
-- AND relkind IN ('i', 'r')
|
||||
ORDER BY 1, 2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,58 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS FOREIGN KEY COUNT FOR EACH TABLE
|
||||
# THEN SHOWS FOREIGN KEYS FOR EACH TABLE
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
count(c.conname)
|
||||
FROM pg_class t
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
JOIN pg_namespace n ON (n.oid = t.relnamespace)
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
GROUP BY 1, 2
|
||||
ORDER BY 1, 3;
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
c.conname as fk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
JOIN pg_namespace n ON (n.oid = t.relnamespace)
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,65 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS ALL TABLES WITH A COLUMN NAME LIKE <ColumnName>
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -c <colname> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:c:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
c) COLNAME="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
echo "col=$COLNAME"
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="enf"
|
||||
fi
|
||||
|
||||
if [ "$COLNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql -a $USER $PORT $DBNAME <<_CODE_
|
||||
|
||||
SELECt c.table_schema as schema,
|
||||
c.table_name as table,
|
||||
c.column_name as column,
|
||||
c.data_type as type,
|
||||
case when c.data_type LIKE '%char%'
|
||||
then COALESCE(character_maximum_length::text, 'N/A')
|
||||
when c.data_type LIKE '%numeric%'
|
||||
then '(' || c.numeric_precision::text || ', ' || c.numeric_scale::text || ')'
|
||||
when c.data_type LIKE '%int%'
|
||||
then c.numeric_precision::text
|
||||
else COALESCE(character_maximum_length::text, 'N/A')
|
||||
end as size
|
||||
FROM information_schema.columns c
|
||||
WHERE c.table_schema NOT LIKE 'pg_%'
|
||||
AND c.table_schema NOT LIKE 'information%'
|
||||
AND c.table_name NOT LIKE 'sql_%'
|
||||
AND c.column_name LIKE '%$COLNAME%'
|
||||
AND c.is_updatable = 'YES'
|
||||
ORDER BY c.table_name, c.column_name;
|
||||
|
||||
_CODE_
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,49 @@
|
||||
#!/bin/bash
|
||||
|
||||
# SHOWS TABLES AND OWNERS FOR ALL TABLES
|
||||
# That have no public grants
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
USER="-U postgres"
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
psql $PORT $USER $DBNAME <<_CODE_
|
||||
SELECT n.nspname as schema,
|
||||
c.relname,
|
||||
a.rolname as owner,
|
||||
array_to_string(ARRAY[c.relacl], '|') as permits
|
||||
FROM pg_class c
|
||||
JOIN pg_authid a ON (a.OID = c.relowner)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE relkind = 'r'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'information%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND position('|=' in array_to_string(ARRAY[relacl], '|')) = 0
|
||||
ORDER BY 1, 2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,103 @@
|
||||
#!/bin/bash
|
||||
# Shows tablespace use for all or selected table
|
||||
# that are not in default tablespace
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL="%"
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-t <table> -U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:t:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
t) TBL=$OPTARG
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $USER $PORT $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
getver
|
||||
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
gettstver()
|
||||
{
|
||||
TSTVER=$(psql -q -t $HOST $PORT $USER $DBNAME <<_QRY_
|
||||
SELECT CASE WHEN SUBSTRING(version() FROM 12 FOR 3) < '9.2'
|
||||
THEN 'oid'
|
||||
ELSE 'func'
|
||||
END;
|
||||
_QRY_
|
||||
|
||||
)
|
||||
|
||||
TSTVER=${TSTVER:1:4}
|
||||
|
||||
}
|
||||
|
||||
gettstver
|
||||
|
||||
if [ "${TSTVER}" = "oid" ]
|
||||
then
|
||||
TSL="t.spclocation"
|
||||
else
|
||||
TSL="pg_tablespace_location(t.oid)"
|
||||
fi
|
||||
|
||||
tspace_use() {
|
||||
TSPACELIST=$(psql -q $USER $PORT $DBNAME <<_QUERY_
|
||||
|
||||
SELECT CASE WHEN t.spcname IS NULL
|
||||
THEN 'pg_default'
|
||||
ELSE t.spcname
|
||||
END as tablespace,
|
||||
$TSL as location,
|
||||
n.nspname as schema,
|
||||
c.relname as relation,
|
||||
a.rolname as owner,
|
||||
c.relkind as type
|
||||
FROM pg_class c
|
||||
LEFT JOIN pg_tablespace t ON (t.oid = c.reltablespace)
|
||||
JOIN pg_authid a ON ( a.oid = c.relowner )
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE relname LIKE '%$TBL%'
|
||||
AND relkind IN ('i', 'r')
|
||||
AND c.relname NOT LIKE 'pg_%'
|
||||
AND c.relname NOT LIKE 'sql_%'
|
||||
ORDER BY 1, 2;
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
tspace_use
|
||||
echo "${TSPACELIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,125 @@
|
||||
#!/bin/bash
|
||||
|
||||
PORT=""
|
||||
USER=""
|
||||
TBL="%"
|
||||
CONSTR=""
|
||||
|
||||
# Shows trigger function on all or selected trigger
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> [-U <user> -p <port> -t <trigger> -s ]"
|
||||
echo "dbname REQUIRED"
|
||||
echo "-s will show constraints"
|
||||
exit 1
|
||||
}
|
||||
|
||||
getver()
|
||||
{
|
||||
VER=$(psql -t $PORT $USER $DBNAME <<_QRYVER_
|
||||
SELECT version();
|
||||
_QRYVER_
|
||||
)
|
||||
}
|
||||
|
||||
set_qry_noconstraints() {
|
||||
|
||||
QRY="SELECT c.relname AS table,
|
||||
t.tgname,
|
||||
p.proname AS function_called,
|
||||
-- t.tgisconstraint AS is_constraint,
|
||||
cn.conname AS constraint_nm,
|
||||
CASE WHEN t.tgconstrrelid > 0
|
||||
THEN (SELECT relname
|
||||
FROM pg_class
|
||||
WHERE oid = t.tgconstrrelid)
|
||||
ELSE ''
|
||||
END AS constr_tbl
|
||||
FROM pg_trigger t
|
||||
INNER JOIN pg_proc p ON ( p.oid = t.tgfoid)
|
||||
INNER JOIN pg_class c ON ( c.oid = t.tgrelid)
|
||||
LEFT JOIN pg_constraint cn ON ( cn.oid = t.tgconstraint )
|
||||
WHERE tgname NOT LIKE 'pg_%'
|
||||
AND tgname NOT LIKE 'RI_%' -- < comment out to see constraints
|
||||
AND tgname LIKE '%$TRG%'
|
||||
ORDER BY 1;"
|
||||
|
||||
}
|
||||
|
||||
set_qry_constraints() {
|
||||
|
||||
QRY="SELECT c.relname AS table,
|
||||
t.tgname,
|
||||
p.proname AS function_called,
|
||||
-- t.tgisconstraint AS is_constraint,
|
||||
cn.conname AS constraint_nm,
|
||||
CASE WHEN t.tgconstrrelid > 0
|
||||
THEN (SELECT relname
|
||||
FROM pg_class
|
||||
WHERE oid = t.tgconstrrelid)
|
||||
ELSE ''
|
||||
END AS constr_tbl
|
||||
FROM pg_trigger t
|
||||
INNER JOIN pg_proc p ON ( p.oid = t.tgfoid)
|
||||
INNER JOIN pg_class c ON ( c.oid = t.tgrelid)
|
||||
LEFT JOIN pg_constraint cn ON ( cn.oid = t.tgconstraint )
|
||||
WHERE tgname NOT LIKE 'pg_%'
|
||||
AND tgname LIKE '%$TRG%'
|
||||
ORDER BY 1;"
|
||||
|
||||
}
|
||||
|
||||
|
||||
list_triggers() {
|
||||
LIST=$(psql -q $PORT $USER $DBNAME <<_QUERY_
|
||||
|
||||
$QRY
|
||||
|
||||
_QUERY_
|
||||
|
||||
)
|
||||
|
||||
}
|
||||
|
||||
###########################################################
|
||||
# MAIN #
|
||||
###########################################################
|
||||
|
||||
while getopts "d:p:st:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
s) CONSTR="TRUE"
|
||||
;;
|
||||
t) TRG="$OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
usage
|
||||
fi
|
||||
|
||||
getver
|
||||
VER=$(echo $VER | cut -c -16)
|
||||
echo "Version is $VER"
|
||||
|
||||
if [ "$CONSTR" = "TRUE" ]
|
||||
then
|
||||
set_qry_constraints
|
||||
else
|
||||
set_qry_noconstraints
|
||||
fi
|
||||
|
||||
list_triggers
|
||||
echo "${LIST}"
|
||||
|
||||
exit 0
|
||||
@@ -0,0 +1,59 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Shows Tables access for user specified
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -a <user2check> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:a:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
a) CKUSER=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="enf"
|
||||
fi
|
||||
|
||||
if [ "$CKUSER" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql $USER $PORT $DBNAME <<_CODE_
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
array_to_string(ARRAY[c.relacl], '|') as permits,
|
||||
position('useradmin' in array_to_string(ARRAY[relacl], '|'))
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname not like 'pg_%'
|
||||
AND n.nspname not like 'inform_%'
|
||||
AND relkind = 'r'
|
||||
AND (position('$CKUSER' in array_to_string(ARRAY[relacl], '|')) > 0
|
||||
OR position( (SELECT g.rolname
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
AND u.rolname LIKE '$CKUSER%') in array_to_string(ARRAY[relacl], '|')) > 0 )
|
||||
ORDER BY 1,2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,76 @@
|
||||
#!/bin/bash
|
||||
|
||||
# Shows table access detail for user specified
|
||||
# SHOULD BE RUN AS SUPERUSER
|
||||
|
||||
USER=""
|
||||
PORT=""
|
||||
|
||||
usage() {
|
||||
echo "Usage: $0 -d <dbname> -a <user2check> [-U <user> -p <port>]"
|
||||
exit 1
|
||||
}
|
||||
|
||||
while getopts "d:a:p:uU:" OPT;
|
||||
do case "${OPT}" in
|
||||
d) DBNAME=$OPTARG
|
||||
;;
|
||||
a) CKUSER=$OPTARG
|
||||
;;
|
||||
p) PORT="-p $OPTARG"
|
||||
;;
|
||||
U) USER="-U $OPTARG"
|
||||
;;
|
||||
u) usage
|
||||
;;
|
||||
[?]) usage
|
||||
esac;
|
||||
done
|
||||
|
||||
if [ "$DBNAME" = "" ]
|
||||
then
|
||||
DBNAME="enf"
|
||||
fi
|
||||
|
||||
if [ "$CKUSER" = "" ]
|
||||
then
|
||||
usage
|
||||
exit 1
|
||||
fi
|
||||
|
||||
psql $USER $PORT $DBNAME <<_CODE_
|
||||
SELECT n.nspname,
|
||||
c.relname,
|
||||
CASE WHEN has_table_privilege('$CKUSER', quote_ident(n.nspname) || '.' || quote_ident(c.relname), 'SELECT')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS "SEL",
|
||||
CASE WHEN has_table_privilege('$CKUSER', quote_ident(n.nspname) || '.' || quote_ident(c.relname), 'INSERT')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS "INS",
|
||||
CASE WHEN has_table_privilege('$CKUSER', quote_ident(n.nspname) || '.' || quote_ident(c.relname), 'UPDATE')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS "UPD",
|
||||
CASE WHEN has_table_privilege('$CKUSER', quote_ident(n.nspname) || '.' || quote_ident(c.relname), 'DELETE')
|
||||
THEN 'YES'
|
||||
ELSE 'no'
|
||||
END AS "DEL",
|
||||
array_to_string(ARRAY[c.relacl], '|') as permits
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
WHERE n.nspname not like 'pg_%'
|
||||
AND n.nspname not like 'inform_%'
|
||||
AND relkind = 'r'
|
||||
/*
|
||||
AND (position('$CKUSER' in array_to_string(ARRAY[relacl], '|')) > 0
|
||||
OR position( (SELECT g.rolname
|
||||
FROM pg_auth_members r
|
||||
JOIN pg_authid g ON (r.roleid = g.oid)
|
||||
JOIN pg_authid u ON (r.member = u.oid)
|
||||
AND u.rolname LIKE '$CKUSER%') in array_to_string(ARRAY[relacl], '|')) > 0 )
|
||||
*/
|
||||
ORDER BY 1,2;
|
||||
|
||||
_CODE_
|
||||
@@ -0,0 +1,31 @@
|
||||
select version (), inet_server_port();
|
||||
|
||||
|
||||
SELECT d.datname as Name, pg_catalog.pg_get_userbyid(d.datdba) as Owner,
|
||||
d.datcollate,
|
||||
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
|
||||
THEN pg_catalog.pg_size_pretty(pg_catalog.pg_database_size(d.datname))
|
||||
ELSE 'No Access'
|
||||
END as Size
|
||||
FROM pg_catalog.pg_database d
|
||||
order by
|
||||
CASE WHEN pg_catalog.has_database_privilege(d.datname, 'CONNECT')
|
||||
THEN pg_catalog.pg_database_size(d.datname)
|
||||
ELSE NULL
|
||||
END desc;
|
||||
|
||||
|
||||
SELECT *,
|
||||
current_setting('archive_mode')::BOOLEAN
|
||||
AND (last_failed_wal IS NULL
|
||||
OR last_failed_wal <= last_archived_wal)
|
||||
AS is_archiving,
|
||||
CAST (archived_count AS NUMERIC)
|
||||
/ EXTRACT (EPOCH FROM age(now(), stats_reset))
|
||||
AS current_archived_wals_per_second
|
||||
FROM pg_stat_archiver;
|
||||
|
||||
select * from pg_stat_replication;
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,37 @@
|
||||
-- Analyze stats for a table testanalyze(schema is public)
|
||||
|
||||
dbaclass=# analyze testanalyze;
|
||||
ANALYZE
|
||||
|
||||
-- For analyzing selected columns for emptab table ( schema is dbatest)
|
||||
|
||||
dbaclass=# analyze dbatest.emptab (datname,datdba);
|
||||
ANALYZE
|
||||
|
||||
dbaclass=# select relname,reltuples from pg_class where relname in ('testanalyze','emptab');
|
||||
relname | reltuples
|
||||
-------------+-----------
|
||||
testanalyze | 4
|
||||
emptab | 4
|
||||
(2 rows)
|
||||
|
||||
dbaclass=# select schemaname,relname,analyze_count,last_analyze,last_autoanalyze from pg_stat_user_tables where relname in ('testanalyze','emptab');
|
||||
schemaname | relname | analyze_count | last_analyze | last_autoanalyze
|
||||
------------+-------------+---------------+----------------------------------+------------------
|
||||
public | testanalyze | 1 | 2020-07-21 17:00:49.687053+05:30 |
|
||||
dbatest | emptab | 1 | 2020-07-21 17:10:01.111517+05:30 |
|
||||
(2 rows)
|
||||
|
||||
---Analyze command with verbose command
|
||||
|
||||
dbaclass=# analyze verbose dbatest.emptab (datname,datdba);
|
||||
|
||||
INFO: analyzing "dbatest.emptab"
|
||||
INFO: "emptab": scanned 1 of 1 pages, containing 4 live rows and 0 dead rows; 4 rows in sample, 4 estimated total rows
|
||||
ANALYZE
|
||||
|
||||
---Analyze tables in the current schema that the user has access to.
|
||||
|
||||
|
||||
|
||||
dbaclass=# analyze ;
|
||||
@@ -0,0 +1,13 @@
|
||||
-- Finding statistics level of a column ( orders.orderdate)
|
||||
-- statistics level range is 1-10000 ( where 100 means 1 percent,10000 means 100 percent)
|
||||
|
||||
edbstore=> SELECT attname as column_name , attstattarget as stats_level FROM pg_attribute WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'orders') and attname='orderdate';
|
||||
|
||||
column_name | stats_level
|
||||
-------------+-------------
|
||||
orderdate | 1000
|
||||
(1 row)
|
||||
|
||||
-- To change statistics level of a column:
|
||||
|
||||
edbstore=> alter table orders alter column orderdate set statistics 1000;
|
||||
@@ -0,0 +1,15 @@
|
||||
dbaclass=# SELECT a.query,p.phase, p.blocks_total,p.blocks_done,p.tuples_total, p.tuples_done FROM pg_stat_progress_create_index p JOIN pg_stat_activity a ON p.pid = a.pid;
|
||||
|
||||
-[ RECORD 1 ]+-------------------------------
|
||||
query | reindex index test_idx;
|
||||
phase | building index: scanning table
|
||||
blocks_total | 61281
|
||||
blocks_done | 15331
|
||||
tuples_total | 0
|
||||
tuples_done | 0
|
||||
|
||||
(or)
|
||||
|
||||
dbaclass=# select pid,datname,command,phase,tuples_total,tuples_done,partitions_total,partitions_done from pg_stat_progress_create_index;
|
||||
|
||||
-[ RECORD 1 ]-
|
||||
@@ -0,0 +1 @@
|
||||
select * from pg_stat_progress_vacuum;
|
||||
@@ -0,0 +1,41 @@
|
||||
REINDEX rebuilds an index using the data stored in the index's table, replacing the old copy of the index. There are several scenarios in which to use REINDEX:
|
||||
|
||||
- Rebuild particular index:
|
||||
|
||||
postgres=# REINDEX INDEX TEST_IDX2;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes on a table:
|
||||
|
||||
postgres=# REINDEX TABLE TEST;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes of tables in a schema:
|
||||
postgres=# reindex schema public;
|
||||
REINDEX
|
||||
|
||||
-- Rebuild all indexes in a database :
|
||||
|
||||
postgres=# reindex database dbaclass;
|
||||
REINDEX
|
||||
|
||||
-- Reindex with verbose option:
|
||||
|
||||
postgres=# reindex (verbose) table test;
|
||||
INFO: index "test_idx" was reindexed
|
||||
DETAIL: CPU: user: 5.44 s, system: 2.72 s, elapsed: 11.96 s
|
||||
INFO: index "test_idx2" was reindexed
|
||||
DETAIL: CPU: user: 3.34 s, system: 1.01 s, elapsed: 5.49 s
|
||||
INFO: index "pg_toast_17395_index" was reindexed
|
||||
DETAIL: CPU: user: 0.00 s, system: 0.00 s, elapsed: 0.00 s
|
||||
REINDEX
|
||||
|
||||
Rebuild index without causing lock on the table:( using concurrently option)
|
||||
|
||||
postgres=# REINDEX ( verbose) table concurrently test;
|
||||
INFO: index "public.test_idx" was reindexed
|
||||
INFO: index "public.test_idx2" was reindexed
|
||||
INFO: index "pg_toast.pg_toast_17395_index" was reindexed
|
||||
INFO: table "public.test" was reindexed
|
||||
DETAIL: CPU: user: 11.09 s, system: 6.23 s, elapsed: 24.63 s.
|
||||
REINDEX
|
||||
@@ -0,0 +1,25 @@
|
||||
VACUUM - > REMOVES DEAD ROWS, AND MARK THEM FOR REUSE, BUT IT DOESN’T RETURN THE SPACE TO ORACLE,. IT DOESN'T NEED EXCLUSIVE LOCK ON THE TABLE.
|
||||
|
||||
-------------------------------------------------------------------------
|
||||
|
||||
vacuum a table:
|
||||
|
||||
dbaclass=# vacuum dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
both vacuum and analyze:
|
||||
|
||||
dbaclass=# vacuum analyze dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
with verbose:
|
||||
|
||||
dbaclass# vacuum verbose analyze dbatest.emptab;
|
||||
|
||||
Monitor vacuum process( if vacuum process runs for a long time)
|
||||
|
||||
dbaclass#select * from pg_stat_progress_vacuum;
|
||||
|
||||
Check vacuum related information for the table
|
||||
|
||||
dbaclass=# select schemaname,relname,last_vacuum,vacuum_count from pg_stat_user_tables where relname='emptab';
|
||||
@@ -0,0 +1,35 @@
|
||||
VACUUM FULL - > JUST LIKE MOVE COMMAND IN ORACLE . IT TAKES MORE TIME, BUT IT RETURNS THE SPACE TO OS BECAUSE OF ITS COMPLEX ALGORITHM. IT also requires additional disk space , which can store the new copy of the table., until the activity is completed. Also it locks the table exclusively, which block all operations on the table .
|
||||
|
||||
-- Command to run vacuum full command for table:
|
||||
|
||||
dbaclass=# VACUUM FULL dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
|
||||
|
||||
DEMO TO CHECK HOW IT RECLAIMS SPACE:
|
||||
|
||||
-- Check existing space and delete some data:
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
114 MB
|
||||
(1 row)
|
||||
|
||||
dbaclass=# delete from dbatest.emptab where oid=13634;
|
||||
DELETE 131072
|
||||
|
||||
-- We can observe size is still same:
|
||||
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
114 MB
|
||||
(1 row)
|
||||
|
||||
-- Run vacuum full and observe the space usage:
|
||||
|
||||
dbaclass=# VACUUM FULL dbatest.emptab;
|
||||
VACUUM
|
||||
|
||||
dbaclass=# select pg_size_pretty(pg_relation_size('dbatest.emptab'));
|
||||
@@ -0,0 +1,24 @@
|
||||
Autovacuum methods automates the executions vacuum,freeze and analyze commands.
|
||||
|
||||
-- Find whether autovacuum is enabled or not:
|
||||
|
||||
dbaclass=# select name,setting,short_desc,boot_val,pending_restart from pg_settings where name in ('autovacuum','track_counts');
|
||||
name. | setting | short_desc | boot_val | pending_restart
|
||||
--------------+---------+-------------------------------------------+----------+-----------------
|
||||
autovacuum | on | Starts the autovacuum subprocess. | on | f
|
||||
track_counts | on | Collects statistics on database activity. | on | f
|
||||
(2 rows)
|
||||
|
||||
-- Find other autovacuum related parameter settings
|
||||
dbaclass=# select name,setting,short_desc,min_val,max_val,enumvals,boot_val,pending_restart from pg_settings where category like 'Autovacuum';
|
||||
|
||||
|
||||
- Change autovacuum settings:( they need restart)
|
||||
|
||||
dbaclass=# alter system set autovacuum_max_workers=10 ;
|
||||
ALTER SYSTEM
|
||||
|
||||
Now restart :
|
||||
|
||||
pg_ctl stop
|
||||
pg_ctl start
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT
|
||||
o.conname AS constraint_name,
|
||||
(SELECT nspname FROM pg_namespace WHERE oid=m.relnamespace) AS source_schema,
|
||||
m.relname AS source_table,
|
||||
(SELECT a.attname FROM pg_attribute a WHERE a.attrelid = m.oid AND a.attnum = o.conkey[1] AND a.attisdropped = false) AS source_column,
|
||||
(SELECT nspname FROM pg_namespace WHERE oid=f.relnamespace) AS target_schema,
|
||||
f.relname AS target_table,
|
||||
(SELECT a.attname FROM pg_attribute a WHERE a.attrelid = f.oid AND a.attnum = o.confkey[1] AND a.attisdropped = false) AS target_column
|
||||
FROM
|
||||
pg_constraint o LEFT JOIN pg_class f ON f.oid = o.confrelid LEFT JOIN pg_class m ON m.oid = o.conrelid
|
||||
WHERE
|
||||
o.contype = 'f' AND o.conrelid IN (SELECT oid FROM pg_class c WHERE c.relkind = 'r');
|
||||
|
||||
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/1152260/postgres-sql-to-list-table-foreign-keys
|
||||
@@ -0,0 +1,47 @@
|
||||
--- It wil find the indexes present on a table 'test'
|
||||
|
||||
postgres=# select * from pg_indexes where tablename='test';
|
||||
schemaname | tablename | indexname | tablespace | indexdef
|
||||
------------+-----------+-----------+-------------+----------------------------------------------------------
|
||||
public. | test | tes_idx1 | ts_postgres | CREATE INDEX tes_idx1 ON public.test USING btree (datid)
|
||||
(1 row)
|
||||
|
||||
-- All indexes present in database:
|
||||
|
||||
postgres#select * from pg_indexes
|
||||
|
||||
-- It will show all index details including size:
|
||||
|
||||
postgres=# \di+
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Table | Size | Description
|
||||
--------+----------+-------+----------+--------+--------+-------------
|
||||
public | tes_idx | index | postgres | test56 | 64 kB |
|
||||
public | tes_idx1 | index | postgres | test | 472 MB |
|
||||
(2 rows)
|
||||
|
||||
-- Find indexes with respective column name for table( here table name is test)
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql
|
||||
select
|
||||
t.relname as table_name,
|
||||
i.relname as index_name,
|
||||
array_to_string(array_agg(a.attname), ', ') as column_names
|
||||
from
|
||||
pg_class t,
|
||||
pg_class i,
|
||||
pg_index ix,
|
||||
pg_attribute a
|
||||
where
|
||||
t.oid = ix.indrelid
|
||||
and i.oid = ix.indexrelid
|
||||
and a.attrelid = t.oid
|
||||
and a.attnum = ANY(ix.indkey)
|
||||
and t.relkind = 'r'
|
||||
and t.relname ='test'
|
||||
group by
|
||||
t.relname,
|
||||
i.relname
|
||||
order by
|
||||
t.relname,
|
||||
i.relname;
|
||||
@@ -0,0 +1,47 @@
|
||||
--- It wil find the indexes present on a table 'test'
|
||||
|
||||
postgres=# select * from pg_indexes where tablename='test';
|
||||
schemaname | tablename | indexname | tablespace | indexdef
|
||||
------------+-----------+-----------+-------------+----------------------------------------------------------
|
||||
public. | test | tes_idx1 | ts_postgres | CREATE INDEX tes_idx1 ON public.test USING btree (datid)
|
||||
(1 row)
|
||||
|
||||
-- All indexes present in database:
|
||||
|
||||
postgres#select * from pg_indexes
|
||||
|
||||
-- It will show all index details including size:
|
||||
|
||||
postgres=# \di+
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Table | Size | Description
|
||||
--------+----------+-------+----------+--------+--------+-------------
|
||||
public | tes_idx | index | postgres | test56 | 64 kB |
|
||||
public | tes_idx1 | index | postgres | test | 472 MB |
|
||||
(2 rows)
|
||||
|
||||
-- Find indexes with respective column name for table( here table name is test)
|
||||
|
||||
REFERENCE - https://stackoverflow.com/questions/2204058/list-columns-with-indexes-in-postgresql
|
||||
select
|
||||
t.relname as table_name,
|
||||
i.relname as index_name,
|
||||
array_to_string(array_agg(a.attname), ', ') as column_names
|
||||
from
|
||||
pg_class t,
|
||||
pg_class i,
|
||||
pg_index ix,
|
||||
pg_attribute a
|
||||
where
|
||||
t.oid = ix.indrelid
|
||||
and i.oid = ix.indexrelid
|
||||
and a.attrelid = t.oid
|
||||
and a.attnum = ANY(ix.indkey)
|
||||
and t.relkind = 'r'
|
||||
and t.relname ='test'
|
||||
group by
|
||||
t.relname,
|
||||
i.relname
|
||||
order by
|
||||
t.relname,
|
||||
i.relname;
|
||||
@@ -0,0 +1,27 @@
|
||||
-- Below of any commands can be used to find the schema details:
|
||||
|
||||
postgres=# select schema_name,schema_owner from information_schema.schemata;
|
||||
schema_name | schema_owner
|
||||
--------------------+--------------
|
||||
raj | postgres
|
||||
information_schema | postgres
|
||||
public | postgres
|
||||
pg_catalog | postgres
|
||||
pg_toast_temp_1 | postgres
|
||||
pg_temp_1. | postgres
|
||||
pg_toast | postgres
|
||||
(7 rows)
|
||||
|
||||
postgres=# select nspname as schema_name , pg_get_userbyid(nspowner) as schema_owner from pg_catalog.pg_namespace;
|
||||
schema_name | schema_owner
|
||||
--------------------+--------------
|
||||
pg_toast | postgres
|
||||
pg_temp_1 | postgres
|
||||
pg_toast_temp_1 | postgres
|
||||
pg_catalog | postgres
|
||||
public | postgres
|
||||
information_schema | postgres
|
||||
raj | postgres
|
||||
(7 rows)
|
||||
|
||||
postgres=# \dn+
|
||||
@@ -0,0 +1,15 @@
|
||||
-- For getting the physical location of a table:
|
||||
|
||||
postgres=# select pg_relation_filepath('test');
|
||||
pg_relation_filepath
|
||||
----------------------
|
||||
base/13635/17395
|
||||
(1 row)
|
||||
|
||||
-- For getting the physical location of an index:
|
||||
|
||||
postgres=# select pg_relation_filepath('test_idx');
|
||||
pg_relation_filepath
|
||||
----------------------
|
||||
base/13635/17638
|
||||
(1 row)
|
||||
@@ -0,0 +1,27 @@
|
||||
Describe the table:
|
||||
|
||||
postgres=# \d test
|
||||
Table "public.test"
|
||||
Column. | Type | Collation | Nullable | Default
|
||||
------------+---------+-----------+----------+---------
|
||||
sourcefile | text. | | | -- >>>> Will get size for this one
|
||||
sourceline | integer | | |. -- >>>> Will get size for this one also
|
||||
seqno. | integer | |. |
|
||||
name. | text | | |
|
||||
setting. | text | | |
|
||||
applied | boolean | | |
|
||||
error | text. | | |
|
||||
Indexes:
|
||||
"test_idx" btree (sourcefile)
|
||||
"test_idx2" btree (sourceline)
|
||||
|
||||
|
||||
-- Find the column size ( for sourcefile and sourceline)
|
||||
|
||||
postgres=# select pg_size_pretty(sum(pg_column_size(sourcefile))) as total_size from test;
|
||||
total_size
|
||||
------------
|
||||
12 MB
|
||||
(1 row)
|
||||
|
||||
postgres=# select pg_size_pretty(sum(pg_column_size(sourceline))) as total_size from test;
|
||||
@@ -0,0 +1,46 @@
|
||||
postgres=# \d test
|
||||
Table "public.test"
|
||||
Column. | Type | Collation | Nullable | Default
|
||||
------------+---------+-----------+----------+---------
|
||||
sourcefile | text | | |
|
||||
sourceline | integer | | |
|
||||
seqno. | integer | |. |
|
||||
name. | text | | |
|
||||
setting. | text | | |
|
||||
applied | boolean | | |
|
||||
error | text. | | |
|
||||
Indexes:
|
||||
"test_idx" btree (sourcefile)
|
||||
"test_idx2" btree (sourceline)
|
||||
|
||||
-- Find the table_size ( excluding the index_size)
|
||||
|
||||
postgres=# SELECT pg_size_pretty (pg_relation_size('test'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
30 MB
|
||||
(1 row)
|
||||
|
||||
-- Find the total_index size of the table
|
||||
|
||||
postgres=# sELECT pg_size_pretty ( pg_indexes_size('test'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
26 MB
|
||||
(1 row)
|
||||
|
||||
-- Find particular index size:
|
||||
|
||||
postgres=# select pg_size_pretty(pg_total_relation_size('test_idx'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
19 MB
|
||||
|
||||
postgres=# select pg_size_pretty(pg_total_relation_size('test_idx2'));
|
||||
pg_size_pretty
|
||||
----------------
|
||||
6496 kB
|
||||
|
||||
Another method:
|
||||
|
||||
postgres=# \di+ "test_idx"
|
||||
@@ -0,0 +1,19 @@
|
||||
-- Find table sizes and its respective index sizes
|
||||
|
||||
SELECT
|
||||
table_name,
|
||||
pg_size_pretty(table_size) AS table_size,
|
||||
pg_size_pretty(indexes_size) AS indexes_size,
|
||||
pg_size_pretty(total_size) AS total_size
|
||||
FROM (
|
||||
SELECT
|
||||
table_name,
|
||||
pg_table_size(table_name) AS table_size,
|
||||
pg_indexes_size(table_name) AS indexes_size,
|
||||
pg_total_relation_size(table_name) AS total_size
|
||||
FROM (
|
||||
SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
|
||||
FROM information_schema.tables
|
||||
) AS all_tables
|
||||
ORDER BY total_size DESC
|
||||
) AS pretty_sizes limit 10;
|
||||
@@ -0,0 +1,33 @@
|
||||
-- Simple create index:
|
||||
postgres=# create index tab_idx2 on scott.customer(emp_name);
|
||||
CREATE INDEX
|
||||
|
||||
-- Create index with tablespace:
|
||||
|
||||
postgres#CREATE INDEX tab_idx2 on scott.customer(emp_name) TABLESPACE IND_TS;
|
||||
CREATE INDEX
|
||||
|
||||
-- Create index without causing blocking:
|
||||
|
||||
postgres=# create index concurrently tab_idx2 on scott.customer(emp_name) TABLESPACE IND_TS;
|
||||
CREATE INDEX
|
||||
|
||||
-- Create unique index:
|
||||
|
||||
postgres=# create unique index tab_idx2 on scott.customer(emp_name)
|
||||
CREATE INDEX
|
||||
|
||||
-- Create functional index:
|
||||
|
||||
postgres=# create index fun_idx on scott.customer(lower(emp_name));
|
||||
CREATE INDEX
|
||||
|
||||
-- Create multi column index:
|
||||
|
||||
postgres=# create index multi_idx on scott.customer(emp_name,emp_id);
|
||||
CREATE INDEX
|
||||
|
||||
-- drop an index:
|
||||
|
||||
postgres=# drop index fun_idx;
|
||||
DROP INDEX
|
||||
@@ -0,0 +1,19 @@
|
||||
--Below is for finding objects under schema scott: Replace your schema_name with scott
|
||||
|
||||
SELECT n.nspname as "Schema",
|
||||
c.relname as "Name",
|
||||
CASE c.relkind WHEN 'r' THEN 'table' WHEN 'v' THEN 'view' WHEN 'm' THEN 'materialized view' WHEN 'i' THEN 'index' WHEN 'S' THEN 'sequence' WHEN 's' THEN 'special' WHEN 'f' THEN 'foreign table' WHEN 'p' THEN 'partitioned table' WHEN 'I' THEN 'partitioned index' END as "Type",
|
||||
pg_catalog.pg_get_userbyid(c.relowner) as "Owner"
|
||||
FROM pg_catalog.pg_class c
|
||||
LEFT JOIN pg_catalog.pg_namespace n ON n.oid = c.relnamespace
|
||||
where n.nspname ='scott'
|
||||
AND pg_catalog.pg_table_is_visible(c.oid)
|
||||
ORDER BY 1,2;
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
NOTE: Make sure that, the schema_name for which you are looking for objects, is present in the search_path of that user. Otherwise it wont return any rows
|
||||
|
||||
postgres=# show search_path;
|
||||
@@ -0,0 +1,5 @@
|
||||
postgres=# select * from pg_views where schemaname not in ('pg_catalog','information_schema','sys');
|
||||
count
|
||||
-------
|
||||
|
||||
postgres#\dv
|
||||
@@ -0,0 +1,47 @@
|
||||
-- Find the sequence details:
|
||||
|
||||
select * from pg_sequences;
|
||||
|
||||
(or)
|
||||
|
||||
\ds+
|
||||
|
||||
List of relations
|
||||
Schema | Name | Type | Owner | Size | Description
|
||||
--------+-----------+----------+--------------+------------+-------------
|
||||
public | class_seq | sequence | enterprisedb | 8192 bytes |
|
||||
(1 row)
|
||||
|
||||
-- Create sequences:
|
||||
|
||||
postgres# CREATE SEQUENCE class_seq INCREMENT 1 MINVALUE 1 MAXVALUE 1000 START 1;
|
||||
CREATE SEQUENCE
|
||||
|
||||
-- Create sequence in descending:
|
||||
|
||||
postgres# CREATE SEQUENCE class_seq INCREMENT -1 MINVALUE 1 MAXVALUE 1000 START 1000;
|
||||
CREATE SEQUENCE
|
||||
|
||||
-- Alter sequence to change maxvalue:
|
||||
|
||||
postgres=# alter sequence class_seq maxvalue 500;
|
||||
ALTER SEQUENCE
|
||||
|
||||
-- Reset a sequence using alter command:
|
||||
|
||||
postgres=# alter sequence class_seq restart with 1;
|
||||
ALTER SEQUENCE
|
||||
|
||||
-- Find next_val and currval of a sequence:
|
||||
|
||||
postgres=# select nextval('class_seq');
|
||||
nextval
|
||||
---------
|
||||
1
|
||||
(1 row)
|
||||
|
||||
postgres=# select currval('class_seq');
|
||||
currval
|
||||
---------
|
||||
1
|
||||
(1 row)
|
||||
@@ -0,0 +1,6 @@
|
||||
Partial index, means index will be created on a specific subset of data of a table.
|
||||
|
||||
edbstore=> create index part_emp_idx on orders(tax) where tax > 400;
|
||||
CREATE INDEX
|
||||
|
||||
edbstore=> \d part_emp_idx
|
||||
@@ -0,0 +1,22 @@
|
||||
Below queries can be used to get schema wise size in postgres db
|
||||
|
||||
postgres=# select schemaname,pg_size_pretty(sum(pg_relation_size(quote_ident(schemaname) || '.' || quote_ident(tablename)))::bigint) as schema_size FROM pg_tables group by schemaname;
|
||||
schemaname | pg_size_pretty
|
||||
--------------------+----------------
|
||||
raj | 8192 bytes
|
||||
public | 3651 MB
|
||||
pg_catalog | 2936 kB
|
||||
information_schema | 96 kB
|
||||
(4 rows)
|
||||
|
||||
postgres=# SELECT schemaname,
|
||||
pg_size_pretty(sum(table_size)::bigint) as schema_size,
|
||||
(sum(table_size) / pg_database_size(current_database())) * 100 as percentage_of_total_db
|
||||
FROM (
|
||||
SELECT pg_catalog.pg_namespace.nspname as schemaname,
|
||||
pg_relation_size(pg_catalog.pg_class.oid) as table_size
|
||||
FROM pg_catalog.pg_class
|
||||
JOIN pg_catalog.pg_namespace ON relnamespace = pg_catalog.pg_namespace.oid
|
||||
) t
|
||||
GROUP BY schemaname
|
||||
ORDER BY schemaname;
|
||||
@@ -0,0 +1,23 @@
|
||||
-- Create a simple table
|
||||
postgres=# Create table member_table ( mem_id integer, member_name varchar(100) , mobile integer not null);
|
||||
CREATE TABLE
|
||||
|
||||
-- Create table with primary key
|
||||
postgres=# Create table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null);
|
||||
CREATE TABLE
|
||||
-- Create table under particular tablespace
|
||||
postgres=# Create table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null) tablespace pg_production_ts;
|
||||
CREATE TABLE
|
||||
|
||||
-- Create table with unique constraint
|
||||
postgres=# Create table member_table ( mem_id integer, member_name varchar(100) , mobile integer not null , constraint mem_id_cons unique(mem_id));
|
||||
CREATE TABLE
|
||||
|
||||
-- Create temporary table:
|
||||
|
||||
postgres=# Create temporary table member_table ( mem_id integer primary key, member_name varchar(100) , mobile integer not null) tablespace pg_default;
|
||||
CREATE TABLE
|
||||
|
||||
-- Drop table
|
||||
|
||||
postgres=# drop table member_table;
|
||||
@@ -0,0 +1,21 @@
|
||||
-- Find table sizes and its respective index sizes
|
||||
|
||||
SELECT
|
||||
table_name,
|
||||
pg_size_pretty(table_size) AS table_size,
|
||||
pg_size_pretty(indexes_size) AS indexes_size,
|
||||
pg_size_pretty(total_size) AS total_size
|
||||
FROM (
|
||||
SELECT
|
||||
table_name,
|
||||
pg_table_size(table_name) AS table_size,
|
||||
pg_indexes_size(table_name) AS indexes_size,
|
||||
pg_total_relation_size(table_name) AS total_size
|
||||
FROM (
|
||||
SELECT ('"' || table_schema || '"."' || table_name || '"') AS table_name
|
||||
FROM information_schema.tables
|
||||
) AS all_tables
|
||||
ORDER BY total_size DESC
|
||||
) AS pretty_sizes limit 10;
|
||||
|
||||
|
||||
@@ -0,0 +1,24 @@
|
||||
-- Top 10 big tables in postgres
|
||||
|
||||
select schemaname as schema_owner,
|
||||
relname as table_name,
|
||||
pg_size_pretty(pg_total_relation_size(relid)) as total_size,
|
||||
pg_size_pretty(pg_relation_size(relid)) as used_size,
|
||||
pg_size_pretty(pg_total_relation_size(relid) - pg_relation_size(relid))
|
||||
as free_space
|
||||
from pg_catalog.pg_statio_user_tables
|
||||
order by pg_total_relation_size(relid) desc,
|
||||
pg_relation_size(relid) desc
|
||||
limit 10;
|
||||
|
||||
|
||||
|
||||
(or)
|
||||
|
||||
SELECT
|
||||
nspname as schema_name,relname as table_name,pg_size_pretty(pg_relation_size(c.oid)) as "table_size"
|
||||
from pg_class c left join pg_namespace n on ( n.oid=c.relnamespace)
|
||||
where nspname not in ('pg_catalog','information_schema')
|
||||
order by pg_relation_size(c.oid) desc limit 10;
|
||||
|
||||
|
||||
@@ -0,0 +1,17 @@
|
||||
SELECT c.table_schema,
|
||||
c.table_name as table,
|
||||
c.ordinal_position as order,
|
||||
c.column_name as column,
|
||||
-- c.data_type as type,
|
||||
CASE WHEN c.data_type IN ('character', 'varchar') THEN c.data_type || '(' || c.character_maximum_length || ')'
|
||||
WHEN c.data_type IN ('numeric') THEN c.data_type || '(' || c.numeric_precision || ',' || c.numeric_scale || ')'
|
||||
ELSE c.data_type
|
||||
END,
|
||||
-- c.character_maximum_length as max_len,
|
||||
c.is_nullable as null,
|
||||
col_description(t.oid, c.ordinal_position)
|
||||
FROM information_schema.columns c
|
||||
JOIN pg_class t ON (t.relname = c.table_name)
|
||||
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2, 3;
|
||||
|
||||
@@ -0,0 +1,7 @@
|
||||
SELECT table_schema,
|
||||
table_name,
|
||||
constraint_name as constraint,
|
||||
constraint_type as type
|
||||
FROM information_schema.table_constraints
|
||||
WHERE table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,6 @@
|
||||
SELECT table_schema as schema,
|
||||
table_name as table
|
||||
FROM information_schema.tables
|
||||
WHERE table_type = 'BASE TABLE'
|
||||
AND table_schema NOT IN ('pg_catalog', 'information_schema')
|
||||
ORDER BY 1, 2;
|
||||
@@ -0,0 +1,81 @@
|
||||
These are generic SQL queries. In some cases you need to replace variable holders -> <VARIABLE>
|
||||
with the actual value you need.
|
||||
|
||||
ALL_COLUMNS.sql -> Lists all tables, their columns and data type
|
||||
ALL_CONSTRAINTS.sql -> lists all tables and their associated constraints
|
||||
ALL_TABLES.sql -> lists all tables
|
||||
bad_idx.sql -> lists all indexes flagged as invalid
|
||||
blocked_transaction.sql -> shows all blocked queries and transaction causing block
|
||||
cache_hit_ratio.sql -> shows cache hit ratio for all databases
|
||||
cancel_all_queries.sql -> cancels all tranactions for specified <user_name>
|
||||
check_fks.sql -> shows fk status for all tables > 9mb in size or greater than 1000 writes
|
||||
column_name_type_difference.sql -> shows all tables and columns that have same column name but different data type
|
||||
columns_of_type_x.sql -> shows all tables that have columns of type <TYPE>
|
||||
connection_counts.sql -> shows connection totals and totals for users, database and user/database
|
||||
current_locks.sql -> shows current_locks
|
||||
current_queries_blocked.sql -> shows blocked transactions and blocking query
|
||||
current_queries.sql -> shows all current_queries
|
||||
database_info.sql -> shows information from pg_database
|
||||
database_sizes.sql -> shows size of all databases and percentage of total size
|
||||
distinct_data_types.sql -> shows all data types in use in cluster
|
||||
enum_maint.sql -> generic query to show all enums and commented delete / add code
|
||||
enums.sql -> generic query to show all enums
|
||||
filenames_and_tables.sql -> shows physical filenames for all tables
|
||||
gen_create_indexes.sql -> generates sql statements to recreate all indexes
|
||||
gen_drop_indexes.sql -> generates sql statements to drop all indexes
|
||||
gen_get_seq_max.sql -> generates sql to get the max value of all sequences
|
||||
gen_revoke_all.sql -> generates sql to revoke all priviliges from all tables for user <some_user>
|
||||
gen_table_compare_counts.sql -> generates sql to compare reltuples to actual row count for all tables
|
||||
get_slony_schemas.sql -> shows all slony schemas
|
||||
get_trans_min_cnt.sql -> shows trandaction count per minute in cluster
|
||||
groups_users.sql -> shows all groups and assigned users
|
||||
inactive_tables.sql -> shows all tables that have 0 fetch's & 0 read's
|
||||
index_bloat.sql -> shows size of index vs. table
|
||||
index_bloat2.sql > a more specific version of index bloat WHERE realbloat > 50 and wastedbytes > 50000000 (adjust to your requirements)
|
||||
index_comments.sql -> shows comments for all indexes
|
||||
index_sizes.sql -> show the size of all indexes
|
||||
index_tablespaces.sql -> shows the tablespace indexes are using
|
||||
make_alter_index_spc.sql -> generates sql to alter all indexes in public to <new_table_space>
|
||||
make_alter_table_spc.sql -> generates sql to alter all tables in public to <new_table_space>
|
||||
most_active_tables.sql -> sorts and lists all tables in descending order by tuples read
|
||||
parent_and_children.sql -> lists all tables and associated children
|
||||
pg_runtime.sql -> lists start and run time for cluster
|
||||
pg_stat_all_indexes.sql -> lists stats and status for all indexes
|
||||
pg_stat_all_tables.sql -> lists stats and vacuum info for all tables
|
||||
remove_ctrl.sh -> removes control characters from queries (used if query written in windows)
|
||||
role_members.sql -> lists all roles and their members
|
||||
rules.sql -> lists all tables and associated rules
|
||||
sequence_cols.sql -> lists all tables with sequence columns
|
||||
show_dead_tuples.sql -> shows table stats and status of dead tuples
|
||||
table_access.sql -> lists all tables and their acl
|
||||
table_columns.sql -> lists all columns for specified <table>
|
||||
table_comments.sql -> lists comments for all tables and columns
|
||||
table_constraints.sql -> lists all constraints for all tables
|
||||
table_dependents.sql -> lists all tables, children and related constraint name
|
||||
table_grants.sql -> lists all tables and their grants
|
||||
table_in_function.sql -> list all functions that reference <TABLE/COLUMN>
|
||||
table_in_schema.sql -> lists all tables in <schema_name>
|
||||
table_in_trigger.sql -> lists all triggers that reference <table>
|
||||
table_row_counts.sql -> lists all tables with greater than 0 rows
|
||||
tables_and_comments.sql -> lists only tables and columns with comments
|
||||
tables_and_filenames.sql -> lists all tables and physical filenames
|
||||
tables_and_owners.sql -> lists all tables, owners and permits
|
||||
tables_and_pkey.sql -> lists all tables and primary key name
|
||||
table_sizes.sql -> lists all tables, owner, physical filename, tuples, size, total size, size in bytes, total_size in bytes and tablespace
|
||||
table_stats.sql -> lists all tables and indexes, last vacuum, inserted, updated and deleted count
|
||||
tables_with_column_name.sql -> lists all tables with column name LIKE <COLUMN_NAME>
|
||||
tables_with_FKs.sql -> lists all tables that have foreign keys and FK name
|
||||
tables_with_large_index_size.sql -> lists all tables where index size > table size
|
||||
tables_with_no_FK.sql -> lists all tables that have no foreign key
|
||||
tables_with_no_pkey.sql -> lists all tables with no primary key
|
||||
tables_with_no_public_grants.sql -> lists all tables that have no public grants (Only owner has access)
|
||||
table_triggers.sql -> lists all tables with triggers, trigger name, trigger type and tgenabled value
|
||||
tcp_settings.sql -> lists the current tcp settings
|
||||
triggers_and_constraints.sql -> lists tables, their triggers and function called by the trigger
|
||||
useless_indexes.sql -> lists tables and associated indexes scanned < 200 times and are not unique indexes
|
||||
useless_indexes2.sql -> lists tables and associated indexes scanned < 200 times and are not unique indexes or primary keys
|
||||
users.sql -> lists all users/groups and if they are superuser or not
|
||||
user_table_access_detail.sql -> lists all table privileges for user <CKUSER>
|
||||
user_table_access.sql -> lists all tables, acl and position in acl for user <USERNAME>
|
||||
wasted_index_space.sql -> shows space wasted by unused indexes
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
SELECT n.nspname,
|
||||
t.relname as table,
|
||||
c.conname as pk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'p')
|
||||
JOIN pg_namespace n ON (n.oid = t.relnamespace)
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY n.nspname, t.relname, c.conname;
|
||||
@@ -0,0 +1,26 @@
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
count(c.conname)
|
||||
FROM pg_class t
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
GROUP BY n.nspname,
|
||||
t.relname
|
||||
ORDER BY count, n.nspname, t.relname;
|
||||
|
||||
|
||||
SELECT n.nspname as schema,
|
||||
t.relname as table,
|
||||
c.conname as fk_name
|
||||
FROM pg_class t
|
||||
JOIN pg_namespace n ON n.oid = t.relnamespace
|
||||
JOIN pg_constraint c ON ( c.conrelid = t.OID AND c.contype = 'f')
|
||||
WHERE relkind = 'r'
|
||||
AND t.relname NOT LIKE 'pg_%'
|
||||
AND t.relname NOT LIKE 'sql_%'
|
||||
ORDER BY n.nspname,
|
||||
t.relname,
|
||||
c.conname;
|
||||
@@ -0,0 +1,15 @@
|
||||
SELECT n.nspname as schema,
|
||||
relname as table
|
||||
FROM pg_class c
|
||||
JOIN pg_namespace n ON ( n.oid = c.relnamespace )
|
||||
WHERE relkind = 'r'
|
||||
AND relname NOT LIKE 'pg_%'
|
||||
AND relname NOT LIKE 'sql_%'
|
||||
AND c.oid NOT IN
|
||||
(SELECT conrelid
|
||||
FROM pg_constraint
|
||||
WHERE contype = 'f'
|
||||
AND contype <> 'p'
|
||||
AND contype <> 'c')
|
||||
ORDER BY n.nspname,
|
||||
relname;
|
||||
@@ -0,0 +1,16 @@
|
||||
SELECT n.nspname,
|
||||
i.relname,
|
||||
i.indexrelname,
|
||||
CASE WHEN idx.indisprimary
|
||||
THEN 'pkey'
|
||||
WHEN idx.indisunique
|
||||
THEN 'uidx'
|
||||
ELSE 'idx'
|
||||
END AS type,
|
||||
'INVALID'
|
||||
FROM pg_stat_all_indexes i
|
||||
JOIN pg_class c ON (c.oid = i.relid)
|
||||
JOIN pg_namespace n ON (n.oid = c.relnamespace)
|
||||
JOIN pg_index idx ON (idx.indexrelid = i.indexrelid )
|
||||
WHERE idx.indisvalid = FALSE
|
||||
ORDER BY 1, 2;
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user