Skip to content

Commit 133d06f

Browse files
committed
Doc: caution against misuse of 'now' and related datetime literals.
Section 8.5.1.4, which defines these literals, made only a vague reference to the fact that they might be evaluated too soon to be safe in non-interactive contexts. Provide a more explicit caution against misuse. Also, generalize the wording in the related tip in section 9.9.4: while it clearly described this problem, it implied (or really, stated outright) that the problem only applies to table DEFAULT clauses. Per gripe from Tijs van Dam. Back-patch to all supported branches. Discussion: https://postgr.es/m/c2LuRv9BiRT3bqIo5mMQiVraEXey_25B4vUn0kDqVqilwOEu_iVF1tbtvLnyQK7yDG3PFaz_GxLLPil2SDkj1MCObNRVaac-7j1dVdFERk8=@thalex.com
1 parent 2b9166d commit 133d06f

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

doc/src/sgml/datatype.sgml

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2103,7 +2103,7 @@ January 8 04:05:06 1999 PST
21032103
</para>
21042104
</sect3>
21052105

2106-
<sect3>
2106+
<sect3 id="datatype-datetime-special-values">
21072107
<title>Special Values</title>
21082108

21092109
<indexterm>
@@ -2191,12 +2191,26 @@ January 8 04:05:06 1999 PST
21912191
type:
21922192
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
21932193
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2194-
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2195-
optional subsecond precision specification. (See <xref
2194+
<literal>LOCALTIMESTAMP</literal>. (See <xref
21962195
linkend="functions-datetime-current"/>.) Note that these are
21972196
SQL functions and are <emphasis>not</emphasis> recognized in data input strings.
21982197
</para>
21992198

2199+
<caution>
2200+
<para>
2201+
While the input strings <literal>now</literal>,
2202+
<literal>today</literal>, <literal>tomorrow</literal>,
2203+
and <literal>yesterday</literal> are fine to use in interactive SQL
2204+
commands, they can have surprising behavior when the command is
2205+
saved to be executed later, for example in prepared statements,
2206+
views, and function definitions. The string can be converted to a
2207+
specific time value that continues to be used long after it becomes
2208+
stale. Use one of the SQL functions instead in such contexts.
2209+
For example, <literal>CURRENT_DATE + 1</literal> is safer than
2210+
<literal>'tomorrow'::date</literal>.
2211+
</para>
2212+
</caution>
2213+
22002214
</sect3>
22012215
</sect2>
22022216

doc/src/sgml/func.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8340,20 +8340,22 @@ now()
83408340
<programlisting>
83418341
SELECT CURRENT_TIMESTAMP;
83428342
SELECT now();
8343-
SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
8343+
SELECT TIMESTAMP 'now'; -- but see tip below
83448344
</programlisting>
83458345
</para>
83468346

83478347
<tip>
83488348
<para>
8349-
You do not want to use the third form when specifying a <literal>DEFAULT</literal>
8350-
clause while creating a table. The system will convert <literal>now</literal>
8349+
Do not use the third form when specifying a value to be evaluated later,
8350+
for example in a <literal>DEFAULT</literal> clause for a table column.
8351+
The system will convert <literal>now</literal>
83518352
to a <type>timestamp</type> as soon as the constant is parsed, so that when
83528353
the default value is needed,
83538354
the time of the table creation would be used! The first two
83548355
forms will not be evaluated until the default value is used,
83558356
because they are function calls. Thus they will give the desired
83568357
behavior of defaulting to the time of row insertion.
8358+
(See also <xref linkend="datatype-datetime-special-values"/>.)
83578359
</para>
83588360
</tip>
83598361
</sect2>

0 commit comments

Comments
 (0)