Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit3f898c9

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 vaguereference to the fact that they might be evaluated too soon to besafe in non-interactive contexts. Provide a more explicit cautionagainst misuse. Also, generalize the wording in the related tip insection 9.9.4: while it clearly described this problem, it implied(or really, stated outright) that the problem only applies to tableDEFAULT 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 parentaae4097 commit3f898c9

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</> 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
@@ -8221,20 +8221,22 @@ now()
82218221
<programlisting>
82228222
SELECT CURRENT_TIMESTAMP;
82238223
SELECT now();
8224-
SELECT TIMESTAMP 'now'; --incorrect for use with DEFAULT
8224+
SELECT TIMESTAMP 'now'; --but see tip below
82258225
</programlisting>
82268226
</para>
82278227

82288228
<tip>
82298229
<para>
8230-
You do not want to use the third form when specifying a <literal>DEFAULT</>
8231-
clause while creating a table. The system will convert <literal>now</literal>
8230+
Do not use the third form when specifying a value to be evaluated later,
8231+
for example in a <literal>DEFAULT</literal> clause for a table column.
8232+
The system will convert <literal>now</literal>
82328233
to a <type>timestamp</type> as soon as the constant is parsed, so that when
82338234
the default value is needed,
82348235
the time of the table creation would be used! The first two
82358236
forms will not be evaluated until the default value is used,
82368237
because they are function calls. Thus they will give the desired
82378238
behavior of defaulting to the time of row insertion.
8239+
(See also <xref linkend="datatype-datetime-special-values">.)
82388240
</para>
82398241
</tip>
82408242
</sect2>

0 commit comments

Comments
 (0)

[8]ページ先頭

©2009-2025 Movatter.jp