Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit782eefc

Browse files
committed
Create a standard function pg_sleep() to sleep for a specified amount of time.
Replace the former ad-hoc implementation used in the regression tests.Joachim Wieland
1 parentfb627b7 commit782eefc

File tree

10 files changed

+112
-35
lines changed

10 files changed

+112
-35
lines changed

‎doc/src/sgml/func.sgml

Lines changed: 51 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.301 2005/12/28 01:29:58 tgl Exp $
2+
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.302 2006/01/11 20:12:38 tgl Exp $
33
PostgreSQL documentation
44
-->
55

@@ -6170,6 +6170,56 @@ SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
61706170
</para>
61716171
</tip>
61726172
</sect2>
6173+
6174+
<sect2 id="functions-datetime-delay">
6175+
<title>Delaying Execution</title>
6176+
6177+
<indexterm>
6178+
<primary>pg_sleep</primary>
6179+
</indexterm>
6180+
<indexterm>
6181+
<primary>sleep</primary>
6182+
</indexterm>
6183+
<indexterm>
6184+
<primary>delay</primary>
6185+
</indexterm>
6186+
6187+
<para>
6188+
The following function is available to delay execution of the server
6189+
process:
6190+
<synopsis>
6191+
pg_sleep(<replaceable>seconds</replaceable>)
6192+
</synopsis>
6193+
6194+
<function>pg_sleep</function> makes the current session's process
6195+
sleep until <replaceable>seconds</replaceable> seconds have
6196+
elapsed. <replaceable>seconds</replaceable> is a value of type
6197+
<type>double precision</>, so fractional-second delays can be specified.
6198+
For example:
6199+
6200+
<programlisting>
6201+
SELECT pg_sleep(1.5);
6202+
</programlisting>
6203+
</para>
6204+
6205+
<note>
6206+
<para>
6207+
The effective resolution of the sleep interval is platform-specific;
6208+
0.01 seconds is a common value. The sleep delay will be at least as long
6209+
as specified. It may be longer depending on factors such as server load.
6210+
</para>
6211+
</note>
6212+
6213+
<warning>
6214+
<para>
6215+
Make sure that your session does not hold more locks than necessary
6216+
when calling <function>pg_sleep</function>. Otherwise other sessions
6217+
might have to wait for your sleeping process, slowing down the entire
6218+
system.
6219+
</para>
6220+
</warning>
6221+
</sect2>
6222+
61736223
</sect1>
61746224

61756225

‎src/backend/utils/adt/misc.c

Lines changed: 50 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
*
99
*
1010
* IDENTIFICATION
11-
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.49 2005/10/15 02:49:29 momjian Exp $
11+
* $PostgreSQL: pgsql/src/backend/utils/adt/misc.c,v 1.50 2006/01/11 20:12:39 tgl Exp $
1212
*
1313
*-------------------------------------------------------------------------
1414
*/
@@ -17,6 +17,7 @@
1717
#include<sys/file.h>
1818
#include<signal.h>
1919
#include<dirent.h>
20+
#include<math.h>
2021

2122
#include"catalog/pg_tablespace.h"
2223
#include"catalog/pg_type.h"
@@ -259,3 +260,51 @@ pg_tablespace_databases(PG_FUNCTION_ARGS)
259260
FreeDir(fctx->dirdesc);
260261
SRF_RETURN_DONE(funcctx);
261262
}
263+
264+
265+
/*
266+
* pg_sleep - delay for N seconds
267+
*/
268+
Datum
269+
pg_sleep(PG_FUNCTION_ARGS)
270+
{
271+
float8secs=PG_GETARG_FLOAT8(0);
272+
float8endtime;
273+
274+
/*
275+
* We break the requested sleep into segments of no more than 1 second,
276+
* to put an upper bound on how long it will take us to respond to a
277+
* cancel or die interrupt. (Note that pg_usleep is interruptible by
278+
* signals on some platforms but not others.) Also, this method avoids
279+
* exposing pg_usleep's upper bound on allowed delays.
280+
*
281+
* By computing the intended stop time initially, we avoid accumulation
282+
* of extra delay across multiple sleeps. This also ensures we won't
283+
* delay less than the specified time if pg_usleep is interrupted
284+
* by other signals such as SIGHUP.
285+
*/
286+
287+
#ifdefHAVE_INT64_TIMESTAMP
288+
#defineGetNowFloat()((float8) GetCurrentTimestamp() / 1000000.0)
289+
#else
290+
#defineGetNowFloat()GetCurrentTimestamp()
291+
#endif
292+
293+
endtime=GetNowFloat()+secs;
294+
295+
for (;;)
296+
{
297+
float8delay;
298+
299+
CHECK_FOR_INTERRUPTS();
300+
delay=endtime-GetNowFloat();
301+
if (delay >=1.0)
302+
pg_usleep(1000000L);
303+
elseif (delay>0.0)
304+
pg_usleep((long)ceil(delay*1000000.0));
305+
else
306+
break;
307+
}
308+
309+
PG_RETURN_VOID();
310+
}

‎src/include/catalog/catversion.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
3838
* Portions Copyright (c) 1994, Regents of the University of California
3939
*
40-
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.309 2006/01/08 07:00:25 neilc Exp $
40+
* $PostgreSQL: pgsql/src/include/catalog/catversion.h,v 1.310 2006/01/11 20:12:39 tgl Exp $
4141
*
4242
*-------------------------------------------------------------------------
4343
*/
@@ -53,6 +53,6 @@
5353
*/
5454

5555
/*yyyymmddN */
56-
#defineCATALOG_VERSION_NO200601081
56+
#defineCATALOG_VERSION_NO200601111
5757

5858
#endif

‎src/include/catalog/pg_proc.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.390 2006/01/08 07:00:25 neilc Exp $
10+
* $PostgreSQL: pgsql/src/include/catalog/pg_proc.h,v 1.391 2006/01/11 20:12:39 tgl Exp $
1111
*
1212
* NOTES
1313
* The script catalog/genbki.sh reads this file and generates .bki
@@ -3025,6 +3025,8 @@ DATA(insert OID = 2624 ( pg_read_filePGNSP PGUID 12 f f t f v 3 25 "25 20 20"
30253025
DESCR("Read text from a file");
30263026
DATA(insertOID=2625 (pg_ls_dirPGNSPPGUID12ffttv125"25"_null__null__null_pg_ls_dir-_null_ ));
30273027
DESCR("List all files in a directory");
3028+
DATA(insertOID=2626 (pg_sleepPGNSPPGUID12fftfv12278"701"_null__null__null_pg_sleep-_null_ ));
3029+
DESCR("Sleep for the specified time in seconds");
30283030

30293031

30303032
/* Aggregates (moved here from pg_aggregate for 7.3) */

‎src/include/utils/builtins.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
* Portions Copyright (c) 1996-2005, PostgreSQL Global Development Group
88
* Portions Copyright (c) 1994, Regents of the University of California
99
*
10-
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.269 2006/01/08 07:00:26 neilc Exp $
10+
* $PostgreSQL: pgsql/src/include/utils/builtins.h,v 1.270 2006/01/11 20:12:42 tgl Exp $
1111
*
1212
*-------------------------------------------------------------------------
1313
*/
@@ -387,6 +387,7 @@ extern Datum pg_cancel_backend(PG_FUNCTION_ARGS);
387387
externDatumpg_reload_conf(PG_FUNCTION_ARGS);
388388
externDatumpg_tablespace_databases(PG_FUNCTION_ARGS);
389389
externDatumpg_rotate_logfile(PG_FUNCTION_ARGS);
390+
externDatumpg_sleep(PG_FUNCTION_ARGS);
390391

391392
/* not_in.c */
392393
externDatumint4notin(PG_FUNCTION_ARGS);

‎src/test/regress/expected/stats.out

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,8 @@ SELECT count(*) FROM tenk2 WHERE unique1 = 1;
3636
(1 row)
3737

3838
-- let stats collector catch up
39-
SELECTdo_sleep(2);
40-
do_sleep
39+
SELECTpg_sleep(2.0);
40+
pg_sleep
4141
----------
4242

4343
(1 row)

‎src/test/regress/input/create_function_1.source

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -52,11 +52,6 @@ CREATE FUNCTION set_ttdummy (int4)
5252
AS '@abs_builddir@/regress@DLSUFFIX@'
5353
LANGUAGE 'C' STRICT;
5454

55-
CREATE FUNCTION do_sleep (int4)
56-
RETURNS void
57-
AS '@abs_builddir@/regress@DLSUFFIX@'
58-
LANGUAGE 'C' STRICT;
59-
6055
-- Things that shouldn't work:
6156

6257
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql

‎src/test/regress/output/create_function_1.source

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -47,10 +47,6 @@ CREATE FUNCTION set_ttdummy (int4)
4747
RETURNS int4
4848
AS '@abs_builddir@/regress@DLSUFFIX@'
4949
LANGUAGE 'C' STRICT;
50-
CREATE FUNCTION do_sleep (int4)
51-
RETURNS void
52-
AS '@abs_builddir@/regress@DLSUFFIX@'
53-
LANGUAGE 'C' STRICT;
5450
-- Things that shouldn't work:
5551
CREATE FUNCTION test1 (int) RETURNS int LANGUAGE sql
5652
AS 'SELECT ''not an integer'';';

‎src/test/regress/regress.c

Lines changed: 1 addition & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.64 2005/10/15 02:49:51 momjian Exp $
2+
* $PostgreSQL: pgsql/src/test/regress/regress.c,v 1.65 2006/01/11 20:12:43 tgl Exp $
33
*/
44

55
#include"postgres.h"
@@ -26,7 +26,6 @@ extern char *reverse_name(char *string);
2626
externintoldstyle_length(intn,text*t);
2727
externDatumint44in(PG_FUNCTION_ARGS);
2828
externDatumint44out(PG_FUNCTION_ARGS);
29-
externDatumdo_sleep(PG_FUNCTION_ARGS);
3029

3130

3231
/*
@@ -735,18 +734,3 @@ int44out(PG_FUNCTION_ARGS)
735734
*--walk='\0';
736735
PG_RETURN_CSTRING(result);
737736
}
738-
739-
/*
740-
* do_sleep - delay for N seconds
741-
*/
742-
PG_FUNCTION_INFO_V1(do_sleep);
743-
744-
Datum
745-
do_sleep(PG_FUNCTION_ARGS)
746-
{
747-
int32secs=PG_GETARG_INT32(0);
748-
749-
pg_usleep(secs*1000000L);
750-
751-
PG_RETURN_VOID();
752-
}

‎src/test/regress/sql/stats.sql

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ SELECT count(*) FROM tenk2;
2626
SELECTcount(*)FROM tenk2WHERE unique1=1;
2727

2828
-- let stats collector catch up
29-
SELECTdo_sleep(2);
29+
SELECTpg_sleep(2.0);
3030

3131
-- check effects
3232
SELECTst.seq_scan>=pr.seq_scan+1,

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp