PostgreSQL pg_sleep() Function
The PostgreSQL pg_sleep()
function suspends the execution of the current server process for the specified number of seconds.
pg_sleep()
Syntax
Here is the syntax of the PostgreSQL pg_sleep()
function:
pg_sleep(seconds DOUBLE)
Parameters
seconds
-
Required. The pause duration in seconds. It should be greater than or equal to 0, and can have a fractional part.
Return value
The PostgreSQL pg_sleep()
function has no return value, it is used to pause the execution of the current server process for the specified number of seconds.
pg_sleep()
Examples
This example combined clock_timestamp()
demonstrates the capabilities of pg_sleep()
.
SELECT
clock_timestamp(),
pg_sleep(10),
clock_timestamp();
-[ RECORD 1 ]---+------------------------------
clock_timestamp | 2022-05-20 11:32:26.813093+08
pg_sleep |
clock_timestamp | 2022-05-20 11:32:36.823482+08
Here, since we use pg_sleep(10)
in the middle of the two clock_timestamp()
functions to pause the current server process for 10 seconds temporarily, so the execution of the second clock_timestamp()
function is 10 seconds later than the first, so the return value of the second funciont is 10 seconds later than the first .