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

Commit 3753e27

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 b39c940 commit 3753e27

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
@@ -2114,7 +2114,7 @@ January 8 04:05:06 1999 PST
21142114
</para>
21152115
</sect3>
21162116

2117-
<sect3>
2117+
<sect3 id="datatype-datetime-special-values">
21182118
<title>Special Values</title>
21192119

21202120
<indexterm>
@@ -2202,12 +2202,26 @@ January 8 04:05:06 1999 PST
22022202
type:
22032203
<literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
22042204
<literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2205-
<literal>LOCALTIMESTAMP</literal>. The latter four accept an
2206-
optional subsecond precision specification. (See <xref
2205+
<literal>LOCALTIMESTAMP</literal>. (See <xref
22072206
linkend="functions-datetime-current"/>.) Note that these are
22082207
SQL functions and are <emphasis>not</emphasis> recognized in data input strings.
22092208
</para>
22102209

2210+
<caution>
2211+
<para>
2212+
While the input strings <literal>now</literal>,
2213+
<literal>today</literal>, <literal>tomorrow</literal>,
2214+
and <literal>yesterday</literal> are fine to use in interactive SQL
2215+
commands, they can have surprising behavior when the command is
2216+
saved to be executed later, for example in prepared statements,
2217+
views, and function definitions. The string can be converted to a
2218+
specific time value that continues to be used long after it becomes
2219+
stale. Use one of the SQL functions instead in such contexts.
2220+
For example, <literal>CURRENT_DATE + 1</literal> is safer than
2221+
<literal>'tomorrow'::date</literal>.
2222+
</para>
2223+
</caution>
2224+
22112225
</sect3>
22122226
</sect2>
22132227

doc/src/sgml/func.sgml

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8727,20 +8727,22 @@ now()
87278727
<programlisting>
87288728
SELECT CURRENT_TIMESTAMP;
87298729
SELECT now();
8730-
SELECT TIMESTAMP 'now'; -- incorrect for use with DEFAULT
8730+
SELECT TIMESTAMP 'now'; -- but see tip below
87318731
</programlisting>
87328732
</para>
87338733

87348734
<tip>
87358735
<para>
8736-
You do not want to use the third form when specifying a <literal>DEFAULT</literal>
8737-
clause while creating a table. The system will convert <literal>now</literal>
8736+
Do not use the third form when specifying a value to be evaluated later,
8737+
for example in a <literal>DEFAULT</literal> clause for a table column.
8738+
The system will convert <literal>now</literal>
87388739
to a <type>timestamp</type> as soon as the constant is parsed, so that when
87398740
the default value is needed,
87408741
the time of the table creation would be used! The first two
87418742
forms will not be evaluated until the default value is used,
87428743
because they are function calls. Thus they will give the desired
87438744
behavior of defaulting to the time of row insertion.
8745+
(See also <xref linkend="datatype-datetime-special-values"/>.)
87448746
</para>
87458747
</tip>
87468748
</sect2>

0 commit comments

Comments
 (0)