Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit d317fd7

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 3f26dca commit d317fd7

File tree

2 files changed

+22
-6
lines changed

2 files changed

+22
-6
lines changed

doc/src/sgml/datatype.sgml

+17-3
Original file line numberDiff line numberDiff line change
@@ -2154,7 +2154,7 @@ TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
21542154
</para>
21552155
</sect3>
21562156

2157-
<sect3>
2157+
<sect3 id="datatype-datetime-special-values">
21582158
<title>Special Values</title>
21592159

21602160
<indexterm>
@@ -2242,12 +2242,26 @@ TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
22422242
type:
22432243
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
22442244
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2245-
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2246-
optional subsecond precision specification. (See <xref
2245+
<literal>LOCALTIMESTAMP</literal>. (See <xref
22472246
linkend="functions-datetime-current"/>.) Note that these are
22482247
SQL functions and are <emphasis>not</emphasis> recognized in data input strings.
22492248
</para>
22502249

2250+
<caution>
2251+
<para>
2252+
While the input strings <literal>now</literal>,
2253+
<literal>today</literal>, <literal>tomorrow</literal>,
2254+
and <literal>yesterday</literal> are fine to use in interactive SQL
2255+
commands, they can have surprising behavior when the command is
2256+
saved to be executed later, for example in prepared statements,
2257+
views, and function definitions. The string can be converted to a
2258+
specific time value that continues to be used long after it becomes
2259+
stale. Use one of the SQL functions instead in such contexts.
2260+
For example, <literal>CURRENT_DATE + 1</literal> is safer than
2261+
<literal>'tomorrow'::date</literal>.
2262+
</para>
2263+
</caution>
2264+
22512265
</sect3>
22522266
</sect2>
22532267

doc/src/sgml/func.sgml

+5-3
Original file line numberDiff line numberDiff line change
@@ -9997,20 +9997,22 @@ now()
99979997
<programlisting>
99989998
SELECT CURRENT_TIMESTAMP;
99999999
SELECT now();
10000-
SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
10000+
SELECT TIMESTAMP 'now'; -- but see tip below
1000110001
</programlisting>
1000210002
</para>
1000310003

1000410004
<tip>
1000510005
<para>
10006-
You do not want to use the third form when specifying a <literal>DEFAULT</literal>
10007-
clause while creating a table. The system will convert <literal>now</literal>
10006+
Do not use the third form when specifying a value to be evaluated later,
10007+
for example in a <literal>DEFAULT</literal> clause for a table column.
10008+
The system will convert <literal>now</literal>
1000810009
to a <type>timestamp</type> as soon as the constant is parsed, so that when
1000910010
the default value is needed,
1001010011
the time of the table creation would be used! The first two
1001110012
forms will not be evaluated until the default value is used,
1001210013
because they are function calls. Thus they will give the desired
1001310014
behavior of defaulting to the time of row insertion.
10015+
(See also <xref linkend="datatype-datetime-special-values"/>.)
1001410016
</para>
1001510017
</tip>
1001610018
</sect2>

0 commit comments

Comments
 (0)