-
Notifications
You must be signed in to change notification settings - Fork 24
/
Copy pathpg_collector.sql
3024 lines (2671 loc) · 131 KB
/
pg_collector.sql
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
-- +-----------------------------------------------------------------------------------+
-- | -- Script Name: pg_collector.sql |
-- | -- Author : Mohamed Ali |
-- | -- Create Date : 16 SEPT 2019 |
-- | -- Description : Script to Collect PostgreSQL Database Informations |
-- | and generate HTML Report |
-- | -- version : V 3.1 |
-- | -- Changelog : https://github.com/awslabs/pg-collector/blob/main/CHANGELOG.md | |
-- | Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. |
-- | SPDX-License-Identifier: MIT-0 |
-- +-----------------------------------------------------------------------------------+
\H
\set filename :DBNAME-`date +%Y-%m-%d_%H%M%S`
\o /tmp/pg_collector_:filename.html
\pset footer off
\qecho <style type='text/css'>
\qecho body {
\qecho font:10pt Arial,Helvetica,sans-serif;
\qecho color:Black Russian; background:white; }
\qecho p {
\qecho font:10pt Arial,sans-serif;
\qecho color:Black Russian; background:white; }
\qecho table,tr,td {
\qecho font:10pt Arial,Helvetica,sans-serif;
\qecho text-align:center;
\qecho color:Black Russian; background:white;
\qecho padding:0px 0px 0px 0px; margin:0px 0px 0px 0px; }
\qecho th {
\qecho font:bold 10pt Arial,Helvetica,sans-serif;
\qecho color:#16191f;
\qecho background:#e59003;
\qecho padding:0px 0px 0px 0px;}
\qecho h1 {
\qecho font:bold 16pt Arial,Helvetica,Geneva,sans-serif;
\qecho color:#16191f;
\qecho background-color:#e59003;
\qecho border-bottom:1px solid #e59003;
\qecho margin-top:0pt; margin-bottom:0pt; padding:0px 0px 0px 0px;}
\qecho h2 {
\qecho font:bold 10pt Arial,Helvetica,Geneva,sans-serif;
\qecho color:#16191f;
\qecho background-color:White;
\qecho margin-top:4pt; margin-bottom:0pt;}
\qecho h3 {
\qecho font:bold 10pt Arial,Helvetica,Geneva,sans-serif;
\qecho color:#16191f;
\qecho background-color:White;
\qecho margin-top:4pt; margin-bottom:0pt;}
\qecho a {
\qecho font:9pt Arial,Helvetica,sans-serif;
\qecho color:#663300;
\qecho background:#ffffff;
\qecho margin-top:0pt; margin-bottom:0pt; vertical-align:top;}
\qecho .threshold-critical {
\qecho font:bold 10pt Arial,Helvetica,sans-serif;
\qecho color:red; }
\qecho .threshold-warning {
\qecho font:bold 10pt Arial,Helvetica,sans-serif;
\qecho color:orange; }
\qecho .threshold-ok {
\qecho font:bold 10pt Arial,Helvetica,sans-serif;
\qecho color:green; }
\qecho </style>
\qecho <h1 align="center" style="background-color:#e59003" >PG COLLECTOR V3.1</h1>
\qecho <font size="+1" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><a href="https://github.com/awslabs/pg-collector" target="_blank">For more information about PG Collector, visit the project github repository</a></font><hr align="left" >
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>DB INFO</b></font><hr align="left" width="150">
\qecho <br>
\qecho 'PG Host Name / PG RDS ENDPOINT: ':HOST
\qecho <br>
\set QUIET 1
select case when count(*)=0 then 'select ''not-aurora'' as avers'
else 'select aurora_version() as avers'
end as aurora_version_query
from pg_settings where name='rds.extensions' and setting like '%aurora_stat_utils%' \gset
prepare detect_aurora as :aurora_version_query;
execute detect_aurora \gset
deallocate detect_aurora;
with
pgvers as (
select current_setting('server_version') as v
), allvers as (
select 1 priority, 'Aurora-'||v||'-'||:'avers' as version from pgvers
where :'avers' <> 'not-aurora'
union all
select 2, 'RDS-'||v from pgvers, pg_settings s
where s.name like 'rds.%'
union all
select 3, 'pg-'||v from pgvers
)
select first_value(version) over (order by priority) as server_version
from allvers limit 1 \gset
select case when pg_is_in_recovery() then 'Standby/Reader DB (Read Only)' else 'Primary/writer DB (Read write)' end as standby_mode \gset
\unset QUIET
\qecho :server_version
\qecho :standby_mode
\qecho <br>
\qecho <br>
select now () as "Date" ,pg_postmaster_start_time() as "DB_START_DATE", current_timestamp - pg_postmaster_start_time() as "UP_TIME" ,current_database() as "DB_connected" ,current_user USER_NAME,inet_server_port() as "DB_PORT ",version() as "DB_Version" , setting AS block_size FROM pg_settings WHERE name = 'block_size';
\qecho <br>
\qecho <br>
\l+
\qecho <br>
select datname as Database_name , datistemplate as database_is_template ,datallowconn as database_allow_connections, datconnlimit as database_connection_limit , datlastsysoid ,datfrozenxid ,datminmxid from pg_database;
\qecho <br>
\qecho <br>
\qecho <table width="90%" border="1">
\qecho <tr><th colspan="4"><div align="center"><font color="#16191f"><b>INFO</b></font></div></th></tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Database_size">Database size</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#DB_parameters">DB parameters</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Transaction_ID_TXID">Transaction ID TXID (Wraparound)</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Table_Size">Table Size</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#index_Size">Index Size</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#vacuum_Statistics">Vacuum & Statistics</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Extensions">Extensions</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Memory_setting">Memory setting</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#pg_stat_statements_extension">pg_stat_statements extension</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Users_Roles_Info">Users & Roles Info</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Schema_Info">schema Info</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Tablespaces_Info">Tablespaces Info</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#table_Access_Profile">Table Access Profile</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Unused_Indexes">Unused Indexes</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Index_Access_Profile">Index Access Profile</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Fragmentation">Fragmentation (Bloat)</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Toast_Tables_Mapping">Toast Tables Mapping</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Replication">Replication</a></td>
\qecho <td nowrap align="center"width="25%"><a class="link"href="#sessions_info">Sessions/Connections Info</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Orphaned_prepared_transactions">Orphaned prepare transactions</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#PK_FK_using_numeric_or_integer_data_type">PK or FK using numeric or integer data type</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#public_Schema">public Schema</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#invalid_indexes">invalid indexes</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#default_access_privileges">Default access privileges</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#pgaudit_extension">pgaudit extension</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#unlogged_tables">Unlogged Tables</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#access_privileges">Access privileges</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#ssl">ssl</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#background_processes">Background processes</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Multixact_ID_MXID">Multixact ID MXID (Wraparound)</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Temp_tables">Temp tables</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Large_objects">Large objects</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Partition_tables">Partition tables</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#pg_shdepend">pg_shdepend</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#FK_without_index">FK without index</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#sequences">sequences</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#pg_hba.conf">pg_hba.conf</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Duplicate_indexes">Duplicate indexes</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#functions_statistics">Functions statistics</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#DB_Load">DB Load</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#triggers">Triggers</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#pg_config">pg_config</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Invalid_databases">Invalid databases</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#******">******</a></td>
\qecho </tr>
\qecho </table>
\qecho <br>
\qecho <br>
\qecho <br>
\qecho <table width="90%" border="1">
\qecho <tr><th colspan="4"><div align="center"><font color="#16191f"><b>Amazon Aurora PostgreSQL</b></font></div></th></tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_version">Aurora version</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_PostgreSQL_built-in_functions">Aurora PostgreSQL built-in functions</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_db_instance_identifier">Aurora db instance identifier</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_cluster_instances">Aurora cluster instances</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_reader_instances-Replica_Lag">Aurora reader instances - Replica Lag</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_cluster_cache_management_(CCM)">Aurora cluster cache management (CCM)</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_global_db_status">Aurora global db status</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_wait_event_stat">Aurora wait event stat</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#query_plan_management">Query Plan Management (QPM)</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#Aurora_dml_activity">Aurora DML activity</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#process_memory_context_usage">process memory context usage</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#logical_replication_write_through_cache">Logical replication write-through cache</a></td>
\qecho </tr>
\qecho <tr>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#******">******</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#******">******</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#******">******</a></td>
\qecho <td nowrap align="center" width="25%"><a class="link" href="#******">******</a></td>
\qecho </tr>
\qecho </table>
\qecho <br>
\qecho <br>
\qecho <br>
-- +----------------------------------------------------------------------------+
-- | - Database_size - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Database_size"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Database size</b></font><hr align="left" width="460">
SELECT pg_database.datname Database_Name , pg_size_pretty(pg_database_size(pg_database.datname)) AS Database_Size FROM pg_database;
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Transaction ID TXID - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Transaction_ID_TXID"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Transaction ID TXID (Wraparound)</b></font><hr align="left" width="460">
\qecho <br>
\qecho <details>
\qecho <h3>oldest xid:</h3>
SELECT max(age(datfrozenxid)) oldest_xid FROM pg_database;
\qecho <h3>oldest xid per database:</h3>
SELECT datname database_name ,age(datfrozenxid) oldest_xid_per_DB
FROM pg_database order by 2 limit 20;
\qecho <h3>percent_towards_emergency_autovac & percent_towards_wraparound :</h3>
WITH max_age AS ( SELECT 2^31-1000000 as max_old_xid , setting AS
autovacuum_freeze_max_age FROM pg_catalog.pg_settings
WHERE name = 'autovacuum_freeze_max_age' ) ,
per_database_stats AS ( SELECT datname , m.max_old_xid::int ,
m.autovacuum_freeze_max_age::int , age(d.datfrozenxid) AS oldest_current_xid
FROM pg_catalog.pg_database d JOIN max_age m ON (true) WHERE d.datallowconn )
SELECT max(oldest_current_xid) AS oldest_current_xid ,
max(ROUND(100*(oldest_current_xid/max_old_xid::float))) AS percent_towards_wraparound
, max(ROUND(100*(oldest_current_xid/autovacuum_freeze_max_age::float))) AS percent_towards_emergency_autovac
FROM per_database_stats ;
\qecho <h3>current running autovacuum process:</h3>
SELECT datname,usename,state,query,
now() - pg_stat_activity.query_start AS duration,
wait_event from pg_stat_activity where query ~ '^autovacuum:' order by 5;
\qecho <h3>current running vacuum process:</h3>
SELECT datname,usename,state,query,
now() - pg_stat_activity.query_start AS duration,
wait_event from pg_stat_activity where query ~* '\A\s*vacuum\M' order by 5;
\qecho <h3>vacuum progress process:</h3>
SELECT p.pid, now() - a.xact_start AS duration, coalesce(wait_event_type ||'.'|| wait_event, 'f') AS waiting,
CASE WHEN a.query ~ '^autovacuum.*to prevent wraparound' THEN 'wraparound' WHEN a.query ~ '^vacuum' THEN 'user' ELSE 'regular' END AS mode,
p.datname AS database, p.relid::regclass AS table, p.phase, a.query ,
pg_size_pretty(p.heap_blks_total * current_setting('block_size')::int) AS table_size,
pg_size_pretty(pg_total_relation_size(p.relid)) AS total_size,
pg_size_pretty(p.heap_blks_scanned * current_setting('block_size')::int) AS scanned,
pg_size_pretty(p.heap_blks_vacuumed * current_setting('block_size')::int) AS vacuumed,
round(100.0 * p.heap_blks_scanned / p.heap_blks_total, 1) AS scanned_pct,
round(100.0 * p.heap_blks_vacuumed / p.heap_blks_total, 1) AS vacuumed_pct,
p.index_vacuum_count,
p.max_dead_tuples as max_dead_tuples_per_cycle,
s.n_dead_tup as total_num_dead_tuples ,
ceil(s.n_dead_tup::float/p.max_dead_tuples::float) index_cycles_required
FROM pg_stat_progress_vacuum p JOIN pg_stat_activity a using (pid)
join pg_stat_all_tables s on s.relid = p.relid
ORDER BY now() - a.xact_start DESC;
\qecho <h3>Inactive replication slots order by age_xmin:</h3>
select *,age(xmin) age_xmin,age(catalog_xmin) age_catalog_xmin
from pg_replication_slots where active = false order by age(xmin) desc;
\qecho <h3>Active replication slots order by age_xmin:</h3>
select *,age(xmin) age_xmin,age(catalog_xmin) age_catalog_xmin
from pg_replication_slots
where active = true
order by age(xmin) desc;
\qecho <h3>Invalid databases count:</h3>
select count(*) FROM pg_database WHERE datconnlimit = '-2' ;
\qecho <h3>Invalid databases list:</h3>
SELECT * FROM pg_database WHERE datconnlimit = '-2' ;
\qecho <h3>Orphaned prepared transactions:</h3>
SELECT gid, prepared, owner, database, age(transaction) AS ag_xmin
FROM pg_prepared_xacts
ORDER BY age(transaction) DESC;
\qecho <h3>MAX XID held:</h3>
SELECT
(SELECT max(age(backend_xmin)) FROM pg_stat_activity) as oldest_running_xact,
(SELECT max(age(transaction)) FROM pg_prepared_xacts) as oldest_prepared_xact,
(SELECT max(age(xmin)) FROM pg_replication_slots) as oldest_replication_slot,
(SELECT max(age(backend_xmin))FROM pg_stat_replication)as oldest_replica_xact;
--\qecho <h3>XID Rate:</h3>
--SELECT max(age(datfrozenxid)) as xid1 FROM pg_database \gset
--select pg_sleep(60);
--SELECT max(age(datfrozenxid)) as xid2 FROM pg_database \gset
--select txid_current() current_txid \gset
--select (select :xid2 - :xid1)as XID_Rate,(2000000000-:current_txid) as Remaining_XIDs,
--(2000000000-:current_txid)/ ( select :xid2 - :xid1 ) /10/3600 hours_before_wraparound_prevention,
--(2000000000-:current_txid)/ ( select :xid2 - :xid1 ) /10/3600/24 days_before_wraparound_prevention
--;
\qecho <h3>Autovacuum , vacuum and maintenance_work_mem Parameters:</h3>
SELECT name,setting,source,sourcefile from pg_settings where name like '%vacuum%' order by 1;
SELECT name,setting,source,sourcefile from pg_settings where name ='maintenance_work_mem';
\qecho <h3>Which tables are currently eligible for autovacuum ? </h3>
WITH vbt AS (SELECT setting AS autovacuum_vacuum_threshold FROM pg_settings WHERE name = 'autovacuum_vacuum_threshold')
, vsf AS (SELECT setting AS autovacuum_vacuum_scale_factor FROM pg_settings WHERE name = 'autovacuum_vacuum_scale_factor')
, fma AS (SELECT setting AS autovacuum_freeze_max_age FROM pg_settings WHERE name = 'autovacuum_freeze_max_age')
, sto AS (select opt_oid, split_part(setting, '=', 1) as param, split_part(setting, '=', 2) as value from (select oid opt_oid, unnest(reloptions) setting from pg_class) opt)
SELECT
'"'||ns.nspname||'"."'||c.relname||'"' as relation
, pg_size_pretty(pg_table_size(c.oid)) as table_size
, age(relfrozenxid) as xid_age
, coalesce(cfma.value::float, autovacuum_freeze_max_age::float) autovacuum_freeze_max_age
, (coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float) + coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) * c.reltuples) as autovacuum_vacuum_tuples
, n_dead_tup as dead_tuples
FROM pg_class c join pg_namespace ns on ns.oid = c.relnamespace
join pg_stat_all_tables stat on stat.relid = c.oid
join vbt on (1=1) join vsf on (1=1) join fma on (1=1)
left join sto cvbt on cvbt.param = 'autovacuum_vacuum_threshold' and c.oid = cvbt.opt_oid
left join sto cvsf on cvsf.param = 'autovacuum_vacuum_scale_factor' and c.oid = cvsf.opt_oid
left join sto cfma on cfma.param = 'autovacuum_freeze_max_age' and c.oid = cfma.opt_oid
WHERE c.relkind = 'r' and nspname <> 'pg_catalog'
and (
age(relfrozenxid) >= coalesce(cfma.value::float, autovacuum_freeze_max_age::float)
or
coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float) + coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) * c.reltuples <= n_dead_tup
-- or 1 = 1
)
ORDER BY age(relfrozenxid) DESC ;
\qecho <h3>autovacuum progress per day:</h3>
\qecho <br>
\qecho <h3> Note:</h3>
\qecho <h4> This section presents the number of tables that have been vacuumed by the autovacuum , grouped by the date (in the format YYYY-MM-DD) of the last_autovacuum column. </h4>
\qecho <h4> If the value in the date column is NULL, it indicates that the corresponding value in the table count column represents the number of tables that the autovacuum did not vacuum. </h4>
\qecho <br>
select to_char(last_autovacuum, 'YYYY-MM-DD') as date , count(*) as table_count from pg_stat_all_tables group by to_char(last_autovacuum, 'YYYY-MM-DD') order by 1;
\qecho <h3>The most recent 20 tables that have been vacuumed by the autovacuum:</h3>
\qecho <h3> Note:</h3>
\qecho <h4> - If the value in the last_autovacuum column is NULL, it indicates that the autovacuum did not vacuum this table. </h4>
\qecho <br>
select schemaname as schema_name,relname as table_name,n_live_tup, n_tup_upd, n_tup_del, n_dead_tup,
last_vacuum, last_autovacuum, last_analyze, last_autoanalyze
from pg_stat_all_tables
order by last_autovacuum desc limit 20 ;
\qecho <h3>Top-20 tables order by xid age:</h3>
-- this need to be run in each DB in the instance
SELECT c.oid::regclass as relation_name,
greatest(age(c.relfrozenxid),age(t.relfrozenxid)) as age,
pg_size_pretty(pg_table_size(c.oid)) as table_size,
c.relkind
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
WHERE c.relkind in ('r', 't','m')
order by 2 desc limit 20;
\qecho <h3>Index Inforamtion for Top-20 tables order by xid age:</h3>
SELECT schemaname,relname AS tablename,
indexrelname AS indexname,
idx_scan ,
pg_relation_size(indexrelid) as index_size,
pg_size_pretty(pg_relation_size(indexrelid)) AS pretty_index_size
FROM pg_catalog.pg_stat_all_indexes
WHERE relname in (select relation_name::text from (SELECT c.oid::regclass as relation_name,
greatest(age(c.relfrozenxid),age(t.relfrozenxid)) as age,
pg_size_pretty(pg_table_size(c.oid)) as table_size,
c.relkind
FROM pg_class c
LEFT JOIN pg_class t ON c.reltoastrelid = t.oid
WHERE c.relkind in ('r', 't','m')
order by 2 desc limit 20) as r1 )
order by 2,4 ;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Table_Size - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Table_Size"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Table Size</b></font><hr align="left" width="460">
\qecho <br>
\qecho <h3>Table Size order by schema name and table size:</h3>
\qecho <br>
\qecho <details>
SELECT *, pg_size_pretty(total_bytes) AS TOTAL_PRETTY
, pg_size_pretty(index_bytes) AS INDEX_PRETTY
, pg_size_pretty(toast_bytes) AS TOAST_PRETTY
, pg_size_pretty(table_bytes) AS TABLE_PRETTY
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS TABLE_BYTES FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples::bigint AS ROW_ESTIMATE
, pg_total_relation_size(c.oid) AS TOTAL_BYTES
, pg_indexes_size(c.oid) AS INDEX_BYTES
, pg_total_relation_size(reltoastrelid) AS TOAST_BYTES
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a
order by 2,8 desc;
\qecho </details>
\qecho <br>
\qecho <h3>biggest 50 tables in the DB: </h3>
\qecho <br>
\qecho <h3> Note:</h3>
\qecho <h4> - If the table has never yet been vacuumed or analyzed, ROW_ESTIMATE column (pg_class.reltuples) contains -1 indicating that the row count is unknown. </h4>
\qecho <details>
SELECT *, pg_size_pretty(total_bytes) AS TOTAL_PRETTY
, pg_size_pretty(index_bytes) AS INDEX_PRETTY
, pg_size_pretty(toast_bytes) AS TOAST_PRETTY
, pg_size_pretty(table_bytes) AS TABLE_PRETTY
FROM (
SELECT *, total_bytes-index_bytes-COALESCE(toast_bytes,0) AS TABLE_BYTES FROM (
SELECT c.oid,nspname AS table_schema, relname AS TABLE_NAME
, c.reltuples::bigint AS ROW_ESTIMATE
, pg_total_relation_size(c.oid) AS TOTAL_BYTES
, pg_indexes_size(c.oid) AS INDEX_BYTES
, pg_total_relation_size(reltoastrelid) AS TOAST_BYTES
FROM pg_class c
LEFT JOIN pg_namespace n ON n.oid = c.relnamespace
WHERE relkind = 'r'
) a
) a
order by 5 desc
LIMIT 50;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - index_Size - |
-- +----------------------------------------------------------------------------+
\qecho <a name="index_Size"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Index_Size</b></font><hr align="left" width="460">
\qecho <br>
\qecho <h3>Index Size order by schema name and table name :</h3>
\qecho <br>
\qecho <details>
SELECT
schemaname,relname as "Table",
indexrelname AS indexname,
pg_relation_size(indexrelid),
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_catalog.pg_statio_all_indexes ORDER BY 1,2 desc ;
\qecho </details>
\qecho <br>
\qecho <h3>biggest 50 Index in the DB :</h3>
\qecho <br>
\qecho <details>
SELECT
schemaname as schema_name,relname as "Table",
indexrelname AS indexname,
pg_relation_size(indexrelid),
pg_size_pretty(pg_relation_size(indexrelid)) AS index_size
FROM pg_catalog.pg_statio_all_indexes ORDER BY 4 desc limit 50;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - vacuum and Statistics - |
-- +----------------------------------------------------------------------------+
\qecho <a name="vacuum_Statistics"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Vacuum & Statistics</b></font><hr align="left" width="460">
\qecho <br>
\qecho <h3>Autovacuum Parameters:</h3>
\qecho <br>
\qecho <details>
SELECT * from pg_settings where category like 'Autovacuum';
\qecho <br>
SELECT * FROM pg_settings where name in ('rds.force_autovacuum_logging_level','log_autovacuum_min_duration') order by category;
\qecho <br>
\qecho </details>
\qecho <br>
\qecho <h3>current running autovacuum process:</h3>
\qecho <br>
\qecho <details>
SELECT datname,usename,state,query,
now() - pg_stat_activity.query_start AS duration,
wait_event from pg_stat_activity where query ~ '^autovacuum:' order by 5;
\qecho <br>
\qecho </details>
\qecho <br>
\qecho <h3>current running vacuum process:</h3>
\qecho <br>
\qecho <details>
SELECT datname,usename,state,query,
now() - pg_stat_activity.query_start AS duration,
wait_event from pg_stat_activity where query ~* '\A\s*vacuum\M' order by 5;
\qecho </details>
\qecho <br>
-- Whenever VACUUM is running, the pg_stat_progress_vacuum view will contain one row for each backend (including autovacuum worker processes) that is currently vacuuming (vacuum porgress)
\qecho <h3>Vacuum progress:</h3>
\qecho <br>
\qecho <details>
SELECT p.pid, now() - a.xact_start AS duration, coalesce(wait_event_type ||'.'|| wait_event, 'f') AS waiting, CASE WHEN a.query ~ '^autovacuum.*to prevent wraparound' THEN 'wraparound' WHEN a.query ~ '^vacuum' THEN 'user' ELSE 'regular' END AS mode, p.datname AS database, p.relid::regclass AS table, p.phase, pg_size_pretty(p.heap_blks_total * current_setting('block_size')::int) AS table_size, pg_size_pretty(pg_total_relation_size(relid)) AS total_size, pg_size_pretty(p.heap_blks_scanned * current_setting('block_size')::int) AS scanned, pg_size_pretty(p.heap_blks_vacuumed * current_setting('block_size')::int) AS vacuumed, round(100.0 * p.heap_blks_scanned / p.heap_blks_total, 1) AS scanned_pct, round(100.0 * p.heap_blks_vacuumed / p.heap_blks_total, 1) AS vacuumed_pct, p.index_vacuum_count, round(100.0 * p.num_dead_tuples / p.max_dead_tuples,1) AS dead_pct FROM pg_stat_progress_vacuum p JOIN pg_stat_activity a using (pid) ORDER BY now() - a.xact_start DESC;
\qecho </details>
\qecho <br>
\qecho <h3>Autovacuum progress per day: </h3>
\qecho <br>
\qecho <h3> Note:</h3>
\qecho <h4> This section presents the number of tables that have been vacuumed by the autovacuum , grouped by the date (in the format YYYY-MM-DD) of the last_autovacuum column. </h4>
\qecho <h4> If the value in the date column is NULL, it indicates that the corresponding value in the table count column represents the number of tables that the autovacuum did not vacuum. </h4>
\qecho <br>
\qecho <details>
select to_char(last_autovacuum, 'YYYY-MM-DD') as date , count(*) as table_count from pg_stat_all_tables group by to_char(last_autovacuum, 'YYYY-MM-DD') order by 1;
\qecho </details>
\qecho <br>
\qecho <h3>Autoanalyze progress per day: </h3>
\qecho <br>
\qecho <h3> Note:</h3>
\qecho <h4> This section presents the number of tables that have been analyzed by the autoanalyze , grouped by the date (in the format YYYY-MM-DD) of the last_autoanalyze column. </h4>
\qecho <h4> If the value in the date column is NULL, it indicates that the corresponding value in the table count column represents the number of tables that the autoanalyze did not analyze. </h4>
\qecho <br>
\qecho <details>
select to_char(last_autoanalyze, 'YYYY-MM-DD') as date , count(*) as table_count from pg_stat_all_tables group by to_char(last_autoanalyze, 'YYYY-MM-DD') order by 1;
\qecho </details>
\qecho <br>
--Which tables are currently eligible for autovacuum based on curret parameters
\qecho <h3>Which tables are currently eligible for autovacuum based on curret parameters : </h3>
\qecho <br>
\qecho <details>
WITH vbt AS (SELECT setting AS autovacuum_vacuum_threshold FROM pg_settings WHERE name = 'autovacuum_vacuum_threshold')
, vsf AS (SELECT setting AS autovacuum_vacuum_scale_factor FROM pg_settings WHERE name = 'autovacuum_vacuum_scale_factor')
, fma AS (SELECT setting AS autovacuum_freeze_max_age FROM pg_settings WHERE name = 'autovacuum_freeze_max_age')
, sto AS (select opt_oid, split_part(setting, '=', 1) as param, split_part(setting, '=', 2) as value from (select oid opt_oid, unnest(reloptions) setting from pg_class) opt)
SELECT
'"'||ns.nspname||'"."'||c.relname||'"' as relation
, pg_size_pretty(pg_table_size(c.oid)) as table_size
, age(relfrozenxid) as xid_age
, coalesce(cfma.value::float, autovacuum_freeze_max_age::float) autovacuum_freeze_max_age
, (coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float) + coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) * c.reltuples) as autovacuum_vacuum_tuples
, n_dead_tup as dead_tuples
FROM pg_class c join pg_namespace ns on ns.oid = c.relnamespace
join pg_stat_all_tables stat on stat.relid = c.oid
join vbt on (1=1) join vsf on (1=1) join fma on (1=1)
left join sto cvbt on cvbt.param = 'autovacuum_vacuum_threshold' and c.oid = cvbt.opt_oid
left join sto cvsf on cvsf.param = 'autovacuum_vacuum_scale_factor' and c.oid = cvsf.opt_oid
left join sto cfma on cfma.param = 'autovacuum_freeze_max_age' and c.oid = cfma.opt_oid
WHERE c.relkind = 'r' and nspname <> 'pg_catalog'
and (
age(relfrozenxid) >= coalesce(cfma.value::float, autovacuum_freeze_max_age::float)
or
coalesce(cvbt.value::float, autovacuum_vacuum_threshold::float) + coalesce(cvsf.value::float,autovacuum_vacuum_scale_factor::float) * c.reltuples <= n_dead_tup
-- or 1 = 1
)
ORDER BY age(relfrozenxid) DESC LIMIT 50;
\qecho </details>
\qecho <br>
-- check if the statistics collector is enabled (track_counts is on)
\qecho <h3>Check if the statistics collector is enabled (track_counts is on) : </h3>
\qecho <br>
\qecho <details>
SELECT name, setting FROM pg_settings WHERE name='track_counts';
\qecho </details>
\qecho <br>
-- to check the number of dead rows for the top 50 table
\qecho <h3>Top 50 tables based on number of dead tuples: </h3>
\qecho <br>
\qecho <details>
select schemaname as schema_name,relname AS table_name,n_live_tup, n_tup_upd, n_tup_del, n_dead_tup, last_vacuum, last_autovacuum, last_analyze, last_autoanalyze from pg_stat_all_tables order by n_dead_tup desc limit 50;
\qecho </details>
\qecho <br>
\qecho <h3>Tables have more than 20% dead rows :</h3>
\qecho <br>
\qecho <details>
select schemaname,relname , last_vacuum,last_autovacuum,n_live_tup,n_dead_tup , trunc((n_dead_tup::numeric/nullif(n_live_tup+n_dead_tup,0))* 100,2) as "n_dead_tup_%" from pg_stat_user_tables where n_dead_tup::float/nullif(n_live_tup+n_dead_tup,0) >.2 order by n_live_tup desc ;
\qecho </details>
\qecho <br>
\qecho <h3>pg_stat_all_tables : </h3>
\qecho <br>
\qecho <details>
select relname,schemaname,last_vacuum,last_autovacuum,last_analyze,last_autoanalyze,vacuum_count,autovacuum_count,analyze_count,autoanalyze_count from pg_stat_all_tables where schemaname not in ('pg_catalog','pg_toast') order by 2;
\qecho </details>
\qecho <br>
\qecho <h3>pg_stat_all_tables order by autovacuum_count : </h3>
\qecho <br>
\qecho <details>
select relname,schemaname,last_vacuum,last_autovacuum,last_analyze,last_autoanalyze,vacuum_count,autovacuum_count,analyze_count,autoanalyze_count from pg_stat_all_tables where schemaname not in ('pg_catalog','pg_toast') order by autovacuum_count desc;
\qecho </details>
\qecho <br>
\qecho <h3>pg_stat_all_tables order by autoanalyze_count: </h3>
\qecho <br>
\qecho <details>
select relname,schemaname,last_vacuum,last_autovacuum,last_analyze,last_autoanalyze,vacuum_count,autovacuum_count,analyze_count,autoanalyze_count from pg_stat_all_tables where schemaname not in ('pg_catalog','pg_toast') order by autoanalyze_count desc;
\qecho </details>
\qecho <br>
\qecho <h3>tables without auto analyze : </h3>
\qecho <br>
\qecho <details>
select count(*) from pg_stat_all_tables where autoanalyze_count = 0 ;
\qecho <br>
select relname,schemaname,last_vacuum,last_autovacuum,autovacuum_count,autoanalyze_count,last_analyze,last_autoanalyze,n_mod_since_analyze from pg_stat_all_tables where autoanalyze_count = 0 order by 2;
\qecho </details>
\qecho <br>
\qecho <h3>tables without auto vacuum : </h3>
\qecho <br>
\qecho <details>
select count(*) from pg_stat_all_tables where autovacuum_count = 0 ;
\qecho <br>
select relname,schemaname,last_vacuum,last_autovacuum,autovacuum_count,autoanalyze_count,last_analyze,last_autoanalyze,n_dead_tup from pg_stat_all_tables where autovacuum_count = 0 order by 2;
\qecho </details>
\qecho <br>
\qecho <h3>Tables that have not been manually analyzed :</h3>
\qecho <br>
\qecho <details>
select count (analyze_count) from pg_stat_all_tables where analyze_count = 0;
\qecho <br>
select relname,schemaname,last_vacuum,vacuum_count,last_autovacuum,autovacuum_count,last_autoanalyze,autoanalyze_count,last_analyze,analyze_count from pg_stat_all_tables where analyze_count = 0;
\qecho </details>
\qecho <br>
\qecho <h3>tables without auto analyze,auto vacuum,vacuum and analyze : </h3>
\qecho <br>
\qecho <details>
select count (*) from pg_stat_all_tables where autoanalyze_count = 0 and autovacuum_count = 0 and analyze_count = 0 and vacuum_count=0 ;
\qecho <br>
select relname,schemaname,last_vacuum,vacuum_count,last_autovacuum,autovacuum_count,last_autoanalyze,autoanalyze_count,last_analyze,analyze_count from pg_stat_all_tables where autoanalyze_count = 0 and autovacuum_count = 0 and analyze_count = 0 and vacuum_count=0 ;
\qecho </details>
\qecho <br>
-- to show tables that have specific table-level parameters set
\qecho <h3>Tables that have specific table-level parameters set : </h3>
\qecho <br>
\qecho <details>
select relname, reloptions from pg_class where reloptions is not null;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Extensions - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Extensions"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Extensions</b></font><hr align="left" width="460">
\qecho <br>
\qecho <details>
\qecho <h3>shared_preload_libraries parameter: </h3>
show shared_preload_libraries;
\qecho <br>
\qecho <h3>Installed extension : </h3>
SELECT e.extname AS "Extension Name", e.extversion AS "Version", n.nspname AS "Schema",pg_get_userbyid(e.extowner) as Owner, c.description AS "Description" , e.extrelocatable as "relocatable to another schema", e.extconfig ,e.extcondition
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
ORDER BY 1;
\qecho <br>
\qecho <h3>Available Extension versions that are available to upgrade the installed Extension: </h3>
select b.name as extension_name , b.version as version ,b.installed as installed
from
(SELECT extname ,extversion FROM pg_extension) a ,
(SELECT name ,version ,installed FROM pg_available_extension_versions where name in (SELECT extname FROM pg_extension)) b
where a.extname = b.name
and b.version > a.extversion
order by b.name , b.version;
\qecho <br>
\qecho <h3>Latest Extension version that is available to upgrade the installed Extension: </h3>
select name as extension_name , max(version) as latest_version
from
(select b.name , b.version ,b.installed
from
(SELECT extname ,extversion FROM pg_extension) a ,
(SELECT name ,version ,installed FROM pg_available_extension_versions where name in (SELECT extname FROM pg_extension)) b
where a.extname = b.name
and b.version > a.extversion
order by b.name , b.version ) e
group by name ;
\qecho </details>
\qecho <br>
\qecho <h3>Available extensions: </h3>
\qecho <br>
\qecho <details>
\qecho <h4> pg_available_extension_versions : </h4>
select * from pg_available_extension_versions order by name,version;
\qecho <br>
\qecho <h4> pg_available_extensions : </h4>
select * from pg_available_extensions order by installed_version;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Memory setting - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Memory_setting"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Memory setting</b></font><hr align="left" width="460">
\qecho <br>
\qecho <details>
(
select name as parameter_name , setting , unit, (setting::BIGINT/1024)::BIGINT as "size_MB" ,(setting::BIGINT/1024/1024)::BIGINT as "size_GB" , pg_size_pretty((setting::BIGINT*1024)::BIGINT)
from pg_settings where name in ('work_mem','maintenance_work_mem')
)
UNION ALL
(
select name as parameter_name, setting , unit , (((setting::BIGINT)*8)/1024)::BIGINT as "size_MB" ,(((setting::BIGINT)*8)/1024/1024)::BIGINT as "size_GB", pg_size_pretty((((setting::BIGINT)*8)*1024)::BIGINT)
from pg_settings where name in ('shared_buffers','wal_buffers','effective_cache_size','temp_buffers')
) order by 4 desc;
select name as parameter_name,setting FROM pg_catalog.pg_settings WHERE name in ('huge_pages' ) ;
\qecho <br>
\qecho cach read hit for the whole instance
select
round((sum(blks_hit)::numeric / (sum(blks_hit) + sum(blks_read)::numeric))*100,2) as cache_read_hit_percentage
from pg_stat_database ;
\qecho <br>
\qecho cach read hit per Database
select datname as database_name,
round((blks_hit::numeric / (blks_hit + blks_read)::numeric)*100,2) as cache_read_hit_percentage
from pg_stat_database
where blks_hit + blks_read > 0
and datname is not null
order by 2 desc;
\qecho </details>
\qecho <br>
\qecho cach read hit per table
\qecho <br>
\qecho <details>
SELECT schemaname,relname as table_name,
round((heap_blks_hit::numeric / (heap_blks_hit + heap_blks_read)::numeric)*100,2) as read_hit_percentage
FROM
pg_statio_all_tables
where heap_blks_hit + heap_blks_read > 0
and schemaname not in ('pg_catalog','information_schema')
order by 3;
\qecho </details>
\qecho <br>
\qecho cach read hit per index
\qecho <br>
\qecho <details>
SELECT schemaname,relname as table_name,indexrelname as index_name ,
round((idx_blks_hit::numeric / (idx_blks_hit + idx_blks_read)::numeric)*100,2) as read_hit_percentage
FROM
pg_statio_all_indexes
where idx_blks_hit + idx_blks_read > 0
and schemaname not in ('pg_catalog','information_schema')
order by 4;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - pg_stat_statements extension - |
-- +----------------------------------------------------------------------------+
\qecho <a name="pg_stat_statements_extension"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>pg_stat_statements extension</b></font><hr align="left" width="460">
\qecho <br>
select count(*) > 0 is_pg_stat_statements_enabled FROM pg_catalog.pg_extension where extname = 'pg_stat_statements' \gset
\if :is_pg_stat_statements_enabled
\qecho <h3> pg_stat_statements installed version: </h3>
\qecho <br>
\qecho <details>
SELECT e.extname AS "Extension Name", e.extversion AS "Version", n.nspname AS "Schema",pg_get_userbyid(e.extowner) as Owner, c.description AS "Description" , e.extrelocatable as "relocatable to another schema", e.extconfig ,e.extcondition
FROM pg_catalog.pg_extension e LEFT JOIN pg_catalog.pg_namespace n ON n.oid = e.extnamespace LEFT JOIN pg_catalog.pg_description c ON c.objoid = e.oid AND c.classoid = 'pg_catalog.pg_extension'::pg_catalog.regclass
where e.extname = 'pg_stat_statements';
\qecho <br>
\qecho <h3>Parameters values: </h3>
-- pg_stat_statements extension configuration
select name as parameter_name, setting from pg_settings where name in ('pg_stat_statements.track','pg_stat_statements.track_utility','pg_stat_statements.save'
,'pg_stat_statements.max','shared_preload_libraries');
\qecho <br>
\qecho <h3>Available versions that are available to upgrade: </h3>
select * from
(
select b.name as extension_name , b.version as version ,b.installed as installed
from
(SELECT extname ,extversion FROM pg_extension) a ,
(SELECT name ,version ,installed FROM pg_available_extension_versions where name in (SELECT extname FROM pg_extension)) b
where a.extname = b.name
and b.version > a.extversion
order by b.name , b.version
) as r
where r.extension_name='pg_stat_statements';
;
\qecho <br>
\qecho <h3>Latest Extension version that is available to upgrade: </h3>
select * from
(
select name as extension_name , max(version) as latest_version
from
(select b.name , b.version ,b.installed
from
(SELECT extname ,extversion FROM pg_extension) a ,
(SELECT name ,version ,installed FROM pg_available_extension_versions where name in (SELECT extname FROM pg_extension)) b
where a.extname = b.name
and b.version > a.extversion
order by b.name , b.version ) e
group by name
) as r
where r.extension_name='pg_stat_statements';
;
\qecho </details>
\qecho <br>
\qecho <h3> Top SQL order by total_time: </h3>
\qecho <br>
\qecho <details>
--Top SQL order by total_time
select queryid,substring(query,1,60) as query , calls,
round(total_time::numeric, 2) as total_time_Msec,
round((total_time::numeric/1000), 2) as total_time_sec,
round(mean_time::numeric,2) as avg_time_Msec,
round((mean_time::numeric/1000),2) as avg_time_sec,
round(stddev_time::numeric, 2) as standard_deviation_time_Msec,
round((stddev_time::numeric/1000), 2) as standard_deviation_time_sec,
round(rows::numeric/calls,2) rows_per_exec,
round((100 * total_time / sum(total_time) over ())::numeric, 4) as percent
from pg_stat_statements
order by total_time_Msec desc limit 20;
\qecho </details>
\qecho <br>
\qecho <h3> Top SQL order by avg_time: </h3>
\qecho <br>
\qecho <details>
--Top SQL order by avg_time
select queryid,substring(query,1,60) as query , calls,
round(total_time::numeric, 2) as total_time_Msec,
round((total_time::numeric/1000), 2) as total_time_sec,
round(mean_time::numeric,2) as avg_time_Msec,
round((mean_time::numeric/1000),2) as avg_time_sec,
round(stddev_time::numeric, 2) as standard_deviation_time_Msec,
round((stddev_time::numeric/1000), 2) as standard_deviation_time_sec,
round(rows::numeric/calls,2) rows_per_exec,
round((100 * total_time / sum(total_time) over ())::numeric, 4) as percent
from pg_stat_statements
order by avg_time_Msec desc limit 20;
\qecho </details>
\qecho <br>
\qecho <h3> Top SQL order by percent of total DB time percent: </h3>
\qecho <br>
\qecho <details>
--Top SQL order by percent of total DB time
select queryid,substring(query,1,60) as query , calls,
round(total_time::numeric, 2) as total_time_Msec,
round((total_time::numeric/1000), 2) as total_time_sec,
round(mean_time::numeric,2) as avg_time_Msec,
round((mean_time::numeric/1000),2) as avg_time_sec,
round(stddev_time::numeric, 2) as standard_deviation_time_Msec,
round((stddev_time::numeric/1000), 2) as standard_deviation_time_sec,
round(rows::numeric/calls,2) rows_per_exec,
round((100 * total_time / sum(total_time) over ())::numeric, 4) as percent
from pg_stat_statements
order by percent desc limit 20;
\qecho </details>
\qecho <br>
\qecho <h3> Top SQL order by number of execution (CALLs): </h3>
\qecho <br>
\qecho <details>
--Top SQL order by number of execution (CALLs)
select queryid,substring(query,1,60) as query , calls,
round(total_time::numeric, 2) as total_time_Msec,
round((total_time::numeric/1000), 2) as total_time_sec,
round(mean_time::numeric,2) as avg_time_Msec,
round((mean_time::numeric/1000),2) as avg_time_sec,
round(stddev_time::numeric, 2) as standard_deviation_time_Msec,
round((stddev_time::numeric/1000), 2) as standard_deviation_time_sec,
round(rows::numeric/calls,2) rows_per_exec,
round((100 * total_time / sum(total_time) over ())::numeric, 4) as percent
from pg_stat_statements
order by calls desc limit 20;
\qecho </details>
\qecho <br>
\qecho <h3> Top SQL order by shared blocks read (physical reads): </h3>
\qecho <br>
\qecho <details>
--Top SQL order by shared blocks read (physical reads)
select queryid, substring(query,1,60) as query , calls,
round(total_time::numeric, 2) as total_time_Msec,
round((total_time::numeric/1000), 2) as total_time_sec,
round(mean_time::numeric,2) as avg_time_Msec,
round((mean_time::numeric/1000),2) as avg_time_sec,
round(stddev_time::numeric, 2) as standard_deviation_time_Msec,
round((stddev_time::numeric/1000), 2) as standard_deviation_time_sec,
round(rows::numeric/calls,2) rows_per_exec,
round((100 * total_time / sum(total_time) over ())::numeric, 4) as percent,
shared_blks_read
from pg_stat_statements
order by shared_blks_read desc limit 20;
\qecho </details>
\else
\if yes
\qecho 'pg_stat_statements extension is not installed'
\endif
\endif
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Users_Roles_Info - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Users_Roles_Info"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Users & Roles Info</b></font><hr align="left" width="460">
\qecho <br>
\qecho <details>
\du
\qecho <br>
-- list of per database role settings (settings set at the role level)
\drds
\qecho <br>
select * FROM pg_user;
\qecho </details>
\qecho <center>[<a class="noLink" href="#top">Top</a>]</center><p>
-- +----------------------------------------------------------------------------+
-- | - Schema_Info - |
-- +----------------------------------------------------------------------------+
\qecho <a name="Schema_Info"></a>
\qecho <font size="+2" face="Arial,Helvetica,Geneva,sans-serif" color="#16191f"><b>Schema Info</b></font><hr align="left" width="460">
\qecho <br>
\qecho <h3>List of schemas:</h3>
\qecho <br>
\qecho <details>
\dn+
\qecho </details>
\qecho <br>
\qecho <h3>Total objects Count in the database:</h3>
\qecho <br>
\qecho <details>
select count (*) from pg_catalog.pg_class;