PostgreSQL Sleep Function pg_sleep – Postgres Delay Execution - Comment Page: 1

One day, I was looking for PostgreSQL database function, which could delay the execution of queries. At first I thought that I will do one myself, because I do not remember ever seen this kind function. But then I found pg_sleep Postgres function which directly delay execution of the server process. pg_sleep makes the current session's process sleep until seconds seconds have elapsed. Seconds is a value of type double precision, so fractional-second delays can be specified. pg_sleep function usage is as easy as it should be: SELECT pg_sleep(seconds); A real example, which adds 60 seconds delay between two queries: SELECT CURRENT_TIMESTAMP; SELECT pg_sleep(60); SELECT...

5 comments on “PostgreSQL Sleep Function pg_sleep – Postgres Delay Execution - Comment Page: 1

    1. Can we create some number of slow queries?
      say create 100 slow queries (200 seconds) from one sql statement only? kind of for loop?

      • Hi Deepak,

        So do you mean in general, do loop, which loops thru 100 queries?

        • yes, actually, I found a way to do it from shell script.
          A script calls one query and I call that script n number of times in bash for loop.

          but just wanted to know if bash for loop like concept is there in postgres or not to make it simpler.

          • First, nice to hear that you get this solved, with bash script.

            Yes, it’s possible do functions on postgresql db and do very complicated structures if you want to.

            Which is better way on your case (pgsql functions or bash script), depends totally on your SQL queries, do you need this functionality on some application or is this just onetime process / cron process, and what you want to do with returned data?

            • I just want to simulate the slow queries to block the psql connections and to check how our monitoring system responds.

              I used the same query which you gave above.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.