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

Commit 5408498

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 c4a803a commit 5408498

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
@@ -2197,7 +2197,7 @@ TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
21972197
</para>
21982198
</sect3>
21992199

2200-
<sect3>
2200+
<sect3 id="datatype-datetime-special-values">
22012201
<title>Special Values</title>
22022202

22032203
<indexterm>
@@ -2285,12 +2285,26 @@ TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
22852285
type:
22862286
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
22872287
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2288-
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2289-
optional subsecond precision specification. (See <xref
2288+
<literal>LOCALTIMESTAMP</literal>. (See <xref
22902289
linkend="functions-datetime-current"/>.) Note that these are
22912290
SQL functions and are <emphasis>not</emphasis> recognized in data input strings.
22922291
</para>
22932292

2293+
<caution>
2294+
<para>
2295+
While the input strings <literal>now</literal>,
2296+
<literal>today</literal>, <literal>tomorrow</literal>,
2297+
and <literal>yesterday</literal> are fine to use in interactive SQL
2298+
commands, they can have surprising behavior when the command is
2299+
saved to be executed later, for example in prepared statements,
2300+
views, and function definitions. The string can be converted to a
2301+
specific time value that continues to be used long after it becomes
2302+
stale. Use one of the SQL functions instead in such contexts.
2303+
For example, <literal>CURRENT_DATE + 1</literal> is safer than
2304+
<literal>'tomorrow'::date</literal>.
2305+
</para>
2306+
</caution>
2307+
22942308
</sect3>
22952309
</sect2>
22962310

doc/src/sgml/func.sgml

+5-3
Original file line numberDiff line numberDiff line change
@@ -10044,20 +10044,22 @@ now()
1004410044
<programlisting>
1004510045
SELECT CURRENT_TIMESTAMP;
1004610046
SELECT now();
10047-
SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
10047+
SELECT TIMESTAMP 'now'; -- but see tip below
1004810048
</programlisting>
1004910049
</para>
1005010050

1005110051
<tip>
1005210052
<para>
10053-
You do not want to use the third form when specifying a <literal>DEFAULT</literal>
10054-
clause while creating a table. The system will convert <literal>now</literal>
10053+
Do not use the third form when specifying a value to be evaluated later,
10054+
for example in a <literal>DEFAULT</literal> clause for a table column.
10055+
The system will convert <literal>now</literal>
1005510056
to a <type>timestamp</type> as soon as the constant is parsed, so that when
1005610057
the default value is needed,
1005710058
the time of the table creation would be used! The first two
1005810059
forms will not be evaluated until the default value is used,
1005910060
because they are function calls. Thus they will give the desired
1006010061
behavior of defaulting to the time of row insertion.
10062+
(See also <xref linkend="datatype-datetime-special-values"/>.)
1006110063
</para>
1006210064
</tip>
1006310065
</sect2>

0 commit comments

Comments
 (0)