Skip to content

Commit 760c770

Browse files
committed
Add convenience functions pg_sleep_for and pg_sleep_until.
Vik Fearing, reviewed by Pavel Stehule and myself
1 parent 043f6ff commit 760c770

File tree

5 files changed

+23
-9
lines changed

5 files changed

+23
-9
lines changed

doc/src/sgml/func.sgml

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -7664,20 +7664,28 @@ SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
76647664
</indexterm>
76657665

76667666
<para>
7667-
The following function is available to delay execution of the server
7667+
The following functions are available to delay execution of the server
76687668
process:
76697669
<synopsis>
76707670
pg_sleep(<replaceable>seconds</replaceable>)
7671+
pg_sleep_for(<type>interval</>)
7672+
pg_sleep_until(<type>timestamp with time zone</>)
76717673
</synopsis>
76727674

76737675
<function>pg_sleep</function> makes the current session's process
76747676
sleep until <replaceable>seconds</replaceable> seconds have
76757677
elapsed. <replaceable>seconds</replaceable> is a value of type
76767678
<type>double precision</>, so fractional-second delays can be specified.
7679+
<function>pg_sleep_for</function> is a convenience function for larger
7680+
sleep times specified as an <type>interval</>.
7681+
<function>pg_sleep_until</function> is a convenience function for when
7682+
a specific wake-up time is desired.
76777683
For example:
76787684

76797685
<programlisting>
76807686
SELECT pg_sleep(1.5);
7687+
SELECT pg_sleep_for('5 minutes');
7688+
SELECT pg_sleep_until('tomorrow 03:00');
76817689
</programlisting>
76827690
</para>
76837691

@@ -7686,15 +7694,17 @@ SELECT pg_sleep(1.5);
76867694
The effective resolution of the sleep interval is platform-specific;
76877695
0.01 seconds is a common value. The sleep delay will be at least as long
76887696
as specified. It might be longer depending on factors such as server load.
7697+
In particular, <function>pg_sleep_until</function> is not guaranteed to
7698+
wake up exactly at the specified time, but it will not wake up any earlier.
76897699
</para>
76907700
</note>
76917701

76927702
<warning>
76937703
<para>
76947704
Make sure that your session does not hold more locks than necessary
7695-
when calling <function>pg_sleep</function>. Otherwise other sessions
7696-
might have to wait for your sleeping process, slowing down the entire
7697-
system.
7705+
when calling <function>pg_sleep</function> or its variants. Otherwise
7706+
other sessions might have to wait for your sleeping process, slowing down
7707+
the entire system.
76987708
</para>
76997709
</warning>
77007710
</sect2>

src/include/catalog/catversion.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/* yyyymmddN */
56-
#define CATALOG_VERSION_NO 201401291
56+
#define CATALOG_VERSION_NO 201401301
5757

5858
#endif

src/include/catalog/pg_proc.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3034,6 +3034,10 @@ DATA(insert OID = 2625 ( pg_ls_dir PGNSP PGUID 12 1 1000 0 0 f f f f t t v 1 0
30343034
DESCR("list all files in a directory");
30353035
DATA(insert OID = 2626 ( pg_sleep PGNSP PGUID 12 1 0 0 0 f f f f t f v 1 0 2278 "701" _null_ _null_ _null_ _null_ pg_sleep _null_ _null_ _null_ ));
30363036
DESCR("sleep for the specified time in seconds");
3037+
DATA(insert OID = 3935 ( pg_sleep_for PGNSP PGUID 14 1 0 0 0 f f f f t f v 1 0 2278 "1186" _null_ _null_ _null_ _null_ "select pg_catalog.pg_sleep(extract(epoch from pg_catalog.now() operator(pg_catalog.+) $1) operator(pg_catalog.-) extract(epoch from pg_catalog.now()))" _null_ _null_ _null_ ));
3038+
DESCR("sleep for the specified interval");
3039+
DATA(insert OID = 3936 ( pg_sleep_until PGNSP PGUID 14 1 0 0 0 f f f f t f v 1 0 2278 "1184" _null_ _null_ _null_ _null_ "select pg_catalog.pg_sleep(extract(epoch from $1) operator(pg_catalog.-) extract(epoch from pg_catalog.now()))" _null_ _null_ _null_ ));
3040+
DESCR("sleep until the specified time");
30373041

30383042
DATA(insert OID = 2971 ( text PGNSP PGUID 12 1 0 0 0 f f f f t f i 1 0 25 "16" _null_ _null_ _null_ _null_ booltext _null_ _null_ _null_ ));
30393043
DESCR("convert boolean to text");

src/test/regress/expected/stats.out

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ SET enable_indexscan TO on;
1818
SET enable_indexonlyscan TO off;
1919
-- wait to let any prior tests finish dumping out stats;
2020
-- else our messages might get lost due to contention
21-
SELECT pg_sleep(2.0);
22-
pg_sleep
23-
----------
21+
SELECT pg_sleep_for('2 seconds');
22+
pg_sleep_for
23+
--------------
2424

2525
(1 row)
2626

src/test/regress/sql/stats.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ SET enable_indexonlyscan TO off;
1616

1717
-- wait to let any prior tests finish dumping out stats;
1818
-- else our messages might get lost due to contention
19-
SELECT pg_sleep(2.0);
19+
SELECT pg_sleep_for('2 seconds');
2020

2121
-- save counters
2222
CREATE TEMP TABLE prevstats AS

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy