Skip to content

Commit e6004f0

Browse files
committed
Add statement_timestamp(), clock_timestamp(), and
transaction_timestamp() (just like now()). Also update statement_timeout() to mention it is statement arrival time that is measured. Catalog version updated.
1 parent f04080d commit e6004f0

File tree

9 files changed

+179
-63
lines changed

9 files changed

+179
-63
lines changed

doc/src/sgml/config.sgml

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.55 2006/04/23 03:39:48 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/config.sgml,v 1.56 2006/04/25 00:25:15 momjian Exp $ -->
22

33
<chapter Id="runtime-config">
44
<title>Server Configuration</title>
@@ -3279,7 +3279,8 @@ SELECT * FROM parent WHERE key = 2400;
32793279
<listitem>
32803280
<para>
32813281
Abort any statement that takes over the specified number of
3282-
milliseconds. If <varname>log_min_error_statement</> is set to
3282+
milliseconds, starting from the time the command arrives at the server
3283+
from the client. If <varname>log_min_error_statement</> is set to
32833284
<literal>ERROR</> or lower, the statement that timed out will also be
32843285
logged. A value of zero (the default) turns off the
32853286
limitation.

doc/src/sgml/func.sgml

Lines changed: 67 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.314 2006/04/23 03:39:50 momjian Exp $ -->
1+
<!-- $PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.315 2006/04/25 00:25:15 momjian Exp $ -->
22

33
<chapter id="functions">
44
<title>Functions and Operators</title>
@@ -5302,6 +5302,15 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
53025302
<indexterm>
53035303
<primary>now</primary>
53045304
</indexterm>
5305+
<indexterm>
5306+
<primary>transaction_timestamp</primary>
5307+
</indexterm>
5308+
<indexterm>
5309+
<primary>statement_timestamp</primary>
5310+
</indexterm>
5311+
<indexterm>
5312+
<primary>clock_timestamp</primary>
5313+
</indexterm>
53055314
<indexterm>
53065315
<primary>timeofday</primary>
53075316
</indexterm>
@@ -5358,7 +5367,7 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
53585367
<row>
53595368
<entry><literal><function>current_timestamp</function></literal></entry>
53605369
<entry><type>timestamp with time zone</type></entry>
5361-
<entry>Date and time; see <xref linkend="functions-datetime-current">
5370+
<entry>Date and time of start of current transaction; see <xref linkend="functions-datetime-current">
53625371
</entry>
53635372
<entry></entry>
53645373
<entry></entry>
@@ -5474,8 +5483,36 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
54745483
<row>
54755484
<entry><literal><function>now</function>()</literal></entry>
54765485
<entry><type>timestamp with time zone</type></entry>
5477-
<entry>Current date and time (equivalent to
5478-
<function>current_timestamp</function>); see <xref linkend="functions-datetime-current">
5486+
<entry>Date and time of start of current transaction (equivalent to
5487+
<function>CURRENT_TIMESTAMP</function>); see <xref linkend="functions-datetime-current">
5488+
</entry>
5489+
<entry></entry>
5490+
<entry></entry>
5491+
</row>
5492+
5493+
<row>
5494+
<entry><literal><function>transaction_timestamp</function>()</literal></entry>
5495+
<entry><type>timestamp with time zone</type></entry>
5496+
<entry>Date and time of start of current transaction (equivalent to
5497+
<function>CURRENT_TIMESTAMP</function>); see <xref linkend="functions-datetime-current">
5498+
</entry>
5499+
<entry></entry>
5500+
<entry></entry>
5501+
</row>
5502+
5503+
<row>
5504+
<entry><literal><function>statement_timestamp</function>()</literal></entry>
5505+
<entry><type>timestamp with time zone</type></entry>
5506+
<entry>Date and time of start of current statement; see <xref linkend="functions-datetime-current">
5507+
</entry>
5508+
<entry></entry>
5509+
<entry></entry>
5510+
</row>
5511+
5512+
<row>
5513+
<entry><literal><function>clock_timestamp</function>()</literal></entry>
5514+
<entry><type>timestamp with time zone</type></entry>
5515+
<entry>Current date and time (changes during statement execution); see <xref linkend="functions-datetime-current">
54795516
</entry>
54805517
<entry></entry>
54815518
<entry></entry>
@@ -5484,7 +5521,8 @@ SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
54845521
<row>
54855522
<entry><literal><function>timeofday</function>()</literal></entry>
54865523
<entry><type>text</type></entry>
5487-
<entry>Current date and time; see <xref linkend="functions-datetime-current">
5524+
<entry>Current date and time (like <function>clock_timestamp</>), but as a Unix-style <type>text</> value;
5525+
see <xref linkend="functions-datetime-current">
54885526
</entry>
54895527
<entry></entry>
54905528
<entry></entry>
@@ -6072,7 +6110,7 @@ SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST';
60726110
</sect2>
60736111

60746112
<sect2 id="functions-datetime-current">
6075-
<title>Current Date/Time</title>
6113+
<title>Date/Time of Transaction Start</title>
60766114

60776115
<indexterm>
60786116
<primary>date</primary>
@@ -6085,8 +6123,8 @@ SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST';
60856123
</indexterm>
60866124

60876125
<para>
6088-
The following functions are available to obtain the current date and/or
6089-
time:
6126+
The following functions are available to obtain the date and/or
6127+
time of the start of the current transaction:
60906128
<synopsis>
60916129
CURRENT_DATE
60926130
CURRENT_TIME
@@ -6138,12 +6176,6 @@ SELECT LOCALTIMESTAMP;
61386176
</screen>
61396177
</para>
61406178

6141-
<para>
6142-
The function <function>now()</function> is the traditional
6143-
<productname>PostgreSQL</productname> equivalent to
6144-
<function>CURRENT_TIMESTAMP</function>.
6145-
</para>
6146-
61476179
<para>
61486180
It is important to know that
61496181
<function>CURRENT_TIMESTAMP</function> and related functions return
@@ -6152,26 +6184,31 @@ SELECT LOCALTIMESTAMP;
61526184
the intent is to allow a single transaction to have a consistent
61536185
notion of the <quote>current</quote> time, so that multiple
61546186
modifications within the same transaction bear the same
6155-
time stamp.
6187+
time stamp. Consider using <function>statement_timestamp</> or
6188+
<function>clock_timestamp</> if you need something that changes
6189+
more frequently.
61566190
</para>
61576191

6158-
<note>
6159-
<para>
6160-
Other database systems may advance these values more
6161-
frequently.
6162-
</para>
6163-
</note>
6192+
<para>
6193+
<function>CURRENT_TIMESTAMP</> might not be the
6194+
transaction start time on other database systems.
6195+
For this reason, and for completeness,
6196+
<function>transaction_timestamp</> is provided.
6197+
The function <function>now()</function> is the traditional
6198+
<productname>PostgreSQL</productname> equivalent to
6199+
the SQL-standard <function>CURRENT_TIMESTAMP</function>.
6200+
</para>
61646201

61656202
<para>
6166-
There is also the function <function>timeofday()</function> which
6167-
returns the wall-clock time and advances during transactions. For
6168-
historical reasons <function>timeofday()</function> returns a
6169-
<type>text</type> string rather than a <type>timestamp</type>
6170-
value:
6171-
<screen>
6172-
SELECT timeofday();
6173-
<lineannotation>Result: </lineannotation><computeroutput>Sat Feb 17 19:07:32.000126 2001 EST</computeroutput>
6174-
</screen>
6203+
<function>STATEMENT_TIMESTAMP</> is the time the statement
6204+
arrived at the server from the client. It is not the time
6205+
the command started execution. If multiple commands were
6206+
sent as a single query string to the server, each command
6207+
has the same <function>STATEMENT_TIMESTAMP</> because they
6208+
all arrived at the same time. Also, commands executed
6209+
by server-side functions have a <function>STATEMENT_TIMESTAMP</>
6210+
based on the time the client sent the query that triggered
6211+
the function, not the time the function was executed.
61756212
</para>
61766213

61776214
<para>

src/backend/access/transam/xact.c

Lines changed: 22 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
*
1111
*
1212
* IDENTIFICATION
13-
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.219 2006/03/29 21:17:37 tgl Exp $
13+
* $PostgreSQL: pgsql/src/backend/access/transam/xact.c,v 1.220 2006/04/25 00:25:17 momjian Exp $
1414
*
1515
*-------------------------------------------------------------------------
1616
*/
@@ -172,6 +172,7 @@ static CommandId currentCommandId;
172172
* keep it inside the TransactionState stack.
173173
*/
174174
static TimestampTz xactStartTimestamp;
175+
static TimestampTz stmtStartTimestamp;
175176

176177
/*
177178
* GID to be used for preparing the current transaction. This is also
@@ -427,6 +428,24 @@ GetCurrentTransactionStartTimestamp(void)
427428
return xactStartTimestamp;
428429
}
429430

431+
/*
432+
* GetCurrentStatementStartTimestamp
433+
*/
434+
TimestampTz
435+
GetCurrentStatementStartTimestamp(void)
436+
{
437+
return stmtStartTimestamp;
438+
}
439+
440+
/*
441+
* SetCurrentStatementStartTimestamp
442+
*/
443+
void
444+
SetCurrentStatementStartTimestamp(void)
445+
{
446+
stmtStartTimestamp = GetCurrentTimestamp();
447+
}
448+
430449
/*
431450
* GetCurrentTransactionNestLevel
432451
*
@@ -1367,9 +1386,9 @@ StartTransaction(void)
13671386
XactLockTableInsert(s->transactionId);
13681387

13691388
/*
1370-
* set now()
1389+
* now() and statement_timestamp() should be the same time
13711390
*/
1372-
xactStartTimestamp = GetCurrentTimestamp();
1391+
xactStartTimestamp = stmtStartTimestamp;
13731392

13741393
/*
13751394
* initialize current transaction state fields

0 commit comments

Comments
 (0)