PostgreSQL PG_SLEEP() Function
Summary: in this tutorial, you will learn how to use the PostgreSQL PG_SLEEP()
function to pause the execution of a query.
Introduction to the PostgreSQL PG_SLEEP() function
The PG_SLEEP()
function allows you to create a delay (sleep) in your queries. The function can be useful when you want to test, simulate real-time processes, or add a pause between operations.
Here’s the syntax of the PG_SLEEP()
function:
In this syntax, the seconds
parameter specifies the number of seconds for which you want the execution to pause. It can be an integer or a decimal number with fractions.
The PostgreSQL PG_SLEEP() function examples
Let’s explore some examples of using the PG_SLEEP()
function.
1) Basic usage of PG_SLEEP() function
The following example uses the PG_SLEEP()
function to pause the execution for 3 seconds before returning any result:
After 3 seconds:
2) Using Fractional Seconds
The following example uses the PG_SLEEP()
function to pause the execution for 1.5
seconds:
After 1.5
seconds:
3) Using the PG_SLEEP() function with NOW() function
The following example uses the PG_SLEEP()
function between the NOW()
functions:
Output:
The output indicates that the result of the NOW()
function does not change within the same statement even though we use pause the execution between the calls of the NOW()
functions for 3 seconds.
4) Using the PG_SLEEP() function with CLOCK_TIMESTAMP() function
The following example uses the PG_SLEEP()
function between the CLOCK_TIMESTAMP()
functions:
Output:
The output shows that the CLOCK_TIMESTAMP()
returns the actual current timestamp when it executes. The results of the CLOCK_TIMESTAMP()
function calls are 3 seconds difference.
Summary
- Use the
PG_SLEEP()
function to pause the SQL execution for a number of seconds.