1- <!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.231 2008/11/03 22:14:40 petere Exp $ -->
1+ <!-- $PostgreSQL: pgsql/doc/src/sgml/datatype.sgml,v 1.232 2008/11/09 00:28:34 tgl Exp $ -->
22
33 <chapter id="datatype">
44 <title id="datatype-title">Data Types</title>
@@ -1419,14 +1419,6 @@ SELECT b, char_length(b) FROM test2;
14191419 <entry>294276 AD</entry>
14201420 <entry>1 microsecond / 14 digits</entry>
14211421 </row>
1422- <row>
1423- <entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
1424- <entry>12 bytes</entry>
1425- <entry>time intervals</entry>
1426- <entry>-178000000 years</entry>
1427- <entry>178000000 years</entry>
1428- <entry>1 microsecond / 14 digits</entry>
1429- </row>
14301422 <row>
14311423 <entry><type>date</type></entry>
14321424 <entry>4 bytes</entry>
@@ -1451,6 +1443,14 @@ SELECT b, char_length(b) FROM test2;
14511443 <entry>24:00:00-1459</entry>
14521444 <entry>1 microsecond / 14 digits</entry>
14531445 </row>
1446+ <row>
1447+ <entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
1448+ <entry>12 bytes</entry>
1449+ <entry>time intervals</entry>
1450+ <entry>-178000000 years</entry>
1451+ <entry>178000000 years</entry>
1452+ <entry>1 microsecond / 14 digits</entry>
1453+ </row>
14541454 </tbody>
14551455 </tgroup>
14561456 </table>
@@ -1928,65 +1928,6 @@ January 8 04:05:06 1999 PST
19281928 </para>
19291929 </sect3>
19301930
1931- <sect3>
1932- <title>Intervals</title>
1933-
1934- <indexterm>
1935- <primary>interval</primary>
1936- </indexterm>
1937-
1938- <para>
1939- <type>interval</type> values can be written with the following syntax:
1940-
1941- <programlisting>
1942- <optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
1943- </programlisting>
1944-
1945- Where: <replaceable>quantity</> is a number (possibly signed);
1946- <replaceable>unit</> is <literal>microsecond</literal>,
1947- <literal>millisecond</literal>, <literal>second</literal>,
1948- <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1949- <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1950- <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
1951- or abbreviations or plurals of these units;
1952- <replaceable>direction</> can be <literal>ago</literal> or
1953- empty. The at sign (<literal>@</>) is optional noise. The amounts
1954- of different units are implicitly added up with appropriate
1955- sign accounting. <literal>ago</literal> negates all the fields.
1956- </para>
1957-
1958- <para>
1959- Quantities of days, hours, minutes, and seconds can be specified without
1960- explicit unit markings. For example, <literal>'1 12:59:10'</> is read
1961- the same as <literal>'1 day 12 hours 59 min 10 sec'</>. Also,
1962- a combination of years and months can be specified with a dash;
1963- for example <literal>'200-10'</> is read the same as <literal>'200 years
1964- 10 months'</>. (These shorter forms are in fact the only ones allowed
1965- by the SQL standard.)
1966- </para>
1967-
1968- <para>
1969- When writing an interval constant with a <replaceable>fields</>
1970- specification, or when assigning to an interval column that was defined
1971- with a <replaceable>fields</> specification, the interpretation of
1972- unmarked quantities depends on the <replaceable>fields</>. For
1973- example <literal>INTERVAL '1' YEAR</> is read as 1 year, whereas
1974- <literal>INTERVAL '1'</> means 1 second.
1975- </para>
1976-
1977- <para>
1978- Internally <type>interval</> values are stored as months, days,
1979- and seconds. This is done because the number of days in a month
1980- varies, and a day can have 23 or 25 hours if a daylight savings
1981- time adjustment is involved. Because intervals are usually created
1982- from constant strings or <type>timestamp</> subtraction, this
1983- storage method works well in most cases. Functions
1984- <function>justify_days</> and <function>justify_hours</> are
1985- available for adjusting days and hours that overflow their normal
1986- periods.
1987- </para>
1988- </sect3>
1989-
19901931 <sect3>
19911932 <title>Special Values</title>
19921933
@@ -2189,18 +2130,6 @@ January 8 04:05:06 1999 PST
21892130 </tgroup>
21902131 </table>
21912132
2192- <para>
2193- <type>interval</type> output looks like the input format, except
2194- that units like <literal>century</literal> or
2195- <literal>week</literal> are converted to years and days and
2196- <literal>ago</literal> is converted to an appropriate sign. In
2197- ISO mode the output looks like:
2198-
2199- <programlisting>
2200- <optional> <replaceable>quantity</> <replaceable>unit</> <optional> ... </> </> <optional> <replaceable>days</> </> <optional> <replaceable>hours</>:<replaceable>minutes</>:<replaceable>seconds</> </optional>
2201- </programlisting>
2202- </para>
2203-
22042133 <para>
22052134 The date/time styles can be selected by the user using the
22062135 <command>SET datestyle</command> command, the <xref
@@ -2209,7 +2138,7 @@ January 8 04:05:06 1999 PST
22092138 <envar>PGDATESTYLE</envar> environment variable on the server or
22102139 client. The formatting function <function>to_char</function>
22112140 (see <xref linkend="functions-formatting">) is also available as
2212- a more flexible way to formatthe date/time output.
2141+ a more flexible way to format date/time output.
22132142 </para>
22142143 </sect2>
22152144
@@ -2413,6 +2342,163 @@ January 8 04:05:06 1999 PST
24132342 </para>
24142343 </sect2>
24152344
2345+ <sect2 id="datatype-interval-input">
2346+ <title>Interval Input</title>
2347+
2348+ <indexterm>
2349+ <primary>interval</primary>
2350+ </indexterm>
2351+
2352+ <para>
2353+ <type>interval</type> values can be written with the following
2354+ verbose syntax:
2355+
2356+ <programlisting>
2357+ <optional>@</> <replaceable>quantity</> <replaceable>unit</> <optional><replaceable>quantity</> <replaceable>unit</>...</> <optional><replaceable>direction</></optional>
2358+ </programlisting>
2359+
2360+ where <replaceable>quantity</> is a number (possibly signed);
2361+ <replaceable>unit</> is <literal>microsecond</literal>,
2362+ <literal>millisecond</literal>, <literal>second</literal>,
2363+ <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
2364+ <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
2365+ <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
2366+ or abbreviations or plurals of these units;
2367+ <replaceable>direction</> can be <literal>ago</literal> or
2368+ empty. The at sign (<literal>@</>) is optional noise. The amounts
2369+ of different units are implicitly added up with appropriate
2370+ sign accounting. <literal>ago</literal> negates all the fields.
2371+ This syntax is also used for interval output, if
2372+ <xref linkend="guc-intervalstyle"> is set to
2373+ <literal>postgres_verbose</>.
2374+ </para>
2375+
2376+ <para>
2377+ Quantities of days, hours, minutes, and seconds can be specified without
2378+ explicit unit markings. For example, <literal>'1 12:59:10'</> is read
2379+ the same as <literal>'1 day 12 hours 59 min 10 sec'</>. Also,
2380+ a combination of years and months can be specified with a dash;
2381+ for example <literal>'200-10'</> is read the same as <literal>'200 years
2382+ 10 months'</>. (These shorter forms are in fact the only ones allowed
2383+ by the <acronym>SQL</acronym> standard, and are used for output when
2384+ <varname>IntervalStyle</> is set to <literal>sql_standard</literal>.)
2385+ </para>
2386+
2387+ <para>
2388+ When writing an interval constant with a <replaceable>fields</>
2389+ specification, or when assigning to an interval column that was defined
2390+ with a <replaceable>fields</> specification, the interpretation of
2391+ unmarked quantities depends on the <replaceable>fields</>. For
2392+ example <literal>INTERVAL '1' YEAR</> is read as 1 year, whereas
2393+ <literal>INTERVAL '1'</> means 1 second.
2394+ </para>
2395+
2396+ <para>
2397+ According to the <acronym>SQL</> standard all fields of an interval
2398+ value must have the same sign, so a leading negative sign applies to all
2399+ fields; for example the negative sign in the interval literal
2400+ <literal>'-1 2:03:04'</> applies to both the days and hour/minute/second
2401+ parts. <productname>PostgreSQL</> allows the fields to have different
2402+ signs, and traditionally treats each field in the textual representation
2403+ as independently signed, so that the hour/minute/second part is
2404+ considered positive in this example. If <varname>IntervalStyle</> is
2405+ set to <literal>sql_standard</literal> then a leading sign is considered
2406+ to apply to all fields (but only if no additional signs appear).
2407+ Otherwise the traditional <productname>PostgreSQL</> interpretation is
2408+ used. To avoid ambiguity, it's recommended to attach an explicit sign
2409+ to each field if any field is negative.
2410+ </para>
2411+
2412+ <para>
2413+ Internally <type>interval</> values are stored as months, days,
2414+ and seconds. This is done because the number of days in a month
2415+ varies, and a day can have 23 or 25 hours if a daylight savings
2416+ time adjustment is involved. Because intervals are usually created
2417+ from constant strings or <type>timestamp</> subtraction, this
2418+ storage method works well in most cases. Functions
2419+ <function>justify_days</> and <function>justify_hours</> are
2420+ available for adjusting days and hours that overflow their normal
2421+ ranges.
2422+ </para>
2423+ </sect2>
2424+
2425+ <sect2 id="datatype-interval-output">
2426+ <title>Interval Output</title>
2427+
2428+ <indexterm>
2429+ <primary>interval</primary>
2430+ <secondary>output format</secondary>
2431+ <seealso>formatting</seealso>
2432+ </indexterm>
2433+
2434+ <para>
2435+ The output format of the interval type can be set to one of the
2436+ three styles <literal>sql_standard</>,
2437+ <literal>postgres</>, or <literal>postgres_verbose</>,
2438+ using the command <literal>SET intervalstyle</literal>.
2439+ The default is the <literal>postgres</> format.
2440+ <xref linkend="interval-style-output-table"> shows examples of each
2441+ output style.
2442+ </para>
2443+
2444+ <para>
2445+ The <literal>sql_standard</> style produces output that conforms to
2446+ the SQL standard's specification for interval literal strings, if
2447+ the interval value meets the standard's restrictions (either year-month
2448+ only or day-time only, with no mixing of positive
2449+ and negative components). Otherwise the output looks like a standard
2450+ year-month literal string followed by a day-time literal string,
2451+ with explicit signs added to disambiguate mixed-sign intervals.
2452+ </para>
2453+
2454+ <para>
2455+ The output of the <literal>postgres</> style matches the output of
2456+ <productname>PostgreSQL</> releases prior to 8.4 when the
2457+ <xref linkend="guc-datestyle"> parameter was set to <literal>ISO</>.
2458+ </para>
2459+
2460+ <para>
2461+ The output of the <literal>postgres_verbose</> style matches the output of
2462+ <productname>PostgreSQL</> releases prior to 8.4 when the
2463+ <varname>DateStyle</> parameter was set to non-<literal>ISO</> output.
2464+ </para>
2465+
2466+ <table id="interval-style-output-table">
2467+ <title>Interval Output Style Examples</title>
2468+ <tgroup cols="4">
2469+ <thead>
2470+ <row>
2471+ <entry>Style Specification</entry>
2472+ <entry>Year-Month Interval</entry>
2473+ <entry>Day-Time Interval</entry>
2474+ <entry>Mixed Interval</entry>
2475+ </row>
2476+ </thead>
2477+ <tbody>
2478+ <row>
2479+ <entry><literal>sql_standard</></entry>
2480+ <entry>1-2</entry>
2481+ <entry>3 4:05:06</entry>
2482+ <entry>-1-2 +3 -4:05:06</entry>
2483+ </row>
2484+ <row>
2485+ <entry><literal>postgres</></entry>
2486+ <entry>1 year 2 mons</entry>
2487+ <entry>3 days 04:05:06</entry>
2488+ <entry>-1 year -2 mons +3 days -04:05:06</entry>
2489+ </row>
2490+ <row>
2491+ <entry><literal>postgres_verbose</></entry>
2492+ <entry>@ 1 year 2 mons</entry>
2493+ <entry>@ 3 days 4 hours 5 mins 6 secs</entry>
2494+ <entry>@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago</entry>
2495+ </row>
2496+ </tbody>
2497+ </tgroup>
2498+ </table>
2499+
2500+ </sect2>
2501+
24162502 <sect2 id="datatype-datetime-internals">
24172503 <title>Internals</title>
24182504