• Home
  • Services
    • DBA Support
    • DBA Consultancy Services
    • PostgreSQL Support
    • Website Maintenance
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum Database

    Greenplum Database

    $2,000.00 $1,500.00
    Read More
  • Company
    • FAQs
    • About Us
    • Contact
  • Events
  • Portfolio
  • Blogs
    • Blog – RayaFeeL
    • Blog – PostgreSQL Support
    • Blog – PostgreSQL Migration
    • Blog – All DB’s
    • Blog – Linux
    • Blog – Medical Coding
      • Cart

        0

    Have any question?
    (+91)8838953252
    ITsupport@rayafeel.com
    RegisterLogin
    RayaFeeL
    • Home
    • Services
      • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
      • Website Maintenance
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00
      Read More
    • Company
      • FAQs
      • About Us
      • Contact
    • Events
    • Portfolio
    • Blogs
      • Blog – RayaFeeL
      • Blog – PostgreSQL Support
      • Blog – PostgreSQL Migration
      • Blog – All DB’s
      • Blog – Linux
      • Blog – Medical Coding
        • Cart

          0

      Blog

      • Home
      • Blog
      • Blog
      • Simple & Best Postgres Free Monitoring Script

      Simple & Best Postgres Free Monitoring Script

      • Posted by Nijamutheen J
      • Categories Blog
      • Date January 16, 2019
      • Comments 0 comment

      Following steps will help you to improve the Postgresql database server performance:
      First  you have to issue the top command then see the process which one process is taking more CPU utilization then note that PID, if that process is postgres process means use this script you can find out query and their status and timing else you can use following query to finding query

      [root@tutorialdba.com]# cat monitor.sh
      echo " "
      echo " "
      HOSTNAME="101.0.1.123"
      PSQL="/opt/edb/as9.6/bin/psql"
      PORT=5444
      DB="edb"
      USER="enterprisedb"
      export PGPASSWORD="tcs@12345" 
      
      echo "ENTER THE TIME IN MINUTES ?"
      echo "For example if you give 10 means it will display "
      echo "queries which is running more than 10 minutes"
      echo "0 means will display all queries:"
      echo ""
      read time
      echo " "
      echo " "
      
      
      echo "WHAT ARE THE QUERY IS RUNING MORE THAN $time MINUTES"
      $PSQL -d $DB -U $USER -p $PORT -h $HOSTNAME <<EOF
      \pset format wrapped
      SELECT pid, now() - query_start as "runtime", usename, datname, state, query
        FROM  pg_stat_activity
        WHERE now() - query_start > '$time minutes'::interval
       ORDER BY runtime DESC;
      
      EOF
      echo "*********************COMPLETED 1****************************** "
      echo " "
      echo " "
      echo " "
      
      echo "CHECKING dISK SPACE:"
              df -h
      
      echo "*********************COMPLETED 2****************************** "
      echo " "
      echo " "
      echo " "
      
      
      
      echo "CHECKING RAM USAGE:"
             free -h
      
      echo "*********************COMPLETED 3****************************** "
      echo " "
      echo " "
      echo " "
      
      
      
      echo "ENTER THE BLOATED COUNT ? "
      echo "For example if you give 1000  means it will display the table count"
      echo "how many table having more than 1000 dead tubles that database:"
      echo ""
      read count
      echo " "
      echo " "
      
      
      
      echo "HOW MANY TABLES HAVING MORE THAN $count BLOATED TABLE :"
      
      $PSQL -d $DB -U $USER -p $PORT -h $HOSTNAME  <<EOF
      \c LC
      select count(*) from pg_stat_all_tables where n_dead_tup > $count;
      EOF
      
      echo " "
      
      $PSQL -d $DB -U $USER -p $PORT -h $HOSTNAME  <<EOF
      \c DC
      select count(*) from pg_stat_all_tables where n_dead_tup >$count;
      EOF
      
      echo "*********************COMPLETED 4****************************** "
      echo " "
      echo " "
      echo " "
      
      
      echo "ENTER THE BLOATED COUNT AGAING ? "
      echo "it will give schema,table name and dead tuble count including "
      echo "with vacuum & analyze script you can copy and run it to the   "
      echo "SQL prompt what are the table having more Bloated table "
      echo ""
      read count1
      echo ""
      echo ""
      
      echo "WHAT ARE THE TABLE HAVING MORE THAN $count1 BLOATED TABLE : "
      
      $PSQL -d $DB -U $USER -p $PORT -h $HOSTNAME  <<EOF
      \c LC
      select 'vacuum ANALYZE '||schemaname||'.'||relname||';' from pg_stat_all_tables where n_dead_tup >$count1;
      select schemaname,relname,n_dead_tup from pg_stat_all_tables where n_dead_tup >$count1;
      EOF
      
      echo " "
      
      $PSQL -d $DB -U $USER -p $PORT -h $HOSTNAME  <<EOF
      \c DC
      select 'vacuum ANALYZE '||schemaname||'.'||relname||';' from pg_stat_all_tables where n_dead_tup >$count1;
      select schemaname,relname,n_dead_tup from pg_stat_all_tables where n_dead_tup >$count1;
      EOF
      
      echo "*********************COMPLETED 5****************************** "
      echo " "
      echo " "
      echo " "
      echo " "
      
      SAMPLE OUTPUT:
      
      [root@ip-10-0-1-109 ec2-user]# sh monitor.sh
      
      
      ENTER THE TIME IN MINUTES ?
      For example if you give 10 means it will display
      queries which is running more than 10 minutes
      0 means will display all queries:
      
      4
      
      
      WHAT ARE THE QUERY IS RUNING MORE THAN 4 MINUTES
      Output format is wrapped.
        pid  |     runtime     | usename  |    datname     | state |   query
      -------+-----------------+----------+----------------+-------+------------
       15832 | 00:05:57.796746 | hr       | LC             | idle  | unlisten *
       15465 | 00:05:35.23556  | ehis     | LC 	     | idle  | unlisten *
       15113 | 00:05:34.880537 | billing  | LC	     | idle  | unlisten *
       15918 | 00:04:50.816381 | security | LC	     | idle  | unlisten *
       15930 | 00:04:48.148488 | billing  | DC 	     | idle  | unlisten *
      ...
      ..
      (10 rows)
      
      *********************COMPLETED 1******************************
      
      
      
      CHECKING dISK SPACE:
      Filesystem      Size  Used Avail Use% Mounted on
      /dev/xvda2      250G  2.0G  249G   1% /
      devtmpfs         16G     0   16G   0% /dev
      tmpfs            16G  8.0K   16G   1% /dev/shm
      tmpfs            16G  177M   16G   2% /run
      tmpfs            16G     0   16G   0% /sys/fs/cgroup
      /dev/xvdb       200G   33G  168G  17% /opt
      tmpfs           3.2G     0  3.2G   0% /run/user/1000
      *********************COMPLETED 2******************************
      
      
      
      CHECKING RAM USAGE:
                    total        used        free      shared  buff/cache   available
      Mem:            31G        717M         27G        1.3G        2.7G         28G
      Swap:            0B          0B          0B
      *********************COMPLETED 3******************************
      
      
      
      ENTER THE BLOATED COUNT ?
      For example if you give 1000  means it will display the table count
      how many table having more than 1000 dead tubles that database:
      
      1500
      
      
      HOW MANY TABLES HAVING MORE THAN 1500 BLOATED TABLE :
      You are now connected to database "LC" as user "enterprisedb".
       count
      -------
           3
      (1 row)
      
      
      You are now connected to database "DC" as user "enterprisedb".
       count
      -------
           1
      (1 row)
      
      *********************COMPLETED 4******************************
      
      
      
      ENTER THE BLOATED COUNT AGAING ?
      it will give schema,table name and dead tuble count including
      with vacuum & analyze script you can copy and run it to the
      SQL prompt what are the table having more Bloated table
      
      2000
      
      
      WHAT ARE THE TABLE HAVING MORE THAN 2000 BLOATED TABLE :
      You are now connected to database "LC" as user "enterprisedb".
                      ?column?
      -----------------------------------------
       vacuum ANALYZE pg_catalog.pg_attribute;
       vacuum ANALYZE pg_catalog.pg_depend;
       vacuum ANALYZE pg_catalog.pg_shdepend;
      (3 rows)
      
       schemaname |   relname    | n_dead_tup
      ------------+--------------+------------
       pg_catalog | pg_attribute |      21218
       pg_catalog | pg_depend    |       9869
       pg_catalog | pg_shdepend  |       2730
      (3 rows)
      
      
      You are now connected to database "DC" as user "enterprisedb".
                      ?column?
      ----------------------------------------
       vacuum ANALYZE pg_catalog.pg_shdepend;
      (1 row)
      
       schemaname |   relname   | n_dead_tup
      ------------+-------------+------------
       pg_catalog | pg_shdepend |       2730
      (1 row)
      
      *********************COMPLETED 5******************************
      
      
      
      
      
      vacuum and analyze the catalog table which is having more dead tuble, Here pg_attribute havig more dead tuble that is what am chose it and vacuum and analyze that.
      
      LC=# vacuum ANALYZE pg_catalog.pg_attribute;
      VACUUM

      • Share:
      Admin bar avatar
      Nijamutheen J

      Nijamutheen J 7+ years of experience in PostgreSQL, Linux admin , web hosting - apache server , Oracle ,mySQL, Mriadb, vertica DB & Server security administrator

      My updated resume is https://www.tutorialdba.com/p/hi-i-have-4.html

      Previous post

      Script For Finding slow Running Query and Most CPU Utilization Query Using Top command PID
      January 16, 2019

      Next post

      Blog Writing Services About yours's product
      January 28, 2019

      Leave A Reply Cancel reply

      You must be logged in to post a comment.

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft


      Search

      ADVERTISEMENT

      Latest Posts

      PostgreSQL Patching version 9, 10,11
      10Oct2019
      Tools for PostgreSQL
      16Sep2019
      Postgres user creation and restrict DDL & database access
      13Sep2019
      PostgreSQL SSL Setup
      07Sep2019
      How to DELETE current XLOG / WAL LOG in postgresql database ?
      19Aug2019

      Latest Courses

      PostgreSQL Database

      PostgreSQL Database

      $600.00 $500.00
      Greenplum Database

      Greenplum Database

      $2,000.00 $1,500.00

      Preview Course

      Free

      Recent Forum Topics

      • thought behind whiteboard activity
      • Are you going to take your first ste
      • How to start working on an application?
      • please let me know pre requirements to increase work_mem
      • how to copy some data in one table to another table in postgres

      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Contact
      • Our Team
      • Blog

      COURSES

      • List Of Course
      • Become An Instructor
      • Events
      • Postgres Support Blog

      Support

      • DBA Support
      • Consultancy Services
      • Postgres Migration Blogs
      • Forum

      Recommend

      • Groups
      • Login
      • FAQs
      • SignUp

      IT Services by rayafeel.com. Powered by Rayafeel Technologies Pvt Ltd.

      • Privacy
      • Terms

      Become An Instructor?

      Join thousand of instructors and earn money hassle free!

      Get Started Now

      Login with:

      Login with Google Login with Twitter Login with LinkedIn Login with Microsoft

      Login with your site account

      Lost your password?

      Not a member yet? Register now

      Register a new account

      Are you a member? Login now