• Home
  • Services
    • HR Services
      • HR Advisory Services
      • Contract Staffing
      • HR Outsourcing Services
      • Manpower Supply Services
      • Payroll Processing
      • Permanent Placement
      • Recruitment and Placement Services
      • Recruitment Process Outsourcing
      • Staffing Agency Services
    • DBA Support
      • DBA Consultancy Services
      • PostgreSQL Support
    • Website Maintenance
    • Company Registration Services
    • Virtual Office Space Address
  • Company
    • FAQs
    • About Us
    • Contact
  • Office Space
    • Virtual Space
    • Co-working Space
    • Private Space
    • Meeting Room
    • Locations
    • Add Listing
    • Dashboard
  • Blogs
  • Careers
    • Jobseeker
    • Employer
  • Courses

    About Courses

    • List Of Courses
    • Become an Instructor
    Greenplum

    Greenplum

    $1,500.00
    Read More
    Have any question?
    (+91)8148383856
    info@rayafeel.com
    Login
    RayaFeeL
    • Home
    • Services
      • HR Services
        • HR Advisory Services
        • Contract Staffing
        • HR Outsourcing Services
        • Manpower Supply Services
        • Payroll Processing
        • Permanent Placement
        • Recruitment and Placement Services
        • Recruitment Process Outsourcing
        • Staffing Agency Services
      • DBA Support
        • DBA Consultancy Services
        • PostgreSQL Support
      • Website Maintenance
      • Company Registration Services
      • Virtual Office Space Address
    • Company
      • FAQs
      • About Us
      • Contact
    • Office Space
      • Virtual Space
      • Co-working Space
      • Private Space
      • Meeting Room
      • Locations
      • Add Listing
      • Dashboard
    • Blogs
    • Careers
      • Jobseeker
      • Employer
    • Courses

      About Courses

      • List Of Courses
      • Become an Instructor
      Greenplum

      Greenplum

      $1,500.00
      Read More

      Blog

      • Home
      • Blog
      • Blog
      • Script for Finding & killing postgres long running query

      Script for Finding & killing postgres long running query

      • Posted by bushra.rayafeel
      • Categories Blog
      • Date May 28, 2022
      • Comments 0 comment

      This script is mostly used for will display  what are the postgresql query is running more than one minutes and it will display query with PID using this PID you can kill what query is taken more CPU utilization and time.
      Before running this script you can monitor Using “top -c” command and you can kill particular process using top command PID for example if unwanted backup is running mistaken schedule crontab then kill that job/process using this Linux shell script

      As a dba I suggested don’t use kill -9 instead of pg_cancel and pg_terminate command

      cat kill.sh
      echo " "
      echo " "
      HOSTNAME=`hostname`
      PSQL=" /opt/PostgreSQL/9.3/bin/psql"
      PORT=5432
      HOST="localhost"
      DB="template1"
      USER="postgres"
      echo "------***WHAT ARE THE QUERY IS RUNING MORE THAN 1 MINUTES***------"
      $PSQL -d $DB -U $USER -p $PORT <<EOF
      \pset format wrapped
      SELECT pid, now() - query_start as "runtime", usename, datname, state, query
        FROM  pg_stat_activity
        WHERE now() - query_start > '1 minutes'::interval
       ORDER BY runtime DESC;
      
      EOF
      
      echo "Enter the value of PID which process you want to kill";
      read pid
      $PSQL -d $DB -U $USER -p $PORT <<EOF
      SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE pid= $pid;
      EOF
      exit 0;
      
      Sample OutPut:
      bash-4.1$ sh kill.sh
      
      
      ------***WHAT ARE THE QUERY IS RUNING MORE THAN 1 MINUTES***------
      Output format is wrapped.
        pid  |     runtime     |  usename  | datname  | state |   query
      -------+-----------------+-----------+----------+-------+------------
       30432 | 00:02:22.036876 | emutation | mhrordhu | idle  | unlisten *
       31761 | 00:02:21.739505 | emutation | mhrordhu | idle  | unlisten *
       
      
      Enter the value of PID which process you want to kill
      30432
       pg_terminate_backend
      ----------------------
       t
      (1 row)

      Tag:performance, postgresql, script

      • Share:
      bushra.rayafeel

      Previous post

      Postgres database Query status
      May 28, 2022

      Next post

      Understanding Postgres Database VACUUM , VACUUM FULL & VACUUM ANALYZE
      May 28, 2022

      Leave A Reply

      You must be logged in to post a comment.

      Connect with



      Search

      ADVERTISEMENT

      Latest Posts

      HOW VIRTUAL OFFICE IS GOING TO CHANGE YOUR BUSINESS STRATEGIES
      21Jun2022
      Take the best advantage of your Virtual Office
      21Jun2022
      Let’s discuss how corporates can improve their productivity through virtual office spaces?
      21Jun2022
      Virtual Office Space & Address Services
      21Jun2022
      HOW VIRTUAL OFFICES ARE TRANSFORMING BUSINESSES
      21Jun2022
      PostgreSQL

      PostgreSQL

      $800.00 $500.00
      Greenplum

      Greenplum

      $1,500.00
      Oracle Database

      Oracle Database

      $350.00
      2ndquadrant.in

      (+91) 8838953252

      ITsupport@rayafeel.com

      Company

      • About Us
      • Our Team

      COURSES

      • List Of Course
      • Become An Instructor

      Support

      • DBA Support
      • Consultancy Services

      Recommend

      • Login
      • Contact Us

      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

      Connect with

      Login with Google

      Login with your site account

      Connect with



      Lost your password?