2 $Header: /cvsroot/pgsql/doc/src/sgml/datatype.sgml,v 1.26 2000/03/31 03:27:40 thomas Exp $
5 <chapter id="datatype">
6 <title id="datatype-title">Data Types</title>
10 Describes the built-in data types available in
11 <productname>Postgres</productname>.
16 <productname>Postgres</productname> has a rich set of native data
17 types available to users.
18 Users may add new types to <productname>Postgres</productname> using the
19 <command>DEFINE TYPE</command> command.
23 In the context of data types, the following sections will discuss
24 <acronym>SQL</acronym> standards compliance, porting issues, and usage.
26 Some <productname>Postgres</productname> types correspond directly to
27 <acronym>SQL92</acronym>-compatible types. In other
28 cases, data types defined by <acronym>SQL92</acronym> syntax are mapped directly
29 into native <productname>Postgres</productname> types.
31 Many of the built-in types have obvious external formats. However, several
32 types are either unique to <productname>Postgres</productname>,
33 such as open and closed paths, or have
34 several possibilities for formats, such as the date and time types.
39 <title><productname>Postgres</productname> Data Types</title>
40 <titleabbrev>Data Types</titleabbrev>
44 <entry><productname>Postgres</productname> Type</entry>
45 <entry><acronym>SQL92</acronym> or <acronym>SQL3</acronym> Type</entry>
46 <entry>Description</entry>
52 <entry>boolean</entry>
53 <entry>logical boolean (true/false)</entry>
58 <entry>rectangular box in 2D plane</entry>
61 <entry>char(n)</entry>
62 <entry>character(n)</entry>
63 <entry>fixed-length character string</entry>
68 <entry>IP version 4 network or host address</entry>
73 <entry>circle in 2D plane</entry>
78 <entry>calendar date without time of day</entry>
81 <entry>decimal</entry>
82 <entry>decimal(p,s)</entry>
83 <entry>exact numeric for p <= 9, s = 0</entry>
87 <entry>float(<replaceable>p</replaceable>), <replaceable>p</replaceable> < 7</entry>
88 <entry>floating-point number with precision <replaceable>p</replaceable></entry>
92 <entry>float(<replaceable>p</replaceable>), 7 <= <replaceable>p</replaceable> < 16</entry>
93 <entry>floating-point number with precision <replaceable>p</replaceable></entry>
98 <entry>IP version 4 network or host address</entry>
102 <entry>smallint</entry>
103 <entry>signed two-byte integer</entry>
107 <entry>int, integer</entry>
108 <entry>signed 4-byte integer</entry>
113 <entry>signed 8-byte integer</entry>
116 <entry>interval</entry>
117 <entry>interval</entry>
118 <entry>general-use time span</entry>
123 <entry>infinite line in 2D plane</entry>
128 <entry>line segment in 2D plane</entry>
132 <entry>decimal(9,2)</entry>
133 <entry>US-style currency</entry>
136 <entry>numeric</entry>
137 <entry>numeric(p,s)</entry>
138 <entry>exact numeric for p == 9, s = 0</entry>
143 <entry>open and closed geometric path in 2D plane</entry>
148 <entry>geometric point in 2D plane</entry>
151 <entry>polygon</entry>
153 <entry>closed geometric path in 2D plane</entry>
156 <entry>serial</entry>
158 <entry>unique id for indexing and cross-reference</entry>
163 <entry>time of day</entry>
166 <entry>timetz</entry>
167 <entry>time with time zone</entry>
168 <entry>time of day, including time zone</entry>
171 <entry>timestamp</entry>
172 <entry>timestamp with time zone</entry>
173 <entry>date/time</entry>
176 <entry>varchar(n)</entry>
177 <entry>character varying(n)</entry>
178 <entry>variable-length character string</entry>
188 The <type>cidr</type> and <type>inet</type> types are designed to handle any IP type
189 but only ipv4 is handled in the current implementation.
190 Everything here that talks about ipv4 will apply to ipv6 in a
198 <title><productname>Postgres</productname> Function Constants</title>
199 <titleabbrev>Constants</titleabbrev>
203 <entry><productname>Postgres</productname> Function</entry>
204 <entry><acronym>SQL92</acronym> Constant</entry>
205 <entry>Description</entry>
210 <entry>getpgusername()</entry>
211 <entry>current_user</entry>
212 <entry>user name in current session</entry>
215 <entry>date('now')</entry>
216 <entry>current_date</entry>
217 <entry>date of current transaction</entry>
220 <entry>time('now')</entry>
221 <entry>current_time</entry>
222 <entry>time of current transaction</entry>
225 <entry>timestamp('now')</entry>
226 <entry>current_timestamp</entry>
227 <entry>date and time of current transaction</entry>
235 <productname>Postgres</productname> has features at the forefront of
236 <acronym>ORDBMS</acronym> development. In addition to
237 <acronym>SQL3</acronym> conformance, substantial portions
238 of <acronym>SQL92</acronym> are also supported.
239 Although we strive for <acronym>SQL92</acronym> compliance,
240 there are some aspects of the standard
241 which are ill considered and which should not live through subsequent standards.
242 <productname>Postgres</productname> will not make great efforts to
243 conform to these features; however, these tend to apply in little-used
244 or obsure cases, and a typical user is not likely to run into them.
248 Most of the input and output functions corresponding to the
249 base types (e.g., integers and floating point numbers) do some
251 Some of the operators and functions (e.g.,
252 addition and multiplication) do not perform run-time error-checking in the
253 interests of improving execution speed.
254 On some systems, for example, the numeric operators for some data types may
255 silently underflow or overflow.
259 Some of the input and output functions are not invertible. That is,
260 the result of an output function may lose precision when compared to
265 The original <productname>Postgres</productname> v4.2 code received from
266 Berkeley rounded all double precision floating point results to six digits for
267 output. Starting with v6.1, floating point numbers are allowed to retain
268 most of the intrinsic precision of the type (typically 15 digits for doubles,
269 6 digits for 4-byte floats).
270 Other types with underlying floating point fields (e.g. geometric
271 types) carry similar precision.
277 <title>Numeric Types</title>
280 Numeric types consist of two- and four-byte integers and four- and eight-byte
281 floating point numbers.
286 <title><productname>Postgres</productname> Numeric Types</title>
287 <titleabbrev>Numerics</titleabbrev>
291 <entry>Numeric Type</entry>
292 <entry>Storage</entry>
293 <entry>Description</entry>
299 <entry>decimal</entry>
300 <entry>variable</entry>
301 <entry>User-specified precision</entry>
302 <entry>no limit</entry>
305 <entry>float4</entry>
306 <entry>4 bytes</entry>
307 <entry>Variable-precision</entry>
308 <entry>6 decimal places</entry>
311 <entry>float8</entry>
312 <entry>8 bytes</entry>
313 <entry>Variable-precision</entry>
314 <entry>15 decimal places</entry>
318 <entry>2 bytes</entry>
319 <entry>Fixed-precision</entry>
320 <entry>-32768 to +32767</entry>
324 <entry>4 bytes</entry>
325 <entry>Usual choice for fixed-precision</entry>
326 <entry>-2147483648 to +2147483647</entry>
330 <entry>8 bytes</entry>
331 <entry>Very large range fixed-precision</entry>
332 <entry>+/- > 18 decimal places</entry>
335 <entry>numeric</entry>
336 <entry>variable</entry>
337 <entry>User-specified precision</entry>
338 <entry>no limit</entry>
341 <entry>serial</entry>
342 <entry>4 bytes</entry>
343 <entry>Identifer or cross-reference</entry>
344 <entry>0 to +2147483647</entry>
352 The numeric types have a full set of corresponding arithmetic operators and
353 functions. Refer to <xref endterm="math-opers" linkend="math-opers">
354 and <xref endterm="math-funcs" linkend="math-funcs"> for more information.
358 The <type>int8</type> type may not be available on all platforms since
359 it relies on compiler support for this.
363 <title>The Serial Type</title>
366 The <type>serial</type> type is a special-case type constructed by
367 <productname>Postgres</productname> from other existing components.
368 It is typically used to create unique identifiers for table entries.
369 In the current implementation, specifying
372 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
375 is equivalent to specifying:
378 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq;
379 CREATE TABLE <replaceable class="parameter">tablename</replaceable>
380 (<replaceable class="parameter">colname</replaceable> INT4 DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq');
381 CREATE UNIQUE INDEX <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_key on <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable>);
386 The implicit sequence created for the <type>serial</type> type will
387 <emphasis>not</emphasis> be automatically removed when the
392 Implicit sequences supporting the <type>serial</type> are
393 not automatically dropped when a table containing a serial type
394 is dropped. So, the following commands executed in order will likely fail:
397 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
398 DROP TABLE <replaceable class="parameter">tablename</replaceable>;
399 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (<replaceable class="parameter">colname</replaceable> SERIAL);
402 The sequence will remain in the database until explicitly dropped using
403 <command>DROP SEQUENCE</command>.
409 <title>Monetary Type</title>
412 <title>Obsolete Type</title>
414 The <type>money</type> is now deprecated. Use <type>numeric</type>
415 or <type>decimal</type> instead. The money type may become a
416 locale-aware layer over the numeric type in a future release.
421 The <type>money</type> type supports US-style currency with
422 fixed decimal point representation.
423 If <productname>Postgres</productname> is compiled with USE_LOCALE
424 then the money type should use the monetary conventions defined for
425 <citetitle>locale(7)</citetitle>.
430 <title><productname>Postgres</productname> Monetary Types</title>
431 <titleabbrev>Money</titleabbrev>
435 <entry>Monetary Type</entry>
436 <entry>Storage</entry>
437 <entry>Description</entry>
444 <entry>4 bytes</entry>
445 <entry>Fixed-precision</entry>
446 <entry>-21474836.48 to +21474836.47</entry>
455 will replace the money type, and should be preferred.
461 <title>Character Types</title>
464 <acronym>SQL92</acronym> defines two primary character types:
465 <type>char</type> and <type>varchar</type>.
466 <productname>Postgres</productname> supports these types, in
467 addition to the more general <type>text</type> type,
468 which unlike <type>varchar</type>
469 does not require an explicit declared upper
470 limit on the size of the field.
475 <title><productname>Postgres</productname> Character Types</title>
476 <titleabbrev>Characters</titleabbrev>
480 <entry>Character Type</entry>
481 <entry>Storage</entry>
482 <entry>Recommendation</entry>
483 <entry>Description</entry>
489 <entry>1 byte</entry>
490 <entry><acronym>SQL92</acronym>-compatible</entry>
491 <entry>Single character</entry>
494 <entry>char(n)</entry>
495 <entry>(4+n) bytes</entry>
496 <entry><acronym>SQL92</acronym>-compatible</entry>
497 <entry>Fixed-length blank padded</entry>
501 <entry>(4+x) bytes</entry>
502 <entry>Best choice</entry>
503 <entry>Variable-length</entry>
506 <entry>varchar(n)</entry>
507 <entry>(4+n) bytes</entry>
508 <entry><acronym>SQL92</acronym>-compatible</entry>
509 <entry>Variable-length with limit</entry>
517 There is one other fixed-length character type in <productname>Postgres</productname>.
518 The <type>name</type> type
519 only has one purpose and that is for storage of internal catalog
521 It is not intended for use by the general user.
522 Its length is currently defined as 32 bytes (31 characters plus terminator)
523 but should be reference using NAMEDATALEN.
524 The length is set at compile time (and is therefore adjustable for
525 special uses); the default maximum length may change in a future release.
530 <title><productname>Postgres</productname> Specialty Character Type</title>
531 <titleabbrev>Specialty Characters</titleabbrev>
535 <entry>Character Type</entry>
536 <entry>Storage</entry>
537 <entry>Description</entry>
543 <entry>32 bytes</entry>
544 <entry>Thirty-one character internal type</entry>
554 <title>Date/Time Types</title>
557 <productname>PostgreSQL</productname> supports the full set of
558 <acronym>SQL</acronym> date and time types.
563 <title><productname>PostgreSQL</productname> Date/Time Types</title>
564 <titleabbrev>Date/Time</titleabbrev>
569 <entry>Description</entry>
570 <entry>Storage</entry>
571 <entry>Earliest</entry>
572 <entry>Latest</entry>
573 <entry>Resolution</entry>
578 <entry><type>timestamp</type></entry>
579 <entry>for data containing both date and time</entry>
580 <entry>8 bytes</entry>
581 <entry>4713 BC</entry>
582 <entry>AD 1465001</entry>
583 <entry>1 microsec / 14 digits</entry>
586 <entry><type>timestamp with time zone</type></entry>
587 <entry>date and time including time zone</entry>
588 <entry>8 bytes</entry>
589 <entry>1903 AD</entry>
590 <entry>2037 AD</entry>
591 <entry>1 microsec / 14 digits</entry>
594 <entry><type>interval</type></entry>
595 <entry>for time intervals</entry>
596 <entry>12 bytes</entry>
597 <entry>-178000000 years</entry>
598 <entry>178000000 years</entry>
599 <entry>1 mircosecond</entry>
602 <entry><type>date</type></entry>
603 <entry>for data containing only dates</entry>
604 <entry>4 bytes</entry>
605 <entry>4713 BC</entry>
606 <entry>32767 AD</entry>
610 <entry><type>time</type></entry>
611 <entry>for data containing only times of the day</entry>
612 <entry>4 bytes</entry>
613 <entry>00:00:00.00</entry>
614 <entry>23:59:59.99</entry>
615 <entry>1 microsecond</entry>
618 <entry><type>time with time zone</type></entry>
619 <entry>times of the day</entry>
620 <entry>4 bytes</entry>
621 <entry>00:00:00.00+12</entry>
622 <entry>23:59:59.99-12</entry>
623 <entry>1 microsecond</entry>
631 To ensure compatibility to earlier versions of <productname>PostgreSQL</productname>
632 we also continue to provide <type>datetime</type> (equivalent to <type>timestamp</type>) and
633 <type>timespan</type> (equivalent to <type>interval</type>). The types <type>abstime</type>
634 and <type>reltime</type> are lower precision types which are used internally.
635 You are discouraged from using any of these types in new
636 applications and are encouraged to move any old
637 ones over when appropriate. Any or all of these types might disappear in a future release.
644 <title>Date/Time Input</title>
647 Date and time input is accepted in almost any reasonable format, including
648 <acronym>ISO-8601</acronym>, <acronym>SQL</acronym>-compatible,
649 traditional <productname>Postgres</productname>, and others.
650 The ordering of month and day in date input can be ambiguous, therefore a setting
651 exists to specify how it should be interpreted. The command
652 <literal>SET DateStyle TO 'US'</literal> or <literal>SET DateStyle TO 'NonEuropean'</literal>
653 specifies the variant <quote>month before day</quote>, the command
654 <literal>SET DateStyle TO 'European'</literal> sets the variant
655 <quote>day before month</quote>. The <literal>US</literal> style
656 is the default but this default can be changed at compile time or at run time.
660 See <xref endterm="datetime-appendix-title" linkend="datetime-appendix-title">
661 for the exact parsing rules of date/time input and for the recognized time zones.
665 Remember that any date or time input needs to be enclosed into single quotes,
672 The following are possible inputs for the <type>date</type> type.
675 <title><productname>PostgreSQL</productname> Date Input</title>
676 <titleabbrev>Date Inputs</titleabbrev>
680 <entry>Example</entry>
681 <entry>Description</entry>
686 <entry>January 8, 1999</entry>
687 <entry>Unambiguous</entry>
690 <entry>1999-01-08</entry>
691 <entry>ISO-8601 format, preferred</entry>
694 <entry>1/8/1999</entry>
695 <entry>US; read as August 1 in European mode</entry>
698 <entry>8/1/1999</entry>
699 <entry>European; read as August 1 in US mode</entry>
702 <entry>1/18/1999</entry>
703 <entry>US; read as January 18 in any mode</entry>
706 <entry>1999.008</entry>
707 <entry>Year and day of year</entry>
710 <entry>19990108</entry>
711 <entry>ISO-8601 year, month, day</entry>
714 <entry>990108</entry>
715 <entry>ISO-8601 year, month, day</entry>
718 <entry>1999.008</entry>
719 <entry>Year and day of year</entry>
723 <entry>Year and day of year</entry>
726 <entry>January 8, 99 BC</entry>
727 <entry>Year 99 before the common era</entry>
736 <title><productname>PostgreSQL</productname> Month Abbreviations</title>
737 <titleabbrev>Month Abbreviations</titleabbrev>
742 <entry>Abbreviations</entry>
751 <entry>August</entry>
755 <entry>December</entry>
759 <entry>February</entry>
763 <entry>January</entry>
779 <entry>November</entry>
783 <entry>October</entry>
787 <entry>September</entry>
788 <entry>Sep, Sept</entry>
796 The month <literal>May</literal> has no explicit abbreviation, for obvious reasons.
803 <title><productname>PostgreSQL</productname> Day of Week Abbreviations</title>
804 <titleabbrev>Day of Week Abbreviations</titleabbrev>
809 <entry>Abbreviation</entry>
814 <entry>Sunday</entry>
818 <entry>Monday</entry>
822 <entry>Tuesday</entry>
823 <entry>Tue, Tues</entry>
826 <entry>Wednesday</entry>
827 <entry>Wed, Weds</entry>
830 <entry>Thursday</entry>
831 <entry>Thu, Thur, Thurs</entry>
834 <entry>Friday</entry>
838 <entry>Saturday</entry>
850 The following are valid <type>time</type> inputs.
853 <title><productname>PostgreSQL</productname> Time Input</title>
854 <titleabbrev>Time Inputs</titleabbrev>
858 <entry>Example</entry>
859 <entry>Description</entry>
864 <entry>04:05:06.789</entry>
865 <entry>ISO-8601</entry>
868 <entry>04:05:06</entry>
869 <entry>ISO-8601</entry>
873 <entry>ISO-8601</entry>
876 <entry>040506</entry>
877 <entry>ISO-8601</entry>
880 <entry>04:05 AM</entry>
881 <entry>Same as 04:05; AM does not affect value</entry>
884 <entry>04:05 PM</entry>
885 <entry>Same as 16:05; input hour must be <= 12</entry>
889 <entry>Same as 00:00:00</entry>
893 <entry>Same as 00:00:00</entry>
896 <entry>allballs</entry>
897 <entry>Same as 00:00:00</entry>
906 <title>time with time zone</title>
908 This type is defined by SQL92, but the definition exhibits
909 fundamental deficiencies which renders the type near useless. In
910 most cases, a combination of <type>date</type>,
911 <type>time</type>, and <type>timestamp with time zone</type>
912 should provide a complete range of date/time functionality
913 required by an application.
917 <type>time with time zone</type> accepts all input also legal
918 for the <type>time</type> type, appended with a legal time zone,
922 <title><productname>PostgreSQL</productname> Time With Time
924 <titleabbrev>Time With Time Zone Inputs</titleabbrev>
928 <entry>Example</entry>
929 <entry>Description</entry>
934 <entry>04:05:06.789-8</entry>
935 <entry>ISO-8601</entry>
938 <entry>04:05:06-08:00</entry>
939 <entry>ISO-8601</entry>
942 <entry>04:05-08:00</entry>
943 <entry>ISO-8601</entry>
946 <entry>040506-08</entry>
947 <entry>ISO-8601</entry>
955 Refer to <xref endterm="timezone-title" linkend="timezone"> for
956 more examples of time zones.
961 <title>timestamp</title>
963 Valid input for the <type>timestamp</type> type consists of a concatenation
964 of a date and a time, followed by an optional <literal>AD</literal> or
965 <literal>BC</literal>, followed by an optional time zone. (See below.)
968 1999-01-08 04:05:06 -8:00
970 is a valid <type>timestamp</type> value, which is <acronym>ISO</acronym>-compliant.
971 In addition, the wide-spread format
973 January 8 04:05:06 1999 PST
979 <table tocentry="1" id="timezone">
980 <title id="timezone-title"><productname>PostgreSQL</productname> Time Zone Input</title>
981 <titleabbrev>Time Zone Inputs</titleabbrev>
985 <entry>Time Zone</entry>
986 <entry>Description</entry>
992 <entry>Pacific Standard Time</entry>
996 <entry>ISO-8601 offset for PST</entry>
1000 <entry>ISO-8601 offset for PST</entry>
1004 <entry>ISO-8601 offset for PST</entry>
1013 <title>interval</title>
1015 <type>interval</type>s can be specified with the following syntax:
1017 Quantity Unit [Quantity Unit...] [Direction]
1018 @ Quantity Unit [Direction]
1020 where: <literal>Quantity</literal> is ..., <literal>-1</literal>,
1021 <literal>0</literal>, <literal>1</literal>, <literal>2</literal>, ...;
1022 <literal>Unit</literal> is <literal>second</literal>,
1023 <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
1024 <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
1025 <literal>decade</literal>, <literal>century</literal>, <literal>millenium</literal>,
1026 or abbreviations or plurals of these units;
1027 <literal>Direction</literal> can be <literal>ago</literal> or
1033 <title>Special values</title>
1035 The following <acronym>SQL</acronym>-compatible functions can be used as date or time
1036 input for the corresponding datatype: <literal>CURRENT_DATE</literal>,
1037 <literal>CURRENT_TIME</literal>, <literal>CURRENT_TIMESTAMP</literal>.
1040 <productname>PostgreSQL</productname> also supports several special constants for
1043 <table tocentry="1">
1044 <title><productname>PostgresSQL</productname> Special Date/Time Constants</title>
1045 <titleabbrev>Constants</titleabbrev>
1049 <entry>Constant</entry>
1050 <entry>Description</entry>
1055 <entry>current</entry>
1056 <entry>Current transaction time, deferred</entry>
1059 <entry>epoch</entry>
1060 <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
1063 <entry>infinity</entry>
1064 <entry>Later than other valid times</entry>
1067 <entry>-infinity</entry>
1068 <entry>Earlier than other valid times</entry>
1071 <entry>invalid</entry>
1072 <entry>Illegal entry</entry>
1076 <entry>Current transaction time</entry>
1079 <entry>today</entry>
1080 <entry>Midnight today</entry>
1083 <entry>tomorrow</entry>
1084 <entry>Midnight tomorrow</entry>
1087 <entry>yesterday</entry>
1088 <entry>Midnight yesterday</entry>
1093 <literal>'now'</literal> is resolved when the value is inserted, <literal>'current'</literal>
1094 is resolved everytime the value is retrieved. So you probably want to use <literal>'now'</literal>
1095 in most applications. (Of course you <emphasis>really</emphasis> want to use
1096 <literal>CURRENT_TIMESTAMP</literal>, which is equivalent to <literal>'now'</literal>.)
1104 <title>Date/Time Output</title>
1107 Output formats can be set to one of the four styles
1108 ISO-8601, <acronym>SQL</acronym> (Ingres), traditional
1109 Postgres, and German, using the <command>SET DateStyle</command>.
1110 The default is the <acronym>ISO</acronym> format.
1112 <table tocentry="1">
1113 <title><productname>PostgreSQL</productname> Date/Time Output Styles</title>
1114 <titleabbrev>Styles</titleabbrev>
1118 <entry>Style Specification</entry>
1119 <entry>Description</entry>
1120 <entry>Example</entry>
1125 <entry>'ISO'</entry>
1126 <entry>ISO-8601 standard</entry>
1127 <entry>1997-12-17 07:37:16-08</entry>
1130 <entry>'SQL'</entry>
1131 <entry>Traditional style</entry>
1132 <entry>12/17/1997 07:37:16.00 PST</entry>
1135 <entry>'Postgres'</entry>
1136 <entry>Original style</entry>
1137 <entry>Wed Dec 17 07:37:16 1997 PST</entry>
1140 <entry>'German'</entry>
1141 <entry>Regional style</entry>
1142 <entry>17.12.1997 07:37:16.00 PST</entry>
1150 The output of the <type>date</type> and <type>time</type> styles is of course
1151 only the date or time part in accordance with the above examples
1155 The <acronym>SQL</acronym> style has European and non-European (US) variants,
1156 which determines whether month follows day or vica versa. (See also above
1157 at Date/Time Input, how this setting affects interpretation of input values.)
1159 <table tocentry="1">
1160 <title><productname>PostgreSQL</productname> Date Order Conventions</title>
1161 <titleabbrev>Order</titleabbrev>
1165 <entry>Style Specification</entry>
1166 <entry>Example</entry>
1171 <entry>European</entry>
1172 <entry>17/12/1997 15:37:16.00 MET</entry>
1176 <entry>12/17/1997 07:37:16.00 PST</entry>
1184 <type>interval</type> output looks like the input format, expect that units like
1185 <literal>week</literal> or <literal>century</literal> are converted to years and days.
1186 In ISO mode the output looks like
1188 [ Quantity Units [ ... ] ] [ Days ] Hours:Minutes [ ago ]
1193 There are several ways to affect the appearance of date/time types:
1195 <itemizedlist spacing="compact" mark="bullet">
1198 The <envar>PGDATESTYLE</envar> environment variable used by the backend directly
1199 on postmaster startup.
1204 The <envar>PGDATESTYLE</envar> environment variable used by the frontend libpq
1210 <command>SET DATESTYLE</command> <acronym>SQL</acronym> command.
1219 <title>Time Zones</title>
1222 <productname>PostgreSQL</productname> endeavors to be compatible with
1223 <acronym>SQL92</acronym> definitions for typical usage.
1224 However, the <acronym>SQL92</acronym> standard has an odd mix of date and
1225 time types and capabilities. Two obvious problems are:
1230 Although the <type>date</type> type
1231 does not have an associated time zone, the
1232 <type>time</type> type can or does.
1238 The default time zone is specified as a constant integer offset
1245 Time zones in the real world can have no meaning unless
1246 associated with a date as well as a time
1247 since the offset may vary through the year with daylight savings
1252 To address these difficulties, <productname>PostgreSQL</productname>
1253 associates time zones only with date and time
1254 types which contain both date and time,
1255 and assumes local time for any type containing only
1256 date or time. Further, time zone support is derived from
1257 the underlying operating system
1258 time zone capabilities, and hence can handle daylight savings time
1259 and other expected behavior.
1263 <productname>PostgreSQL</productname> obtains time zone support
1264 from the underlying operating system for dates between 1902 and
1265 2038 (near the typical date limits for Unix-style
1266 systems). Outside of this range, all dates are assumed to be
1267 specified and used in Universal Coordinated Time (UTC).
1271 All dates and times are stored internally in Universal UTC,
1272 alternately known as Greenwich Mean Time (GMT).
1273 Times are converted to local time on the database server before being
1274 sent to the client frontend, hence by default are in the server
1279 There are several ways to affect the time zone behavior:
1281 <itemizedlist spacing="compact" mark="bullet">
1284 The TZ environment variable used by the backend directly
1285 on postmaster startup as the default time zone.
1290 The PGTZ environment variable set at the client used by libpq
1291 to send time zone information to the backend upon connection.
1296 The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
1297 sets the time zone for the session.
1304 If an invalid time zone is specified,
1305 the time zone becomes GMT (on most systems anyway).
1312 If the compiler option USE_AUSTRALIAN_RULES is set
1313 then <literal>EST</literal> refers to Australia Eastern Std Time,
1314 which has an offset of +10:00 hours from UTC.
1322 <title>Internals</title>
1325 <productname>PostgreSQL</productname> uses Julian dates
1326 for all date/time calculations. They have the nice property of correctly
1327 predicting/calculating any date more recent than 4713BC
1328 to far into the future, using the assumption that the length of the
1329 year is 365.2425 days.
1333 Date conventions before the 19th century make for interesting reading,
1334 but are not consistant enough to warrant coding into a date/time handler.
1341 <title>Boolean Type</title>
1344 <productname>Postgres</productname> supports <type>bool</type> as
1345 the <acronym>SQL3</acronym> boolean type.
1346 <type>bool</type> can have one of only two states: 'true' or 'false'.
1347 A third state, 'unknown', is not
1348 implemented and is not suggested in <acronym>SQL3</acronym>;
1349 <acronym>NULL</acronym> is an
1350 effective substitute. <type>bool</type> can be used in any boolean expression,
1351 and boolean expressions
1352 always evaluate to a result compatible with this type.</para>
1355 <type>bool</type> uses 1 byte of storage.
1359 <table tocentry="1">
1360 <title><productname>Postgres</productname> Boolean Type</title>
1361 <titleabbrev>Booleans</titleabbrev>
1365 <entry>State</entry>
1366 <entry>Output</entry>
1367 <entry>Input</entry>
1374 <entry>TRUE, 't', 'true', 'y', 'yes', '1'</entry>
1377 <entry>False</entry>
1379 <entry>FALSE, 'f', 'false', 'n', 'no', '0'</entry>
1388 <title>Geometric Types</title>
1391 Geometric types represent two-dimensional spatial objects.
1392 The most fundamental type,
1393 the point, forms the basis for all of the other types.
1397 <table tocentry="1">
1398 <title><productname>Postgres</productname> Geometric Types</title>
1399 <titleabbrev>Geometrics</titleabbrev>
1403 <entry>Geometric Type</entry>
1404 <entry>Storage</entry>
1405 <entry>Representation</entry>
1406 <entry>Description</entry>
1411 <entry>point</entry>
1412 <entry>16 bytes</entry>
1413 <entry>(x,y)</entry>
1414 <entry>Point in space</entry>
1418 <entry>32 bytes</entry>
1419 <entry>((x1,y1),(x2,y2))</entry>
1420 <entry>Infinite line</entry>
1424 <entry>32 bytes</entry>
1425 <entry>((x1,y1),(x2,y2))</entry>
1426 <entry>Finite line segment</entry>
1430 <entry>32 bytes</entry>
1431 <entry>((x1,y1),(x2,y2))</entry>
1432 <entry>Rectangular box</entry>
1436 <entry>4+32n bytes</entry>
1437 <entry>((x1,y1),...)</entry>
1438 <entry>Closed path (similar to polygon)</entry>
1442 <entry>4+32n bytes</entry>
1443 <entry>[(x1,y1),...]</entry>
1444 <entry>Open path</entry>
1447 <entry>polygon</entry>
1448 <entry>4+32n bytes</entry>
1449 <entry>((x1,y1),...)</entry>
1450 <entry>Polygon (similar to closed path)</entry>
1453 <entry>circle</entry>
1454 <entry>24 bytes</entry>
1455 <entry><(x,y),r></entry>
1456 <entry>Circle (center and radius)</entry>
1464 A rich set of functions and operators is available to perform various geometric
1465 operations such as scaling, translation, rotation, and determining
1470 <title>Point</title>
1473 Points are the fundamental two-dimensional building block for geometric types.
1477 <type>point</type> is specified using the following syntax:
1483 x is the x-axis coordinate as a floating point number
1484 y is the y-axis coordinate as a floating point number
1490 <title>Line Segment</title>
1493 Line segments (<type>lseg</type>) are represented by pairs of points.
1497 <type>lseg</type> is specified using the following syntax:
1499 ( ( x1 , y1 ) , ( x2 , y2 ) )
1500 ( x1 , y1 ) , ( x2 , y2 )
1503 (x1,y1) and (x2,y2) are the endpoints of the segment
1512 Boxes are represented by pairs of points which are opposite
1517 <type>box</type> is specified using the following syntax:
1520 ( ( x1 , y1 ) , ( x2 , y2 ) )
1521 ( x1 , y1 ) , ( x2 , y2 )
1524 (x1,y1) and (x2,y2) are opposite corners
1527 Boxes are output using the first syntax.
1528 The corners are reordered on input to store
1529 the lower left corner first and the upper right corner last.
1530 Other corners of the box can be entered, but the lower
1531 left and upper right corners are determined from the input and stored.
1539 Paths are represented by connected sets of points. Paths can be "open", where
1540 the first and last points in the set are not connected, and "closed",
1541 where the first and last point are connected. Functions
1542 <function>popen(p)</function>
1544 <function>pclose(p)</function>
1545 are supplied to force a path to be open or closed, and functions
1546 <function>isopen(p)</function>
1548 <function>isclosed(p)</function>
1549 are supplied to select either type in a query.
1553 <type>path</type> is specified using the following syntax:
1556 ( ( x1 , y1 ) , ... , ( xn , yn ) )
1557 [ ( x1 , y1 ) , ... , ( xn , yn ) ]
1558 ( x1 , y1 ) , ... , ( xn , yn )
1559 ( x1 , y1 , ... , xn , yn )
1560 x1 , y1 , ... , xn , yn
1562 (x1,y1),...,(xn,yn) are points 1 through n
1563 a leading "[" indicates an open path
1564 a leading "(" indicates a closed path
1567 Paths are output using the first syntax.
1568 Note that <productname>Postgres</productname> versions prior to
1569 v6.1 used a format for paths which had a single leading parenthesis,
1571 an integer count of the number of points, then the list of points followed by a
1572 closing parenthesis.
1573 The built-in function <function>upgradepath</function> is supplied to convert
1574 paths dumped and reloaded from pre-v6.1 databases.
1579 <title>Polygon</title>
1582 Polygons are represented by sets of points. Polygons should probably be
1583 considered equivalent to closed paths, but are stored differently
1584 and have their own set of support routines.
1588 <type>polygon</type> is specified using the following syntax:
1591 ( ( x1 , y1 ) , ... , ( xn , yn ) )
1592 ( x1 , y1 ) , ... , ( xn , yn )
1593 ( x1 , y1 , ... , xn , yn )
1594 x1 , y1 , ... , xn , yn
1596 (x1,y1),...,(xn,yn) are points 1 through n
1599 Polygons are output using the first syntax.
1600 Note that <productname>Postgres</productname> versions prior to
1601 v6.1 used a format for polygons which had a single leading parenthesis, the list
1602 of x-axis coordinates, the list of y-axis coordinates,
1603 followed by a closing parenthesis.
1604 The built-in function <function>upgradepoly</function> is supplied to convert
1605 polygons dumped and reloaded from pre-v6.1 databases.
1610 <title>Circle</title>
1613 Circles are represented by a center point and a radius.
1617 <type>circle</type> is specified using the following syntax:
1625 (x,y) is the center of the circle
1626 r is the radius of the circle
1629 Circles are output using the first syntax.
1636 <title>IP Version 4 Networks and Host Addresses</title>
1639 The <type>cidr</type> type stores networks specified
1640 in <acronym>CIDR</acronym> (Classless Inter-Domain Routing) notation.
1641 The <type>inet</type> type stores hosts and networks in CIDR notation using a simple
1642 variation in representation to represent simple host TCP/IP addresses.
1646 <table tocentry="1">
1647 <title><productname>Postgres</productname>IP Version 4 Types</title>
1648 <titleabbrev>IPV4</titleabbrev>
1652 <entry>IPV4 Type</entry>
1653 <entry>Storage</entry>
1654 <entry>Description</entry>
1655 <entry>Range</entry>
1661 <entry>variable</entry>
1662 <entry>CIDR networks</entry>
1663 <entry>Valid IPV4 CIDR blocks</entry>
1667 <entry>variable</entry>
1668 <entry>nets and hosts</entry>
1669 <entry>Valid IPV4 CIDR blocks</entry>
1680 The <type>cidr</type> type holds a CIDR network.
1681 The format for specifying classless networks is
1682 <replaceable class="parameter">x.x.x.x/y</replaceable>
1683 where <replaceable class="parameter">x.x.x.x</replaceable> is the
1684 network and <replaceable class="parameter">/y</replaceable> is
1685 the number of bits in the netmask.
1686 If <replaceable class="parameter">/y</replaceable> omitted, it is
1687 calculated using assumptions from
1688 the older classfull naming system except that it is extended to include at least
1689 all of the octets in the input.
1693 Here are some examples:
1695 <table tocentry="1">
1696 <title><productname>Postgres</productname>IP Types Examples</title>
1700 <entry>CIDR Input</entry>
1701 <entry>CIDR Displayed</entry>
1706 <entry>192.168.1</entry>
1707 <entry>192.168.1/24</entry>
1710 <entry>192.168</entry>
1711 <entry>192.168.0/24</entry>
1714 <entry>128.1</entry>
1715 <entry>128.1/16</entry>
1719 <entry>128.0/16</entry>
1722 <entry>128.1.2</entry>
1723 <entry>128.1.2/24</entry>
1726 <entry>10.1.2</entry>
1727 <entry>10.1.2/24</entry>
1731 <entry>10.1/16</entry>
1744 <title id="inet-type"><type>inet</type></title>
1747 The <type>inet</type> type is designed to hold, in one field, all of the information
1748 about a host including the CIDR-style subnet that it is in.
1749 Note that if you want to store proper CIDR networks,
1750 you should use the <type>cidr</type> type.
1751 The <type>inet</type> type is similar to the <type>cidr</type>
1752 type except that the bits in the
1753 host part can be non-zero.
1754 Functions exist to extract the various elements of the field.
1758 The input format for this function is
1759 <replaceable class="parameter">x.x.x.x/y</replaceable>
1760 where <replaceable class="parameter">x.x.x.x</replaceable> is
1761 an internet host and <replaceable class="parameter">y</replaceable>
1762 is the number of bits in the netmask.
1763 If the <replaceable class="parameter">/y</replaceable> part is left off,
1764 it is treated as <literal>/32</literal>.
1765 On output, the <replaceable class="parameter">/y</replaceable> part is not printed
1766 if it is <literal>/32</literal>.
1767 This allows the type to be used as a straight host type by just leaving off
1775 <!-- Keep this comment at the end of the file
1780 sgml-minimize-attributes:nil
1781 sgml-always-quote-attributes:t
1784 sgml-parent-document:nil
1785 sgml-default-dtd-file:"./reference.ced"
1786 sgml-exposed-tags:nil
1787 sgml-local-catalogs:("/usr/lib/sgml/catalog")
1788 sgml-local-ecat-files:nil