PostgreSQL pg_sleep_for() Function
The PostgreSQL pg_sleep_for() function receives an interval parameter that pauses the execution of the current server process for the specified interval.
pg_sleep_for() Syntax
Here is the syntax of the PostgreSQL pg_sleep_for() function:
pg_sleep_for(duration INTERVAL)
Parameters
duration-
Required. A value of
INTERVALtype that indicates how long the current server process is paused.
Return value
The PostgreSQL pg_sleep_for() function has no return value, it is used to suspend the execution of the current server process for the specified interval.
pg_sleep_for() Examples
This example combined clock_timestamp() demonstrates the capabilities of pg_sleep_for().
SELECT
clock_timestamp(),
pg_sleep_for('10 seconds'),
clock_timestamp();
-[ RECORD 1 ]---+------------------------------
clock_timestamp | 2022-05-20 11:31:05.691725+08
pg_sleep_for |
clock_timestamp | 2022-05-20 11:31:15.695237+08here:
- We use an interval value
10 secondsforpg_sleep_for(), which is used to pause the current server process for 10 seconds. - Since we use
pg_sleep_for('10 seconds')in the middle of the twoclock_timestamp()functions to pause the execution of current server process for 10 seconds, so the execution of the secondclock_timestamp()function is 10 seconds later than the first, so the return value of the second is 10 seconds later than the first.