Repair memory leaks in plpython.
[pgsql.git] / doc / src / sgml / datatype.sgml
blob3e6751d64cc70f6b5d5b44fd62ce8d6db1cede15
1 <!-- doc/src/sgml/datatype.sgml -->
3 <chapter id="datatype">
4 <title>Data Types</title>
6 <indexterm zone="datatype">
7 <primary>data type</primary>
8 </indexterm>
10 <indexterm>
11 <primary>type</primary>
12 <see>data type</see>
13 </indexterm>
15 <para>
16 <productname>PostgreSQL</productname> has a rich set of native data
17 types available to users. Users can add new types to
18 <productname>PostgreSQL</productname> using the <xref
19 linkend="sql-createtype"/> command.
20 </para>
22 <para>
23 <xref linkend="datatype-table"/> shows all the built-in general-purpose data
24 types. Most of the alternative names listed in the
25 <quote>Aliases</quote> column are the names used internally by
26 <productname>PostgreSQL</productname> for historical reasons. In
27 addition, some internally used or deprecated types are available,
28 but are not listed here.
29 </para>
31 <table id="datatype-table">
32 <title>Data Types</title>
33 <tgroup cols="3">
34 <colspec colname="col1" colwidth="2*"/>
35 <colspec colname="col2" colwidth="1*"/>
36 <colspec colname="col3" colwidth="2*"/>
37 <thead>
38 <row>
39 <entry>Name</entry>
40 <entry>Aliases</entry>
41 <entry>Description</entry>
42 </row>
43 </thead>
45 <tbody>
46 <row>
47 <entry><type>bigint</type></entry>
48 <entry><type>int8</type></entry>
49 <entry>signed eight-byte integer</entry>
50 </row>
52 <row>
53 <entry><type>bigserial</type></entry>
54 <entry><type>serial8</type></entry>
55 <entry>autoincrementing eight-byte integer</entry>
56 </row>
58 <row>
59 <entry><type>bit [ (<replaceable>n</replaceable>) ]</type></entry>
60 <entry></entry>
61 <entry>fixed-length bit string</entry>
62 </row>
64 <row>
65 <entry><type>bit varying [ (<replaceable>n</replaceable>) ]</type></entry>
66 <entry><type>varbit [ (<replaceable>n</replaceable>) ]</type></entry>
67 <entry>variable-length bit string</entry>
68 </row>
70 <row>
71 <entry><type>boolean</type></entry>
72 <entry><type>bool</type></entry>
73 <entry>logical Boolean (true/false)</entry>
74 </row>
76 <row>
77 <entry><type>box</type></entry>
78 <entry></entry>
79 <entry>rectangular box on a plane</entry>
80 </row>
82 <row>
83 <entry><type>bytea</type></entry>
84 <entry></entry>
85 <entry>binary data (<quote>byte array</quote>)</entry>
86 </row>
88 <row>
89 <entry><type>character [ (<replaceable>n</replaceable>) ]</type></entry>
90 <entry><type>char [ (<replaceable>n</replaceable>) ]</type></entry>
91 <entry>fixed-length character string</entry>
92 </row>
94 <row>
95 <entry><type>character varying [ (<replaceable>n</replaceable>) ]</type></entry>
96 <entry><type>varchar [ (<replaceable>n</replaceable>) ]</type></entry>
97 <entry>variable-length character string</entry>
98 </row>
100 <row>
101 <entry><type>cidr</type></entry>
102 <entry></entry>
103 <entry>IPv4 or IPv6 network address</entry>
104 </row>
106 <row>
107 <entry><type>circle</type></entry>
108 <entry></entry>
109 <entry>circle on a plane</entry>
110 </row>
112 <row>
113 <entry><type>date</type></entry>
114 <entry></entry>
115 <entry>calendar date (year, month, day)</entry>
116 </row>
118 <row>
119 <entry><type>double precision</type></entry>
120 <entry><type>float8</type></entry>
121 <entry>double precision floating-point number (8 bytes)</entry>
122 </row>
124 <row>
125 <entry><type>inet</type></entry>
126 <entry></entry>
127 <entry>IPv4 or IPv6 host address</entry>
128 </row>
130 <row>
131 <entry><type>integer</type></entry>
132 <entry><type>int</type>, <type>int4</type></entry>
133 <entry>signed four-byte integer</entry>
134 </row>
136 <row>
137 <entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
138 <entry></entry>
139 <entry>time span</entry>
140 </row>
142 <row>
143 <entry><type>json</type></entry>
144 <entry></entry>
145 <entry>textual JSON data</entry>
146 </row>
148 <row>
149 <entry><type>jsonb</type></entry>
150 <entry></entry>
151 <entry>binary JSON data, decomposed</entry>
152 </row>
154 <row>
155 <entry><type>line</type></entry>
156 <entry></entry>
157 <entry>infinite line on a plane</entry>
158 </row>
160 <row>
161 <entry><type>lseg</type></entry>
162 <entry></entry>
163 <entry>line segment on a plane</entry>
164 </row>
166 <row>
167 <entry><type>macaddr</type></entry>
168 <entry></entry>
169 <entry>MAC (Media Access Control) address</entry>
170 </row>
172 <row>
173 <entry><type>macaddr8</type></entry>
174 <entry></entry>
175 <entry>MAC (Media Access Control) address (EUI-64 format)</entry>
176 </row>
178 <row>
179 <entry><type>money</type></entry>
180 <entry></entry>
181 <entry>currency amount</entry>
182 </row>
184 <row>
185 <entry><type>numeric [ (<replaceable>p</replaceable>,
186 <replaceable>s</replaceable>) ]</type></entry>
187 <entry><type>decimal [ (<replaceable>p</replaceable>,
188 <replaceable>s</replaceable>) ]</type></entry>
189 <entry>exact numeric of selectable precision</entry>
190 </row>
192 <row>
193 <entry><type>path</type></entry>
194 <entry></entry>
195 <entry>geometric path on a plane</entry>
196 </row>
198 <row>
199 <entry><type>pg_lsn</type></entry>
200 <entry></entry>
201 <entry><productname>PostgreSQL</productname> Log Sequence Number</entry>
202 </row>
204 <row>
205 <entry><type>pg_snapshot</type></entry>
206 <entry></entry>
207 <entry>user-level transaction ID snapshot</entry>
208 </row>
210 <row>
211 <entry><type>point</type></entry>
212 <entry></entry>
213 <entry>geometric point on a plane</entry>
214 </row>
216 <row>
217 <entry><type>polygon</type></entry>
218 <entry></entry>
219 <entry>closed geometric path on a plane</entry>
220 </row>
222 <row>
223 <entry><type>real</type></entry>
224 <entry><type>float4</type></entry>
225 <entry>single precision floating-point number (4 bytes)</entry>
226 </row>
228 <row>
229 <entry><type>smallint</type></entry>
230 <entry><type>int2</type></entry>
231 <entry>signed two-byte integer</entry>
232 </row>
234 <row>
235 <entry><type>smallserial</type></entry>
236 <entry><type>serial2</type></entry>
237 <entry>autoincrementing two-byte integer</entry>
238 </row>
240 <row>
241 <entry><type>serial</type></entry>
242 <entry><type>serial4</type></entry>
243 <entry>autoincrementing four-byte integer</entry>
244 </row>
246 <row>
247 <entry><type>text</type></entry>
248 <entry></entry>
249 <entry>variable-length character string</entry>
250 </row>
252 <row>
253 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
254 <entry></entry>
255 <entry>time of day (no time zone)</entry>
256 </row>
258 <row>
259 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
260 <entry><type>timetz</type></entry>
261 <entry>time of day, including time zone</entry>
262 </row>
264 <row>
265 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
266 <entry></entry>
267 <entry>date and time (no time zone)</entry>
268 </row>
270 <row>
271 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
272 <entry><type>timestamptz</type></entry>
273 <entry>date and time, including time zone</entry>
274 </row>
276 <row>
277 <entry><type>tsquery</type></entry>
278 <entry></entry>
279 <entry>text search query</entry>
280 </row>
282 <row>
283 <entry><type>tsvector</type></entry>
284 <entry></entry>
285 <entry>text search document</entry>
286 </row>
288 <row>
289 <entry><type>txid_snapshot</type></entry>
290 <entry></entry>
291 <entry>user-level transaction ID snapshot (deprecated; see <type>pg_snapshot</type>)</entry>
292 </row>
294 <row>
295 <entry><type>uuid</type></entry>
296 <entry></entry>
297 <entry>universally unique identifier</entry>
298 </row>
300 <row>
301 <entry><type>xml</type></entry>
302 <entry></entry>
303 <entry>XML data</entry>
304 </row>
305 </tbody>
306 </tgroup>
307 </table>
309 <note>
310 <title>Compatibility</title>
311 <para>
312 The following types (or spellings thereof) are specified by
313 <acronym>SQL</acronym>: <type>bigint</type>, <type>bit</type>, <type>bit
314 varying</type>, <type>boolean</type>, <type>char</type>,
315 <type>character varying</type>, <type>character</type>,
316 <type>varchar</type>, <type>date</type>, <type>double
317 precision</type>, <type>integer</type>, <type>interval</type>,
318 <type>numeric</type>, <type>decimal</type>, <type>real</type>,
319 <type>smallint</type>, <type>time</type> (with or without time zone),
320 <type>timestamp</type> (with or without time zone),
321 <type>xml</type>.
322 </para>
323 </note>
325 <para>
326 Each data type has an external representation determined by its input
327 and output functions. Many of the built-in types have
328 obvious external formats. However, several types are either unique
329 to <productname>PostgreSQL</productname>, such as geometric
330 paths, or have several possible formats, such as the date
331 and time types.
332 Some of the input and output functions are not invertible, i.e.,
333 the result of an output function might lose accuracy when compared to
334 the original input.
335 </para>
337 <sect1 id="datatype-numeric">
338 <title>Numeric Types</title>
340 <indexterm zone="datatype-numeric">
341 <primary>data type</primary>
342 <secondary>numeric</secondary>
343 </indexterm>
345 <para>
346 Numeric types consist of two-, four-, and eight-byte integers,
347 four- and eight-byte floating-point numbers, and selectable-precision
348 decimals. <xref linkend="datatype-numeric-table"/> lists the
349 available types.
350 </para>
352 <table id="datatype-numeric-table">
353 <title>Numeric Types</title>
354 <tgroup cols="4">
355 <colspec colname="col1" colwidth="2*"/>
356 <colspec colname="col2" colwidth="1*"/>
357 <colspec colname="col3" colwidth="2*"/>
358 <colspec colname="col4" colwidth="2*"/>
359 <thead>
360 <row>
361 <entry>Name</entry>
362 <entry>Storage Size</entry>
363 <entry>Description</entry>
364 <entry>Range</entry>
365 </row>
366 </thead>
368 <tbody>
369 <row>
370 <entry><type>smallint</type></entry>
371 <entry>2 bytes</entry>
372 <entry>small-range integer</entry>
373 <entry>-32768 to +32767</entry>
374 </row>
375 <row>
376 <entry><type>integer</type></entry>
377 <entry>4 bytes</entry>
378 <entry>typical choice for integer</entry>
379 <entry>-2147483648 to +2147483647</entry>
380 </row>
381 <row>
382 <entry><type>bigint</type></entry>
383 <entry>8 bytes</entry>
384 <entry>large-range integer</entry>
385 <entry>-9223372036854775808 to +9223372036854775807</entry>
386 </row>
388 <row>
389 <entry><type>decimal</type></entry>
390 <entry>variable</entry>
391 <entry>user-specified precision, exact</entry>
392 <entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
393 </row>
394 <row>
395 <entry><type>numeric</type></entry>
396 <entry>variable</entry>
397 <entry>user-specified precision, exact</entry>
398 <entry>up to 131072 digits before the decimal point; up to 16383 digits after the decimal point</entry>
399 </row>
401 <row>
402 <entry><type>real</type></entry>
403 <entry>4 bytes</entry>
404 <entry>variable-precision, inexact</entry>
405 <entry>6 decimal digits precision</entry>
406 </row>
407 <row>
408 <entry><type>double precision</type></entry>
409 <entry>8 bytes</entry>
410 <entry>variable-precision, inexact</entry>
411 <entry>15 decimal digits precision</entry>
412 </row>
414 <row>
415 <entry><type>smallserial</type></entry>
416 <entry>2 bytes</entry>
417 <entry>small autoincrementing integer</entry>
418 <entry>1 to 32767</entry>
419 </row>
421 <row>
422 <entry><type>serial</type></entry>
423 <entry>4 bytes</entry>
424 <entry>autoincrementing integer</entry>
425 <entry>1 to 2147483647</entry>
426 </row>
428 <row>
429 <entry><type>bigserial</type></entry>
430 <entry>8 bytes</entry>
431 <entry>large autoincrementing integer</entry>
432 <entry>1 to 9223372036854775807</entry>
433 </row>
434 </tbody>
435 </tgroup>
436 </table>
438 <para>
439 The syntax of constants for the numeric types is described in
440 <xref linkend="sql-syntax-constants"/>. The numeric types have a
441 full set of corresponding arithmetic operators and
442 functions. Refer to <xref linkend="functions"/> for more
443 information. The following sections describe the types in detail.
444 </para>
446 <sect2 id="datatype-int">
447 <title>Integer Types</title>
449 <indexterm zone="datatype-int">
450 <primary>integer</primary>
451 </indexterm>
453 <indexterm zone="datatype-int">
454 <primary>smallint</primary>
455 </indexterm>
457 <indexterm zone="datatype-int">
458 <primary>bigint</primary>
459 </indexterm>
461 <indexterm>
462 <primary>int4</primary>
463 <see>integer</see>
464 </indexterm>
466 <indexterm>
467 <primary>int2</primary>
468 <see>smallint</see>
469 </indexterm>
471 <indexterm>
472 <primary>int8</primary>
473 <see>bigint</see>
474 </indexterm>
476 <para>
477 The types <type>smallint</type>, <type>integer</type>, and
478 <type>bigint</type> store whole numbers, that is, numbers without
479 fractional components, of various ranges. Attempts to store
480 values outside of the allowed range will result in an error.
481 </para>
483 <para>
484 The type <type>integer</type> is the common choice, as it offers
485 the best balance between range, storage size, and performance.
486 The <type>smallint</type> type is generally only used if disk
487 space is at a premium. The <type>bigint</type> type is designed to be
488 used when the range of the <type>integer</type> type is insufficient.
489 </para>
491 <para>
492 <acronym>SQL</acronym> only specifies the integer types
493 <type>integer</type> (or <type>int</type>),
494 <type>smallint</type>, and <type>bigint</type>. The
495 type names <type>int2</type>, <type>int4</type>, and
496 <type>int8</type> are extensions, which are also used by some
497 other <acronym>SQL</acronym> database systems.
498 </para>
500 </sect2>
502 <sect2 id="datatype-numeric-decimal">
503 <title>Arbitrary Precision Numbers</title>
505 <indexterm>
506 <primary>numeric (data type)</primary>
507 </indexterm>
509 <indexterm>
510 <primary>arbitrary precision numbers</primary>
511 </indexterm>
513 <indexterm>
514 <primary>decimal</primary>
515 <see>numeric</see>
516 </indexterm>
518 <para>
519 The type <type>numeric</type> can store numbers with a
520 very large number of digits. It is especially recommended for
521 storing monetary amounts and other quantities where exactness is
522 required. Calculations with <type>numeric</type> values yield exact
523 results where possible, e.g., addition, subtraction, multiplication.
524 However, calculations on <type>numeric</type> values are very slow
525 compared to the integer types, or to the floating-point types
526 described in the next section.
527 </para>
529 <para>
530 We use the following terms below: The
531 <firstterm>precision</firstterm> of a <type>numeric</type>
532 is the total count of significant digits in the whole number,
533 that is, the number of digits to both sides of the decimal point.
534 The <firstterm>scale</firstterm> of a <type>numeric</type> is the
535 count of decimal digits in the fractional part, to the right of the
536 decimal point. So the number 23.5141 has a precision of 6 and a
537 scale of 4. Integers can be considered to have a scale of zero.
538 </para>
540 <para>
541 Both the maximum precision and the maximum scale of a
542 <type>numeric</type> column can be
543 configured. To declare a column of type <type>numeric</type> use
544 the syntax:
545 <programlisting>
546 NUMERIC(<replaceable>precision</replaceable>, <replaceable>scale</replaceable>)
547 </programlisting>
548 The precision must be positive, while the scale may be positive or
549 negative (see below). Alternatively:
550 <programlisting>
551 NUMERIC(<replaceable>precision</replaceable>)
552 </programlisting>
553 selects a scale of 0. Specifying:
554 <programlisting>
555 NUMERIC
556 </programlisting>
557 without any precision or scale creates an <quote>unconstrained
558 numeric</quote> column in which numeric values of any length can be
559 stored, up to the implementation limits. A column of this kind will
560 not coerce input values to any particular scale, whereas
561 <type>numeric</type> columns with a declared scale will coerce
562 input values to that scale. (The <acronym>SQL</acronym> standard
563 requires a default scale of 0, i.e., coercion to integer
564 precision. We find this a bit useless. If you're concerned
565 about portability, always specify the precision and scale
566 explicitly.)
567 </para>
569 <note>
570 <para>
571 The maximum precision that can be explicitly specified in
572 a <type>numeric</type> type declaration is 1000. An
573 unconstrained <type>numeric</type> column is subject to the limits
574 described in <xref linkend="datatype-numeric-table"/>.
575 </para>
576 </note>
578 <para>
579 If the scale of a value to be stored is greater than the declared
580 scale of the column, the system will round the value to the specified
581 number of fractional digits. Then, if the number of digits to the
582 left of the decimal point exceeds the declared precision minus the
583 declared scale, an error is raised.
584 For example, a column declared as
585 <programlisting>
586 NUMERIC(3, 1)
587 </programlisting>
588 will round values to 1 decimal place and can store values between
589 -99.9 and 99.9, inclusive.
590 </para>
592 <para>
593 Beginning in <productname>PostgreSQL</productname> 15, it is allowed
594 to declare a <type>numeric</type> column with a negative scale. Then
595 values will be rounded to the left of the decimal point. The
596 precision still represents the maximum number of non-rounded digits.
597 Thus, a column declared as
598 <programlisting>
599 NUMERIC(2, -3)
600 </programlisting>
601 will round values to the nearest thousand and can store values
602 between -99000 and 99000, inclusive.
603 It is also allowed to declare a scale larger than the declared
604 precision. Such a column can only hold fractional values, and it
605 requires the number of zero digits just to the right of the decimal
606 point to be at least the declared scale minus the declared precision.
607 For example, a column declared as
608 <programlisting>
609 NUMERIC(3, 5)
610 </programlisting>
611 will round values to 5 decimal places and can store values between
612 -0.00999 and 0.00999, inclusive.
613 </para>
615 <note>
616 <para>
617 <productname>PostgreSQL</productname> permits the scale in a
618 <type>numeric</type> type declaration to be any value in the range
619 -1000 to 1000. However, the <acronym>SQL</acronym> standard requires
620 the scale to be in the range 0 to <replaceable>precision</replaceable>.
621 Using scales outside that range may not be portable to other database
622 systems.
623 </para>
624 </note>
626 <para>
627 Numeric values are physically stored without any extra leading or
628 trailing zeroes. Thus, the declared precision and scale of a column
629 are maximums, not fixed allocations. (In this sense the <type>numeric</type>
630 type is more akin to <type>varchar(<replaceable>n</replaceable>)</type>
631 than to <type>char(<replaceable>n</replaceable>)</type>.) The actual storage
632 requirement is two bytes for each group of four decimal digits,
633 plus three to eight bytes overhead.
634 </para>
636 <indexterm>
637 <primary>infinity</primary>
638 <secondary>numeric (data type)</secondary>
639 </indexterm>
641 <indexterm>
642 <primary>NaN</primary>
643 <see>not a number</see>
644 </indexterm>
646 <indexterm>
647 <primary>not a number</primary>
648 <secondary>numeric (data type)</secondary>
649 </indexterm>
651 <para>
652 In addition to ordinary numeric values, the <type>numeric</type> type
653 has several special values:
654 <literallayout>
655 <literal>Infinity</literal>
656 <literal>-Infinity</literal>
657 <literal>NaN</literal>
658 </literallayout>
659 These are adapted from the IEEE 754 standard, and represent
660 <quote>infinity</quote>, <quote>negative infinity</quote>, and
661 <quote>not-a-number</quote>, respectively. When writing these values
662 as constants in an SQL command, you must put quotes around them,
663 for example <literal>UPDATE table SET x = '-Infinity'</literal>.
664 On input, these strings are recognized in a case-insensitive manner.
665 The infinity values can alternatively be spelled <literal>inf</literal>
666 and <literal>-inf</literal>.
667 </para>
669 <para>
670 The infinity values behave as per mathematical expectations. For
671 example, <literal>Infinity</literal> plus any finite value equals
672 <literal>Infinity</literal>, as does <literal>Infinity</literal>
673 plus <literal>Infinity</literal>; but <literal>Infinity</literal>
674 minus <literal>Infinity</literal> yields <literal>NaN</literal> (not a
675 number), because it has no well-defined interpretation. Note that an
676 infinity can only be stored in an unconstrained <type>numeric</type>
677 column, because it notionally exceeds any finite precision limit.
678 </para>
680 <para>
681 The <literal>NaN</literal> (not a number) value is used to represent
682 undefined calculational results. In general, any operation with
683 a <literal>NaN</literal> input yields another <literal>NaN</literal>.
684 The only exception is when the operation's other inputs are such that
685 the same output would be obtained if the <literal>NaN</literal> were to
686 be replaced by any finite or infinite numeric value; then, that output
687 value is used for <literal>NaN</literal> too. (An example of this
688 principle is that <literal>NaN</literal> raised to the zero power
689 yields one.)
690 </para>
692 <note>
693 <para>
694 In most implementations of the <quote>not-a-number</quote> concept,
695 <literal>NaN</literal> is not considered equal to any other numeric
696 value (including <literal>NaN</literal>). In order to allow
697 <type>numeric</type> values to be sorted and used in tree-based
698 indexes, <productname>PostgreSQL</productname> treats <literal>NaN</literal>
699 values as equal, and greater than all non-<literal>NaN</literal>
700 values.
701 </para>
702 </note>
704 <para>
705 The types <type>decimal</type> and <type>numeric</type> are
706 equivalent. Both types are part of the <acronym>SQL</acronym>
707 standard.
708 </para>
710 <para>
711 When rounding values, the <type>numeric</type> type rounds ties away
712 from zero, while (on most machines) the <type>real</type>
713 and <type>double precision</type> types round ties to the nearest even
714 number. For example:
716 <programlisting>
717 SELECT x,
718 round(x::numeric) AS num_round,
719 round(x::double precision) AS dbl_round
720 FROM generate_series(-3.5, 3.5, 1) as x;
721 x | num_round | dbl_round
722 ------+-----------+-----------
723 -3.5 | -4 | -4
724 -2.5 | -3 | -2
725 -1.5 | -2 | -2
726 -0.5 | -1 | -0
727 0.5 | 1 | 0
728 1.5 | 2 | 2
729 2.5 | 3 | 2
730 3.5 | 4 | 4
731 (8 rows)
732 </programlisting>
733 </para>
734 </sect2>
737 <sect2 id="datatype-float">
738 <title>Floating-Point Types</title>
740 <indexterm zone="datatype-float">
741 <primary>real</primary>
742 </indexterm>
744 <indexterm zone="datatype-float">
745 <primary>double precision</primary>
746 </indexterm>
748 <indexterm>
749 <primary>float4</primary>
750 <see>real</see>
751 </indexterm>
753 <indexterm>
754 <primary>float8</primary>
755 <see>double precision</see>
756 </indexterm>
758 <indexterm zone="datatype-float">
759 <primary>floating point</primary>
760 </indexterm>
762 <para>
763 The data types <type>real</type> and <type>double precision</type> are
764 inexact, variable-precision numeric types. On all currently supported
765 platforms, these types are implementations of <acronym>IEEE</acronym>
766 Standard 754 for Binary Floating-Point Arithmetic (single and double
767 precision, respectively), to the extent that the underlying processor,
768 operating system, and compiler support it.
769 </para>
771 <para>
772 Inexact means that some values cannot be converted exactly to the
773 internal format and are stored as approximations, so that storing
774 and retrieving a value might show slight discrepancies.
775 Managing these errors and how they propagate through calculations
776 is the subject of an entire branch of mathematics and computer
777 science and will not be discussed here, except for the
778 following points:
779 <itemizedlist>
780 <listitem>
781 <para>
782 If you require exact storage and calculations (such as for
783 monetary amounts), use the <type>numeric</type> type instead.
784 </para>
785 </listitem>
787 <listitem>
788 <para>
789 If you want to do complicated calculations with these types
790 for anything important, especially if you rely on certain
791 behavior in boundary cases (infinity, underflow), you should
792 evaluate the implementation carefully.
793 </para>
794 </listitem>
796 <listitem>
797 <para>
798 Comparing two floating-point values for equality might not
799 always work as expected.
800 </para>
801 </listitem>
802 </itemizedlist>
803 </para>
805 <para>
806 On all currently supported platforms, the <type>real</type> type has a
807 range of around 1E-37 to 1E+37 with a precision of at least 6 decimal
808 digits. The <type>double precision</type> type has a range of around
809 1E-307 to 1E+308 with a precision of at least 15 digits. Values that are
810 too large or too small will cause an error. Rounding might take place if
811 the precision of an input number is too high. Numbers too close to zero
812 that are not representable as distinct from zero will cause an underflow
813 error.
814 </para>
816 <para>
817 By default, floating point values are output in text form in their
818 shortest precise decimal representation; the decimal value produced is
819 closer to the true stored binary value than to any other value
820 representable in the same binary precision. (However, the output value is
821 currently never <emphasis>exactly</emphasis> midway between two
822 representable values, in order to avoid a widespread bug where input
823 routines do not properly respect the round-to-nearest-even rule.) This value will
824 use at most 17 significant decimal digits for <type>float8</type>
825 values, and at most 9 digits for <type>float4</type> values.
826 </para>
828 <note>
829 <para>
830 This shortest-precise output format is much faster to generate than the
831 historical rounded format.
832 </para>
833 </note>
835 <para>
836 For compatibility with output generated by older versions
837 of <productname>PostgreSQL</productname>, and to allow the output
838 precision to be reduced, the <xref linkend="guc-extra-float-digits"/>
839 parameter can be used to select rounded decimal output instead. Setting a
840 value of 0 restores the previous default of rounding the value to 6
841 (for <type>float4</type>) or 15 (for <type>float8</type>)
842 significant decimal digits. Setting a negative value reduces the number
843 of digits further; for example -2 would round output to 4 or 13 digits
844 respectively.
845 </para>
847 <para>
848 Any value of <xref linkend="guc-extra-float-digits"/> greater than 0
849 selects the shortest-precise format.
850 </para>
852 <note>
853 <para>
854 Applications that wanted precise values have historically had to set
855 <xref linkend="guc-extra-float-digits"/> to 3 to obtain them. For
856 maximum compatibility between versions, they should continue to do so.
857 </para>
858 </note>
860 <indexterm>
861 <primary>infinity</primary>
862 <secondary>floating point</secondary>
863 </indexterm>
865 <indexterm>
866 <primary>not a number</primary>
867 <secondary>floating point</secondary>
868 </indexterm>
870 <para>
871 In addition to ordinary numeric values, the floating-point types
872 have several special values:
873 <literallayout>
874 <literal>Infinity</literal>
875 <literal>-Infinity</literal>
876 <literal>NaN</literal>
877 </literallayout>
878 These represent the IEEE 754 special values
879 <quote>infinity</quote>, <quote>negative infinity</quote>, and
880 <quote>not-a-number</quote>, respectively. When writing these values
881 as constants in an SQL command, you must put quotes around them,
882 for example <literal>UPDATE table SET x = '-Infinity'</literal>. On input,
883 these strings are recognized in a case-insensitive manner.
884 The infinity values can alternatively be spelled <literal>inf</literal>
885 and <literal>-inf</literal>.
886 </para>
888 <note>
889 <para>
890 IEEE 754 specifies that <literal>NaN</literal> should not compare equal
891 to any other floating-point value (including <literal>NaN</literal>).
892 In order to allow floating-point values to be sorted and used
893 in tree-based indexes, <productname>PostgreSQL</productname> treats
894 <literal>NaN</literal> values as equal, and greater than all
895 non-<literal>NaN</literal> values.
896 </para>
897 </note>
899 <para>
900 <productname>PostgreSQL</productname> also supports the SQL-standard
901 notations <type>float</type> and
902 <type>float(<replaceable>p</replaceable>)</type> for specifying
903 inexact numeric types. Here, <replaceable>p</replaceable> specifies
904 the minimum acceptable precision in <emphasis>binary</emphasis> digits.
905 <productname>PostgreSQL</productname> accepts
906 <type>float(1)</type> to <type>float(24)</type> as selecting the
907 <type>real</type> type, while
908 <type>float(25)</type> to <type>float(53)</type> select
909 <type>double precision</type>. Values of <replaceable>p</replaceable>
910 outside the allowed range draw an error.
911 <type>float</type> with no precision specified is taken to mean
912 <type>double precision</type>.
913 </para>
915 </sect2>
917 <sect2 id="datatype-serial">
918 <title>Serial Types</title>
920 <indexterm zone="datatype-serial">
921 <primary>smallserial</primary>
922 </indexterm>
924 <indexterm zone="datatype-serial">
925 <primary>serial</primary>
926 </indexterm>
928 <indexterm zone="datatype-serial">
929 <primary>bigserial</primary>
930 </indexterm>
932 <indexterm zone="datatype-serial">
933 <primary>serial2</primary>
934 </indexterm>
936 <indexterm zone="datatype-serial">
937 <primary>serial4</primary>
938 </indexterm>
940 <indexterm zone="datatype-serial">
941 <primary>serial8</primary>
942 </indexterm>
944 <indexterm>
945 <primary>auto-increment</primary>
946 <see>serial</see>
947 </indexterm>
949 <indexterm>
950 <primary>sequence</primary>
951 <secondary>and serial type</secondary>
952 </indexterm>
954 <note>
955 <para>
956 This section describes a PostgreSQL-specific way to create an
957 autoincrementing column. Another way is to use the SQL-standard
958 identity column feature, described at <xref linkend="ddl-identity-columns"/>.
959 </para>
960 </note>
962 <para>
963 The data types <type>smallserial</type>, <type>serial</type> and
964 <type>bigserial</type> are not true types, but merely
965 a notational convenience for creating unique identifier columns
966 (similar to the <literal>AUTO_INCREMENT</literal> property
967 supported by some other databases). In the current
968 implementation, specifying:
970 <programlisting>
971 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
972 <replaceable class="parameter">colname</replaceable> SERIAL
974 </programlisting>
976 is equivalent to specifying:
978 <programlisting>
979 CREATE SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq AS integer;
980 CREATE TABLE <replaceable class="parameter">tablename</replaceable> (
981 <replaceable class="parameter">colname</replaceable> integer NOT NULL DEFAULT nextval('<replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq')
983 ALTER SEQUENCE <replaceable class="parameter">tablename</replaceable>_<replaceable class="parameter">colname</replaceable>_seq OWNED BY <replaceable class="parameter">tablename</replaceable>.<replaceable class="parameter">colname</replaceable>;
984 </programlisting>
986 Thus, we have created an integer column and arranged for its default
987 values to be assigned from a sequence generator. A <literal>NOT NULL</literal>
988 constraint is applied to ensure that a null value cannot be
989 inserted. (In most cases you would also want to attach a
990 <literal>UNIQUE</literal> or <literal>PRIMARY KEY</literal> constraint to prevent
991 duplicate values from being inserted by accident, but this is
992 not automatic.) Lastly, the sequence is marked as <quote>owned by</quote>
993 the column, so that it will be dropped if the column or table is dropped.
994 </para>
996 <note>
997 <para>
998 Because <type>smallserial</type>, <type>serial</type> and
999 <type>bigserial</type> are implemented using sequences, there may
1000 be "holes" or gaps in the sequence of values which appears in the
1001 column, even if no rows are ever deleted. A value allocated
1002 from the sequence is still "used up" even if a row containing that
1003 value is never successfully inserted into the table column. This
1004 may happen, for example, if the inserting transaction rolls back.
1005 See <literal>nextval()</literal> in <xref linkend="functions-sequence"/>
1006 for details.
1007 </para>
1008 </note>
1010 <para>
1011 To insert the next value of the sequence into the <type>serial</type>
1012 column, specify that the <type>serial</type>
1013 column should be assigned its default value. This can be done
1014 either by excluding the column from the list of columns in
1015 the <command>INSERT</command> statement, or through the use of
1016 the <literal>DEFAULT</literal> key word.
1017 </para>
1019 <para>
1020 The type names <type>serial</type> and <type>serial4</type> are
1021 equivalent: both create <type>integer</type> columns. The type
1022 names <type>bigserial</type> and <type>serial8</type> work
1023 the same way, except that they create a <type>bigint</type>
1024 column. <type>bigserial</type> should be used if you anticipate
1025 the use of more than 2<superscript>31</superscript> identifiers over the
1026 lifetime of the table. The type names <type>smallserial</type> and
1027 <type>serial2</type> also work the same way, except that they
1028 create a <type>smallint</type> column.
1029 </para>
1031 <para>
1032 The sequence created for a <type>serial</type> column is
1033 automatically dropped when the owning column is dropped.
1034 You can drop the sequence without dropping the column, but this
1035 will force removal of the column default expression.
1036 </para>
1037 </sect2>
1038 </sect1>
1040 <sect1 id="datatype-money">
1041 <title>Monetary Types</title>
1043 <para>
1044 The <type>money</type> type stores a currency amount with a fixed
1045 fractional precision; see <xref
1046 linkend="datatype-money-table"/>. The fractional precision is
1047 determined by the database's <xref linkend="guc-lc-monetary"/> setting.
1048 The range shown in the table assumes there are two fractional digits.
1049 Input is accepted in a variety of formats, including integer and
1050 floating-point literals, as well as typical
1051 currency formatting, such as <literal>'$1,000.00'</literal>.
1052 Output is generally in the latter form but depends on the locale.
1053 </para>
1055 <table id="datatype-money-table">
1056 <title>Monetary Types</title>
1057 <tgroup cols="4">
1058 <colspec colname="col1" colwidth="2*"/>
1059 <colspec colname="col2" colwidth="1*"/>
1060 <colspec colname="col3" colwidth="2*"/>
1061 <colspec colname="col4" colwidth="2*"/>
1062 <thead>
1063 <row>
1064 <entry>Name</entry>
1065 <entry>Storage Size</entry>
1066 <entry>Description</entry>
1067 <entry>Range</entry>
1068 </row>
1069 </thead>
1070 <tbody>
1071 <row>
1072 <entry><type>money</type></entry>
1073 <entry>8 bytes</entry>
1074 <entry>currency amount</entry>
1075 <entry>-92233720368547758.08 to +92233720368547758.07</entry>
1076 </row>
1077 </tbody>
1078 </tgroup>
1079 </table>
1081 <para>
1082 Since the output of this data type is locale-sensitive, it might not
1083 work to load <type>money</type> data into a database that has a different
1084 setting of <varname>lc_monetary</varname>. To avoid problems, before
1085 restoring a dump into a new database make sure <varname>lc_monetary</varname> has
1086 the same or equivalent value as in the database that was dumped.
1087 </para>
1089 <para>
1090 Values of the <type>numeric</type>, <type>int</type>, and
1091 <type>bigint</type> data types can be cast to <type>money</type>.
1092 Conversion from the <type>real</type> and <type>double precision</type>
1093 data types can be done by casting to <type>numeric</type> first, for
1094 example:
1095 <programlisting>
1096 SELECT '12.34'::float8::numeric::money;
1097 </programlisting>
1098 However, this is not recommended. Floating point numbers should not be
1099 used to handle money due to the potential for rounding errors.
1100 </para>
1102 <para>
1103 A <type>money</type> value can be cast to <type>numeric</type> without
1104 loss of precision. Conversion to other types could potentially lose
1105 precision, and must also be done in two stages:
1106 <programlisting>
1107 SELECT '52093.89'::money::numeric::float8;
1108 </programlisting>
1109 </para>
1111 <para>
1112 Division of a <type>money</type> value by an integer value is performed
1113 with truncation of the fractional part towards zero. To get a rounded
1114 result, divide by a floating-point value, or cast the <type>money</type>
1115 value to <type>numeric</type> before dividing and back to <type>money</type>
1116 afterwards. (The latter is preferable to avoid risking precision loss.)
1117 When a <type>money</type> value is divided by another <type>money</type>
1118 value, the result is <type>double precision</type> (i.e., a pure number,
1119 not money); the currency units cancel each other out in the division.
1120 </para>
1121 </sect1>
1124 <sect1 id="datatype-character">
1125 <title>Character Types</title>
1127 <indexterm zone="datatype-character">
1128 <primary>character string</primary>
1129 <secondary>data types</secondary>
1130 </indexterm>
1132 <indexterm>
1133 <primary>string</primary>
1134 <see>character string</see>
1135 </indexterm>
1137 <indexterm zone="datatype-character">
1138 <primary>character</primary>
1139 </indexterm>
1141 <indexterm zone="datatype-character">
1142 <primary>character varying</primary>
1143 </indexterm>
1145 <indexterm zone="datatype-character">
1146 <primary>text</primary>
1147 </indexterm>
1149 <indexterm zone="datatype-character">
1150 <primary>char</primary>
1151 </indexterm>
1153 <indexterm zone="datatype-character">
1154 <primary>varchar</primary>
1155 </indexterm>
1157 <indexterm zone="datatype-character">
1158 <primary>bpchar</primary>
1159 </indexterm>
1161 <table id="datatype-character-table">
1162 <title>Character Types</title>
1163 <tgroup cols="2">
1164 <thead>
1165 <row>
1166 <entry>Name</entry>
1167 <entry>Description</entry>
1168 </row>
1169 </thead>
1170 <tbody>
1171 <row>
1172 <entry><type>character varying(<replaceable>n</replaceable>)</type>, <type>varchar(<replaceable>n</replaceable>)</type></entry>
1173 <entry>variable-length with limit</entry>
1174 </row>
1175 <row>
1176 <entry><type>character(<replaceable>n</replaceable>)</type>, <type>char(<replaceable>n</replaceable>)</type>, <type>bpchar(<replaceable>n</replaceable>)</type></entry>
1177 <entry>fixed-length, blank-padded</entry>
1178 </row>
1179 <row>
1180 <entry><type>bpchar</type></entry>
1181 <entry>variable unlimited length, blank-trimmed</entry>
1182 </row>
1183 <row>
1184 <entry><type>text</type></entry>
1185 <entry>variable unlimited length</entry>
1186 </row>
1187 </tbody>
1188 </tgroup>
1189 </table>
1191 <para>
1192 <xref linkend="datatype-character-table"/> shows the
1193 general-purpose character types available in
1194 <productname>PostgreSQL</productname>.
1195 </para>
1197 <para>
1198 <acronym>SQL</acronym> defines two primary character types:
1199 <type>character varying(<replaceable>n</replaceable>)</type> and
1200 <type>character(<replaceable>n</replaceable>)</type>, where <replaceable>n</replaceable>
1201 is a positive integer. Both of these types can store strings up to
1202 <replaceable>n</replaceable> characters (not bytes) in length. An attempt to store a
1203 longer string into a column of these types will result in an
1204 error, unless the excess characters are all spaces, in which case
1205 the string will be truncated to the maximum length. (This somewhat
1206 bizarre exception is required by the <acronym>SQL</acronym>
1207 standard.)
1208 However, if one explicitly casts a value to <type>character
1209 varying(<replaceable>n</replaceable>)</type> or
1210 <type>character(<replaceable>n</replaceable>)</type>, then an over-length
1211 value will be truncated to <replaceable>n</replaceable> characters without
1212 raising an error. (This too is required by the
1213 <acronym>SQL</acronym> standard.)
1214 If the string to be stored is shorter than the declared
1215 length, values of type <type>character</type> will be space-padded;
1216 values of type <type>character varying</type> will simply store the
1217 shorter
1218 string.
1219 </para>
1221 <para>
1222 In addition, <productname>PostgreSQL</productname> provides the
1223 <type>text</type> type, which stores strings of any length.
1224 Although the <type>text</type> type is not in the
1225 <acronym>SQL</acronym> standard, several other SQL database
1226 management systems have it as well.
1227 <type>text</type> is <productname>PostgreSQL</productname>'s native
1228 string data type, in that most built-in functions operating on strings
1229 are declared to take or return <type>text</type> not <type>character
1230 varying</type>. For many purposes, <type>character varying</type>
1231 acts as though it were a <link linkend="domains">domain</link>
1232 over <type>text</type>.
1233 </para>
1235 <para>
1236 The type name <type>varchar</type> is an alias for <type>character
1237 varying</type>, while <type>bpchar</type> (with length specifier) and
1238 <type>char</type> are aliases for <type>character</type>. The
1239 <type>varchar</type> and <type>char</type> aliases are defined in the
1240 <acronym>SQL</acronym> standard; <type>bpchar</type> is a
1241 <productname>PostgreSQL</productname> extension.
1242 </para>
1244 <para>
1245 If specified, the length <replaceable>n</replaceable> must be greater
1246 than zero and cannot exceed 10,485,760. If <type>character
1247 varying</type> (or <type>varchar</type>) is used without
1248 length specifier, the type accepts strings of any length. If
1249 <type>bpchar</type> lacks a length specifier, it also accepts strings
1250 of any length, but trailing spaces are semantically insignificant.
1251 If <type>character</type> (or <type>char</type>) lacks a specifier,
1252 it is equivalent to <type>character(1)</type>.
1253 </para>
1255 <para>
1256 Values of type <type>character</type> are physically padded
1257 with spaces to the specified width <replaceable>n</replaceable>, and are
1258 stored and displayed that way. However, trailing spaces are treated as
1259 semantically insignificant and disregarded when comparing two values
1260 of type <type>character</type>. In collations where whitespace
1261 is significant, this behavior can produce unexpected results;
1262 for example <command>SELECT 'a '::CHAR(2) collate "C" &lt;
1263 E'a\n'::CHAR(2)</command> returns true, even though <literal>C</literal>
1264 locale would consider a space to be greater than a newline.
1265 Trailing spaces are removed when converting a <type>character</type> value
1266 to one of the other string types. Note that trailing spaces
1267 <emphasis>are</emphasis> semantically significant in
1268 <type>character varying</type> and <type>text</type> values, and
1269 when using pattern matching, that is <literal>LIKE</literal> and
1270 regular expressions.
1271 </para>
1273 <para>
1274 The characters that can be stored in any of these data types are
1275 determined by the database character set, which is selected when
1276 the database is created. Regardless of the specific character set,
1277 the character with code zero (sometimes called NUL) cannot be stored.
1278 For more information refer to <xref linkend="multibyte"/>.
1279 </para>
1281 <para>
1282 The storage requirement for a short string (up to 126 bytes) is 1 byte
1283 plus the actual string, which includes the space padding in the case of
1284 <type>character</type>. Longer strings have 4 bytes of overhead instead
1285 of 1. Long strings are compressed by the system automatically, so
1286 the physical requirement on disk might be less. Very long values are also
1287 stored in background tables so that they do not interfere with rapid
1288 access to shorter column values. In any case, the longest
1289 possible character string that can be stored is about 1 GB. (The
1290 maximum value that will be allowed for <replaceable>n</replaceable> in the data
1291 type declaration is less than that. It wouldn't be useful to
1292 change this because with multibyte character encodings the number of
1293 characters and bytes can be quite different. If you desire to
1294 store long strings with no specific upper limit, use
1295 <type>text</type> or <type>character varying</type> without a length
1296 specifier, rather than making up an arbitrary length limit.)
1297 </para>
1299 <tip>
1300 <para>
1301 There is no performance difference among these three types,
1302 apart from increased storage space when using the blank-padded
1303 type, and a few extra CPU cycles to check the length when storing into
1304 a length-constrained column. While
1305 <type>character(<replaceable>n</replaceable>)</type> has performance
1306 advantages in some other database systems, there is no such advantage in
1307 <productname>PostgreSQL</productname>; in fact
1308 <type>character(<replaceable>n</replaceable>)</type> is usually the slowest of
1309 the three because of its additional storage costs. In most situations
1310 <type>text</type> or <type>character varying</type> should be used
1311 instead.
1312 </para>
1313 </tip>
1315 <para>
1316 Refer to <xref linkend="sql-syntax-strings"/> for information about
1317 the syntax of string literals, and to <xref linkend="functions"/>
1318 for information about available operators and functions.
1319 </para>
1321 <example>
1322 <title>Using the Character Types</title>
1324 <programlisting>
1325 CREATE TABLE test1 (a character(4));
1326 INSERT INTO test1 VALUES ('ok');
1327 SELECT a, char_length(a) FROM test1; -- <co id="co.datatype-char"/>
1328 <computeroutput>
1329 a | char_length
1330 ------+-------------
1331 ok | 2
1332 </computeroutput>
1334 CREATE TABLE test2 (b varchar(5));
1335 INSERT INTO test2 VALUES ('ok');
1336 INSERT INTO test2 VALUES ('good ');
1337 INSERT INTO test2 VALUES ('too long');
1338 <computeroutput>ERROR: value too long for type character varying(5)</computeroutput>
1339 INSERT INTO test2 VALUES ('too long'::varchar(5)); -- explicit truncation
1340 SELECT b, char_length(b) FROM test2;
1341 <computeroutput>
1342 b | char_length
1343 -------+-------------
1344 ok | 2
1345 good | 5
1346 too l | 5
1347 </computeroutput>
1348 </programlisting>
1349 <calloutlist>
1350 <callout arearefs="co.datatype-char">
1351 <para>
1352 The <function>char_length</function> function is discussed in
1353 <xref linkend="functions-string"/>.
1354 </para>
1355 </callout>
1356 </calloutlist>
1357 </example>
1359 <para>
1360 There are two other fixed-length character types in
1361 <productname>PostgreSQL</productname>, shown in <xref
1362 linkend="datatype-character-special-table"/>.
1363 These are not intended for general-purpose use, only for use
1364 in the internal system catalogs.
1365 The <type>name</type> type is used to store identifiers. Its
1366 length is currently defined as 64 bytes (63 usable characters plus
1367 terminator) but should be referenced using the constant
1368 <symbol>NAMEDATALEN</symbol> in <literal>C</literal> source code.
1369 The length is set at compile time (and
1370 is therefore adjustable for special uses); the default maximum
1371 length might change in a future release. The type <type>"char"</type>
1372 (note the quotes) is different from <type>char(1)</type> in that it
1373 only uses one byte of storage, and therefore can store only a single
1374 ASCII character. It is used in the system
1375 catalogs as a simplistic enumeration type.
1376 </para>
1378 <table id="datatype-character-special-table">
1379 <title>Special Character Types</title>
1380 <tgroup cols="3">
1381 <thead>
1382 <row>
1383 <entry>Name</entry>
1384 <entry>Storage Size</entry>
1385 <entry>Description</entry>
1386 </row>
1387 </thead>
1388 <tbody>
1389 <row>
1390 <entry><type>"char"</type></entry>
1391 <entry>1 byte</entry>
1392 <entry>single-byte internal type</entry>
1393 </row>
1394 <row>
1395 <entry><type>name</type></entry>
1396 <entry>64 bytes</entry>
1397 <entry>internal type for object names</entry>
1398 </row>
1399 </tbody>
1400 </tgroup>
1401 </table>
1403 </sect1>
1405 <sect1 id="datatype-binary">
1406 <title>Binary Data Types</title>
1408 <indexterm zone="datatype-binary">
1409 <primary>binary data</primary>
1410 </indexterm>
1412 <indexterm zone="datatype-binary">
1413 <primary>bytea</primary>
1414 </indexterm>
1416 <para>
1417 The <type>bytea</type> data type allows storage of binary strings;
1418 see <xref linkend="datatype-binary-table"/>.
1419 </para>
1421 <table id="datatype-binary-table">
1422 <title>Binary Data Types</title>
1423 <tgroup cols="3">
1424 <colspec colname="col1" colwidth="1*"/>
1425 <colspec colname="col2" colwidth="3*"/>
1426 <colspec colname="col3" colwidth="2*"/>
1427 <thead>
1428 <row>
1429 <entry>Name</entry>
1430 <entry>Storage Size</entry>
1431 <entry>Description</entry>
1432 </row>
1433 </thead>
1434 <tbody>
1435 <row>
1436 <entry><type>bytea</type></entry>
1437 <entry>1 or 4 bytes plus the actual binary string</entry>
1438 <entry>variable-length binary string</entry>
1439 </row>
1440 </tbody>
1441 </tgroup>
1442 </table>
1444 <para>
1445 A binary string is a sequence of octets (or bytes). Binary
1446 strings are distinguished from character strings in two
1447 ways. First, binary strings specifically allow storing
1448 octets of value zero and other <quote>non-printable</quote>
1449 octets (usually, octets outside the decimal range 32 to 126).
1450 Character strings disallow zero octets, and also disallow any
1451 other octet values and sequences of octet values that are invalid
1452 according to the database's selected character set encoding.
1453 Second, operations on binary strings process the actual bytes,
1454 whereas the processing of character strings depends on locale settings.
1455 In short, binary strings are appropriate for storing data that the
1456 programmer thinks of as <quote>raw bytes</quote>, whereas character
1457 strings are appropriate for storing text.
1458 </para>
1460 <para>
1461 The <type>bytea</type> type supports two
1462 formats for input and output: <quote>hex</quote> format
1463 and <productname>PostgreSQL</productname>'s historical
1464 <quote>escape</quote> format. Both
1465 of these are always accepted on input. The output format depends
1466 on the configuration parameter <xref linkend="guc-bytea-output"/>;
1467 the default is hex. (Note that the hex format was introduced in
1468 <productname>PostgreSQL</productname> 9.0; earlier versions and some
1469 tools don't understand it.)
1470 </para>
1472 <para>
1473 The <acronym>SQL</acronym> standard defines a different binary
1474 string type, called <type>BLOB</type> or <type>BINARY LARGE
1475 OBJECT</type>. The input format is different from
1476 <type>bytea</type>, but the provided functions and operators are
1477 mostly the same.
1478 </para>
1480 <sect2 id="datatype-binary-bytea-hex-format">
1481 <title><type>bytea</type> Hex Format</title>
1483 <para>
1484 The <quote>hex</quote> format encodes binary data as 2 hexadecimal digits
1485 per byte, most significant nibble first. The entire string is
1486 preceded by the sequence <literal>\x</literal> (to distinguish it
1487 from the escape format). In some contexts, the initial backslash may
1488 need to be escaped by doubling it
1489 (see <xref linkend="sql-syntax-strings"/>).
1490 For input, the hexadecimal digits can
1491 be either upper or lower case, and whitespace is permitted between
1492 digit pairs (but not within a digit pair nor in the starting
1493 <literal>\x</literal> sequence).
1494 The hex format is compatible with a wide
1495 range of external applications and protocols, and it tends to be
1496 faster to convert than the escape format, so its use is preferred.
1497 </para>
1499 <para>
1500 Example:
1501 <programlisting>
1502 SET bytea_output = 'hex';
1504 SELECT '\xDEADBEEF'::bytea;
1505 bytea
1506 ------------
1507 \xdeadbeef
1508 </programlisting>
1509 </para>
1510 </sect2>
1512 <sect2 id="datatype-binary-bytea-escape-format">
1513 <title><type>bytea</type> Escape Format</title>
1515 <para>
1516 The <quote>escape</quote> format is the traditional
1517 <productname>PostgreSQL</productname> format for the <type>bytea</type>
1518 type. It
1519 takes the approach of representing a binary string as a sequence
1520 of ASCII characters, while converting those bytes that cannot be
1521 represented as an ASCII character into special escape sequences.
1522 If, from the point of view of the application, representing bytes
1523 as characters makes sense, then this representation can be
1524 convenient. But in practice it is usually confusing because it
1525 fuzzes up the distinction between binary strings and character
1526 strings, and also the particular escape mechanism that was chosen is
1527 somewhat unwieldy. Therefore, this format should probably be avoided
1528 for most new applications.
1529 </para>
1531 <para>
1532 When entering <type>bytea</type> values in escape format,
1533 octets of certain
1534 values <emphasis>must</emphasis> be escaped, while all octet
1535 values <emphasis>can</emphasis> be escaped. In
1536 general, to escape an octet, convert it into its three-digit
1537 octal value and precede it by a backslash.
1538 Backslash itself (octet decimal value 92) can alternatively be represented by
1539 double backslashes.
1540 <xref linkend="datatype-binary-sqlesc"/>
1541 shows the characters that must be escaped, and gives the alternative
1542 escape sequences where applicable.
1543 </para>
1545 <table id="datatype-binary-sqlesc">
1546 <title><type>bytea</type> Literal Escaped Octets</title>
1547 <tgroup cols="5">
1548 <colspec colname="col1" colwidth="1*"/>
1549 <colspec colname="col2" colwidth="1*"/>
1550 <colspec colname="col3" colwidth="1*"/>
1551 <colspec colname="col4" colwidth="1.25*"/>
1552 <colspec colname="col5" colwidth="1*"/>
1553 <thead>
1554 <row>
1555 <entry>Decimal Octet Value</entry>
1556 <entry>Description</entry>
1557 <entry>Escaped Input Representation</entry>
1558 <entry>Example</entry>
1559 <entry>Hex Representation</entry>
1560 </row>
1561 </thead>
1563 <tbody>
1564 <row>
1565 <entry>0</entry>
1566 <entry>zero octet</entry>
1567 <entry><literal>'\000'</literal></entry>
1568 <entry><literal>'\000'::bytea</literal></entry>
1569 <entry><literal>\x00</literal></entry>
1570 </row>
1572 <row>
1573 <entry>39</entry>
1574 <entry>single quote</entry>
1575 <entry><literal>''''</literal> or <literal>'\047'</literal></entry>
1576 <entry><literal>''''::bytea</literal></entry>
1577 <entry><literal>\x27</literal></entry>
1578 </row>
1580 <row>
1581 <entry>92</entry>
1582 <entry>backslash</entry>
1583 <entry><literal>'\\'</literal> or <literal>'\134'</literal></entry>
1584 <entry><literal>'\\'::bytea</literal></entry>
1585 <entry><literal>\x5c</literal></entry>
1586 </row>
1588 <row>
1589 <entry>0 to 31 and 127 to 255</entry>
1590 <entry><quote>non-printable</quote> octets</entry>
1591 <entry><literal>'\<replaceable>xxx'</replaceable></literal> (octal value)</entry>
1592 <entry><literal>'\001'::bytea</literal></entry>
1593 <entry><literal>\x01</literal></entry>
1594 </row>
1596 </tbody>
1597 </tgroup>
1598 </table>
1600 <para>
1601 The requirement to escape <emphasis>non-printable</emphasis> octets
1602 varies depending on locale settings. In some instances you can get away
1603 with leaving them unescaped.
1604 </para>
1606 <para>
1607 The reason that single quotes must be doubled, as shown
1608 in <xref linkend="datatype-binary-sqlesc"/>, is that this
1609 is true for any string literal in an SQL command. The generic
1610 string-literal parser consumes the outermost single quotes
1611 and reduces any pair of single quotes to one data character.
1612 What the <type>bytea</type> input function sees is just one
1613 single quote, which it treats as a plain data character.
1614 However, the <type>bytea</type> input function treats
1615 backslashes as special, and the other behaviors shown in
1616 <xref linkend="datatype-binary-sqlesc"/> are implemented by
1617 that function.
1618 </para>
1620 <para>
1621 In some contexts, backslashes must be doubled compared to what is
1622 shown above, because the generic string-literal parser will also
1623 reduce pairs of backslashes to one data character;
1624 see <xref linkend="sql-syntax-strings"/>.
1625 </para>
1627 <para>
1628 <type>Bytea</type> octets are output in <literal>hex</literal>
1629 format by default. If you change <xref linkend="guc-bytea-output"/>
1630 to <literal>escape</literal>,
1631 <quote>non-printable</quote> octets are converted to their
1632 equivalent three-digit octal value and preceded by one backslash.
1633 Most <quote>printable</quote> octets are output by their standard
1634 representation in the client character set, e.g.:
1636 <programlisting>
1637 SET bytea_output = 'escape';
1639 SELECT 'abc \153\154\155 \052\251\124'::bytea;
1640 bytea
1641 ----------------
1642 abc klm *\251T
1643 </programlisting>
1645 The octet with decimal value 92 (backslash) is doubled in the output.
1646 Details are in <xref linkend="datatype-binary-resesc"/>.
1647 </para>
1649 <table id="datatype-binary-resesc">
1650 <title><type>bytea</type> Output Escaped Octets</title>
1651 <tgroup cols="5">
1652 <colspec colname="col1" colwidth="1*"/>
1653 <colspec colname="col2" colwidth="1*"/>
1654 <colspec colname="col3" colwidth="1*"/>
1655 <colspec colname="col4" colwidth="1.25*"/>
1656 <colspec colname="col5" colwidth="1*"/>
1657 <thead>
1658 <row>
1659 <entry>Decimal Octet Value</entry>
1660 <entry>Description</entry>
1661 <entry>Escaped Output Representation</entry>
1662 <entry>Example</entry>
1663 <entry>Output Result</entry>
1664 </row>
1665 </thead>
1667 <tbody>
1669 <row>
1670 <entry>92</entry>
1671 <entry>backslash</entry>
1672 <entry><literal>\\</literal></entry>
1673 <entry><literal>'\134'::bytea</literal></entry>
1674 <entry><literal>\\</literal></entry>
1675 </row>
1677 <row>
1678 <entry>0 to 31 and 127 to 255</entry>
1679 <entry><quote>non-printable</quote> octets</entry>
1680 <entry><literal>\<replaceable>xxx</replaceable></literal> (octal value)</entry>
1681 <entry><literal>'\001'::bytea</literal></entry>
1682 <entry><literal>\001</literal></entry>
1683 </row>
1685 <row>
1686 <entry>32 to 126</entry>
1687 <entry><quote>printable</quote> octets</entry>
1688 <entry>client character set representation</entry>
1689 <entry><literal>'\176'::bytea</literal></entry>
1690 <entry><literal>~</literal></entry>
1691 </row>
1693 </tbody>
1694 </tgroup>
1695 </table>
1697 <para>
1698 Depending on the front end to <productname>PostgreSQL</productname> you use,
1699 you might have additional work to do in terms of escaping and
1700 unescaping <type>bytea</type> strings. For example, you might also
1701 have to escape line feeds and carriage returns if your interface
1702 automatically translates these.
1703 </para>
1704 </sect2>
1705 </sect1>
1708 <sect1 id="datatype-datetime">
1709 <title>Date/Time Types</title>
1711 <indexterm zone="datatype-datetime">
1712 <primary>date</primary>
1713 </indexterm>
1714 <indexterm zone="datatype-datetime">
1715 <primary>time</primary>
1716 </indexterm>
1717 <indexterm zone="datatype-datetime">
1718 <primary>time without time zone</primary>
1719 </indexterm>
1720 <indexterm zone="datatype-datetime">
1721 <primary>time with time zone</primary>
1722 </indexterm>
1723 <indexterm zone="datatype-datetime">
1724 <primary>timestamp</primary>
1725 </indexterm>
1726 <indexterm zone="datatype-datetime">
1727 <primary>timestamptz</primary>
1728 </indexterm>
1729 <indexterm zone="datatype-datetime">
1730 <primary>timestamp with time zone</primary>
1731 </indexterm>
1732 <indexterm zone="datatype-datetime">
1733 <primary>timestamp without time zone</primary>
1734 </indexterm>
1735 <indexterm zone="datatype-datetime">
1736 <primary>interval</primary>
1737 </indexterm>
1738 <indexterm zone="datatype-datetime">
1739 <primary>time span</primary>
1740 </indexterm>
1742 <para>
1743 <productname>PostgreSQL</productname> supports the full set of
1744 <acronym>SQL</acronym> date and time types, shown in <xref
1745 linkend="datatype-datetime-table"/>. The operations available
1746 on these data types are described in
1747 <xref linkend="functions-datetime"/>.
1748 Dates are counted according to the Gregorian calendar, even in
1749 years before that calendar was introduced (see <xref
1750 linkend="datetime-units-history"/> for more information).
1751 </para>
1753 <table id="datatype-datetime-table">
1754 <title>Date/Time Types</title>
1755 <tgroup cols="6">
1756 <thead>
1757 <row>
1758 <entry>Name</entry>
1759 <entry>Storage Size</entry>
1760 <entry>Description</entry>
1761 <entry>Low Value</entry>
1762 <entry>High Value</entry>
1763 <entry>Resolution</entry>
1764 </row>
1765 </thead>
1766 <tbody>
1767 <row>
1768 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1769 <entry>8 bytes</entry>
1770 <entry>both date and time (no time zone)</entry>
1771 <entry>4713 BC</entry>
1772 <entry>294276 AD</entry>
1773 <entry>1 microsecond</entry>
1774 </row>
1775 <row>
1776 <entry><type>timestamp [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1777 <entry>8 bytes</entry>
1778 <entry>both date and time, with time zone</entry>
1779 <entry>4713 BC</entry>
1780 <entry>294276 AD</entry>
1781 <entry>1 microsecond</entry>
1782 </row>
1783 <row>
1784 <entry><type>date</type></entry>
1785 <entry>4 bytes</entry>
1786 <entry>date (no time of day)</entry>
1787 <entry>4713 BC</entry>
1788 <entry>5874897 AD</entry>
1789 <entry>1 day</entry>
1790 </row>
1791 <row>
1792 <entry><type>time [ (<replaceable>p</replaceable>) ] [ without time zone ]</type></entry>
1793 <entry>8 bytes</entry>
1794 <entry>time of day (no date)</entry>
1795 <entry>00:00:00</entry>
1796 <entry>24:00:00</entry>
1797 <entry>1 microsecond</entry>
1798 </row>
1799 <row>
1800 <entry><type>time [ (<replaceable>p</replaceable>) ] with time zone</type></entry>
1801 <entry>12 bytes</entry>
1802 <entry>time of day (no date), with time zone</entry>
1803 <!-- see MAX_TZDISP_HOUR in datatype/timestamp.h -->
1804 <entry>00:00:00+1559</entry>
1805 <entry>24:00:00-1559</entry>
1806 <entry>1 microsecond</entry>
1807 </row>
1808 <row>
1809 <entry><type>interval [ <replaceable>fields</replaceable> ] [ (<replaceable>p</replaceable>) ]</type></entry>
1810 <entry>16 bytes</entry>
1811 <entry>time interval</entry>
1812 <entry>-178000000 years</entry>
1813 <entry>178000000 years</entry>
1814 <entry>1 microsecond</entry>
1815 </row>
1816 </tbody>
1817 </tgroup>
1818 </table>
1820 <note>
1821 <para>
1822 The SQL standard requires that writing just <type>timestamp</type>
1823 be equivalent to <type>timestamp without time
1824 zone</type>, and <productname>PostgreSQL</productname> honors that
1825 behavior. <type>timestamptz</type> is accepted as an
1826 abbreviation for <type>timestamp with time zone</type>; this is a
1827 <productname>PostgreSQL</productname> extension.
1828 </para>
1829 </note>
1831 <para>
1832 <type>time</type>, <type>timestamp</type>, and
1833 <type>interval</type> accept an optional precision value
1834 <replaceable>p</replaceable> which specifies the number of
1835 fractional digits retained in the seconds field. By default, there
1836 is no explicit bound on precision. The allowed range of
1837 <replaceable>p</replaceable> is from 0 to 6.
1838 </para>
1840 <para>
1841 The <type>interval</type> type has an additional option, which is
1842 to restrict the set of stored fields by writing one of these phrases:
1843 <literallayout class="monospaced">
1844 YEAR
1845 MONTH
1847 HOUR
1848 MINUTE
1849 SECOND
1850 YEAR TO MONTH
1851 DAY TO HOUR
1852 DAY TO MINUTE
1853 DAY TO SECOND
1854 HOUR TO MINUTE
1855 HOUR TO SECOND
1856 MINUTE TO SECOND
1857 </literallayout>
1858 Note that if both <replaceable>fields</replaceable> and
1859 <replaceable>p</replaceable> are specified, the
1860 <replaceable>fields</replaceable> must include <literal>SECOND</literal>,
1861 since the precision applies only to the seconds.
1862 </para>
1864 <para>
1865 The type <type>time with time zone</type> is defined by the SQL
1866 standard, but the definition exhibits properties which lead to
1867 questionable usefulness. In most cases, a combination of
1868 <type>date</type>, <type>time</type>, <type>timestamp without time
1869 zone</type>, and <type>timestamp with time zone</type> should
1870 provide a complete range of date/time functionality required by
1871 any application.
1872 </para>
1874 <sect2 id="datatype-datetime-input">
1875 <title>Date/Time Input</title>
1877 <para>
1878 Date and time input is accepted in almost any reasonable format, including
1879 ISO 8601, <acronym>SQL</acronym>-compatible,
1880 traditional <productname>POSTGRES</productname>, and others.
1881 For some formats, ordering of day, month, and year in date input is
1882 ambiguous and there is support for specifying the expected
1883 ordering of these fields. Set the <xref linkend="guc-datestyle"/> parameter
1884 to <literal>MDY</literal> to select month-day-year interpretation,
1885 <literal>DMY</literal> to select day-month-year interpretation, or
1886 <literal>YMD</literal> to select year-month-day interpretation.
1887 </para>
1889 <para>
1890 <productname>PostgreSQL</productname> is more flexible in
1891 handling date/time input than the
1892 <acronym>SQL</acronym> standard requires.
1893 See <xref linkend="datetime-appendix"/>
1894 for the exact parsing rules of date/time input and for the
1895 recognized text fields including months, days of the week, and
1896 time zones.
1897 </para>
1899 <para>
1900 Remember that any date or time literal input needs to be enclosed
1901 in single quotes, like text strings. Refer to
1902 <xref linkend="sql-syntax-constants-generic"/> for more
1903 information.
1904 <acronym>SQL</acronym> requires the following syntax
1905 <synopsis>
1906 <replaceable>type</replaceable> [ (<replaceable>p</replaceable>) ] '<replaceable>value</replaceable>'
1907 </synopsis>
1908 where <replaceable>p</replaceable> is an optional precision
1909 specification giving the number of
1910 fractional digits in the seconds field. Precision can be
1911 specified for <type>time</type>, <type>timestamp</type>, and
1912 <type>interval</type> types, and can range from 0 to 6.
1913 If no precision is specified in a constant specification,
1914 it defaults to the precision of the literal value (but not
1915 more than 6 digits).
1916 </para>
1918 <sect3 id="datatype-datetime-input-dates">
1919 <title>Dates</title>
1921 <indexterm>
1922 <primary>date</primary>
1923 </indexterm>
1925 <para>
1926 <xref linkend="datatype-datetime-date-table"/> shows some possible
1927 inputs for the <type>date</type> type.
1928 </para>
1930 <table id="datatype-datetime-date-table">
1931 <title>Date Input</title>
1932 <tgroup cols="2">
1933 <colspec colname="col1" colwidth="1*"/>
1934 <colspec colname="col2" colwidth="2*"/>
1935 <thead>
1936 <row>
1937 <entry>Example</entry>
1938 <entry>Description</entry>
1939 </row>
1940 </thead>
1941 <tbody>
1942 <row>
1943 <entry>1999-01-08</entry>
1944 <entry>ISO 8601; January 8 in any mode
1945 (recommended format)</entry>
1946 </row>
1947 <row>
1948 <entry>January 8, 1999</entry>
1949 <entry>unambiguous in any <varname>datestyle</varname> input mode</entry>
1950 </row>
1951 <row>
1952 <entry>1/8/1999</entry>
1953 <entry>January 8 in <literal>MDY</literal> mode;
1954 August 1 in <literal>DMY</literal> mode</entry>
1955 </row>
1956 <row>
1957 <entry>1/18/1999</entry>
1958 <entry>January 18 in <literal>MDY</literal> mode;
1959 rejected in other modes</entry>
1960 </row>
1961 <row>
1962 <entry>01/02/03</entry>
1963 <entry>January 2, 2003 in <literal>MDY</literal> mode;
1964 February 1, 2003 in <literal>DMY</literal> mode;
1965 February 3, 2001 in <literal>YMD</literal> mode
1966 </entry>
1967 </row>
1968 <row>
1969 <entry>1999-Jan-08</entry>
1970 <entry>January 8 in any mode</entry>
1971 </row>
1972 <row>
1973 <entry>Jan-08-1999</entry>
1974 <entry>January 8 in any mode</entry>
1975 </row>
1976 <row>
1977 <entry>08-Jan-1999</entry>
1978 <entry>January 8 in any mode</entry>
1979 </row>
1980 <row>
1981 <entry>99-Jan-08</entry>
1982 <entry>January 8 in <literal>YMD</literal> mode, else error</entry>
1983 </row>
1984 <row>
1985 <entry>08-Jan-99</entry>
1986 <entry>January 8, except error in <literal>YMD</literal> mode</entry>
1987 </row>
1988 <row>
1989 <entry>Jan-08-99</entry>
1990 <entry>January 8, except error in <literal>YMD</literal> mode</entry>
1991 </row>
1992 <row>
1993 <entry>19990108</entry>
1994 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1995 </row>
1996 <row>
1997 <entry>990108</entry>
1998 <entry>ISO 8601; January 8, 1999 in any mode</entry>
1999 </row>
2000 <row>
2001 <entry>1999.008</entry>
2002 <entry>year and day of year</entry>
2003 </row>
2004 <row>
2005 <entry>J2451187</entry>
2006 <entry>Julian date</entry>
2007 </row>
2008 <row>
2009 <entry>January 8, 99 BC</entry>
2010 <entry>year 99 BC</entry>
2011 </row>
2012 </tbody>
2013 </tgroup>
2014 </table>
2015 </sect3>
2017 <sect3 id="datatype-datetime-input-times">
2018 <title>Times</title>
2020 <indexterm>
2021 <primary>time</primary>
2022 </indexterm>
2023 <indexterm>
2024 <primary>time without time zone</primary>
2025 </indexterm>
2026 <indexterm>
2027 <primary>time with time zone</primary>
2028 </indexterm>
2030 <para>
2031 The time-of-day types are <type>time [
2032 (<replaceable>p</replaceable>) ] without time zone</type> and
2033 <type>time [ (<replaceable>p</replaceable>) ] with time
2034 zone</type>. <type>time</type> alone is equivalent to
2035 <type>time without time zone</type>.
2036 </para>
2038 <para>
2039 Valid input for these types consists of a time of day followed
2040 by an optional time zone. (See <xref
2041 linkend="datatype-datetime-time-table"/>
2042 and <xref linkend="datatype-timezone-table"/>.) If a time zone is
2043 specified in the input for <type>time without time zone</type>,
2044 it is silently ignored. You can also specify a date but it will
2045 be ignored, except when you use a time zone name that involves a
2046 daylight-savings rule, such as
2047 <literal>America/New_York</literal>. In this case specifying the date
2048 is required in order to determine whether standard or daylight-savings
2049 time applies. The appropriate time zone offset is recorded in the
2050 <type>time with time zone</type> value and is output as stored;
2051 it is not adjusted to the active time zone.
2052 </para>
2054 <table id="datatype-datetime-time-table">
2055 <title>Time Input</title>
2056 <tgroup cols="2">
2057 <colspec colname="col1" colwidth="3*"/>
2058 <colspec colname="col2" colwidth="2*"/>
2059 <thead>
2060 <row>
2061 <entry>Example</entry>
2062 <entry>Description</entry>
2063 </row>
2064 </thead>
2065 <tbody>
2066 <row>
2067 <entry><literal>04:05:06.789</literal></entry>
2068 <entry>ISO 8601</entry>
2069 </row>
2070 <row>
2071 <entry><literal>04:05:06</literal></entry>
2072 <entry>ISO 8601</entry>
2073 </row>
2074 <row>
2075 <entry><literal>04:05</literal></entry>
2076 <entry>ISO 8601</entry>
2077 </row>
2078 <row>
2079 <entry><literal>040506</literal></entry>
2080 <entry>ISO 8601</entry>
2081 </row>
2082 <row>
2083 <entry><literal>04:05 AM</literal></entry>
2084 <entry>same as 04:05; AM does not affect value</entry>
2085 </row>
2086 <row>
2087 <entry><literal>04:05 PM</literal></entry>
2088 <entry>same as 16:05; input hour must be &lt;= 12</entry>
2089 </row>
2090 <row>
2091 <entry><literal>04:05:06.789-8</literal></entry>
2092 <entry>ISO 8601, with time zone as UTC offset</entry>
2093 </row>
2094 <row>
2095 <entry><literal>04:05:06-08:00</literal></entry>
2096 <entry>ISO 8601, with time zone as UTC offset</entry>
2097 </row>
2098 <row>
2099 <entry><literal>04:05-08:00</literal></entry>
2100 <entry>ISO 8601, with time zone as UTC offset</entry>
2101 </row>
2102 <row>
2103 <entry><literal>040506-08</literal></entry>
2104 <entry>ISO 8601, with time zone as UTC offset</entry>
2105 </row>
2106 <row>
2107 <entry><literal>040506+0730</literal></entry>
2108 <entry>ISO 8601, with fractional-hour time zone as UTC offset</entry>
2109 </row>
2110 <row>
2111 <entry><literal>040506+07:30:00</literal></entry>
2112 <entry>UTC offset specified to seconds (not allowed in ISO 8601)</entry>
2113 </row>
2114 <row>
2115 <entry><literal>04:05:06 PST</literal></entry>
2116 <entry>time zone specified by abbreviation</entry>
2117 </row>
2118 <row>
2119 <entry><literal>2003-04-12 04:05:06 America/New_York</literal></entry>
2120 <entry>time zone specified by full name</entry>
2121 </row>
2122 </tbody>
2123 </tgroup>
2124 </table>
2126 <table tocentry="1" id="datatype-timezone-table">
2127 <title>Time Zone Input</title>
2128 <tgroup cols="2">
2129 <thead>
2130 <row>
2131 <entry>Example</entry>
2132 <entry>Description</entry>
2133 </row>
2134 </thead>
2135 <tbody>
2136 <row>
2137 <entry><literal>PST</literal></entry>
2138 <entry>Abbreviation (for Pacific Standard Time)</entry>
2139 </row>
2140 <row>
2141 <entry><literal>America/New_York</literal></entry>
2142 <entry>Full time zone name</entry>
2143 </row>
2144 <row>
2145 <entry><literal>PST8PDT</literal></entry>
2146 <entry>POSIX-style time zone specification</entry>
2147 </row>
2148 <row>
2149 <entry><literal>-8:00:00</literal></entry>
2150 <entry>UTC offset for PST</entry>
2151 </row>
2152 <row>
2153 <entry><literal>-8:00</literal></entry>
2154 <entry>UTC offset for PST (ISO 8601 extended format)</entry>
2155 </row>
2156 <row>
2157 <entry><literal>-800</literal></entry>
2158 <entry>UTC offset for PST (ISO 8601 basic format)</entry>
2159 </row>
2160 <row>
2161 <entry><literal>-8</literal></entry>
2162 <entry>UTC offset for PST (ISO 8601 basic format)</entry>
2163 </row>
2164 <row>
2165 <entry><literal>zulu</literal></entry>
2166 <entry>Military abbreviation for UTC</entry>
2167 </row>
2168 <row>
2169 <entry><literal>z</literal></entry>
2170 <entry>Short form of <literal>zulu</literal> (also in ISO 8601)</entry>
2171 </row>
2172 </tbody>
2173 </tgroup>
2174 </table>
2176 <para>
2177 Refer to <xref linkend="datatype-timezones"/> for more information on how
2178 to specify time zones.
2179 </para>
2180 </sect3>
2182 <sect3 id="datatype-datetime-input-time-stamps">
2183 <title>Time Stamps</title>
2185 <indexterm>
2186 <primary>timestamp</primary>
2187 </indexterm>
2189 <indexterm>
2190 <primary>timestamp with time zone</primary>
2191 </indexterm>
2193 <indexterm>
2194 <primary>timestamp without time zone</primary>
2195 </indexterm>
2197 <para>
2198 Valid input for the time stamp types consists of the concatenation
2199 of a date and a time, followed by an optional time zone,
2200 followed by an optional <literal>AD</literal> or <literal>BC</literal>.
2201 (Alternatively, <literal>AD</literal>/<literal>BC</literal> can appear
2202 before the time zone, but this is not the preferred ordering.)
2203 Thus:
2205 <programlisting>
2206 1999-01-08 04:05:06
2207 </programlisting>
2208 and:
2209 <programlisting>
2210 1999-01-08 04:05:06 -8:00
2211 </programlisting>
2213 are valid values, which follow the <acronym>ISO</acronym> 8601
2214 standard. In addition, the common format:
2215 <programlisting>
2216 January 8 04:05:06 1999 PST
2217 </programlisting>
2218 is supported.
2219 </para>
2221 <para>
2222 The <acronym>SQL</acronym> standard differentiates
2223 <type>timestamp without time zone</type>
2224 and <type>timestamp with time zone</type> literals by the presence of a
2225 <quote>+</quote> or <quote>-</quote> symbol and time zone offset after
2226 the time. Hence, according to the standard,
2228 <programlisting>
2229 TIMESTAMP '2004-10-19 10:23:54'
2230 </programlisting>
2232 is a <type>timestamp without time zone</type>, while
2234 <programlisting>
2235 TIMESTAMP '2004-10-19 10:23:54+02'
2236 </programlisting>
2238 is a <type>timestamp with time zone</type>.
2239 <productname>PostgreSQL</productname> never examines the content of a
2240 literal string before determining its type, and therefore will treat
2241 both of the above as <type>timestamp without time zone</type>. To
2242 ensure that a literal is treated as <type>timestamp with time
2243 zone</type>, give it the correct explicit type:
2245 <programlisting>
2246 TIMESTAMP WITH TIME ZONE '2004-10-19 10:23:54+02'
2247 </programlisting>
2249 In a literal that has been determined to be <type>timestamp without time
2250 zone</type>, <productname>PostgreSQL</productname> will silently ignore
2251 any time zone indication.
2252 That is, the resulting value is derived from the date/time
2253 fields in the input value, and is not adjusted for time zone.
2254 </para>
2256 <para>
2257 For <type>timestamp with time zone</type>, the internally stored
2258 value is always in UTC (Universal
2259 Coordinated Time, traditionally known as Greenwich Mean Time,
2260 <acronym>GMT</acronym>). An input value that has an explicit
2261 time zone specified is converted to UTC using the appropriate offset
2262 for that time zone. If no time zone is stated in the input string,
2263 then it is assumed to be in the time zone indicated by the system's
2264 <xref linkend="guc-timezone"/> parameter, and is converted to UTC using the
2265 offset for the <varname>timezone</varname> zone.
2266 </para>
2268 <para>
2269 When a <type>timestamp with time
2270 zone</type> value is output, it is always converted from UTC to the
2271 current <varname>timezone</varname> zone, and displayed as local time in that
2272 zone. To see the time in another time zone, either change
2273 <varname>timezone</varname> or use the <literal>AT TIME ZONE</literal> construct
2274 (see <xref linkend="functions-datetime-zoneconvert"/>).
2275 </para>
2277 <para>
2278 Conversions between <type>timestamp without time zone</type> and
2279 <type>timestamp with time zone</type> normally assume that the
2280 <type>timestamp without time zone</type> value should be taken or given
2281 as <varname>timezone</varname> local time. A different time zone can
2282 be specified for the conversion using <literal>AT TIME ZONE</literal>.
2283 </para>
2284 </sect3>
2286 <sect3 id="datatype-datetime-special-values">
2287 <title>Special Values</title>
2289 <indexterm>
2290 <primary>time</primary>
2291 <secondary>constants</secondary>
2292 </indexterm>
2294 <indexterm>
2295 <primary>date</primary>
2296 <secondary>constants</secondary>
2297 </indexterm>
2299 <para>
2300 <productname>PostgreSQL</productname> supports several
2301 special date/time input values for convenience, as shown in <xref
2302 linkend="datatype-datetime-special-table"/>. The values
2303 <literal>infinity</literal> and <literal>-infinity</literal>
2304 are specially represented inside the system and will be displayed
2305 unchanged; but the others are simply notational shorthands
2306 that will be converted to ordinary date/time values when read.
2307 (In particular, <literal>now</literal> and related strings are converted
2308 to a specific time value as soon as they are read.)
2309 All of these values need to be enclosed in single quotes when used
2310 as constants in SQL commands.
2311 </para>
2313 <table id="datatype-datetime-special-table">
2314 <title>Special Date/Time Inputs</title>
2315 <tgroup cols="3">
2316 <thead>
2317 <row>
2318 <entry>Input String</entry>
2319 <entry>Valid Types</entry>
2320 <entry>Description</entry>
2321 </row>
2322 </thead>
2323 <tbody>
2324 <row>
2325 <entry><literal>epoch</literal></entry>
2326 <entry><type>date</type>, <type>timestamp</type></entry>
2327 <entry>1970-01-01 00:00:00+00 (Unix system time zero)</entry>
2328 </row>
2329 <row>
2330 <entry><literal>infinity</literal></entry>
2331 <entry><type>date</type>, <type>timestamp</type>, <type>interval</type></entry>
2332 <entry>later than all other time stamps</entry>
2333 </row>
2334 <row>
2335 <entry><literal>-infinity</literal></entry>
2336 <entry><type>date</type>, <type>timestamp</type>, <type>interval</type></entry>
2337 <entry>earlier than all other time stamps</entry>
2338 </row>
2339 <row>
2340 <entry><literal>now</literal></entry>
2341 <entry><type>date</type>, <type>time</type>, <type>timestamp</type></entry>
2342 <entry>current transaction's start time</entry>
2343 </row>
2344 <row>
2345 <entry><literal>today</literal></entry>
2346 <entry><type>date</type>, <type>timestamp</type></entry>
2347 <entry>midnight (<literal>00:00</literal>) today</entry>
2348 </row>
2349 <row>
2350 <entry><literal>tomorrow</literal></entry>
2351 <entry><type>date</type>, <type>timestamp</type></entry>
2352 <entry>midnight (<literal>00:00</literal>) tomorrow</entry>
2353 </row>
2354 <row>
2355 <entry><literal>yesterday</literal></entry>
2356 <entry><type>date</type>, <type>timestamp</type></entry>
2357 <entry>midnight (<literal>00:00</literal>) yesterday</entry>
2358 </row>
2359 <row>
2360 <entry><literal>allballs</literal></entry>
2361 <entry><type>time</type></entry>
2362 <entry>00:00:00.00 UTC</entry>
2363 </row>
2364 </tbody>
2365 </tgroup>
2366 </table>
2368 <para>
2369 The following <acronym>SQL</acronym>-compatible functions can also
2370 be used to obtain the current time value for the corresponding data
2371 type:
2372 <literal>CURRENT_DATE</literal>, <literal>CURRENT_TIME</literal>,
2373 <literal>CURRENT_TIMESTAMP</literal>, <literal>LOCALTIME</literal>,
2374 <literal>LOCALTIMESTAMP</literal>. (See <xref
2375 linkend="functions-datetime-current"/>.) Note that these are
2376 SQL functions and are <emphasis>not</emphasis> recognized in data input strings.
2377 </para>
2379 <caution>
2380 <para>
2381 While the input strings <literal>now</literal>,
2382 <literal>today</literal>, <literal>tomorrow</literal>,
2383 and <literal>yesterday</literal> are fine to use in interactive SQL
2384 commands, they can have surprising behavior when the command is
2385 saved to be executed later, for example in prepared statements,
2386 views, and function definitions. The string can be converted to a
2387 specific time value that continues to be used long after it becomes
2388 stale. Use one of the SQL functions instead in such contexts.
2389 For example, <literal>CURRENT_DATE + 1</literal> is safer than
2390 <literal>'tomorrow'::date</literal>.
2391 </para>
2392 </caution>
2394 </sect3>
2395 </sect2>
2397 <sect2 id="datatype-datetime-output">
2398 <title>Date/Time Output</title>
2400 <indexterm>
2401 <primary>date</primary>
2402 <secondary>output format</secondary>
2403 <seealso>formatting</seealso>
2404 </indexterm>
2406 <indexterm>
2407 <primary>time</primary>
2408 <secondary>output format</secondary>
2409 <seealso>formatting</seealso>
2410 </indexterm>
2412 <para>
2413 The output format of the date/time types can be set to one of the four
2414 styles ISO 8601,
2415 <acronym>SQL</acronym> (Ingres), traditional <productname>POSTGRES</productname>
2416 (Unix <application>date</application> format), or
2417 German. The default
2418 is the <acronym>ISO</acronym> format. (The
2419 <acronym>SQL</acronym> standard requires the use of the ISO 8601
2420 format. The name of the <quote>SQL</quote> output format is a
2421 historical accident.) <xref
2422 linkend="datatype-datetime-output-table"/> shows examples of each
2423 output style. The output of the <type>date</type> and
2424 <type>time</type> types is generally only the date or time part
2425 in accordance with the given examples. However, the
2426 <productname>POSTGRES</productname> style outputs date-only values in
2427 <acronym>ISO</acronym> format.
2428 </para>
2430 <table id="datatype-datetime-output-table">
2431 <title>Date/Time Output Styles</title>
2432 <tgroup cols="3">
2433 <colspec colname="col1" colwidth="1*"/>
2434 <colspec colname="col2" colwidth="1*"/>
2435 <colspec colname="col3" colwidth="2*"/>
2436 <thead>
2437 <row>
2438 <entry>Style Specification</entry>
2439 <entry>Description</entry>
2440 <entry>Example</entry>
2441 </row>
2442 </thead>
2443 <tbody>
2444 <row>
2445 <entry><literal>ISO</literal></entry>
2446 <entry>ISO 8601, SQL standard</entry>
2447 <entry><literal>1997-12-17 07:37:16-08</literal></entry>
2448 </row>
2449 <row>
2450 <entry><literal>SQL</literal></entry>
2451 <entry>traditional style</entry>
2452 <entry><literal>12/17/1997 07:37:16.00 PST</literal></entry>
2453 </row>
2454 <row>
2455 <entry><literal>Postgres</literal></entry>
2456 <entry>original style</entry>
2457 <entry><literal>Wed Dec 17 07:37:16 1997 PST</literal></entry>
2458 </row>
2459 <row>
2460 <entry><literal>German</literal></entry>
2461 <entry>regional style</entry>
2462 <entry><literal>17.12.1997 07:37:16.00 PST</literal></entry>
2463 </row>
2464 </tbody>
2465 </tgroup>
2466 </table>
2468 <note>
2469 <para>
2470 ISO 8601 specifies the use of uppercase letter <literal>T</literal> to separate
2471 the date and time. <productname>PostgreSQL</productname> accepts that format on
2472 input, but on output it uses a space rather than <literal>T</literal>, as shown
2473 above. This is for readability and for consistency with
2474 <ulink url="https://datatracker.ietf.org/doc/html/rfc3339">RFC 3339</ulink> as
2475 well as some other database systems.
2476 </para>
2477 </note>
2479 <para>
2480 In the <acronym>SQL</acronym> and POSTGRES styles, day appears before
2481 month if DMY field ordering has been specified, otherwise month appears
2482 before day.
2483 (See <xref linkend="datatype-datetime-input"/>
2484 for how this setting also affects interpretation of input values.)
2485 <xref linkend="datatype-datetime-output2-table"/> shows examples.
2486 </para>
2488 <table id="datatype-datetime-output2-table">
2489 <title>Date Order Conventions</title>
2490 <tgroup cols="3">
2491 <colspec colname="col1" colwidth="1*"/>
2492 <colspec colname="col2" colwidth="1*"/>
2493 <colspec colname="col3" colwidth="2*"/>
2494 <thead>
2495 <row>
2496 <entry><varname>datestyle</varname> Setting</entry>
2497 <entry>Input Ordering</entry>
2498 <entry>Example Output</entry>
2499 </row>
2500 </thead>
2501 <tbody>
2502 <row>
2503 <entry><literal>SQL, DMY</literal></entry>
2504 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2505 <entry><literal>17/12/1997 15:37:16.00 CET</literal></entry>
2506 </row>
2507 <row>
2508 <entry><literal>SQL, MDY</literal></entry>
2509 <entry><replaceable>month</replaceable>/<replaceable>day</replaceable>/<replaceable>year</replaceable></entry>
2510 <entry><literal>12/17/1997 07:37:16.00 PST</literal></entry>
2511 </row>
2512 <row>
2513 <entry><literal>Postgres, DMY</literal></entry>
2514 <entry><replaceable>day</replaceable>/<replaceable>month</replaceable>/<replaceable>year</replaceable></entry>
2515 <entry><literal>Wed 17 Dec 07:37:16 1997 PST</literal></entry>
2516 </row>
2517 </tbody>
2518 </tgroup>
2519 </table>
2521 <para>
2522 In the <acronym>ISO</acronym> style, the time zone is always shown as
2523 a signed numeric offset from UTC, with positive sign used for zones
2524 east of Greenwich. The offset will be shown
2525 as <replaceable>hh</replaceable> (hours only) if it is an integral
2526 number of hours, else
2527 as <replaceable>hh</replaceable>:<replaceable>mm</replaceable> if it
2528 is an integral number of minutes, else as
2529 <replaceable>hh</replaceable>:<replaceable>mm</replaceable>:<replaceable>ss</replaceable>.
2530 (The third case is not possible with any modern time zone standard,
2531 but it can appear when working with timestamps that predate the
2532 adoption of standardized time zones.)
2533 In the other date styles, the time zone is shown as an alphabetic
2534 abbreviation if one is in common use in the current zone. Otherwise
2535 it appears as a signed numeric offset in ISO 8601 basic format
2536 (<replaceable>hh</replaceable> or <replaceable>hhmm</replaceable>).
2537 </para>
2539 <para>
2540 The date/time style can be selected by the user using the
2541 <command>SET datestyle</command> command, the <xref
2542 linkend="guc-datestyle"/> parameter in the
2543 <filename>postgresql.conf</filename> configuration file, or the
2544 <envar>PGDATESTYLE</envar> environment variable on the server or
2545 client.
2546 </para>
2548 <para>
2549 The formatting function <function>to_char</function>
2550 (see <xref linkend="functions-formatting"/>) is also available as
2551 a more flexible way to format date/time output.
2552 </para>
2553 </sect2>
2555 <sect2 id="datatype-timezones">
2556 <title>Time Zones</title>
2558 <indexterm zone="datatype-timezones">
2559 <primary>time zone</primary>
2560 </indexterm>
2562 <para>
2563 Time zones, and time-zone conventions, are influenced by
2564 political decisions, not just earth geometry. Time zones around the
2565 world became somewhat standardized during the 1900s,
2566 but continue to be prone to arbitrary changes, particularly with
2567 respect to daylight-savings rules.
2568 <productname>PostgreSQL</productname> uses the widely-used
2569 IANA (Olson) time zone database for information about
2570 historical time zone rules. For times in the future, the assumption
2571 is that the latest known rules for a given time zone will
2572 continue to be observed indefinitely far into the future.
2573 </para>
2575 <para>
2576 <productname>PostgreSQL</productname> endeavors to be compatible with
2577 the <acronym>SQL</acronym> standard definitions for typical usage.
2578 However, the <acronym>SQL</acronym> standard has an odd mix of date and
2579 time types and capabilities. Two obvious problems are:
2581 <itemizedlist>
2582 <listitem>
2583 <para>
2584 Although the <type>date</type> type
2585 cannot have an associated time zone, the
2586 <type>time</type> type can.
2587 Time zones in the real world have little meaning unless
2588 associated with a date as well as a time,
2589 since the offset can vary through the year with daylight-saving
2590 time boundaries.
2591 </para>
2592 </listitem>
2594 <listitem>
2595 <para>
2596 The default time zone is specified as a constant numeric offset
2597 from <acronym>UTC</acronym>. It is therefore impossible to adapt to
2598 daylight-saving time when doing date/time arithmetic across
2599 <acronym>DST</acronym> boundaries.
2600 </para>
2601 </listitem>
2603 </itemizedlist>
2604 </para>
2606 <para>
2607 To address these difficulties, we recommend using date/time types
2608 that contain both date and time when using time zones. We
2609 do <emphasis>not</emphasis> recommend using the type <type>time with
2610 time zone</type> (though it is supported by
2611 <productname>PostgreSQL</productname> for legacy applications and
2612 for compliance with the <acronym>SQL</acronym> standard).
2613 <productname>PostgreSQL</productname> assumes
2614 your local time zone for any type containing only date or time.
2615 </para>
2617 <para>
2618 All timezone-aware dates and times are stored internally in
2619 <acronym>UTC</acronym>. They are converted to local time
2620 in the zone specified by the <xref linkend="guc-timezone"/> configuration
2621 parameter before being displayed to the client.
2622 </para>
2624 <para>
2625 <productname>PostgreSQL</productname> allows you to specify time zones in
2626 three different forms:
2627 <itemizedlist>
2628 <listitem>
2629 <para>
2630 A full time zone name, for example <literal>America/New_York</literal>.
2631 The recognized time zone names are listed in the
2632 <literal>pg_timezone_names</literal> view (see <xref
2633 linkend="view-pg-timezone-names"/>).
2634 <productname>PostgreSQL</productname> uses the widely-used IANA
2635 time zone data for this purpose, so the same time zone
2636 names are also recognized by other software.
2637 </para>
2638 </listitem>
2639 <listitem>
2640 <para>
2641 A time zone abbreviation, for example <literal>PST</literal>. Such a
2642 specification merely defines a particular offset from UTC, in
2643 contrast to full time zone names which can imply a set of daylight
2644 savings transition rules as well. The recognized abbreviations
2645 are listed in the <literal>pg_timezone_abbrevs</literal> view (see <xref
2646 linkend="view-pg-timezone-abbrevs"/>). You cannot set the
2647 configuration parameters <xref linkend="guc-timezone"/> or
2648 <xref linkend="guc-log-timezone"/> to a time
2649 zone abbreviation, but you can use abbreviations in
2650 date/time input values and with the <literal>AT TIME ZONE</literal>
2651 operator.
2652 </para>
2653 </listitem>
2654 <listitem>
2655 <para>
2656 In addition to the timezone names and abbreviations,
2657 <productname>PostgreSQL</productname> will accept POSIX-style time zone
2658 specifications, as described in
2659 <xref linkend="datetime-posix-timezone-specs"/>. This option is not
2660 normally preferable to using a named time zone, but it may be
2661 necessary if no suitable IANA time zone entry is available.
2662 </para>
2663 </listitem>
2664 </itemizedlist>
2666 In short, this is the difference between abbreviations
2667 and full names: abbreviations represent a specific offset from UTC,
2668 whereas many of the full names imply a local daylight-savings time
2669 rule, and so have two possible UTC offsets. As an example,
2670 <literal>2014-06-04 12:00 America/New_York</literal> represents noon local
2671 time in New York, which for this particular date was Eastern Daylight
2672 Time (UTC-4). So <literal>2014-06-04 12:00 EDT</literal> specifies that
2673 same time instant. But <literal>2014-06-04 12:00 EST</literal> specifies
2674 noon Eastern Standard Time (UTC-5), regardless of whether daylight
2675 savings was nominally in effect on that date.
2676 </para>
2678 <para>
2679 To complicate matters, some jurisdictions have used the same timezone
2680 abbreviation to mean different UTC offsets at different times; for
2681 example, in Moscow <literal>MSK</literal> has meant UTC+3 in some years and
2682 UTC+4 in others. <productname>PostgreSQL</productname> interprets such
2683 abbreviations according to whatever they meant (or had most recently
2684 meant) on the specified date; but, as with the <literal>EST</literal> example
2685 above, this is not necessarily the same as local civil time on that date.
2686 </para>
2688 <para>
2689 In all cases, timezone names and abbreviations are recognized
2690 case-insensitively. (This is a change from <productname>PostgreSQL</productname>
2691 versions prior to 8.2, which were case-sensitive in some contexts but
2692 not others.)
2693 </para>
2695 <para>
2696 Neither timezone names nor abbreviations are hard-wired into the server;
2697 they are obtained from configuration files stored under
2698 <filename>.../share/timezone/</filename> and <filename>.../share/timezonesets/</filename>
2699 of the installation directory
2700 (see <xref linkend="datetime-config-files"/>).
2701 </para>
2703 <para>
2704 The <xref linkend="guc-timezone"/> configuration parameter can
2705 be set in the file <filename>postgresql.conf</filename>, or in any of the
2706 other standard ways described in <xref linkend="runtime-config"/>.
2707 There are also some special ways to set it:
2709 <itemizedlist>
2710 <listitem>
2711 <para>
2712 The <acronym>SQL</acronym> command <command>SET TIME ZONE</command>
2713 sets the time zone for the session. This is an alternative spelling
2714 of <command>SET TIMEZONE TO</command> with a more SQL-spec-compatible syntax.
2715 </para>
2716 </listitem>
2718 <listitem>
2719 <para>
2720 The <envar>PGTZ</envar> environment variable is used by
2721 <application>libpq</application> clients
2722 to send a <command>SET TIME ZONE</command>
2723 command to the server upon connection.
2724 </para>
2725 </listitem>
2726 </itemizedlist>
2727 </para>
2728 </sect2>
2730 <sect2 id="datatype-interval-input">
2731 <title>Interval Input</title>
2733 <indexterm>
2734 <primary>interval</primary>
2735 </indexterm>
2737 <para>
2738 <type>interval</type> values can be written using the following
2739 verbose syntax:
2741 <synopsis>
2742 <optional>@</optional> <replaceable>quantity</replaceable> <replaceable>unit</replaceable> <optional><replaceable>quantity</replaceable> <replaceable>unit</replaceable>...</optional> <optional><replaceable>direction</replaceable></optional>
2743 </synopsis>
2745 where <replaceable>quantity</replaceable> is a number (possibly signed);
2746 <replaceable>unit</replaceable> is <literal>microsecond</literal>,
2747 <literal>millisecond</literal>, <literal>second</literal>,
2748 <literal>minute</literal>, <literal>hour</literal>, <literal>day</literal>,
2749 <literal>week</literal>, <literal>month</literal>, <literal>year</literal>,
2750 <literal>decade</literal>, <literal>century</literal>, <literal>millennium</literal>,
2751 or abbreviations or plurals of these units;
2752 <replaceable>direction</replaceable> can be <literal>ago</literal> or
2753 empty. The at sign (<literal>@</literal>) is optional noise. The amounts
2754 of the different units are implicitly added with appropriate
2755 sign accounting. <literal>ago</literal> negates all the fields.
2756 This syntax is also used for interval output, if
2757 <xref linkend="guc-intervalstyle"/> is set to
2758 <literal>postgres_verbose</literal>.
2759 </para>
2761 <para>
2762 Quantities of days, hours, minutes, and seconds can be specified without
2763 explicit unit markings. For example, <literal>'1 12:59:10'</literal> is read
2764 the same as <literal>'1 day 12 hours 59 min 10 sec'</literal>. Also,
2765 a combination of years and months can be specified with a dash;
2766 for example <literal>'200-10'</literal> is read the same as <literal>'200 years
2767 10 months'</literal>. (These shorter forms are in fact the only ones allowed
2768 by the <acronym>SQL</acronym> standard, and are used for output when
2769 <varname>IntervalStyle</varname> is set to <literal>sql_standard</literal>.)
2770 </para>
2772 <para>
2773 Interval values can also be written as ISO 8601 time intervals, using
2774 either the <quote>format with designators</quote> of the standard's section
2775 4.4.3.2 or the <quote>alternative format</quote> of section 4.4.3.3. The
2776 format with designators looks like this:
2777 <synopsis>
2778 P <replaceable>quantity</replaceable> <replaceable>unit</replaceable> <optional> <replaceable>quantity</replaceable> <replaceable>unit</replaceable> ...</optional> <optional> T <optional> <replaceable>quantity</replaceable> <replaceable>unit</replaceable> ...</optional></optional>
2779 </synopsis>
2780 The string must start with a <literal>P</literal>, and may include a
2781 <literal>T</literal> that introduces the time-of-day units. The
2782 available unit abbreviations are given in <xref
2783 linkend="datatype-interval-iso8601-units"/>. Units may be
2784 omitted, and may be specified in any order, but units smaller than
2785 a day must appear after <literal>T</literal>. In particular, the meaning of
2786 <literal>M</literal> depends on whether it is before or after
2787 <literal>T</literal>.
2788 </para>
2790 <table id="datatype-interval-iso8601-units">
2791 <title>ISO 8601 Interval Unit Abbreviations</title>
2792 <tgroup cols="2">
2793 <thead>
2794 <row>
2795 <entry>Abbreviation</entry>
2796 <entry>Meaning</entry>
2797 </row>
2798 </thead>
2799 <tbody>
2800 <row>
2801 <entry>Y</entry>
2802 <entry>Years</entry>
2803 </row>
2804 <row>
2805 <entry>M</entry>
2806 <entry>Months (in the date part)</entry>
2807 </row>
2808 <row>
2809 <entry>W</entry>
2810 <entry>Weeks</entry>
2811 </row>
2812 <row>
2813 <entry>D</entry>
2814 <entry>Days</entry>
2815 </row>
2816 <row>
2817 <entry>H</entry>
2818 <entry>Hours</entry>
2819 </row>
2820 <row>
2821 <entry>M</entry>
2822 <entry>Minutes (in the time part)</entry>
2823 </row>
2824 <row>
2825 <entry>S</entry>
2826 <entry>Seconds</entry>
2827 </row>
2828 </tbody>
2829 </tgroup>
2830 </table>
2832 <para>
2833 In the alternative format:
2834 <synopsis>
2835 P <optional> <replaceable>years</replaceable>-<replaceable>months</replaceable>-<replaceable>days</replaceable> </optional> <optional> T <replaceable>hours</replaceable>:<replaceable>minutes</replaceable>:<replaceable>seconds</replaceable> </optional>
2836 </synopsis>
2837 the string must begin with <literal>P</literal>, and a
2838 <literal>T</literal> separates the date and time parts of the interval.
2839 The values are given as numbers similar to ISO 8601 dates.
2840 </para>
2842 <para>
2843 When writing an interval constant with a <replaceable>fields</replaceable>
2844 specification, or when assigning a string to an interval column that was
2845 defined with a <replaceable>fields</replaceable> specification, the interpretation of
2846 unmarked quantities depends on the <replaceable>fields</replaceable>. For
2847 example <literal>INTERVAL '1' YEAR</literal> is read as 1 year, whereas
2848 <literal>INTERVAL '1'</literal> means 1 second. Also, field values
2849 <quote>to the right</quote> of the least significant field allowed by the
2850 <replaceable>fields</replaceable> specification are silently discarded. For
2851 example, writing <literal>INTERVAL '1 day 2:03:04' HOUR TO MINUTE</literal>
2852 results in dropping the seconds field, but not the day field.
2853 </para>
2855 <para>
2856 According to the <acronym>SQL</acronym> standard all fields of an interval
2857 value must have the same sign, so a leading negative sign applies to all
2858 fields; for example the negative sign in the interval literal
2859 <literal>'-1 2:03:04'</literal> applies to both the days and hour/minute/second
2860 parts. <productname>PostgreSQL</productname> allows the fields to have different
2861 signs, and traditionally treats each field in the textual representation
2862 as independently signed, so that the hour/minute/second part is
2863 considered positive in this example. If <varname>IntervalStyle</varname> is
2864 set to <literal>sql_standard</literal> then a leading sign is considered
2865 to apply to all fields (but only if no additional signs appear).
2866 Otherwise the traditional <productname>PostgreSQL</productname> interpretation is
2867 used. To avoid ambiguity, it's recommended to attach an explicit sign
2868 to each field if any field is negative.
2869 </para>
2871 <para>
2872 Internally, <type>interval</type> values are stored as three integral
2873 fields: months, days, and microseconds. These fields are kept
2874 separate because the number of days in a month varies, while a day
2875 can have 23 or 25 hours if a daylight savings time transition is
2876 involved. An interval input string that uses other units is
2877 normalized into this format, and then reconstructed in a standardized
2878 way for output, for example:
2880 <programlisting>
2881 SELECT '2 years 15 months 100 weeks 99 hours 123456789 milliseconds'::interval;
2882 interval
2883 ---------------------------------------
2884 3 years 3 mons 700 days 133:17:36.789
2885 </programlisting>
2887 Here weeks, which are understood as <quote>7 days</quote>, have been
2888 kept separate, while the smaller and larger time units were
2889 combined and normalized.
2890 </para>
2892 <para>
2893 Input field values can have fractional parts, for example <literal>'1.5
2894 weeks'</literal> or <literal>'01:02:03.45'</literal>. However,
2895 because <type>interval</type> internally stores only integral fields,
2896 fractional values must be converted into smaller
2897 units. Fractional parts of units greater than months are rounded to
2898 be an integer number of months, e.g. <literal>'1.5 years'</literal>
2899 becomes <literal>'1 year 6 mons'</literal>. Fractional parts of
2900 weeks and days are computed to be an integer number of days and
2901 microseconds, assuming 30 days per month and 24 hours per day, e.g.,
2902 <literal>'1.75 months'</literal> becomes <literal>1 mon 22 days
2903 12:00:00</literal>. Only seconds will ever be shown as fractional
2904 on output.
2905 </para>
2907 <para>
2908 <xref linkend="datatype-interval-input-examples"/> shows some examples
2909 of valid <type>interval</type> input.
2910 </para>
2912 <table id="datatype-interval-input-examples">
2913 <title>Interval Input</title>
2914 <tgroup cols="2">
2915 <thead>
2916 <row>
2917 <entry>Example</entry>
2918 <entry>Description</entry>
2919 </row>
2920 </thead>
2921 <tbody>
2922 <row>
2923 <entry><literal>1-2</literal></entry>
2924 <entry>SQL standard format: 1 year 2 months</entry>
2925 </row>
2926 <row>
2927 <entry><literal>3 4:05:06</literal></entry>
2928 <entry>SQL standard format: 3 days 4 hours 5 minutes 6 seconds</entry>
2929 </row>
2930 <row>
2931 <entry><literal>1 year 2 months 3 days 4 hours 5 minutes 6 seconds</literal></entry>
2932 <entry>Traditional Postgres format: 1 year 2 months 3 days 4 hours 5 minutes 6 seconds</entry>
2933 </row>
2934 <row>
2935 <entry><literal>P1Y2M3DT4H5M6S</literal></entry>
2936 <entry>ISO 8601 <quote>format with designators</quote>: same meaning as above</entry>
2937 </row>
2938 <row>
2939 <entry><literal>P0001-02-03T04:05:06</literal></entry>
2940 <entry>ISO 8601 <quote>alternative format</quote>: same meaning as above</entry>
2941 </row>
2942 </tbody>
2943 </tgroup>
2944 </table>
2946 </sect2>
2948 <sect2 id="datatype-interval-output">
2949 <title>Interval Output</title>
2951 <indexterm>
2952 <primary>interval</primary>
2953 <secondary>output format</secondary>
2954 <seealso>formatting</seealso>
2955 </indexterm>
2957 <para>
2958 As previously explained, <productname>PostgreSQL</productname>
2959 stores <type>interval</type> values as months, days, and
2960 microseconds. For output, the months field is converted to years and
2961 months by dividing by 12. The days field is shown as-is. The
2962 microseconds field is converted to hours, minutes, seconds, and
2963 fractional seconds. Thus months, minutes, and seconds will never be
2964 shown as exceeding the ranges 0&ndash;11, 0&ndash;59, and 0&ndash;59
2965 respectively, while the displayed years, days, and hours fields can
2966 be quite large. (The <link
2967 linkend="function-justify-days"><function>justify_days</function></link>
2968 and <link
2969 linkend="function-justify-hours"><function>justify_hours</function></link>
2970 functions can be used if it is desirable to transpose large days or
2971 hours values into the next higher field.)
2972 </para>
2974 <para>
2975 The output format of the interval type can be set to one of the
2976 four styles <literal>sql_standard</literal>, <literal>postgres</literal>,
2977 <literal>postgres_verbose</literal>, or <literal>iso_8601</literal>,
2978 using the command <literal>SET intervalstyle</literal>.
2979 The default is the <literal>postgres</literal> format.
2980 <xref linkend="interval-style-output-table"/> shows examples of each
2981 output style.
2982 </para>
2984 <para>
2985 The <literal>sql_standard</literal> style produces output that conforms to
2986 the SQL standard's specification for interval literal strings, if
2987 the interval value meets the standard's restrictions (either year-month
2988 only or day-time only, with no mixing of positive
2989 and negative components). Otherwise the output looks like a standard
2990 year-month literal string followed by a day-time literal string,
2991 with explicit signs added to disambiguate mixed-sign intervals.
2992 </para>
2994 <para>
2995 The output of the <literal>postgres</literal> style matches the output of
2996 <productname>PostgreSQL</productname> releases prior to 8.4 when the
2997 <xref linkend="guc-datestyle"/> parameter was set to <literal>ISO</literal>.
2998 </para>
3000 <para>
3001 The output of the <literal>postgres_verbose</literal> style matches the output of
3002 <productname>PostgreSQL</productname> releases prior to 8.4 when the
3003 <varname>DateStyle</varname> parameter was set to non-<literal>ISO</literal> output.
3004 </para>
3006 <para>
3007 The output of the <literal>iso_8601</literal> style matches the <quote>format
3008 with designators</quote> described in section 4.4.3.2 of the
3009 ISO 8601 standard.
3010 </para>
3012 <table id="interval-style-output-table">
3013 <title>Interval Output Style Examples</title>
3014 <tgroup cols="4">
3015 <thead>
3016 <row>
3017 <entry>Style Specification</entry>
3018 <entry>Year-Month Interval</entry>
3019 <entry>Day-Time Interval</entry>
3020 <entry>Mixed Interval</entry>
3021 </row>
3022 </thead>
3023 <tbody>
3024 <row>
3025 <entry><literal>sql_standard</literal></entry>
3026 <entry>1-2</entry>
3027 <entry>3 4:05:06</entry>
3028 <entry>-1-2 +3 -4:05:06</entry>
3029 </row>
3030 <row>
3031 <entry><literal>postgres</literal></entry>
3032 <entry>1 year 2 mons</entry>
3033 <entry>3 days 04:05:06</entry>
3034 <entry>-1 year -2 mons +3 days -04:05:06</entry>
3035 </row>
3036 <row>
3037 <entry><literal>postgres_verbose</literal></entry>
3038 <entry>@ 1 year 2 mons</entry>
3039 <entry>@ 3 days 4 hours 5 mins 6 secs</entry>
3040 <entry>@ 1 year 2 mons -3 days 4 hours 5 mins 6 secs ago</entry>
3041 </row>
3042 <row>
3043 <entry><literal>iso_8601</literal></entry>
3044 <entry>P1Y2M</entry>
3045 <entry>P3DT4H5M6S</entry>
3046 <entry>P-1Y-2M3D&zwsp;T-4H-5M-6S</entry>
3047 </row>
3048 </tbody>
3049 </tgroup>
3050 </table>
3052 </sect2>
3054 </sect1>
3056 <sect1 id="datatype-boolean">
3057 <title>Boolean Type</title>
3059 <indexterm zone="datatype-boolean">
3060 <primary>Boolean</primary>
3061 <secondary>data type</secondary>
3062 </indexterm>
3064 <indexterm zone="datatype-boolean">
3065 <primary>true</primary>
3066 </indexterm>
3068 <indexterm zone="datatype-boolean">
3069 <primary>false</primary>
3070 </indexterm>
3072 <para>
3073 <productname>PostgreSQL</productname> provides the
3074 standard <acronym>SQL</acronym> type <type>boolean</type>;
3075 see <xref linkend="datatype-boolean-table"/>.
3076 The <type>boolean</type> type can have several states:
3077 <quote>true</quote>, <quote>false</quote>, and a third state,
3078 <quote>unknown</quote>, which is represented by the
3079 <acronym>SQL</acronym> null value.
3080 </para>
3082 <table id="datatype-boolean-table">
3083 <title>Boolean Data Type</title>
3084 <tgroup cols="3">
3085 <thead>
3086 <row>
3087 <entry>Name</entry>
3088 <entry>Storage Size</entry>
3089 <entry>Description</entry>
3090 </row>
3091 </thead>
3092 <tbody>
3093 <row>
3094 <entry><type>boolean</type></entry>
3095 <entry>1 byte</entry>
3096 <entry>state of true or false</entry>
3097 </row>
3098 </tbody>
3099 </tgroup>
3100 </table>
3102 <para>
3103 Boolean constants can be represented in SQL queries by the SQL
3104 key words <literal>TRUE</literal>, <literal>FALSE</literal>,
3105 and <literal>NULL</literal>.
3106 </para>
3108 <para>
3109 The datatype input function for type <type>boolean</type> accepts these
3110 string representations for the <quote>true</quote> state:
3111 <simplelist>
3112 <member><literal>true</literal></member>
3113 <member><literal>yes</literal></member>
3114 <member><literal>on</literal></member>
3115 <member><literal>1</literal></member>
3116 </simplelist>
3117 and these representations for the <quote>false</quote> state:
3118 <simplelist>
3119 <member><literal>false</literal></member>
3120 <member><literal>no</literal></member>
3121 <member><literal>off</literal></member>
3122 <member><literal>0</literal></member>
3123 </simplelist>
3124 Unique prefixes of these strings are also accepted, for
3125 example <literal>t</literal> or <literal>n</literal>.
3126 Leading or trailing whitespace is ignored, and case does not matter.
3127 </para>
3129 <para>
3130 The datatype output function for type <type>boolean</type> always emits
3131 either <literal>t</literal> or <literal>f</literal>, as shown in
3132 <xref linkend="datatype-boolean-example"/>.
3133 </para>
3135 <example id="datatype-boolean-example">
3136 <title>Using the <type>boolean</type> Type</title>
3138 <programlisting>
3139 CREATE TABLE test1 (a boolean, b text);
3140 INSERT INTO test1 VALUES (TRUE, 'sic est');
3141 INSERT INTO test1 VALUES (FALSE, 'non est');
3142 SELECT * FROM test1;
3143 a | b
3144 ---+---------
3145 t | sic est
3146 f | non est
3148 SELECT * FROM test1 WHERE a;
3149 a | b
3150 ---+---------
3151 t | sic est
3152 </programlisting>
3153 </example>
3155 <para>
3156 The key words <literal>TRUE</literal> and <literal>FALSE</literal> are
3157 the preferred (<acronym>SQL</acronym>-compliant) method for writing
3158 Boolean constants in SQL queries. But you can also use the string
3159 representations by following the generic string-literal constant syntax
3160 described in <xref linkend="sql-syntax-constants-generic"/>, for
3161 example <literal>'yes'::boolean</literal>.
3162 </para>
3164 <para>
3165 Note that the parser automatically understands
3166 that <literal>TRUE</literal> and <literal>FALSE</literal> are of
3167 type <type>boolean</type>, but this is not so
3168 for <literal>NULL</literal> because that can have any type.
3169 So in some contexts you might have to cast <literal>NULL</literal>
3170 to <type>boolean</type> explicitly, for
3171 example <literal>NULL::boolean</literal>. Conversely, the cast can be
3172 omitted from a string-literal Boolean value in contexts where the parser
3173 can deduce that the literal must be of type <type>boolean</type>.
3174 </para>
3175 </sect1>
3177 <sect1 id="datatype-enum">
3178 <title>Enumerated Types</title>
3180 <indexterm zone="datatype-enum">
3181 <primary>data type</primary>
3182 <secondary>enumerated (enum)</secondary>
3183 </indexterm>
3185 <indexterm zone="datatype-enum">
3186 <primary>enumerated types</primary>
3187 </indexterm>
3189 <para>
3190 Enumerated (enum) types are data types that
3191 comprise a static, ordered set of values.
3192 They are equivalent to the <type>enum</type>
3193 types supported in a number of programming languages. An example of an enum
3194 type might be the days of the week, or a set of status values for
3195 a piece of data.
3196 </para>
3198 <sect2 id="datatype-enum-declaration">
3199 <title>Declaration of Enumerated Types</title>
3201 <para>
3202 Enum types are created using the <xref
3203 linkend="sql-createtype"/> command,
3204 for example:
3206 <programlisting>
3207 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
3208 </programlisting>
3210 Once created, the enum type can be used in table and function
3211 definitions much like any other type:
3212 <programlisting>
3213 CREATE TYPE mood AS ENUM ('sad', 'ok', 'happy');
3214 CREATE TABLE person (
3215 name text,
3216 current_mood mood
3218 INSERT INTO person VALUES ('Moe', 'happy');
3219 SELECT * FROM person WHERE current_mood = 'happy';
3220 name | current_mood
3221 ------+--------------
3222 Moe | happy
3223 (1 row)
3224 </programlisting>
3225 </para>
3226 </sect2>
3228 <sect2 id="datatype-enum-ordering">
3229 <title>Ordering</title>
3231 <para>
3232 The ordering of the values in an enum type is the
3233 order in which the values were listed when the type was created.
3234 All standard comparison operators and related
3235 aggregate functions are supported for enums. For example:
3237 <programlisting>
3238 INSERT INTO person VALUES ('Larry', 'sad');
3239 INSERT INTO person VALUES ('Curly', 'ok');
3240 SELECT * FROM person WHERE current_mood > 'sad';
3241 name | current_mood
3242 -------+--------------
3243 Moe | happy
3244 Curly | ok
3245 (2 rows)
3247 SELECT * FROM person WHERE current_mood > 'sad' ORDER BY current_mood;
3248 name | current_mood
3249 -------+--------------
3250 Curly | ok
3251 Moe | happy
3252 (2 rows)
3254 SELECT name
3255 FROM person
3256 WHERE current_mood = (SELECT MIN(current_mood) FROM person);
3257 name
3258 -------
3259 Larry
3260 (1 row)
3261 </programlisting>
3262 </para>
3263 </sect2>
3265 <sect2 id="datatype-enum-type-safety">
3266 <title>Type Safety</title>
3268 <para>
3269 Each enumerated data type is separate and cannot
3270 be compared with other enumerated types. See this example:
3272 <programlisting>
3273 CREATE TYPE happiness AS ENUM ('happy', 'very happy', 'ecstatic');
3274 CREATE TABLE holidays (
3275 num_weeks integer,
3276 happiness happiness
3278 INSERT INTO holidays(num_weeks,happiness) VALUES (4, 'happy');
3279 INSERT INTO holidays(num_weeks,happiness) VALUES (6, 'very happy');
3280 INSERT INTO holidays(num_weeks,happiness) VALUES (8, 'ecstatic');
3281 INSERT INTO holidays(num_weeks,happiness) VALUES (2, 'sad');
3282 ERROR: invalid input value for enum happiness: "sad"
3283 SELECT person.name, holidays.num_weeks FROM person, holidays
3284 WHERE person.current_mood = holidays.happiness;
3285 ERROR: operator does not exist: mood = happiness
3286 </programlisting>
3287 </para>
3289 <para>
3290 If you really need to do something like that, you can either
3291 write a custom operator or add explicit casts to your query:
3293 <programlisting>
3294 SELECT person.name, holidays.num_weeks FROM person, holidays
3295 WHERE person.current_mood::text = holidays.happiness::text;
3296 name | num_weeks
3297 ------+-----------
3298 Moe | 4
3299 (1 row)
3301 </programlisting>
3302 </para>
3303 </sect2>
3305 <sect2 id="datatype-enum-implementation-details">
3306 <title>Implementation Details</title>
3308 <para>
3309 Enum labels are case sensitive, so
3310 <type>'happy'</type> is not the same as <type>'HAPPY'</type>.
3311 White space in the labels is significant too.
3312 </para>
3314 <para>
3315 Although enum types are primarily intended for static sets of values,
3316 there is support for adding new values to an existing enum type, and for
3317 renaming values (see <xref linkend="sql-altertype"/>). Existing values
3318 cannot be removed from an enum type, nor can the sort ordering of such
3319 values be changed, short of dropping and re-creating the enum type.
3320 </para>
3322 <para>
3323 An enum value occupies four bytes on disk. The length of an enum
3324 value's textual label is limited by the <symbol>NAMEDATALEN</symbol>
3325 setting compiled into <productname>PostgreSQL</productname>; in standard
3326 builds this means at most 63 bytes.
3327 </para>
3329 <para>
3330 The translations from internal enum values to textual labels are
3331 kept in the system catalog
3332 <link linkend="catalog-pg-enum"><structname>pg_enum</structname></link>.
3333 Querying this catalog directly can be useful.
3334 </para>
3336 </sect2>
3337 </sect1>
3339 <sect1 id="datatype-geometric">
3340 <title>Geometric Types</title>
3342 <para>
3343 Geometric data types represent two-dimensional spatial
3344 objects. <xref linkend="datatype-geo-table"/> shows the geometric
3345 types available in <productname>PostgreSQL</productname>.
3346 </para>
3348 <table id="datatype-geo-table">
3349 <title>Geometric Types</title>
3350 <tgroup cols="4">
3351 <colspec colname="col1" colwidth="1*"/>
3352 <colspec colname="col2" colwidth="1*"/>
3353 <colspec colname="col3" colwidth="2*"/>
3354 <colspec colname="col4" colwidth="1*"/>
3355 <thead>
3356 <row>
3357 <entry>Name</entry>
3358 <entry>Storage Size</entry>
3359 <entry>Description</entry>
3360 <entry>Representation</entry>
3361 </row>
3362 </thead>
3363 <tbody>
3364 <row>
3365 <entry><type>point</type></entry>
3366 <entry>16 bytes</entry>
3367 <entry>Point on a plane</entry>
3368 <entry>(x,y)</entry>
3369 </row>
3370 <row>
3371 <entry><type>line</type></entry>
3372 <entry>24 bytes</entry>
3373 <entry>Infinite line</entry>
3374 <entry>{A,B,C}</entry>
3375 </row>
3376 <row>
3377 <entry><type>lseg</type></entry>
3378 <entry>32 bytes</entry>
3379 <entry>Finite line segment</entry>
3380 <entry>[(x1,y1),(x2,y2)]</entry>
3381 </row>
3382 <row>
3383 <entry><type>box</type></entry>
3384 <entry>32 bytes</entry>
3385 <entry>Rectangular box</entry>
3386 <entry>(x1,y1),(x2,y2)</entry>
3387 </row>
3388 <row>
3389 <entry><type>path</type></entry>
3390 <entry>16+16n bytes</entry>
3391 <entry>Closed path (similar to polygon)</entry>
3392 <entry>((x1,y1),...)</entry>
3393 </row>
3394 <row>
3395 <entry><type>path</type></entry>
3396 <entry>16+16n bytes</entry>
3397 <entry>Open path</entry>
3398 <entry>[(x1,y1),...]</entry>
3399 </row>
3400 <row>
3401 <entry><type>polygon</type></entry>
3402 <entry>40+16n bytes</entry>
3403 <entry>Polygon (similar to closed path)</entry>
3404 <entry>((x1,y1),...)</entry>
3405 </row>
3406 <row>
3407 <entry><type>circle</type></entry>
3408 <entry>24 bytes</entry>
3409 <entry>Circle</entry>
3410 <entry>&lt;(x,y),r&gt; (center point and radius)</entry>
3411 </row>
3412 </tbody>
3413 </tgroup>
3414 </table>
3416 <para>
3417 In all these types, the individual coordinates are stored as
3418 <type>double precision</type> (<type>float8</type>) numbers.
3419 </para>
3421 <para>
3422 A rich set of functions and operators is available to perform various geometric
3423 operations such as scaling, translation, rotation, and determining
3424 intersections. They are explained in <xref linkend="functions-geometry"/>.
3425 </para>
3427 <sect2 id="datatype-geometric-points">
3428 <title>Points</title>
3430 <indexterm>
3431 <primary>point</primary>
3432 </indexterm>
3434 <para>
3435 Points are the fundamental two-dimensional building block for geometric
3436 types. Values of type <type>point</type> are specified using either of
3437 the following syntaxes:
3439 <synopsis>
3440 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> )
3441 <replaceable>x</replaceable> , <replaceable>y</replaceable>
3442 </synopsis>
3444 where <replaceable>x</replaceable> and <replaceable>y</replaceable> are the respective
3445 coordinates, as floating-point numbers.
3446 </para>
3448 <para>
3449 Points are output using the first syntax.
3450 </para>
3451 </sect2>
3453 <sect2 id="datatype-line">
3454 <title>Lines</title>
3456 <indexterm>
3457 <primary>line</primary>
3458 </indexterm>
3460 <para>
3461 Lines are represented by the linear
3462 equation <replaceable>A</replaceable>x + <replaceable>B</replaceable>y + <replaceable>C</replaceable> = 0,
3463 where <replaceable>A</replaceable> and <replaceable>B</replaceable> are not both zero. Values
3464 of type <type>line</type> are input and output in the following form:
3465 <synopsis>
3466 { <replaceable>A</replaceable>, <replaceable>B</replaceable>, <replaceable>C</replaceable> }
3467 </synopsis>
3469 Alternatively, any of the following forms can be used for input:
3471 <synopsis>
3472 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) ]
3473 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3474 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3475 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3476 </synopsis>
3478 where
3479 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3481 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3482 are two different points on the line.
3483 </para>
3484 </sect2>
3486 <sect2 id="datatype-lseg">
3487 <title>Line Segments</title>
3489 <indexterm>
3490 <primary>lseg</primary>
3491 </indexterm>
3493 <indexterm>
3494 <primary>line segment</primary>
3495 </indexterm>
3497 <para>
3498 Line segments are represented by pairs of points that are the endpoints
3499 of the segment. Values of type <type>lseg</type> are specified using any
3500 of the following syntaxes:
3502 <synopsis>
3503 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) ]
3504 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3505 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3506 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3507 </synopsis>
3509 where
3510 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3512 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3513 are the end points of the line segment.
3514 </para>
3516 <para>
3517 Line segments are output using the first syntax.
3518 </para>
3519 </sect2>
3521 <sect2 id="datatype-geometric-boxes">
3522 <title>Boxes</title>
3524 <indexterm>
3525 <primary>box (data type)</primary>
3526 </indexterm>
3528 <indexterm>
3529 <primary>rectangle</primary>
3530 </indexterm>
3532 <para>
3533 Boxes are represented by pairs of points that are opposite
3534 corners of the box.
3535 Values of type <type>box</type> are specified using any of the following
3536 syntaxes:
3538 <synopsis>
3539 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> ) )
3540 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ( <replaceable>x2</replaceable> , <replaceable>y2</replaceable> )
3541 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , <replaceable>x2</replaceable> , <replaceable>y2</replaceable>
3542 </synopsis>
3544 where
3545 <literal>(<replaceable>x1</replaceable>,<replaceable>y1</replaceable>)</literal>
3547 <literal>(<replaceable>x2</replaceable>,<replaceable>y2</replaceable>)</literal>
3548 are any two opposite corners of the box.
3549 </para>
3551 <para>
3552 Boxes are output using the second syntax.
3553 </para>
3555 <para>
3556 Any two opposite corners can be supplied on input, but the values
3557 will be reordered as needed to store the
3558 upper right and lower left corners, in that order.
3559 </para>
3560 </sect2>
3562 <sect2 id="datatype-geometric-paths">
3563 <title>Paths</title>
3565 <indexterm>
3566 <primary>path (data type)</primary>
3567 </indexterm>
3569 <para>
3570 Paths are represented by lists of connected points. Paths can be
3571 <firstterm>open</firstterm>, where
3572 the first and last points in the list are considered not connected, or
3573 <firstterm>closed</firstterm>,
3574 where the first and last points are considered connected.
3575 </para>
3577 <para>
3578 Values of type <type>path</type> are specified using any of the following
3579 syntaxes:
3581 <synopsis>
3582 [ ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) ]
3583 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3584 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3585 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3586 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3587 </synopsis>
3589 where the points are the end points of the line segments
3590 comprising the path. Square brackets (<literal>[]</literal>) indicate
3591 an open path, while parentheses (<literal>()</literal>) indicate a
3592 closed path. When the outermost parentheses are omitted, as
3593 in the third through fifth syntaxes, a closed path is assumed.
3594 </para>
3596 <para>
3597 Paths are output using the first or second syntax, as appropriate.
3598 </para>
3599 </sect2>
3601 <sect2 id="datatype-polygon">
3602 <title>Polygons</title>
3604 <indexterm>
3605 <primary>polygon</primary>
3606 </indexterm>
3608 <para>
3609 Polygons are represented by lists of points (the vertices of the
3610 polygon). Polygons are very similar to closed paths; the essential
3611 semantic difference is that a polygon is considered to include the
3612 area within it, while a path is not.
3613 </para>
3615 <para>
3616 An important implementation difference between polygons and
3617 paths is that the stored representation of a polygon includes its
3618 smallest bounding box. This speeds up certain search operations,
3619 although computing the bounding box adds overhead while constructing
3620 new polygons.
3621 </para>
3623 <para>
3624 Values of type <type>polygon</type> are specified using any of the
3625 following syntaxes:
3627 <synopsis>
3628 ( ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> ) )
3629 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> ) , ... , ( <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3630 ( <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable> )
3631 <replaceable>x1</replaceable> , <replaceable>y1</replaceable> , ... , <replaceable>xn</replaceable> , <replaceable>yn</replaceable>
3632 </synopsis>
3634 where the points are the end points of the line segments
3635 comprising the boundary of the polygon.
3636 </para>
3638 <para>
3639 Polygons are output using the first syntax.
3640 </para>
3641 </sect2>
3643 <sect2 id="datatype-circle">
3644 <title>Circles</title>
3646 <indexterm>
3647 <primary>circle</primary>
3648 </indexterm>
3650 <para>
3651 Circles are represented by a center point and radius.
3652 Values of type <type>circle</type> are specified using any of the
3653 following syntaxes:
3655 <synopsis>
3656 &lt; ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> &gt;
3657 ( ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable> )
3658 ( <replaceable>x</replaceable> , <replaceable>y</replaceable> ) , <replaceable>r</replaceable>
3659 <replaceable>x</replaceable> , <replaceable>y</replaceable> , <replaceable>r</replaceable>
3660 </synopsis>
3662 where
3663 <literal>(<replaceable>x</replaceable>,<replaceable>y</replaceable>)</literal>
3664 is the center point and <replaceable>r</replaceable> is the radius of the
3665 circle.
3666 </para>
3668 <para>
3669 Circles are output using the first syntax.
3670 </para>
3671 </sect2>
3673 </sect1>
3675 <sect1 id="datatype-net-types">
3676 <title>Network Address Types</title>
3678 <indexterm zone="datatype-net-types">
3679 <primary>network</primary>
3680 <secondary>data types</secondary>
3681 </indexterm>
3683 <para>
3684 <productname>PostgreSQL</productname> offers data types to store IPv4, IPv6, and MAC
3685 addresses, as shown in <xref linkend="datatype-net-types-table"/>. It
3686 is better to use these types instead of plain text types to store
3687 network addresses, because
3688 these types offer input error checking and specialized
3689 operators and functions (see <xref linkend="functions-net"/>).
3690 </para>
3692 <table tocentry="1" id="datatype-net-types-table">
3693 <title>Network Address Types</title>
3694 <tgroup cols="3">
3695 <colspec colname="col1" colwidth="1*"/>
3696 <colspec colname="col2" colwidth="1*"/>
3697 <colspec colname="col3" colwidth="2*"/>
3698 <thead>
3699 <row>
3700 <entry>Name</entry>
3701 <entry>Storage Size</entry>
3702 <entry>Description</entry>
3703 </row>
3704 </thead>
3705 <tbody>
3707 <row>
3708 <entry><type>cidr</type></entry>
3709 <entry>7 or 19 bytes</entry>
3710 <entry>IPv4 and IPv6 networks</entry>
3711 </row>
3713 <row>
3714 <entry><type>inet</type></entry>
3715 <entry>7 or 19 bytes</entry>
3716 <entry>IPv4 and IPv6 hosts and networks</entry>
3717 </row>
3719 <row>
3720 <entry><type>macaddr</type></entry>
3721 <entry>6 bytes</entry>
3722 <entry>MAC addresses</entry>
3723 </row>
3725 <row>
3726 <entry><type>macaddr8</type></entry>
3727 <entry>8 bytes</entry>
3728 <entry>MAC addresses (EUI-64 format)</entry>
3729 </row>
3731 </tbody>
3732 </tgroup>
3733 </table>
3735 <para>
3736 When sorting <type>inet</type> or <type>cidr</type> data types,
3737 IPv4 addresses will always sort before IPv6 addresses, including
3738 IPv4 addresses encapsulated or mapped to IPv6 addresses, such as
3739 ::10.2.3.4 or ::ffff:10.4.3.2.
3740 </para>
3743 <sect2 id="datatype-inet">
3744 <title><type>inet</type></title>
3746 <indexterm>
3747 <primary>inet (data type)</primary>
3748 </indexterm>
3750 <para>
3751 The <type>inet</type> type holds an IPv4 or IPv6 host address, and
3752 optionally its subnet, all in one field.
3753 The subnet is represented by the number of network address bits
3754 present in the host address (the
3755 <quote>netmask</quote>). If the netmask is 32 and the address is IPv4,
3756 then the value does not indicate a subnet, only a single host.
3757 In IPv6, the address length is 128 bits, so 128 bits specify a
3758 unique host address. Note that if you
3759 want to accept only networks, you should use the
3760 <type>cidr</type> type rather than <type>inet</type>.
3761 </para>
3763 <para>
3764 The input format for this type is
3765 <replaceable class="parameter">address/y</replaceable>
3766 where
3767 <replaceable class="parameter">address</replaceable>
3768 is an IPv4 or IPv6 address and
3769 <replaceable class="parameter">y</replaceable>
3770 is the number of bits in the netmask. If the
3771 <replaceable class="parameter">/y</replaceable>
3772 portion is omitted, the
3773 netmask is taken to be 32 for IPv4 or 128 for IPv6,
3774 so the value represents
3775 just a single host. On display, the
3776 <replaceable class="parameter">/y</replaceable>
3777 portion is suppressed if the netmask specifies a single host.
3778 </para>
3779 </sect2>
3781 <sect2 id="datatype-cidr">
3782 <title><type>cidr</type></title>
3784 <indexterm>
3785 <primary>cidr</primary>
3786 </indexterm>
3788 <para>
3789 The <type>cidr</type> type holds an IPv4 or IPv6 network specification.
3790 Input and output formats follow Classless Internet Domain Routing
3791 conventions.
3792 The format for specifying networks is <replaceable
3793 class="parameter">address/y</replaceable> where <replaceable
3794 class="parameter">address</replaceable> is the network's lowest
3795 address represented as an
3796 IPv4 or IPv6 address, and <replaceable
3797 class="parameter">y</replaceable> is the number of bits in the netmask. If
3798 <replaceable class="parameter">y</replaceable> is omitted, it is calculated
3799 using assumptions from the older classful network numbering system, except
3800 it will be at least large enough to include all of the octets
3801 written in the input. It is an error to specify a network address
3802 that has bits set to the right of the specified netmask.
3803 </para>
3805 <para>
3806 <xref linkend="datatype-net-cidr-table"/> shows some examples.
3807 </para>
3809 <table id="datatype-net-cidr-table">
3810 <title><type>cidr</type> Type Input Examples</title>
3811 <tgroup cols="3">
3812 <thead>
3813 <row>
3814 <entry><type>cidr</type> Input</entry>
3815 <entry><type>cidr</type> Output</entry>
3816 <entry><literal><function>abbrev(<type>cidr</type>)</function></literal></entry>
3817 </row>
3818 </thead>
3819 <tbody>
3820 <row>
3821 <entry>192.168.100.128/25</entry>
3822 <entry>192.168.100.128/25</entry>
3823 <entry>192.168.100.128/25</entry>
3824 </row>
3825 <row>
3826 <entry>192.168/24</entry>
3827 <entry>192.168.0.0/24</entry>
3828 <entry>192.168.0/24</entry>
3829 </row>
3830 <row>
3831 <entry>192.168/25</entry>
3832 <entry>192.168.0.0/25</entry>
3833 <entry>192.168.0.0/25</entry>
3834 </row>
3835 <row>
3836 <entry>192.168.1</entry>
3837 <entry>192.168.1.0/24</entry>
3838 <entry>192.168.1/24</entry>
3839 </row>
3840 <row>
3841 <entry>192.168</entry>
3842 <entry>192.168.0.0/24</entry>
3843 <entry>192.168.0/24</entry>
3844 </row>
3845 <row>
3846 <entry>128.1</entry>
3847 <entry>128.1.0.0/16</entry>
3848 <entry>128.1/16</entry>
3849 </row>
3850 <row>
3851 <entry>128</entry>
3852 <entry>128.0.0.0/16</entry>
3853 <entry>128.0/16</entry>
3854 </row>
3855 <row>
3856 <entry>128.1.2</entry>
3857 <entry>128.1.2.0/24</entry>
3858 <entry>128.1.2/24</entry>
3859 </row>
3860 <row>
3861 <entry>10.1.2</entry>
3862 <entry>10.1.2.0/24</entry>
3863 <entry>10.1.2/24</entry>
3864 </row>
3865 <row>
3866 <entry>10.1</entry>
3867 <entry>10.1.0.0/16</entry>
3868 <entry>10.1/16</entry>
3869 </row>
3870 <row>
3871 <entry>10</entry>
3872 <entry>10.0.0.0/8</entry>
3873 <entry>10/8</entry>
3874 </row>
3875 <row>
3876 <entry>10.1.2.3/32</entry>
3877 <entry>10.1.2.3/32</entry>
3878 <entry>10.1.2.3/32</entry>
3879 </row>
3880 <row>
3881 <entry>2001:4f8:3:ba::/64</entry>
3882 <entry>2001:4f8:3:ba::/64</entry>
3883 <entry>2001:4f8:3:ba/64</entry>
3884 </row>
3885 <row>
3886 <entry>2001:4f8:3:ba:&zwsp;2e0:81ff:fe22:d1f1/128</entry>
3887 <entry>2001:4f8:3:ba:&zwsp;2e0:81ff:fe22:d1f1/128</entry>
3888 <entry>2001:4f8:3:ba:&zwsp;2e0:81ff:fe22:d1f1/128</entry>
3889 </row>
3890 <row>
3891 <entry>::ffff:1.2.3.0/120</entry>
3892 <entry>::ffff:1.2.3.0/120</entry>
3893 <entry>::ffff:1.2.3/120</entry>
3894 </row>
3895 <row>
3896 <entry>::ffff:1.2.3.0/128</entry>
3897 <entry>::ffff:1.2.3.0/128</entry>
3898 <entry>::ffff:1.2.3.0/128</entry>
3899 </row>
3900 </tbody>
3901 </tgroup>
3902 </table>
3903 </sect2>
3905 <sect2 id="datatype-inet-vs-cidr">
3906 <title><type>inet</type> vs. <type>cidr</type></title>
3908 <para>
3909 The essential difference between <type>inet</type> and <type>cidr</type>
3910 data types is that <type>inet</type> accepts values with nonzero bits to
3911 the right of the netmask, whereas <type>cidr</type> does not. For
3912 example, <literal>192.168.0.1/24</literal> is valid for <type>inet</type>
3913 but not for <type>cidr</type>.
3914 </para>
3916 <tip>
3917 <para>
3918 If you do not like the output format for <type>inet</type> or
3919 <type>cidr</type> values, try the functions <function>host</function>,
3920 <function>text</function>, and <function>abbrev</function>.
3921 </para>
3922 </tip>
3923 </sect2>
3925 <sect2 id="datatype-macaddr">
3926 <title><type>macaddr</type></title>
3928 <indexterm>
3929 <primary>macaddr (data type)</primary>
3930 </indexterm>
3932 <indexterm>
3933 <primary>MAC address</primary>
3934 <see>macaddr</see>
3935 </indexterm>
3937 <para>
3938 The <type>macaddr</type> type stores MAC addresses, known for example
3939 from Ethernet card hardware addresses (although MAC addresses are
3940 used for other purposes as well). Input is accepted in the
3941 following formats:
3943 <simplelist>
3944 <member><literal>'08:00:2b:01:02:03'</literal></member>
3945 <member><literal>'08-00-2b-01-02-03'</literal></member>
3946 <member><literal>'08002b:010203'</literal></member>
3947 <member><literal>'08002b-010203'</literal></member>
3948 <member><literal>'0800.2b01.0203'</literal></member>
3949 <member><literal>'0800-2b01-0203'</literal></member>
3950 <member><literal>'08002b010203'</literal></member>
3951 </simplelist>
3953 These examples all specify the same address. Upper and
3954 lower case is accepted for the digits
3955 <literal>a</literal> through <literal>f</literal>. Output is always in the
3956 first of the forms shown.
3957 </para>
3959 <para>
3960 IEEE Standard 802-2001 specifies the second form shown (with hyphens)
3961 as the canonical form for MAC addresses, and specifies the first
3962 form (with colons) as used with bit-reversed, MSB-first notation, so that
3963 08-00-2b-01-02-03 = 10:00:D4:80:40:C0. This convention is widely
3964 ignored nowadays, and it is relevant only for obsolete network
3965 protocols (such as Token Ring). PostgreSQL makes no provisions
3966 for bit reversal; all accepted formats use the canonical LSB
3967 order.
3968 </para>
3970 <para>
3971 The remaining five input formats are not part of any standard.
3972 </para>
3973 </sect2>
3975 <sect2 id="datatype-macaddr8">
3976 <title><type>macaddr8</type></title>
3978 <indexterm>
3979 <primary>macaddr8 (data type)</primary>
3980 </indexterm>
3982 <indexterm>
3983 <primary>MAC address (EUI-64 format)</primary>
3984 <see>macaddr</see>
3985 </indexterm>
3987 <para>
3988 The <type>macaddr8</type> type stores MAC addresses in EUI-64
3989 format, known for example from Ethernet card hardware addresses
3990 (although MAC addresses are used for other purposes as well).
3991 This type can accept both 6 and 8 byte length MAC addresses
3992 and stores them in 8 byte length format. MAC addresses given
3993 in 6 byte format will be stored in 8 byte length format with the
3994 4th and 5th bytes set to FF and FE, respectively.
3996 Note that IPv6 uses a modified EUI-64 format where the 7th bit
3997 should be set to one after the conversion from EUI-48. The
3998 function <function>macaddr8_set7bit</function> is provided to make this
3999 change.
4001 Generally speaking, any input which is comprised of pairs of hex
4002 digits (on byte boundaries), optionally separated consistently by
4003 one of <literal>':'</literal>, <literal>'-'</literal> or <literal>'.'</literal>, is
4004 accepted. The number of hex digits must be either 16 (8 bytes) or
4005 12 (6 bytes). Leading and trailing whitespace is ignored.
4007 The following are examples of input formats that are accepted:
4009 <simplelist>
4010 <member><literal>'08:00:2b:01:02:03:04:05'</literal></member>
4011 <member><literal>'08-00-2b-01-02-03-04-05'</literal></member>
4012 <member><literal>'08002b:0102030405'</literal></member>
4013 <member><literal>'08002b-0102030405'</literal></member>
4014 <member><literal>'0800.2b01.0203.0405'</literal></member>
4015 <member><literal>'0800-2b01-0203-0405'</literal></member>
4016 <member><literal>'08002b01:02030405'</literal></member>
4017 <member><literal>'08002b0102030405'</literal></member>
4018 </simplelist>
4020 These examples all specify the same address. Upper and
4021 lower case is accepted for the digits
4022 <literal>a</literal> through <literal>f</literal>. Output is always in the
4023 first of the forms shown.
4024 </para>
4026 <para>
4027 The last six input formats shown above are not part of any standard.
4028 </para>
4030 <para>
4031 To convert a traditional 48 bit MAC address in EUI-48 format to
4032 modified EUI-64 format to be included as the host portion of an
4033 IPv6 address, use <function>macaddr8_set7bit</function> as shown:
4035 <programlisting>
4036 SELECT macaddr8_set7bit('08:00:2b:01:02:03');
4037 <computeroutput>
4038 macaddr8_set7bit
4039 -------------------------
4040 0a:00:2b:ff:fe:01:02:03
4041 (1 row)
4042 </computeroutput>
4043 </programlisting>
4045 </para>
4047 </sect2>
4049 </sect1>
4051 <sect1 id="datatype-bit">
4052 <title>Bit String Types</title>
4054 <indexterm zone="datatype-bit">
4055 <primary>bit string</primary>
4056 <secondary>data type</secondary>
4057 </indexterm>
4059 <para>
4060 Bit strings are strings of 1's and 0's. They can be used to store
4061 or visualize bit masks. There are two SQL bit types:
4062 <type>bit(<replaceable>n</replaceable>)</type> and <type>bit
4063 varying(<replaceable>n</replaceable>)</type>, where
4064 <replaceable>n</replaceable> is a positive integer.
4065 </para>
4067 <para>
4068 <type>bit</type> type data must match the length
4069 <replaceable>n</replaceable> exactly; it is an error to attempt to
4070 store shorter or longer bit strings. <type>bit varying</type> data is
4071 of variable length up to the maximum length
4072 <replaceable>n</replaceable>; longer strings will be rejected.
4073 Writing <type>bit</type> without a length is equivalent to
4074 <literal>bit(1)</literal>, while <type>bit varying</type> without a length
4075 specification means unlimited length.
4076 </para>
4078 <note>
4079 <para>
4080 If one explicitly casts a bit-string value to
4081 <type>bit(<replaceable>n</replaceable>)</type>, it will be truncated or
4082 zero-padded on the right to be exactly <replaceable>n</replaceable> bits,
4083 without raising an error. Similarly,
4084 if one explicitly casts a bit-string value to
4085 <type>bit varying(<replaceable>n</replaceable>)</type>, it will be truncated
4086 on the right if it is more than <replaceable>n</replaceable> bits.
4087 </para>
4088 </note>
4090 <para>
4091 Refer to <xref
4092 linkend="sql-syntax-bit-strings"/> for information about the syntax
4093 of bit string constants. Bit-logical operators and string
4094 manipulation functions are available; see <xref
4095 linkend="functions-bitstring"/>.
4096 </para>
4098 <example>
4099 <title>Using the Bit String Types</title>
4101 <programlisting>
4102 CREATE TABLE test (a BIT(3), b BIT VARYING(5));
4103 INSERT INTO test VALUES (B'101', B'00');
4104 INSERT INTO test VALUES (B'10', B'101');
4105 <computeroutput>
4106 ERROR: bit string length 2 does not match type bit(3)
4107 </computeroutput>
4108 INSERT INTO test VALUES (B'10'::bit(3), B'101');
4109 SELECT * FROM test;
4110 <computeroutput>
4111 a | b
4112 -----+-----
4113 101 | 00
4114 100 | 101
4115 </computeroutput>
4116 </programlisting>
4117 </example>
4119 <para>
4120 A bit string value requires 1 byte for each group of 8 bits, plus
4121 5 or 8 bytes overhead depending on the length of the string
4122 (but long values may be compressed or moved out-of-line, as explained
4123 in <xref linkend="datatype-character"/> for character strings).
4124 </para>
4125 </sect1>
4127 <sect1 id="datatype-textsearch">
4128 <title>Text Search Types</title>
4130 <indexterm zone="datatype-textsearch">
4131 <primary>full text search</primary>
4132 <secondary>data types</secondary>
4133 </indexterm>
4135 <indexterm zone="datatype-textsearch">
4136 <primary>text search</primary>
4137 <secondary>data types</secondary>
4138 </indexterm>
4140 <para>
4141 <productname>PostgreSQL</productname> provides two data types that
4142 are designed to support full text search, which is the activity of
4143 searching through a collection of natural-language <firstterm>documents</firstterm>
4144 to locate those that best match a <firstterm>query</firstterm>.
4145 The <type>tsvector</type> type represents a document in a form optimized
4146 for text search; the <type>tsquery</type> type similarly represents
4147 a text query.
4148 <xref linkend="textsearch"/> provides a detailed explanation of this
4149 facility, and <xref linkend="functions-textsearch"/> summarizes the
4150 related functions and operators.
4151 </para>
4153 <sect2 id="datatype-tsvector">
4154 <title><type>tsvector</type></title>
4156 <indexterm>
4157 <primary>tsvector (data type)</primary>
4158 </indexterm>
4160 <para>
4161 A <type>tsvector</type> value is a sorted list of distinct
4162 <firstterm>lexemes</firstterm>, which are words that have been
4163 <firstterm>normalized</firstterm> to merge different variants of the same word
4164 (see <xref linkend="textsearch"/> for details). Sorting and
4165 duplicate-elimination are done automatically during input, as shown in
4166 this example:
4168 <programlisting>
4169 SELECT 'a fat cat sat on a mat and ate a fat rat'::tsvector;
4170 tsvector
4171 ----------------------------------------------------
4172 'a' 'and' 'ate' 'cat' 'fat' 'mat' 'on' 'rat' 'sat'
4173 </programlisting>
4175 To represent
4176 lexemes containing whitespace or punctuation, surround them with quotes:
4178 <programlisting>
4179 SELECT $$the lexeme ' ' contains spaces$$::tsvector;
4180 tsvector
4181 -------------------------------------------
4182 ' ' 'contains' 'lexeme' 'spaces' 'the'
4183 </programlisting>
4185 (We use dollar-quoted string literals in this example and the next one
4186 to avoid the confusion of having to double quote marks within the
4187 literals.) Embedded quotes and backslashes must be doubled:
4189 <programlisting>
4190 SELECT $$the lexeme 'Joe''s' contains a quote$$::tsvector;
4191 tsvector
4192 ------------------------------------------------
4193 'Joe''s' 'a' 'contains' 'lexeme' 'quote' 'the'
4194 </programlisting>
4196 Optionally, integer <firstterm>positions</firstterm>
4197 can be attached to lexemes:
4199 <programlisting>
4200 SELECT 'a:1 fat:2 cat:3 sat:4 on:5 a:6 mat:7 and:8 ate:9 a:10 fat:11 rat:12'::tsvector;
4201 tsvector
4202 -------------------------------------------------------------------&zwsp;------------
4203 'a':1,6,10 'and':8 'ate':9 'cat':3 'fat':2,11 'mat':7 'on':5 'rat':12 'sat':4
4204 </programlisting>
4206 A position normally indicates the source word's location in the
4207 document. Positional information can be used for
4208 <firstterm>proximity ranking</firstterm>. Position values can
4209 range from 1 to 16383; larger numbers are silently set to 16383.
4210 Duplicate positions for the same lexeme are discarded.
4211 </para>
4213 <para>
4214 Lexemes that have positions can further be labeled with a
4215 <firstterm>weight</firstterm>, which can be <literal>A</literal>,
4216 <literal>B</literal>, <literal>C</literal>, or <literal>D</literal>.
4217 <literal>D</literal> is the default and hence is not shown on output:
4219 <programlisting>
4220 SELECT 'a:1A fat:2B,4C cat:5D'::tsvector;
4221 tsvector
4222 ----------------------------
4223 'a':1A 'cat':5 'fat':2B,4C
4224 </programlisting>
4226 Weights are typically used to reflect document structure, for example
4227 by marking title words differently from body words. Text search
4228 ranking functions can assign different priorities to the different
4229 weight markers.
4230 </para>
4232 <para>
4233 It is important to understand that the
4234 <type>tsvector</type> type itself does not perform any word
4235 normalization; it assumes the words it is given are normalized
4236 appropriately for the application. For example,
4238 <programlisting>
4239 SELECT 'The Fat Rats'::tsvector;
4240 tsvector
4241 --------------------
4242 'Fat' 'Rats' 'The'
4243 </programlisting>
4245 For most English-text-searching applications the above words would
4246 be considered non-normalized, but <type>tsvector</type> doesn't care.
4247 Raw document text should usually be passed through
4248 <function>to_tsvector</function> to normalize the words appropriately
4249 for searching:
4251 <programlisting>
4252 SELECT to_tsvector('english', 'The Fat Rats');
4253 to_tsvector
4254 -----------------
4255 'fat':2 'rat':3
4256 </programlisting>
4258 Again, see <xref linkend="textsearch"/> for more detail.
4259 </para>
4261 </sect2>
4263 <sect2 id="datatype-tsquery">
4264 <title><type>tsquery</type></title>
4266 <indexterm>
4267 <primary>tsquery (data type)</primary>
4268 </indexterm>
4270 <para>
4271 A <type>tsquery</type> value stores lexemes that are to be
4272 searched for, and can combine them using the Boolean operators
4273 <literal>&amp;</literal> (AND), <literal>|</literal> (OR), and
4274 <literal>!</literal> (NOT), as well as the phrase search operator
4275 <literal>&lt;-&gt;</literal> (FOLLOWED BY). There is also a variant
4276 <literal>&lt;<replaceable>N</replaceable>&gt;</literal> of the FOLLOWED BY
4277 operator, where <replaceable>N</replaceable> is an integer constant that
4278 specifies the distance between the two lexemes being searched
4279 for. <literal>&lt;-&gt;</literal> is equivalent to <literal>&lt;1&gt;</literal>.
4280 </para>
4282 <para>
4283 Parentheses can be used to enforce grouping of these operators.
4284 In the absence of parentheses, <literal>!</literal> (NOT) binds most tightly,
4285 <literal>&lt;-&gt;</literal> (FOLLOWED BY) next most tightly, then
4286 <literal>&amp;</literal> (AND), with <literal>|</literal> (OR) binding
4287 the least tightly.
4288 </para>
4290 <para>
4291 Here are some examples:
4293 <programlisting>
4294 SELECT 'fat &amp; rat'::tsquery;
4295 tsquery
4296 ---------------
4297 'fat' &amp; 'rat'
4299 SELECT 'fat &amp; (rat | cat)'::tsquery;
4300 tsquery
4301 ---------------------------
4302 'fat' &amp; ( 'rat' | 'cat' )
4304 SELECT 'fat &amp; rat &amp; ! cat'::tsquery;
4305 tsquery
4306 ------------------------
4307 'fat' &amp; 'rat' &amp; !'cat'
4308 </programlisting>
4309 </para>
4311 <para>
4312 Optionally, lexemes in a <type>tsquery</type> can be labeled with
4313 one or more weight letters, which restricts them to match only
4314 <type>tsvector</type> lexemes with one of those weights:
4316 <programlisting>
4317 SELECT 'fat:ab &amp; cat'::tsquery;
4318 tsquery
4319 ------------------
4320 'fat':AB &amp; 'cat'
4321 </programlisting>
4322 </para>
4324 <para>
4325 Also, lexemes in a <type>tsquery</type> can be labeled with <literal>*</literal>
4326 to specify prefix matching:
4327 <programlisting>
4328 SELECT 'super:*'::tsquery;
4329 tsquery
4330 -----------
4331 'super':*
4332 </programlisting>
4333 This query will match any word in a <type>tsvector</type> that begins
4334 with <quote>super</quote>.
4335 </para>
4337 <para>
4338 Quoting rules for lexemes are the same as described previously for
4339 lexemes in <type>tsvector</type>; and, as with <type>tsvector</type>,
4340 any required normalization of words must be done before converting
4341 to the <type>tsquery</type> type. The <function>to_tsquery</function>
4342 function is convenient for performing such normalization:
4344 <programlisting>
4345 SELECT to_tsquery('Fat:ab &amp; Cats');
4346 to_tsquery
4347 ------------------
4348 'fat':AB &amp; 'cat'
4349 </programlisting>
4351 Note that <function>to_tsquery</function> will process prefixes in the same way
4352 as other words, which means this comparison returns true:
4354 <programlisting>
4355 SELECT to_tsvector( 'postgraduate' ) @@ to_tsquery( 'postgres:*' );
4356 ?column?
4357 ----------
4359 </programlisting>
4360 because <literal>postgres</literal> gets stemmed to <literal>postgr</literal>:
4361 <programlisting>
4362 SELECT to_tsvector( 'postgraduate' ), to_tsquery( 'postgres:*' );
4363 to_tsvector | to_tsquery
4364 ---------------+------------
4365 'postgradu':1 | 'postgr':*
4366 </programlisting>
4367 which will match the stemmed form of <literal>postgraduate</literal>.
4368 </para>
4370 </sect2>
4372 </sect1>
4374 <sect1 id="datatype-uuid">
4375 <title><acronym>UUID</acronym> Type</title>
4377 <indexterm zone="datatype-uuid">
4378 <primary>UUID</primary>
4379 </indexterm>
4381 <para>
4382 The data type <type>uuid</type> stores Universally Unique Identifiers
4383 (UUID) as defined by <ulink url="https://datatracker.ietf.org/doc/html/rfc9562">RFC 9562</ulink>,
4384 ISO/IEC 9834-8:2005, and related standards.
4385 (Some systems refer to this data type as a globally unique identifier, or
4386 GUID,<indexterm><primary>GUID</primary></indexterm> instead.) This
4387 identifier is a 128-bit quantity that is generated by an algorithm chosen
4388 to make it very unlikely that the same identifier will be generated by
4389 anyone else in the known universe using the same algorithm. Therefore,
4390 for distributed systems, these identifiers provide a better uniqueness
4391 guarantee than sequence generators, which
4392 are only unique within a single database.
4393 </para>
4395 <para>
4396 A UUID is written as a sequence of lower-case hexadecimal digits,
4397 in several groups separated by hyphens, specifically a group of 8
4398 digits followed by three groups of 4 digits followed by a group of
4399 12 digits, for a total of 32 digits representing the 128 bits. An
4400 example of a UUID in this standard form is:
4401 <programlisting>
4402 a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11
4403 </programlisting>
4404 <productname>PostgreSQL</productname> also accepts the following
4405 alternative forms for input:
4406 use of upper-case digits, the standard format surrounded by
4407 braces, omitting some or all hyphens, adding a hyphen after any
4408 group of four digits. Examples are:
4409 <programlisting>
4410 A0EEBC99-9C0B-4EF8-BB6D-6BB9BD380A11
4411 {a0eebc99-9c0b-4ef8-bb6d-6bb9bd380a11}
4412 a0eebc999c0b4ef8bb6d6bb9bd380a11
4413 a0ee-bc99-9c0b-4ef8-bb6d-6bb9-bd38-0a11
4414 {a0eebc99-9c0b4ef8-bb6d6bb9-bd380a11}
4415 </programlisting>
4416 Output is always in the standard form.
4417 </para>
4419 <para>
4420 See <xref linkend="functions-uuid"/> for how to generate a UUID in
4421 <productname>PostgreSQL</productname>.
4422 </para>
4423 </sect1>
4425 <sect1 id="datatype-xml">
4426 <title><acronym>XML</acronym> Type</title>
4428 <indexterm zone="datatype-xml">
4429 <primary>XML</primary>
4430 </indexterm>
4432 <para>
4433 The <type>xml</type> data type can be used to store XML data. Its
4434 advantage over storing XML data in a <type>text</type> field is that it
4435 checks the input values for well-formedness, and there are support
4436 functions to perform type-safe operations on it; see <xref
4437 linkend="functions-xml"/>. Use of this data type requires the
4438 installation to have been built with <command>configure
4439 --with-libxml</command>.
4440 </para>
4442 <para>
4443 The <type>xml</type> type can store well-formed
4444 <quote>documents</quote>, as defined by the XML standard, as well
4445 as <quote>content</quote> fragments, which are defined by reference
4446 to the more permissive
4447 <ulink url="https://www.w3.org/TR/2010/REC-xpath-datamodel-20101214/#DocumentNode"><quote>document node</quote></ulink>
4448 of the XQuery and XPath data model.
4449 Roughly, this means that content fragments can have
4450 more than one top-level element or character node. The expression
4451 <literal><replaceable>xmlvalue</replaceable> IS DOCUMENT</literal>
4452 can be used to evaluate whether a particular <type>xml</type>
4453 value is a full document or only a content fragment.
4454 </para>
4456 <para>
4457 Limits and compatibility notes for the <type>xml</type> data type
4458 can be found in <xref linkend="xml-limits-conformance"/>.
4459 </para>
4461 <sect2 id="datatype-xml-creating">
4462 <title>Creating XML Values</title>
4463 <para>
4464 To produce a value of type <type>xml</type> from character data,
4465 use the function
4466 <function>xmlparse</function>:<indexterm><primary>xmlparse</primary></indexterm>
4467 <synopsis>
4468 XMLPARSE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable>)
4469 </synopsis>
4470 Examples:
4471 <programlisting><![CDATA[
4472 XMLPARSE (DOCUMENT '<?xml version="1.0"?><book><title>Manual</title><chapter>...</chapter></book>')
4473 XMLPARSE (CONTENT 'abc<foo>bar</foo><bar>foo</bar>')
4474 ]]></programlisting>
4475 While this is the only way to convert character strings into XML
4476 values according to the SQL standard, the PostgreSQL-specific
4477 syntaxes:
4478 <programlisting><![CDATA[
4479 xml '<foo>bar</foo>'
4480 '<foo>bar</foo>'::xml
4481 ]]></programlisting>
4482 can also be used.
4483 </para>
4485 <para>
4486 The <type>xml</type> type does not validate input values
4487 against a document type declaration
4488 (DTD),<indexterm><primary>DTD</primary></indexterm>
4489 even when the input value specifies a DTD.
4490 There is also currently no built-in support for validating against
4491 other XML schema languages such as XML Schema.
4492 </para>
4494 <para>
4495 The inverse operation, producing a character string value from
4496 <type>xml</type>, uses the function
4497 <function>xmlserialize</function>:<indexterm><primary>xmlserialize</primary></indexterm>
4498 <synopsis>
4499 XMLSERIALIZE ( { DOCUMENT | CONTENT } <replaceable>value</replaceable> AS <replaceable>type</replaceable> [ [ NO ] INDENT ] )
4500 </synopsis>
4501 <replaceable>type</replaceable> can be
4502 <type>character</type>, <type>character varying</type>, or
4503 <type>text</type> (or an alias for one of those). Again, according
4504 to the SQL standard, this is the only way to convert between type
4505 <type>xml</type> and character types, but PostgreSQL also allows
4506 you to simply cast the value.
4507 </para>
4509 <para>
4510 The <literal>INDENT</literal> option causes the result to be
4511 pretty-printed, while <literal>NO INDENT</literal> (which is the
4512 default) just emits the original input string. Casting to a character
4513 type likewise produces the original string.
4514 </para>
4516 <para>
4517 When a character string value is cast to or from type
4518 <type>xml</type> without going through <type>XMLPARSE</type> or
4519 <type>XMLSERIALIZE</type>, respectively, the choice of
4520 <literal>DOCUMENT</literal> versus <literal>CONTENT</literal> is
4521 determined by the <quote>XML option</quote>
4522 <indexterm><primary>XML option</primary></indexterm>
4523 session configuration parameter, which can be set using the
4524 standard command:
4525 <synopsis>
4526 SET XML OPTION { DOCUMENT | CONTENT };
4527 </synopsis>
4528 or the more PostgreSQL-like syntax
4529 <synopsis>
4530 SET xmloption TO { DOCUMENT | CONTENT };
4531 </synopsis>
4532 The default is <literal>CONTENT</literal>, so all forms of XML
4533 data are allowed.
4534 </para>
4536 </sect2>
4538 <sect2 id="datatype-xml-encoding-handling">
4539 <title>Encoding Handling</title>
4540 <para>
4541 Care must be taken when dealing with multiple character encodings
4542 on the client, server, and in the XML data passed through them.
4543 When using the text mode to pass queries to the server and query
4544 results to the client (which is the normal mode), PostgreSQL
4545 converts all character data passed between the client and the
4546 server and vice versa to the character encoding of the respective
4547 end; see <xref linkend="multibyte"/>. This includes string
4548 representations of XML values, such as in the above examples.
4549 This would ordinarily mean that encoding declarations contained in
4550 XML data can become invalid as the character data is converted
4551 to other encodings while traveling between client and server,
4552 because the embedded encoding declaration is not changed. To cope
4553 with this behavior, encoding declarations contained in
4554 character strings presented for input to the <type>xml</type> type
4555 are <emphasis>ignored</emphasis>, and content is assumed
4556 to be in the current server encoding. Consequently, for correct
4557 processing, character strings of XML data must be sent
4558 from the client in the current client encoding. It is the
4559 responsibility of the client to either convert documents to the
4560 current client encoding before sending them to the server, or to
4561 adjust the client encoding appropriately. On output, values of
4562 type <type>xml</type> will not have an encoding declaration, and
4563 clients should assume all data is in the current client
4564 encoding.
4565 </para>
4567 <para>
4568 When using binary mode to pass query parameters to the server
4569 and query results back to the client, no encoding conversion
4570 is performed, so the situation is different. In this case, an
4571 encoding declaration in the XML data will be observed, and if it
4572 is absent, the data will be assumed to be in UTF-8 (as required by
4573 the XML standard; note that PostgreSQL does not support UTF-16).
4574 On output, data will have an encoding declaration
4575 specifying the client encoding, unless the client encoding is
4576 UTF-8, in which case it will be omitted.
4577 </para>
4579 <para>
4580 Needless to say, processing XML data with PostgreSQL will be less
4581 error-prone and more efficient if the XML data encoding, client encoding,
4582 and server encoding are the same. Since XML data is internally
4583 processed in UTF-8, computations will be most efficient if the
4584 server encoding is also UTF-8.
4585 </para>
4587 <caution>
4588 <para>
4589 Some XML-related functions may not work at all on non-ASCII data
4590 when the server encoding is not UTF-8. This is known to be an
4591 issue for <function>xmltable()</function> and <function>xpath()</function> in particular.
4592 </para>
4593 </caution>
4594 </sect2>
4596 <sect2 id="datatype-xml-accessing-xml-values">
4597 <title>Accessing XML Values</title>
4599 <para>
4600 The <type>xml</type> data type is unusual in that it does not
4601 provide any comparison operators. This is because there is no
4602 well-defined and universally useful comparison algorithm for XML
4603 data. One consequence of this is that you cannot retrieve rows by
4604 comparing an <type>xml</type> column against a search value. XML
4605 values should therefore typically be accompanied by a separate key
4606 field such as an ID. An alternative solution for comparing XML
4607 values is to convert them to character strings first, but note
4608 that character string comparison has little to do with a useful
4609 XML comparison method.
4610 </para>
4612 <para>
4613 Since there are no comparison operators for the <type>xml</type>
4614 data type, it is not possible to create an index directly on a
4615 column of this type. If speedy searches in XML data are desired,
4616 possible workarounds include casting the expression to a
4617 character string type and indexing that, or indexing an XPath
4618 expression. Of course, the actual query would have to be adjusted
4619 to search by the indexed expression.
4620 </para>
4622 <para>
4623 The text-search functionality in PostgreSQL can also be used to speed
4624 up full-document searches of XML data. The necessary
4625 preprocessing support is, however, not yet available in the PostgreSQL
4626 distribution.
4627 </para>
4628 </sect2>
4629 </sect1>
4631 &json;
4633 &array;
4635 &rowtypes;
4637 &rangetypes;
4639 <sect1 id="domains">
4640 <title>Domain Types</title>
4642 <indexterm zone="domains">
4643 <primary>domain</primary>
4644 </indexterm>
4646 <indexterm zone="domains">
4647 <primary>data type</primary>
4648 <secondary>domain</secondary>
4649 </indexterm>
4651 <para>
4652 A <firstterm>domain</firstterm> is a user-defined data type that is
4653 based on another <firstterm>underlying type</firstterm>. Optionally,
4654 it can have constraints that restrict its valid values to a subset of
4655 what the underlying type would allow. Otherwise it behaves like the
4656 underlying type &mdash; for example, any operator or function that
4657 can be applied to the underlying type will work on the domain type.
4658 The underlying type can be any built-in or user-defined base type,
4659 enum type, array type, composite type, range type, or another domain.
4660 </para>
4662 <para>
4663 For example, we could create a domain over integers that accepts only
4664 positive integers:
4665 <programlisting>
4666 CREATE DOMAIN posint AS integer CHECK (VALUE &gt; 0);
4667 CREATE TABLE mytable (id posint);
4668 INSERT INTO mytable VALUES(1); -- works
4669 INSERT INTO mytable VALUES(-1); -- fails
4670 </programlisting>
4671 </para>
4673 <para>
4674 When an operator or function of the underlying type is applied to a
4675 domain value, the domain is automatically down-cast to the underlying
4676 type. Thus, for example, the result of <literal>mytable.id - 1</literal>
4677 is considered to be of type <type>integer</type> not <type>posint</type>.
4678 We could write <literal>(mytable.id - 1)::posint</literal> to cast the
4679 result back to <type>posint</type>, causing the domain's constraints
4680 to be rechecked. In this case, that would result in an error if the
4681 expression had been applied to an <structfield>id</structfield> value of
4682 1. Assigning a value of the underlying type to a field or variable of
4683 the domain type is allowed without writing an explicit cast, but the
4684 domain's constraints will be checked.
4685 </para>
4687 <para>
4688 For additional information see <xref linkend="sql-createdomain"/>.
4689 </para>
4690 </sect1>
4692 <sect1 id="datatype-oid">
4693 <title>Object Identifier Types</title>
4695 <indexterm zone="datatype-oid">
4696 <primary>object identifier</primary>
4697 <secondary>data type</secondary>
4698 </indexterm>
4700 <indexterm zone="datatype-oid">
4701 <primary>oid</primary>
4702 </indexterm>
4704 <indexterm zone="datatype-oid">
4705 <primary>regclass</primary>
4706 </indexterm>
4708 <indexterm zone="datatype-oid">
4709 <primary>regcollation</primary>
4710 </indexterm>
4712 <indexterm zone="datatype-oid">
4713 <primary>regconfig</primary>
4714 </indexterm>
4716 <indexterm zone="datatype-oid">
4717 <primary>regdictionary</primary>
4718 </indexterm>
4720 <indexterm zone="datatype-oid">
4721 <primary>regnamespace</primary>
4722 </indexterm>
4724 <indexterm zone="datatype-oid">
4725 <primary>regoper</primary>
4726 </indexterm>
4728 <indexterm zone="datatype-oid">
4729 <primary>regoperator</primary>
4730 </indexterm>
4732 <indexterm zone="datatype-oid">
4733 <primary>regproc</primary>
4734 </indexterm>
4736 <indexterm zone="datatype-oid">
4737 <primary>regprocedure</primary>
4738 </indexterm>
4740 <indexterm zone="datatype-oid">
4741 <primary>regrole</primary>
4742 </indexterm>
4744 <indexterm zone="datatype-oid">
4745 <primary>regtype</primary>
4746 </indexterm>
4748 <indexterm zone="datatype-oid">
4749 <primary>xid8</primary>
4750 </indexterm>
4752 <indexterm zone="datatype-oid">
4753 <primary>cid</primary>
4754 </indexterm>
4756 <indexterm zone="datatype-oid">
4757 <primary>tid</primary>
4758 </indexterm>
4760 <indexterm zone="datatype-oid">
4761 <primary>xid</primary>
4762 </indexterm>
4764 <para>
4765 Object identifiers (OIDs) are used internally by
4766 <productname>PostgreSQL</productname> as primary keys for various
4767 system tables.
4768 Type <type>oid</type> represents an object identifier. There are also
4769 several alias types for <type>oid</type>, each
4770 named <type>reg<replaceable>something</replaceable></type>.
4771 <xref linkend="datatype-oid-table"/> shows an
4772 overview.
4773 </para>
4775 <para>
4776 The <type>oid</type> type is currently implemented as an unsigned
4777 four-byte integer. Therefore, it is not large enough to provide
4778 database-wide uniqueness in large databases, or even in large
4779 individual tables.
4780 </para>
4782 <para>
4783 The <type>oid</type> type itself has few operations beyond comparison.
4784 It can be cast to integer, however, and then manipulated using the
4785 standard integer operators. (Beware of possible
4786 signed-versus-unsigned confusion if you do this.)
4787 </para>
4789 <para>
4790 The OID alias types have no operations of their own except
4791 for specialized input and output routines. These routines are able
4792 to accept and display symbolic names for system objects, rather than
4793 the raw numeric value that type <type>oid</type> would use. The alias
4794 types allow simplified lookup of OID values for objects. For example,
4795 to examine the <structname>pg_attribute</structname> rows related to a table
4796 <literal>mytable</literal>, one could write:
4797 <programlisting>
4798 SELECT * FROM pg_attribute WHERE attrelid = 'mytable'::regclass;
4799 </programlisting>
4800 rather than:
4801 <programlisting>
4802 SELECT * FROM pg_attribute
4803 WHERE attrelid = (SELECT oid FROM pg_class WHERE relname = 'mytable');
4804 </programlisting>
4805 While that doesn't look all that bad by itself, it's still oversimplified.
4806 A far more complicated sub-select would be needed to
4807 select the right OID if there are multiple tables named
4808 <literal>mytable</literal> in different schemas.
4809 The <type>regclass</type> input converter handles the table lookup according
4810 to the schema path setting, and so it does the <quote>right thing</quote>
4811 automatically. Similarly, casting a table's OID to
4812 <type>regclass</type> is handy for symbolic display of a numeric OID.
4813 </para>
4815 <table id="datatype-oid-table">
4816 <title>Object Identifier Types</title>
4817 <tgroup cols="4">
4818 <thead>
4819 <row>
4820 <entry>Name</entry>
4821 <entry>References</entry>
4822 <entry>Description</entry>
4823 <entry>Value Example</entry>
4824 </row>
4825 </thead>
4827 <tbody>
4829 <row>
4830 <entry><type>oid</type></entry>
4831 <entry>any</entry>
4832 <entry>numeric object identifier</entry>
4833 <entry><literal>564182</literal></entry>
4834 </row>
4836 <row>
4837 <entry><type>regclass</type></entry>
4838 <entry><structname>pg_class</structname></entry>
4839 <entry>relation name</entry>
4840 <entry><literal>pg_type</literal></entry>
4841 </row>
4843 <row>
4844 <entry><type>regcollation</type></entry>
4845 <entry><structname>pg_collation</structname></entry>
4846 <entry>collation name</entry>
4847 <entry><literal>"POSIX"</literal></entry>
4848 </row>
4850 <row>
4851 <entry><type>regconfig</type></entry>
4852 <entry><structname>pg_ts_config</structname></entry>
4853 <entry>text search configuration</entry>
4854 <entry><literal>english</literal></entry>
4855 </row>
4857 <row>
4858 <entry><type>regdictionary</type></entry>
4859 <entry><structname>pg_ts_dict</structname></entry>
4860 <entry>text search dictionary</entry>
4861 <entry><literal>simple</literal></entry>
4862 </row>
4864 <row>
4865 <entry><type>regnamespace</type></entry>
4866 <entry><structname>pg_namespace</structname></entry>
4867 <entry>namespace name</entry>
4868 <entry><literal>pg_catalog</literal></entry>
4869 </row>
4871 <row>
4872 <entry><type>regoper</type></entry>
4873 <entry><structname>pg_operator</structname></entry>
4874 <entry>operator name</entry>
4875 <entry><literal>+</literal></entry>
4876 </row>
4878 <row>
4879 <entry><type>regoperator</type></entry>
4880 <entry><structname>pg_operator</structname></entry>
4881 <entry>operator with argument types</entry>
4882 <entry><literal>*(integer,&zwsp;integer)</literal>
4883 or <literal>-(NONE,&zwsp;integer)</literal></entry>
4884 </row>
4886 <row>
4887 <entry><type>regproc</type></entry>
4888 <entry><structname>pg_proc</structname></entry>
4889 <entry>function name</entry>
4890 <entry><literal>sum</literal></entry>
4891 </row>
4893 <row>
4894 <entry><type>regprocedure</type></entry>
4895 <entry><structname>pg_proc</structname></entry>
4896 <entry>function with argument types</entry>
4897 <entry><literal>sum(int4)</literal></entry>
4898 </row>
4900 <row>
4901 <entry><type>regrole</type></entry>
4902 <entry><structname>pg_authid</structname></entry>
4903 <entry>role name</entry>
4904 <entry><literal>smithee</literal></entry>
4905 </row>
4907 <row>
4908 <entry><type>regtype</type></entry>
4909 <entry><structname>pg_type</structname></entry>
4910 <entry>data type name</entry>
4911 <entry><literal>integer</literal></entry>
4912 </row>
4913 </tbody>
4914 </tgroup>
4915 </table>
4917 <para>
4918 All of the OID alias types for objects that are grouped by namespace
4919 accept schema-qualified names, and will
4920 display schema-qualified names on output if the object would not
4921 be found in the current search path without being qualified.
4922 For example, <literal>myschema.mytable</literal> is acceptable input
4923 for <type>regclass</type> (if there is such a table). That value
4924 might be output as <literal>myschema.mytable</literal>, or
4925 just <literal>mytable</literal>, depending on the current search path.
4926 The <type>regproc</type> and <type>regoper</type> alias types will only
4927 accept input names that are unique (not overloaded), so they are
4928 of limited use; for most uses <type>regprocedure</type> or
4929 <type>regoperator</type> are more appropriate. For <type>regoperator</type>,
4930 unary operators are identified by writing <literal>NONE</literal> for the unused
4931 operand.
4932 </para>
4934 <para>
4935 The input functions for these types allow whitespace between tokens,
4936 and will fold upper-case letters to lower case, except within double
4937 quotes; this is done to make the syntax rules similar to the way
4938 object names are written in SQL. Conversely, the output functions
4939 will use double quotes if needed to make the output be a valid SQL
4940 identifier. For example, the OID of a function
4941 named <literal>Foo</literal> (with upper case <literal>F</literal>)
4942 taking two integer arguments could be entered as
4943 <literal>' "Foo" ( int, integer ) '::regprocedure</literal>. The
4944 output would look like <literal>"Foo"(integer,integer)</literal>.
4945 Both the function name and the argument type names could be
4946 schema-qualified, too.
4947 </para>
4949 <para>
4950 Many built-in <productname>PostgreSQL</productname> functions accept
4951 the OID of a table, or another kind of database object, and for
4952 convenience are declared as taking <type>regclass</type> (or the
4953 appropriate OID alias type). This means you do not have to look up
4954 the object's OID by hand, but can just enter its name as a string
4955 literal. For example, the <function>nextval(regclass)</function> function
4956 takes a sequence relation's OID, so you could call it like this:
4957 <programlisting>
4958 nextval('foo') <lineannotation>operates on sequence <literal>foo</literal></lineannotation>
4959 nextval('FOO') <lineannotation>same as above</lineannotation>
4960 nextval('"Foo"') <lineannotation>operates on sequence <literal>Foo</literal></lineannotation>
4961 nextval('myschema.foo') <lineannotation>operates on <literal>myschema.foo</literal></lineannotation>
4962 nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
4963 nextval('foo') <lineannotation>searches search path for <literal>foo</literal></lineannotation>
4964 </programlisting>
4965 </para>
4967 <note>
4968 <para>
4969 When you write the argument of such a function as an unadorned
4970 literal string, it becomes a constant of type <type>regclass</type>
4971 (or the appropriate type).
4972 Since this is really just an OID, it will track the originally
4973 identified object despite later renaming, schema reassignment,
4974 etc. This <quote>early binding</quote> behavior is usually desirable for
4975 object references in column defaults and views. But sometimes you might
4976 want <quote>late binding</quote> where the object reference is resolved
4977 at run time. To get late-binding behavior, force the constant to be
4978 stored as a <type>text</type> constant instead of <type>regclass</type>:
4979 <programlisting>
4980 nextval('foo'::text) <lineannotation><literal>foo</literal> is looked up at runtime</lineannotation>
4981 </programlisting>
4982 The <function>to_regclass()</function> function and its siblings
4983 can also be used to perform run-time lookups. See
4984 <xref linkend="functions-info-catalog-table"/>.
4985 </para>
4986 </note>
4988 <para>
4989 Another practical example of use of <type>regclass</type>
4990 is to look up the OID of a table listed in
4991 the <literal>information_schema</literal> views, which don't supply
4992 such OIDs directly. One might for example wish to call
4993 the <function>pg_relation_size()</function> function, which requires
4994 the table OID. Taking the above rules into account, the correct way
4995 to do that is
4996 <programlisting>
4997 SELECT table_schema, table_name,
4998 pg_relation_size((quote_ident(table_schema) || '.' ||
4999 quote_ident(table_name))::regclass)
5000 FROM information_schema.tables
5001 WHERE ...
5002 </programlisting>
5003 The <function>quote_ident()</function> function will take care of
5004 double-quoting the identifiers where needed. The seemingly easier
5005 <programlisting>
5006 SELECT pg_relation_size(table_name)
5007 FROM information_schema.tables
5008 WHERE ...
5009 </programlisting>
5010 is <emphasis>not recommended</emphasis>, because it will fail for
5011 tables that are outside your search path or have names that require
5012 quoting.
5013 </para>
5015 <para>
5016 An additional property of most of the OID alias types is the creation of
5017 dependencies. If a
5018 constant of one of these types appears in a stored expression
5019 (such as a column default expression or view), it creates a dependency
5020 on the referenced object. For example, if a column has a default
5021 expression <literal>nextval('my_seq'::regclass)</literal>,
5022 <productname>PostgreSQL</productname>
5023 understands that the default expression depends on the sequence
5024 <literal>my_seq</literal>, so the system will not let the sequence
5025 be dropped without first removing the default expression. The
5026 alternative of <literal>nextval('my_seq'::text)</literal> does not
5027 create a dependency.
5028 (<type>regrole</type> is an exception to this property. Constants of this
5029 type are not allowed in stored expressions.)
5030 </para>
5032 <para>
5033 Another identifier type used by the system is <type>xid</type>, or transaction
5034 (abbreviated <abbrev>xact</abbrev>) identifier. This is the data type of the system columns
5035 <structfield>xmin</structfield> and <structfield>xmax</structfield>. Transaction identifiers are 32-bit quantities.
5036 In some contexts, a 64-bit variant <type>xid8</type> is used. Unlike
5037 <type>xid</type> values, <type>xid8</type> values increase strictly
5038 monotonically and cannot be reused in the lifetime of a database
5039 cluster. See <xref linkend="transaction-id"/> for more details.
5040 </para>
5042 <para>
5043 A third identifier type used by the system is <type>cid</type>, or
5044 command identifier. This is the data type of the system columns
5045 <structfield>cmin</structfield> and <structfield>cmax</structfield>. Command identifiers are also 32-bit quantities.
5046 </para>
5048 <para>
5049 A final identifier type used by the system is <type>tid</type>, or tuple
5050 identifier (row identifier). This is the data type of the system column
5051 <structfield>ctid</structfield>. A tuple ID is a pair
5052 (block number, tuple index within block) that identifies the
5053 physical location of the row within its table.
5054 </para>
5056 <para>
5057 (The system columns are further explained in <xref
5058 linkend="ddl-system-columns"/>.)
5059 </para>
5060 </sect1>
5062 <sect1 id="datatype-pg-lsn">
5063 <title><type>pg_lsn</type> Type</title>
5065 <indexterm zone="datatype-pg-lsn">
5066 <primary>pg_lsn</primary>
5067 </indexterm>
5069 <para>
5070 The <type>pg_lsn</type> data type can be used to store LSN (Log Sequence
5071 Number) data which is a pointer to a location in the WAL. This type is a
5072 representation of <type>XLogRecPtr</type> and an internal system type of
5073 <productname>PostgreSQL</productname>.
5074 </para>
5076 <para>
5077 Internally, an LSN is a 64-bit integer, representing a byte position in
5078 the write-ahead log stream. It is printed as two hexadecimal numbers of
5079 up to 8 digits each, separated by a slash; for example,
5080 <literal>16/B374D848</literal>. The <type>pg_lsn</type> type supports the
5081 standard comparison operators, like <literal>=</literal> and
5082 <literal>&gt;</literal>. Two LSNs can be subtracted using the
5083 <literal>-</literal> operator; the result is the number of bytes separating
5084 those write-ahead log locations. Also the number of bytes can be
5085 added into and subtracted from LSN using the
5086 <literal>+(pg_lsn,numeric)</literal> and
5087 <literal>-(pg_lsn,numeric)</literal> operators, respectively. Note that
5088 the calculated LSN should be in the range of <type>pg_lsn</type> type,
5089 i.e., between <literal>0/0</literal> and
5090 <literal>FFFFFFFF/FFFFFFFF</literal>.
5091 </para>
5092 </sect1>
5094 <sect1 id="datatype-pseudo">
5095 <title>Pseudo-Types</title>
5097 <indexterm zone="datatype-pseudo">
5098 <primary>record</primary>
5099 </indexterm>
5101 <indexterm zone="datatype-pseudo">
5102 <primary>any</primary>
5103 </indexterm>
5105 <indexterm zone="datatype-pseudo">
5106 <primary>anyelement</primary>
5107 </indexterm>
5109 <indexterm zone="datatype-pseudo">
5110 <primary>anyarray</primary>
5111 </indexterm>
5113 <indexterm zone="datatype-pseudo">
5114 <primary>anynonarray</primary>
5115 </indexterm>
5117 <indexterm zone="datatype-pseudo">
5118 <primary>anyenum</primary>
5119 </indexterm>
5121 <indexterm zone="datatype-pseudo">
5122 <primary>anyrange</primary>
5123 </indexterm>
5125 <indexterm zone="datatype-pseudo">
5126 <primary>anymultirange</primary>
5127 </indexterm>
5129 <indexterm zone="datatype-pseudo">
5130 <primary>anycompatible</primary>
5131 </indexterm>
5133 <indexterm zone="datatype-pseudo">
5134 <primary>anycompatiblearray</primary>
5135 </indexterm>
5137 <indexterm zone="datatype-pseudo">
5138 <primary>anycompatiblenonarray</primary>
5139 </indexterm>
5141 <indexterm zone="datatype-pseudo">
5142 <primary>anycompatiblerange</primary>
5143 </indexterm>
5145 <indexterm zone="datatype-pseudo">
5146 <primary>anycompatiblemultirange</primary>
5147 </indexterm>
5149 <indexterm zone="datatype-pseudo">
5150 <primary>void</primary>
5151 </indexterm>
5153 <indexterm zone="datatype-pseudo">
5154 <primary>trigger</primary>
5155 </indexterm>
5157 <indexterm zone="datatype-pseudo">
5158 <primary>event_trigger</primary>
5159 </indexterm>
5161 <indexterm zone="datatype-pseudo">
5162 <primary>pg_ddl_command</primary>
5163 </indexterm>
5165 <indexterm zone="datatype-pseudo">
5166 <primary>language_handler</primary>
5167 </indexterm>
5169 <indexterm zone="datatype-pseudo">
5170 <primary>fdw_handler</primary>
5171 </indexterm>
5173 <indexterm zone="datatype-pseudo">
5174 <primary>table_am_handler</primary>
5175 </indexterm>
5177 <indexterm zone="datatype-pseudo">
5178 <primary>index_am_handler</primary>
5179 </indexterm>
5181 <indexterm zone="datatype-pseudo">
5182 <primary>tsm_handler</primary>
5183 </indexterm>
5185 <indexterm zone="datatype-pseudo">
5186 <primary>cstring</primary>
5187 </indexterm>
5189 <indexterm zone="datatype-pseudo">
5190 <primary>internal</primary>
5191 </indexterm>
5193 <indexterm zone="datatype-pseudo">
5194 <primary>unknown</primary>
5195 </indexterm>
5197 <para>
5198 The <productname>PostgreSQL</productname> type system contains a
5199 number of special-purpose entries that are collectively called
5200 <firstterm>pseudo-types</firstterm>. A pseudo-type cannot be used as a
5201 column data type, but it can be used to declare a function's
5202 argument or result type. Each of the available pseudo-types is
5203 useful in situations where a function's behavior does not
5204 correspond to simply taking or returning a value of a specific
5205 <acronym>SQL</acronym> data type. <xref
5206 linkend="datatype-pseudotypes-table"/> lists the existing
5207 pseudo-types.
5208 </para>
5210 <table id="datatype-pseudotypes-table">
5211 <title>Pseudo-Types</title>
5212 <tgroup cols="2">
5213 <colspec colname="col1" colwidth="2*"/>
5214 <colspec colname="col2" colwidth="3*"/>
5215 <thead>
5216 <row>
5217 <entry>Name</entry>
5218 <entry>Description</entry>
5219 </row>
5220 </thead>
5222 <tbody>
5223 <row>
5224 <entry><type>any</type></entry>
5225 <entry>Indicates that a function accepts any input data type.</entry>
5226 </row>
5228 <row>
5229 <entry><type>anyelement</type></entry>
5230 <entry>Indicates that a function accepts any data type
5231 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5232 </row>
5234 <row>
5235 <entry><type>anyarray</type></entry>
5236 <entry>Indicates that a function accepts any array data type
5237 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5238 </row>
5240 <row>
5241 <entry><type>anynonarray</type></entry>
5242 <entry>Indicates that a function accepts any non-array data type
5243 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5244 </row>
5246 <row>
5247 <entry><type>anyenum</type></entry>
5248 <entry>Indicates that a function accepts any enum data type
5249 (see <xref linkend="extend-types-polymorphic"/> and
5250 <xref linkend="datatype-enum"/>).</entry>
5251 </row>
5253 <row>
5254 <entry><type>anyrange</type></entry>
5255 <entry>Indicates that a function accepts any range data type
5256 (see <xref linkend="extend-types-polymorphic"/> and
5257 <xref linkend="rangetypes"/>).</entry>
5258 </row>
5260 <row>
5261 <entry><type>anymultirange</type></entry>
5262 <entry>Indicates that a function accepts any multirange data type
5263 (see <xref linkend="extend-types-polymorphic"/> and
5264 <xref linkend="rangetypes"/>).</entry>
5265 </row>
5267 <row>
5268 <entry><type>anycompatible</type></entry>
5269 <entry>Indicates that a function accepts any data type,
5270 with automatic promotion of multiple arguments to a common data type
5271 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5272 </row>
5274 <row>
5275 <entry><type>anycompatiblearray</type></entry>
5276 <entry>Indicates that a function accepts any array data type,
5277 with automatic promotion of multiple arguments to a common data type
5278 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5279 </row>
5281 <row>
5282 <entry><type>anycompatiblenonarray</type></entry>
5283 <entry>Indicates that a function accepts any non-array data type,
5284 with automatic promotion of multiple arguments to a common data type
5285 (see <xref linkend="extend-types-polymorphic"/>).</entry>
5286 </row>
5288 <row>
5289 <entry><type>anycompatiblerange</type></entry>
5290 <entry>Indicates that a function accepts any range data type,
5291 with automatic promotion of multiple arguments to a common data type
5292 (see <xref linkend="extend-types-polymorphic"/> and
5293 <xref linkend="rangetypes"/>).</entry>
5294 </row>
5296 <row>
5297 <entry><type>anycompatiblemultirange</type></entry>
5298 <entry>Indicates that a function accepts any multirange data type,
5299 with automatic promotion of multiple arguments to a common data type
5300 (see <xref linkend="extend-types-polymorphic"/> and
5301 <xref linkend="rangetypes"/>).</entry>
5302 </row>
5304 <row>
5305 <entry><type>cstring</type></entry>
5306 <entry>Indicates that a function accepts or returns a null-terminated C string.</entry>
5307 </row>
5309 <row>
5310 <entry><type>internal</type></entry>
5311 <entry>Indicates that a function accepts or returns a server-internal
5312 data type.</entry>
5313 </row>
5315 <row>
5316 <entry><type>language_handler</type></entry>
5317 <entry>A procedural language call handler is declared to return <type>language_handler</type>.</entry>
5318 </row>
5320 <row>
5321 <entry><type>fdw_handler</type></entry>
5322 <entry>A foreign-data wrapper handler is declared to return <type>fdw_handler</type>.</entry>
5323 </row>
5325 <row>
5326 <entry><type>table_am_handler</type></entry>
5327 <entry>A table access method handler is declared to return <type>table_am_handler</type>.</entry>
5328 </row>
5330 <row>
5331 <entry><type>index_am_handler</type></entry>
5332 <entry>An index access method handler is declared to return <type>index_am_handler</type>.</entry>
5333 </row>
5335 <row>
5336 <entry><type>tsm_handler</type></entry>
5337 <entry>A tablesample method handler is declared to return <type>tsm_handler</type>.</entry>
5338 </row>
5340 <row>
5341 <entry><type>record</type></entry>
5342 <entry>Identifies a function taking or returning an unspecified row type.</entry>
5343 </row>
5345 <row>
5346 <entry><type>trigger</type></entry>
5347 <entry>A trigger function is declared to return <type>trigger.</type></entry>
5348 </row>
5350 <row>
5351 <entry><type>event_trigger</type></entry>
5352 <entry>An event trigger function is declared to return <type>event_trigger.</type></entry>
5353 </row>
5355 <row>
5356 <entry><type>pg_ddl_command</type></entry>
5357 <entry>Identifies a representation of DDL commands that is available to event triggers.</entry>
5358 </row>
5360 <row>
5361 <entry><type>void</type></entry>
5362 <entry>Indicates that a function returns no value.</entry>
5363 </row>
5365 <row>
5366 <entry><type>unknown</type></entry>
5367 <entry>Identifies a not-yet-resolved type, e.g., of an undecorated
5368 string literal.</entry>
5369 </row>
5370 </tbody>
5371 </tgroup>
5372 </table>
5374 <para>
5375 Functions coded in C (whether built-in or dynamically loaded) can be
5376 declared to accept or return any of these pseudo-types. It is up to
5377 the function author to ensure that the function will behave safely
5378 when a pseudo-type is used as an argument type.
5379 </para>
5381 <para>
5382 Functions coded in procedural languages can use pseudo-types only as
5383 allowed by their implementation languages. At present most procedural
5384 languages forbid use of a pseudo-type as an argument type, and allow
5385 only <type>void</type> and <type>record</type> as a result type (plus
5386 <type>trigger</type> or <type>event_trigger</type> when the function is used
5387 as a trigger or event trigger). Some also support polymorphic functions
5388 using the polymorphic pseudo-types, which are shown above and discussed
5389 in detail in <xref linkend="extend-types-polymorphic"/>.
5390 </para>
5392 <para>
5393 The <type>internal</type> pseudo-type is used to declare functions
5394 that are meant only to be called internally by the database
5395 system, and not by direct invocation in an <acronym>SQL</acronym>
5396 query. If a function has at least one <type>internal</type>-type
5397 argument then it cannot be called from <acronym>SQL</acronym>. To
5398 preserve the type safety of this restriction it is important to
5399 follow this coding rule: do not create any function that is
5400 declared to return <type>internal</type> unless it has at least one
5401 <type>internal</type> argument.
5402 </para>
5404 </sect1>
5406 </chapter>