Skip to content

Commit d74ed6f

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 c8cb243 commit d74ed6f

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
@@ -2097,7 +2097,7 @@ January 8 04:05:06 1999 PST
20972097
</para>
20982098
</sect3>
20992099

2100-
<sect3>
2100+
<sect3 id="datatype-datetime-special-values">
21012101
<title>Special Values</title>
21022102

21032103
<indexterm>
@@ -2185,12 +2185,26 @@ January 8 04:05:06 1999 PST
21852185
type:
21862186
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
21872187
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2188-
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2189-
optional subsecond precision specification. (See <xref
2188+
<literal>LOCALTIMESTAMP</literal>. (See <xref
21902189
linkend="functions-datetime-current">.) Note that these are
21912190
SQL functions and are <emphasis>not</> recognized in data input strings.
21922191
</para>
21932192

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

doc/src/sgml/func.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7883,20 +7883,22 @@ now()
78837883
<programlisting>
78847884
SELECT CURRENT_TIMESTAMP;
78857885
SELECT now();
7886-
SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
7886+
SELECT TIMESTAMP 'now'; -- but see tip below
78877887
</programlisting>
78887888
</para>
78897889

78907890
<tip>
78917891
<para>
7892-
You do not want to use the third form when specifying a <literal>DEFAULT</>
7893-
clause while creating a table. The system will convert <literal>now</literal>
7892+
Do not use the third form when specifying a value to be evaluated later,
7893+
for example in a <literal>DEFAULT</literal> clause for a table column.
7894+
The system will convert <literal>now</literal>
78947895
to a <type>timestamp</type> as soon as the constant is parsed, so that when
78957896
the default value is needed,
78967897
the time of the table creation would be used! The first two
78977898
forms will not be evaluated until the default value is used,
78987899
because they are function calls. Thus they will give the desired
78997900
behavior of defaulting to the time of row insertion.
7901+
(See also <xref linkend="datatype-datetime-special-values">.)
79007902
</para>
79017903
</tip>
79027904
</sect2>

0 commit comments

Comments
 (0)