func.sgml 312.2 KB
Newer Older
1
<!--
2
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.244 2005/04/01 14:25:22 momjian Exp $
3
PostgreSQL documentation
4
-->
5

6 7 8 9
 <chapter id="functions">
  <title>Functions and Operators</title>

  <indexterm zone="functions">
P
Peter Eisentraut 已提交
10
   <primary>function</primary>
11 12
  </indexterm>

13
  <indexterm zone="functions">
P
Peter Eisentraut 已提交
14
   <primary>operator</primary>
15 16
  </indexterm>

17
  <para>
18 19
   <productname>PostgreSQL</productname> provides a large number of
   functions and operators for the built-in data types.  Users can also
20 21
   define their own functions and operators, as described in
   <xref linkend="server-programming">.  The
22 23 24 25
   <application>psql</application> commands <command>\df</command> and
   <command>\do</command> can be used to show the list of all actually
   available functions and operators, respectively.
  </para>
26

27 28 29 30 31
  <para>
   If you are concerned about portability then take note that most of
   the functions and operators described in this chapter, with the
   exception of the most trivial arithmetic and comparison operators
   and some explicitly marked functions, are not specified by the
P
Peter Eisentraut 已提交
32 33 34 35
   <acronym>SQL</acronym> standard. Some of the extended functionality
   is present in other <acronym>SQL</acronym> database management
   systems, and in many cases this functionality is compatible and
   consistent between the various implementations.
36
  </para>
37

38 39 40 41 42

  <sect1 id="functions-logical">
   <title>Logical Operators</title>

   <indexterm zone="functions-logical">
P
Peter Eisentraut 已提交
43
    <primary>operator</primary>
44
    <secondary>logical</secondary>
45 46 47
   </indexterm>

   <indexterm>
48 49 50
    <primary>Boolean</primary>
    <secondary>operators</secondary>
    <see>operators, logical</see>
51 52
   </indexterm>

53 54
   <para>
    The usual logical operators are available:
55

56
    <indexterm>
P
Peter Eisentraut 已提交
57
     <primary>AND (operator)</primary>
58
    </indexterm>
59

60
    <indexterm>
P
Peter Eisentraut 已提交
61
     <primary>OR (operator)</primary>
62
    </indexterm>
63

64
    <indexterm>
P
Peter Eisentraut 已提交
65 66 67 68 69 70 71 72 73 74 75 76 77
     <primary>NOT (operator)</primary>
    </indexterm>

    <indexterm>
     <primary>conjunction</primary>
    </indexterm>

    <indexterm>
     <primary>disjunction</primary>
    </indexterm>

    <indexterm>
     <primary>negation</primary>
78
    </indexterm>
79

80
    <simplelist>
81 82 83
     <member><literal>AND</></member>
     <member><literal>OR</></member>
     <member><literal>NOT</></member>
84
    </simplelist>
85

P
Peter Eisentraut 已提交
86
    <acronym>SQL</acronym> uses a three-valued Boolean logic where the null value represents
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159
    <quote>unknown</quote>.  Observe the following truth tables:

    <informaltable>
     <tgroup cols="4">
      <thead>
       <row>
        <entry><replaceable>a</replaceable></entry>
        <entry><replaceable>b</replaceable></entry>
        <entry><replaceable>a</replaceable> AND <replaceable>b</replaceable></entry>
        <entry><replaceable>a</replaceable> OR <replaceable>b</replaceable></entry>
       </row>
      </thead>

      <tbody>
       <row>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>TRUE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>TRUE</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
        <entry>FALSE</entry>
       </row>

       <row>
        <entry>FALSE</entry>
        <entry>NULL</entry>
        <entry>FALSE</entry>
        <entry>NULL</entry>
       </row>

       <row>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
        <entry>NULL</entry>
       </row>
      </tbody>
     </tgroup>
    </informaltable>

    <informaltable>
     <tgroup cols="2">
      <thead>
       <row>
        <entry><replaceable>a</replaceable></entry>
        <entry>NOT <replaceable>a</replaceable></entry>
       </row>
      </thead>

      <tbody>
       <row>
        <entry>TRUE</entry>
        <entry>FALSE</entry>
       </row>
160

161 162 163 164 165 166 167 168 169 170 171 172 173
       <row>
        <entry>FALSE</entry>
        <entry>TRUE</entry>
       </row>

       <row>
        <entry>NULL</entry>
        <entry>NULL</entry>
       </row>
      </tbody>
     </tgroup>
    </informaltable>
   </para>
174 175 176 177 178 179 180 181

   <para>
    The operators <literal>AND</literal> and <literal>OR</literal> are
    commutative, that is, you can switch the left and right operand
    without affecting the result.  But see <xref
    linkend="syntax-express-eval"> for more information about the
    order of evaluation of subexpressions.
   </para>
182 183 184 185 186 187 188 189 190 191
  </sect1>

  <sect1 id="functions-comparison">
   <title>Comparison Operators</title>

   <indexterm zone="functions-comparison">
    <primary>comparison</primary>
    <secondary>operators</secondary>
   </indexterm>

192 193 194 195 196 197
   <para>
    The usual comparison operators are available, shown in <xref
    linkend="functions-comparison-table">.
   </para>

   <table id="functions-comparison-table">
198 199 200
    <title>Comparison Operators</title>
    <tgroup cols="2">
     <thead>
201
      <row>
202 203
       <entry>Operator</entry>
       <entry>Description</entry>
204
      </row>
205
     </thead>
206

207
     <tbody>
208
      <row>
209 210
       <entry> <literal>&lt;</literal> </entry>
       <entry>less than</entry>
211
      </row>
212 213

      <row>
214 215
       <entry> <literal>&gt;</literal> </entry>
       <entry>greater than</entry>
216
      </row>
217 218

      <row>
219 220
       <entry> <literal>&lt;=</literal> </entry>
       <entry>less than or equal to</entry>
221 222 223
      </row>

      <row>
224 225
       <entry> <literal>&gt;=</literal> </entry>
       <entry>greater than or equal to</entry>
226 227 228
      </row>

      <row>
229 230
       <entry> <literal>=</literal> </entry>
       <entry>equal</entry>
231 232 233
      </row>

      <row>
234 235
       <entry> <literal>&lt;&gt;</literal> or <literal>!=</literal> </entry>
       <entry>not equal</entry>
236 237 238
      </row>
     </tbody>
    </tgroup>
239
   </table>
240

241 242 243 244 245 246 247 248
   <note>
    <para>
     The <literal>!=</literal> operator is converted to
     <literal>&lt;&gt;</literal> in the parser stage.  It is not
     possible to implement <literal>!=</literal> and
     <literal>&lt;&gt;</literal> operators that do different things.
    </para>
   </note>
249

T
Thomas G. Lockhart 已提交
250
   <para>
251 252 253 254 255 256
    Comparison operators are available for all data types where this
    makes sense.  All comparison operators are binary operators that
    return values of type <type>boolean</type>; expressions like
    <literal>1 &lt; 2 &lt; 3</literal> are not valid (because there is
    no <literal>&lt;</literal> operator to compare a Boolean value with
    <literal>3</literal>).
T
Thomas G. Lockhart 已提交
257
   </para>
258

259 260
   <para>
    <indexterm>
261
     <primary>BETWEEN</primary>
262 263
    </indexterm>
    In addition to the comparison operators, the special
P
Peter Eisentraut 已提交
264
    <token>BETWEEN</token> construct is available.<indexterm><primary>BETWEEN</primary></indexterm>
265
<synopsis>
P
Peter Eisentraut 已提交
266
<replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
267
</synopsis>
268
    is equivalent to
269
<synopsis>
B
Bruce Momjian 已提交
270
<replaceable>a</replaceable> &gt;= <replaceable>x</replaceable> AND <replaceable>a</replaceable> &lt;= <replaceable>y</replaceable>
271
</synopsis>
272
    Similarly,
273
<synopsis>
P
Peter Eisentraut 已提交
274
<replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
275
</synopsis>
276
    is equivalent to
277
<synopsis>
B
Bruce Momjian 已提交
278
<replaceable>a</replaceable> &lt; <replaceable>x</replaceable> OR <replaceable>a</replaceable> &gt; <replaceable>y</replaceable>
279
</synopsis>
280 281 282 283
    There is no difference between the two respective forms apart from
    the <acronym>CPU</acronym> cycles required to rewrite the first one
    into the second one internally.
   </para>
P
Peter Eisentraut 已提交
284

285
   <para>
286 287 288 289 290 291 292 293 294 295 296 297
    <indexterm>
     <primary>IS NULL</primary>
    </indexterm>
    <indexterm>
     <primary>IS NOT NULL</primary>
    </indexterm>
    <indexterm>
     <primary>ISNULL</primary>
    </indexterm>
    <indexterm>
     <primary>NOTNULL</primary>
    </indexterm>
P
Peter Eisentraut 已提交
298
    To check whether a value is or is not null, use the constructs
299
<synopsis>
300 301
<replaceable>expression</replaceable> IS NULL
<replaceable>expression</replaceable> IS NOT NULL
302 303 304
</synopsis>
    or the equivalent, but nonstandard, constructs
<synopsis>
305 306
<replaceable>expression</replaceable> ISNULL
<replaceable>expression</replaceable> NOTNULL
307
</synopsis>
P
Peter Eisentraut 已提交
308
    <indexterm><primary>null value</primary><secondary>comparing</secondary></indexterm>
309
   </para>
310

311 312 313
   <para>
    Do <emphasis>not</emphasis> write
    <literal><replaceable>expression</replaceable> = NULL</literal>
P
Peter Eisentraut 已提交
314 315
    because <literal>NULL</> is not <quote>equal to</quote>
    <literal>NULL</>.  (The null value represents an unknown value,
316 317
    and it is not known whether two unknown values are equal.) This
    behavior conforms to the SQL standard.
318
   </para>
319

320
  <tip>
321
   <para>
322
    Some applications may expect that
323 324
    <literal><replaceable>expression</replaceable> = NULL</literal>
    returns true if <replaceable>expression</replaceable> evaluates to
325 326
    the null value.  It is highly recommended that these applications
    be modified to comply with the SQL standard. However, if that
327
    cannot be done the <xref linkend="guc-transform-null-equals">
328 329 330 331 332
    configuration variable is available. If it is enabled,
    <productname>PostgreSQL</productname> will convert <literal>x =
    NULL</literal> clauses to <literal>x IS NULL</literal>.  This was
    the default behavior in <productname>PostgreSQL</productname>
    releases 6.5 through 7.1.
333
   </para>
334
  </tip>
335

336
   <para>
337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370
    <indexterm>
     <primary>IS DISTINCT FROM</primary>
    </indexterm>
    The ordinary comparison operators yield null (signifying <quote>unknown</>)
    when either input is null.  Another way to do comparisons is with the
    <literal>IS DISTINCT FROM</literal> construct:
<synopsis>
<replaceable>expression</replaceable> IS DISTINCT FROM <replaceable>expression</replaceable>
</synopsis>
    For non-null inputs this is the same as the <literal>&lt;&gt;</> operator.
    However, when both inputs are null it will return false, and when just
    one input is null it will return true.  Thus it effectively acts as though
    null were a normal data value, rather than <quote>unknown</>.
   </para>

   <para>
    <indexterm>
     <primary>IS TRUE</primary>
    </indexterm>
    <indexterm>
     <primary>IS NOT TRUE</primary>
    </indexterm>
    <indexterm>
     <primary>IS FALSE</primary>
    </indexterm>
    <indexterm>
     <primary>IS NOT FALSE</primary>
    </indexterm>
    <indexterm>
     <primary>IS UNKNOWN</primary>
    </indexterm>
    <indexterm>
     <primary>IS NOT UNKNOWN</primary>
    </indexterm>
371
    Boolean values can also be tested using the constructs
372
<synopsis>
373 374 375 376 377 378
<replaceable>expression</replaceable> IS TRUE
<replaceable>expression</replaceable> IS NOT TRUE
<replaceable>expression</replaceable> IS FALSE
<replaceable>expression</replaceable> IS NOT FALSE
<replaceable>expression</replaceable> IS UNKNOWN
<replaceable>expression</replaceable> IS NOT UNKNOWN
379
</synopsis>
380 381
    These will always return true or false, never a null value, even when the
    operand is null.
P
Peter Eisentraut 已提交
382
    A null input is treated as the logical value <quote>unknown</>.
383 384 385 386
    Notice that <literal>IS UNKNOWN</> and <literal>IS NOT UNKNOWN</> are
    effectively the same as <literal>IS NULL</literal> and
    <literal>IS NOT NULL</literal>, respectively, except that the input
    expression must be of Boolean type.
387 388
   </para>
  </sect1>
389

390 391
  <sect1 id="functions-math">
   <title>Mathematical Functions and Operators</title>
392

393 394 395 396
   <para>
    Mathematical operators are provided for many
    <productname>PostgreSQL</productname> types. For types without
    common mathematical conventions for all possible permutations 
397
    (e.g., date/time types) we
398 399
    describe the actual behavior in subsequent sections.
   </para>
400

401 402 403 404 405
   <para>
    <xref linkend="functions-math-op-table"> shows the available mathematical operators.
   </para>

   <table id="functions-math-op-table">
406
    <title>Mathematical Operators</title>
407

408
    <tgroup cols="4">
409 410
     <thead>
      <row>
411
       <entry>Operator</entry>
412
       <entry>Description</entry>
413 414 415 416 417 418 419
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
420
       <entry> <literal>+</literal> </entry>
421
       <entry>addition</entry>
422 423
       <entry><literal>2 + 3</literal></entry>
       <entry><literal>5</literal></entry>
424
      </row>
425

426
      <row>
427
       <entry> <literal>-</literal> </entry>
428
       <entry>subtraction</entry>
429 430
       <entry><literal>2 - 3</literal></entry>
       <entry><literal>-1</literal></entry>
431
      </row>
432

433
      <row>
434
       <entry> <literal>*</literal> </entry>
435
       <entry>multiplication</entry>
436 437
       <entry><literal>2 * 3</literal></entry>
       <entry><literal>6</literal></entry>
438
      </row>
439

440
      <row>
441
       <entry> <literal>/</literal> </entry>
442
       <entry>division (integer division truncates results)</entry>
443 444
       <entry><literal>4 / 2</literal></entry>
       <entry><literal>2</literal></entry>
445
      </row>
446

447
      <row>
448
       <entry> <literal>%</literal> </entry>
449
       <entry>modulo (remainder)</entry>
450 451
       <entry><literal>5 % 4</literal></entry>
       <entry><literal>1</literal></entry>
452
      </row>
453

454
      <row>
455
       <entry> <literal>^</literal> </entry>
456
       <entry>exponentiation</entry>
457 458
       <entry><literal>2.0 ^ 3.0</literal></entry>
       <entry><literal>8</literal></entry>
459 460
      </row>

461 462
      <row>
       <entry> <literal>|/</literal> </entry>
463
       <entry>square root</entry>
464 465
       <entry><literal>|/ 25.0</literal></entry>
       <entry><literal>5</literal></entry>
466
      </row>
467

468 469
      <row>
       <entry> <literal>||/</literal> </entry>
470
       <entry>cube root</entry>
471 472
       <entry><literal>||/ 27.0</literal></entry>
       <entry><literal>3</literal></entry>
473
      </row>
474

475 476
      <row>
       <entry> <literal>!</literal> </entry>
477
       <entry>factorial</entry>
478 479
       <entry><literal>5 !</literal></entry>
       <entry><literal>120</literal></entry>
480
      </row>
481

482 483
      <row>
       <entry> <literal>!!</literal> </entry>
484
       <entry>factorial (prefix operator)</entry>
485 486
       <entry><literal>!! 5</literal></entry>
       <entry><literal>120</literal></entry>
487
      </row>
488

489 490
      <row>
       <entry> <literal>@</literal> </entry>
491
       <entry>absolute value</entry>
492 493
       <entry><literal>@ -5.0</literal></entry>
       <entry><literal>5</literal></entry>
494
      </row>
495

496 497
      <row>
       <entry> <literal>&amp;</literal> </entry>
498 499 500
       <entry>bitwise AND</entry>
       <entry><literal>91 &amp; 15</literal></entry>
       <entry><literal>11</literal></entry>
501
      </row>
502

503 504
      <row>
       <entry> <literal>|</literal> </entry>
505 506 507
       <entry>bitwise OR</entry>
       <entry><literal>32 | 3</literal></entry>
       <entry><literal>35</literal></entry>
508
      </row>
509

510 511
      <row>
       <entry> <literal>#</literal> </entry>
512 513 514
       <entry>bitwise XOR</entry>
       <entry><literal>17 # 5</literal></entry>
       <entry><literal>20</literal></entry>
515
      </row>
516

517 518
      <row>
       <entry> <literal>~</literal> </entry>
519 520 521
       <entry>bitwise NOT</entry>
       <entry><literal>~1</literal></entry>
       <entry><literal>-2</literal></entry>
522
      </row>
523

524
      <row>
525
       <entry> <literal>&lt;&lt;</literal> </entry>
526
       <entry>bitwise shift left</entry>
527 528
       <entry><literal>1 &lt;&lt; 4</literal></entry>
       <entry><literal>16</literal></entry>
529
      </row>
530

531
      <row>
532 533 534 535
       <entry> <literal>&gt;&gt;</literal> </entry>
       <entry>bitwise shift right</entry>
       <entry><literal>8 &gt;&gt; 2</literal></entry>
       <entry><literal>2</literal></entry>
536
      </row>
537

538 539 540
     </tbody>
    </tgroup>
   </table>
541

542
   <para>
543 544 545
    The bitwise operators work only on integral data types, whereas
    the others are available for all numeric data types.  The bitwise
    operators are also available for the bit
546
    string types <type>bit</type> and <type>bit varying</type>, as
547
    shown in <xref linkend="functions-bit-string-op-table">.
548
   </para>
549

550 551 552
  <para>
   <xref linkend="functions-math-func-table"> shows the available
   mathematical functions.  In the table, <literal>dp</literal>
553 554 555
   indicates <type>double precision</type>.  Many of these functions
   are provided in multiple forms with different argument types.
   Except where noted, any given form of a function returns the same
556
   data type as its argument.
557 558 559
   The functions working with <type>double precision</type> data are mostly
   implemented on top of the host system's C library; accuracy and behavior in
   boundary cases may therefore vary depending on the host system.
560
  </para>
561

562
   <table id="functions-math-func-table">
563 564 565 566 567 568 569 570 571 572 573 574 575 576
    <title>Mathematical Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
577
       <entry><literal><function>abs</>(<replaceable>x</replaceable>)</literal></entry>
578
       <entry>(same as <replaceable>x</>)</entry>
579 580
       <entry>absolute value</entry>
       <entry><literal>abs(-17.4)</literal></entry>
581
       <entry><literal>17.4</literal></entry>
582 583 584
      </row>

      <row>
585
       <entry><literal><function>cbrt</function>(<type>dp</type>)</literal></entry>
586 587 588
       <entry><type>dp</type></entry>
       <entry>cube root</entry>
       <entry><literal>cbrt(27.0)</literal></entry>
589
       <entry><literal>3</literal></entry>
590 591 592
      </row>

      <row>
593
       <entry><literal><function>ceil</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
594
       <entry>(same as input)</entry>
595 596
       <entry>smallest integer not less than argument</entry>
       <entry><literal>ceil(-42.8)</literal></entry>
597
       <entry><literal>-42</literal></entry>
598 599
      </row>

600 601 602 603 604 605 606 607
      <row>
       <entry><literal><function>ceiling</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
       <entry>(same as input)</entry>
       <entry>smallest integer not less than argument (alias for <function>ceil</function>)</entry>
       <entry><literal>ceiling(-95.3)</literal></entry>
       <entry><literal>-95</literal></entry>
      </row>

608
      <row>
609
       <entry><literal><function>degrees</function>(<type>dp</type>)</literal></entry>
610 611 612
       <entry><type>dp</type></entry>
       <entry>radians to degrees</entry>
       <entry><literal>degrees(0.5)</literal></entry>
613
       <entry><literal>28.6478897565412</literal></entry>
614 615 616
      </row>

      <row>
617
       <entry><literal><function>exp</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
618
       <entry>(same as input)</entry>
619 620
       <entry>exponential</entry>
       <entry><literal>exp(1.0)</literal></entry>
621
       <entry><literal>2.71828182845905</literal></entry>
622 623 624
      </row>

      <row>
625
       <entry><literal><function>floor</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
626
       <entry>(same as input)</entry>
627 628
       <entry>largest integer not greater than argument</entry>
       <entry><literal>floor(-42.8)</literal></entry>
629
       <entry><literal>-43</literal></entry>
630 631 632
      </row>

      <row>
633
       <entry><literal><function>ln</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
634
       <entry>(same as input)</entry>
635 636
       <entry>natural logarithm</entry>
       <entry><literal>ln(2.0)</literal></entry>
637
       <entry><literal>0.693147180559945</literal></entry>
638 639 640
      </row>

      <row>
641
       <entry><literal><function>log</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
642
       <entry>(same as input)</entry>
643 644
       <entry>base 10 logarithm</entry>
       <entry><literal>log(100.0)</literal></entry>
645
       <entry><literal>2</literal></entry>
646 647 648
      </row>

      <row>
649 650
       <entry><literal><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
        <parameter>x</parameter> <type>numeric</type>)</literal></entry>
651 652 653
       <entry><type>numeric</type></entry>
       <entry>logarithm to base <parameter>b</parameter></entry>
       <entry><literal>log(2.0, 64.0)</literal></entry>
654
       <entry><literal>6.0000000000</literal></entry>
655 656 657
      </row>

      <row>
658 659
       <entry><literal><function>mod</function>(<parameter>y</parameter>,
        <parameter>x</parameter>)</literal></entry>
660 661 662
       <entry>(same as argument types)</entry>
       <entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
       <entry><literal>mod(9,4)</literal></entry>
663
       <entry><literal>1</literal></entry>
664 665 666
      </row>

      <row>
667
       <entry><literal><function>pi</function>()</literal></entry>
668
       <entry><type>dp</type></entry>
669
       <entry><quote>&pi;</quote> constant</entry>
670
       <entry><literal>pi()</literal></entry>
671
       <entry><literal>3.14159265358979</literal></entry>
672 673 674
      </row>

      <row>
675
       <entry><literal><function>power</function>(<parameter>a</parameter> <type>dp</type>,
676
        <parameter>b</parameter> <type>dp</type>)</literal></entry>
677
       <entry><type>dp</type></entry>
678
       <entry><parameter>a</> raised to the power of <parameter>b</parameter></entry>
679
       <entry><literal>power(9.0, 3.0)</literal></entry>
680
       <entry><literal>729</literal></entry>
681 682
      </row>

683
      <row>
684
       <entry><literal><function>power</function>(<parameter>a</parameter> <type>numeric</type>,
685
        <parameter>b</parameter> <type>numeric</type>)</literal></entry>
686
       <entry><type>numeric</type></entry>
687
       <entry><parameter>a</> raised to the power of <parameter>b</parameter></entry>
688
       <entry><literal>power(9.0, 3.0)</literal></entry>
689
       <entry><literal>729</literal></entry>
690 691
      </row>

692
      <row>
693
       <entry><literal><function>radians</function>(<type>dp</type>)</literal></entry>
694 695 696
       <entry><type>dp</type></entry>
       <entry>degrees to radians</entry>
       <entry><literal>radians(45.0)</literal></entry>
697
       <entry><literal>0.785398163397448</literal></entry>
698 699 700
      </row>

      <row>
701
       <entry><literal><function>random</function>()</literal></entry>
702
       <entry><type>dp</type></entry>
703
       <entry>random value between 0.0 and 1.0</entry>
704 705 706 707 708
       <entry><literal>random()</literal></entry>
       <entry></entry>
      </row>

      <row>
709
       <entry><literal><function>round</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
710
       <entry>(same as input)</entry>
711 712
       <entry>round to nearest integer</entry>
       <entry><literal>round(42.4)</literal></entry>
713
       <entry><literal>42</literal></entry>
714 715 716
      </row>

      <row>
717
       <entry><literal><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</literal></entry>
718 719 720
       <entry><type>numeric</type></entry>
       <entry>round to <parameter>s</parameter> decimal places</entry>
       <entry><literal>round(42.4382, 2)</literal></entry>
721
       <entry><literal>42.44</literal></entry>
722
      </row>
723 724

      <row>
725
       <entry><literal><function>setseed</function>(<type>dp</type>)</literal></entry>
726
       <entry><type>integer</type></entry>
727
       <entry>set seed for subsequent <literal>random()</literal> calls</entry>
728
       <entry><literal>setseed(0.54823)</literal></entry>
729
       <entry><literal>1177314959</literal></entry>
730 731
      </row>

732
      <row>
733
       <entry><literal><function>sign</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
734
       <entry>(same as input)</entry>
735 736
       <entry>sign of the argument (-1, 0, +1)</entry>
       <entry><literal>sign(-8.4)</literal></entry>
737
       <entry><literal>-1</literal></entry>
738
      </row>
P
Peter Eisentraut 已提交
739

740
      <row>
741
       <entry><literal><function>sqrt</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
742
       <entry>(same as input)</entry>
743 744
       <entry>square root</entry>
       <entry><literal>sqrt(2.0)</literal></entry>
745
       <entry><literal>1.4142135623731</literal></entry>
746
      </row>
747

748
      <row>
749
       <entry><literal><function>trunc</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
750
       <entry>(same as input)</entry>
751 752
       <entry>truncate toward zero</entry>
       <entry><literal>trunc(42.8)</literal></entry>
753
       <entry><literal>42</literal></entry>
754
      </row>
755

756
      <row>
757
       <entry><literal><function>trunc</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</literal></entry>
758 759 760
       <entry><type>numeric</type></entry>
       <entry>truncate to <parameter>s</parameter> decimal places</entry>
       <entry><literal>trunc(42.4382, 2)</literal></entry>
761
       <entry><literal>42.43</literal></entry>
762
      </row>
763

764 765 766 767 768 769 770 771 772 773
      <row>
       <entry><literal><function>width_bucket</function>(<parameter>op</parameter> <type>numeric</type>, <parameter>b1</parameter> <type>numeric</type>, <parameter>b2</parameter> <type>numeric</type>, <parameter>count</parameter> <type>integer</type>)</literal></entry>
       <entry><type>integer</type></entry>
       <entry>return the bucket to which <parameter>operand</> would
       be assigned in an equidepth histogram with <parameter>count</>
       buckets, an upper bound of <parameter>b1</>, and a lower bound
       of <parameter>b2</></entry>
       <entry><literal>width_bucket(5.35, 0.024, 10.06, 5)</literal></entry>
       <entry><literal>3</literal></entry>
      </row>
774 775 776
     </tbody>
    </tgroup>
   </table>
777

778 779 780
  <para>
   Finally, <xref linkend="functions-math-trig-table"> shows the
   available trigonometric functions.  All trigonometric functions
781
   take arguments and return values of type <type>double
782 783
   precision</type>.
  </para>
784

785
   <table id="functions-math-trig-table">
786
    <title>Trigonometric Functions</title>
787

788 789 790 791 792 793 794
    <tgroup cols="2">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Description</entry>
      </row>
     </thead>
795

796 797
     <tbody>
      <row>
798
       <entry><literal><function>acos</function>(<replaceable>x</replaceable>)</literal></entry>
799 800
       <entry>inverse cosine</entry>
      </row>
801

802
      <row>
803
       <entry><literal><function>asin</function>(<replaceable>x</replaceable>)</literal></entry>
804 805
       <entry>inverse sine</entry>
      </row>
806

807
      <row>
808
       <entry><literal><function>atan</function>(<replaceable>x</replaceable>)</literal></entry>
809 810
       <entry>inverse tangent</entry>
      </row>
811

812
      <row>
813 814
       <entry><literal><function>atan2</function>(<replaceable>x</replaceable>,
        <replaceable>y</replaceable>)</literal></entry>
815
       <entry>inverse tangent of
816
        <literal><replaceable>x</replaceable>/<replaceable>y</replaceable></literal></entry>
817
      </row>
818

819
      <row>
820
       <entry><literal><function>cos</function>(<replaceable>x</replaceable>)</literal></entry>
821 822
       <entry>cosine</entry>
      </row>
823

824
      <row>
825
       <entry><literal><function>cot</function>(<replaceable>x</replaceable>)</literal></entry>
826 827
       <entry>cotangent</entry>
      </row>
828

829
      <row>
830
       <entry><literal><function>sin</function>(<replaceable>x</replaceable>)</literal></entry>
831 832
       <entry>sine</entry>
      </row>
833

834
      <row>
835
       <entry><literal><function>tan</function>(<replaceable>x</replaceable>)</literal></entry>
836 837 838 839 840
       <entry>tangent</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
841

842
  </sect1>
843 844


845 846
  <sect1 id="functions-string">
   <title>String Functions and Operators</title>
847

848 849 850
   <para>
    This section describes functions and operators for examining and
    manipulating string values.  Strings in this context include values
851 852
    of all the types <type>character</type>, <type>character
     varying</type>, and <type>text</type>.  Unless otherwise noted, all
853 854
    of the functions listed below work on all of these types, but be
    wary of potential effects of the automatic padding when using the
855
    <type>character</type> type.  Generally, the functions described
856 857
    here also work on data of non-string types by converting that data
    to a string representation first.  Some functions also exist
858
    natively for the bit-string types.
859
   </para>
860

861 862
   <para>
    <acronym>SQL</acronym> defines some string functions with a special syntax where
863
    certain key words rather than commas are used to separate the
864 865 866 867
    arguments.  Details are in <xref linkend="functions-string-sql">.
    These functions are also implemented using the regular syntax for
    function invocation.  (See <xref linkend="functions-string-other">.)
   </para>
868

869 870 871 872 873 874 875 876 877 878 879 880
   <table id="functions-string-sql">
    <title><acronym>SQL</acronym> String Functions and Operators</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>  
      </row>
     </thead>
881

882 883
     <tbody>
      <row>
884 885
       <entry><literal><parameter>string</parameter> <literal>||</literal>
        <parameter>string</parameter></literal></entry>
886 887
       <entry> <type>text</type> </entry>
       <entry>
888
        String concatenation
889
        <indexterm>
P
Peter Eisentraut 已提交
890
         <primary>character string</primary>
891 892 893
         <secondary>concatenation</secondary>
        </indexterm>
       </entry>
894
       <entry><literal>'Post' || 'greSQL'</literal></entry>
895 896
       <entry><literal>PostgreSQL</literal></entry>
      </row>
897

898
      <row>
899
       <entry><literal><function>bit_length</function>(<parameter>string</parameter>)</literal></entry>
900
       <entry><type>integer</type></entry>
901
       <entry>Number of bits in string</entry>
902 903 904
       <entry><literal>bit_length('jose')</literal></entry>
       <entry><literal>32</literal></entry>
      </row>
P
Peter Eisentraut 已提交
905

906
      <row>
907
       <entry><literal><function>char_length</function>(<parameter>string</parameter>)</literal> or <literal><function>character_length</function>(<parameter>string</parameter>)</literal></entry>
908 909
       <entry><type>integer</type></entry>
       <entry>
910
        Number of characters in string
911
        <indexterm>
P
Peter Eisentraut 已提交
912
         <primary>character string</primary>
913 914 915 916
         <secondary>length</secondary>
        </indexterm>
        <indexterm>
         <primary>length</primary>
P
Peter Eisentraut 已提交
917
         <secondary sortas="character string">of a character string</secondary>
918 919 920 921 922 923
         <see>character strings, length</see>
        </indexterm>
       </entry>
       <entry><literal>char_length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>
924

T
Tatsuo Ishii 已提交
925
      <row>
926 927
       <entry><literal><function>convert</function>(<parameter>string</parameter>
       using <parameter>conversion_name</parameter>)</literal></entry>
T
Tatsuo Ishii 已提交
928
       <entry><type>text</type></entry>
929 930 931 932 933 934 935
       <entry>
        Change encoding using specified conversion name.  Conversions
        can be defined by <command>CREATE CONVERSION</command>.  Also
        there are some pre-defined conversion names. See <xref
        linkend="conversion-names"> for available conversion
        names.
       </entry>
936 937
       <entry><literal>convert('PostgreSQL' using iso_8859_1_to_utf8)</literal></entry>
       <entry><literal>'PostgreSQL'</literal> in UTF8 (Unicode, 8-bit) encoding</entry>
T
Tatsuo Ishii 已提交
938 939
      </row>

940
      <row>
941
       <entry><literal><function>lower</function>(<parameter>string</parameter>)</literal></entry>
942
       <entry><type>text</type></entry>
943
       <entry>Convert string to lower case</entry>
944 945 946
       <entry><literal>lower('TOM')</literal></entry>
       <entry><literal>tom</literal></entry>
      </row>
947

948
      <row>
949
       <entry><literal><function>octet_length</function>(<parameter>string</parameter>)</literal></entry>
950
       <entry><type>integer</type></entry>
951
       <entry>Number of bytes in string</entry>
952 953 954
       <entry><literal>octet_length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>
955

956
      <row>
957
       <entry><literal><function>overlay</function>(<parameter>string</parameter> placing <parameter>string</parameter> from <type>integer</type> <optional>for <type>integer</type></optional>)</literal></entry>
958 959
       <entry><type>text</type></entry>
       <entry>
960
        Replace substring
961 962 963 964 965 966 967
        <indexterm>
         <primary>overlay</primary>
        </indexterm>
       </entry>
       <entry><literal>overlay('Txxxxas' placing 'hom' from 2 for 4)</literal></entry>
       <entry><literal>Thomas</literal></entry>
      </row>
968

969
      <row>
970
       <entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
971
       <entry><type>integer</type></entry>
972
       <entry>Location of specified substring</entry>
973 974 975
       <entry><literal>position('om' in 'Thomas')</literal></entry>
       <entry><literal>3</literal></entry>
      </row>
976

977
      <row>
978
       <entry><literal><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</literal></entry>
979 980
       <entry><type>text</type></entry>
       <entry>
981
        Extract substring
982 983 984 985 986 987 988
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
       <entry><literal>hom</literal></entry>
      </row>
989

990
      <row>
991
       <entry><literal><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable>)</literal></entry>
992 993
       <entry><type>text</type></entry>
       <entry>
994
        Extract substring matching POSIX regular expression
995 996 997 998
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
999
       <entry><literal>substring('Thomas' from '...$')</literal></entry>
1000 1001
       <entry><literal>mas</literal></entry>
      </row>
1002

1003
      <row>
1004
       <entry><literal><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable> for <replaceable>escape</replaceable>)</literal></entry>
1005 1006
       <entry><type>text</type></entry>
       <entry>
1007 1008
        Extract substring matching <acronym>SQL</acronym> regular
        expression
1009 1010 1011 1012 1013 1014 1015 1016
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Thomas' from '%#"o_a#"_' for '#')</literal></entry>
       <entry><literal>oma</literal></entry>
      </row>

1017 1018
      <row>
       <entry>
1019
        <literal><function>trim</function>(<optional>leading | trailing | both</optional>
1020
        <optional><parameter>characters</parameter></optional> from
1021
        <parameter>string</parameter>)</literal>
1022 1023 1024
       </entry>
       <entry><type>text</type></entry>
       <entry>
1025
        Remove the longest string containing only the
1026
        <parameter>characters</parameter> (a space by default) from the
1027
        start/end/both ends of the <parameter>string</parameter>.
1028 1029 1030 1031
       </entry>
       <entry><literal>trim(both 'x' from 'xTomxx')</literal></entry>
       <entry><literal>Tom</literal></entry>
      </row>
1032

1033
      <row>
1034
       <entry><literal><function>upper</function>(<parameter>string</parameter>)</literal></entry>
1035
       <entry><type>text</type></entry>
1036
       <entry>Convert string to uppercase</entry>
1037 1038 1039 1040 1041 1042
       <entry><literal>upper('tom')</literal></entry>
       <entry><literal>TOM</literal></entry>
      </row>
     </tbody>
    </tgroup>
   </table>
1043

1044 1045
   <para>
    Additional string manipulation functions are available and are
1046 1047
    listed in <xref linkend="functions-string-other">.  Some of them are used internally to implement the
    <acronym>SQL</acronym>-standard string functions listed in <xref linkend="functions-string-sql">.
1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064
   </para>

   <table id="functions-string-other">
    <title>Other String Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
1065
       <entry><literal><function>ascii</function>(<type>text</type>)</literal></entry>
1066
       <entry><type>integer</type></entry>
1067
       <entry><acronym>ASCII</acronym> code of the first character of the argument</entry>
1068 1069 1070 1071 1072
       <entry><literal>ascii('x')</literal></entry>
       <entry><literal>120</literal></entry>
      </row>

      <row>
1073 1074
       <entry><literal><function>btrim</function>(<parameter>string</parameter> <type>text</type>
       <optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal></entry>
1075 1076
       <entry><type>text</type></entry>
       <entry>
1077
        Remove the longest string consisting only of characters
B
Bruce Momjian 已提交
1078
        in <parameter>characters</parameter> (a space by default)
1079
        from the start and end of <parameter>string</parameter>.
1080
       </entry>
1081
       <entry><literal>btrim('xyxtrimyyx', 'xy')</literal></entry>
1082 1083 1084 1085
       <entry><literal>trim</literal></entry>
      </row>

      <row>
1086
       <entry><literal><function>chr</function>(<type>integer</type>)</literal></entry>
1087
       <entry><type>text</type></entry>
1088
       <entry>Character with the given <acronym>ASCII</acronym> code</entry>
1089 1090 1091 1092 1093 1094
       <entry><literal>chr(65)</literal></entry>
       <entry><literal>A</literal></entry>
      </row>

      <row>
       <entry>
1095
        <literal><function>convert</function>(<parameter>string</parameter>
1096 1097
        <type>text</type>,
        <optional><parameter>src_encoding</parameter> <type>name</type>,</optional>
1098
        <parameter>dest_encoding</parameter> <type>name</type>)</literal>
1099 1100 1101
       </entry>
       <entry><type>text</type></entry>
       <entry>
1102
        Convert string to <parameter>dest_encoding</parameter>.
1103 1104 1105 1106 1107
        The original encoding is specified by
        <parameter>src_encoding</parameter>.  If
        <parameter>src_encoding</parameter> is omitted, database
        encoding is assumed.
       </entry>
1108 1109
       <entry><literal>convert( 'text_in_utf8', 'UTF8', 'LATIN1')</literal></entry>
       <entry><literal>text_in_utf8</literal> represented in ISO 8859-1 encoding</entry>
1110 1111 1112 1113
      </row>

      <row>
       <entry>
1114 1115
        <literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
        <parameter>type</parameter> <type>text</type>)</literal>
1116 1117 1118
       </entry>
       <entry><type>bytea</type></entry>
       <entry>
1119
        Decode binary data from <parameter>string</parameter> previously 
1120
        encoded with <function>encode</>.  Parameter type is same as in <function>encode</>.
1121 1122 1123 1124 1125 1126 1127
       </entry>
       <entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
       <entry><literal>123\000\001</literal></entry>
      </row>       

      <row>
       <entry>
1128 1129
        <literal><function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
        <parameter>type</parameter> <type>text</type>)</literal>
1130 1131 1132
       </entry>
       <entry><type>text</type></entry>
       <entry>
1133
        Encode binary data to <acronym>ASCII</acronym>-only representation.  Supported
1134
        types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
1135
       </entry>
1136
       <entry><literal>encode( '123\\000\\001', 'base64')</literal></entry>
1137 1138 1139 1140
       <entry><literal>MTIzAAE=</literal></entry>
      </row>       

      <row>
1141
       <entry><literal><function>initcap</function>(<type>text</type>)</literal></entry>
1142
       <entry><type>text</type></entry>
1143 1144 1145 1146 1147 1148
       <entry>
        Convert the first letter of each word to uppercase and the
        rest to lowercase. Words are sequences of alphanumeric
        characters separated by non-alphanumeric characters.
       </entry>
       <entry><literal>initcap('hi THOMAS')</literal></entry>
1149 1150 1151 1152
       <entry><literal>Hi Thomas</literal></entry>
      </row>

      <row>
1153
       <entry><literal><function>length</function>(<parameter>string</parameter> <type>text</type>)</literal></entry>
1154 1155
       <entry><type>integer</type></entry>
       <entry>
1156
        Number of characters in <parameter>string</parameter>.
1157
        <indexterm>
P
Peter Eisentraut 已提交
1158
         <primary>character string</primary>
1159 1160 1161 1162
         <secondary>length</secondary>
        </indexterm>
        <indexterm>
         <primary>length</primary>
P
Peter Eisentraut 已提交
1163
         <secondary sortas="character string">of a character string</secondary>
1164 1165 1166 1167 1168 1169 1170 1171 1172
         <see>character strings, length</see>
        </indexterm>
       </entry>
       <entry><literal>length('jose')</literal></entry>
       <entry><literal>4</literal></entry>
      </row>

      <row>
       <entry>
1173
        <literal><function>lpad</function>(<parameter>string</parameter> <type>text</type>,
1174
        <parameter>length</parameter> <type>integer</type>
1175
        <optional>, <parameter>fill</parameter> <type>text</type></optional>)</literal>
1176
       </entry>
1177
       <entry><type>text</type></entry>
1178
       <entry>
1179
        Fill up the <parameter>string</parameter> to length
1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190
        <parameter>length</parameter> by prepending the characters
        <parameter>fill</parameter> (a space by default).  If the
        <parameter>string</parameter> is already longer than
        <parameter>length</parameter> then it is truncated (on the
        right).
       </entry>
       <entry><literal>lpad('hi', 5, 'xy')</literal></entry>
       <entry><literal>xyxhi</literal></entry>
      </row>

      <row>
1191 1192 1193
       <entry><literal><function>ltrim</function>(<parameter>string</parameter> <type>text</type>
        <optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal>
       </entry>
1194 1195
       <entry><type>text</type></entry>
       <entry>
1196
        Remove the longest string containing only characters from
1197
        <parameter>characters</parameter> (a space by default) from the start of
1198
        <parameter>string</parameter>.
1199
       </entry>
1200
       <entry><literal>ltrim('zzzytrim', 'xyz')</literal></entry>
1201 1202 1203
       <entry><literal>trim</literal></entry>
      </row>

1204
      <row>
1205
       <entry><literal><function>md5</function>(<parameter>string</parameter> <type>text</type>)</literal></entry>
1206 1207
       <entry><type>text</type></entry>
       <entry>
1208 1209
        Calculates the MD5 hash of <parameter>string</parameter>,
        returning the result in hexadecimal.
1210 1211
       </entry>
       <entry><literal>md5('abc')</literal></entry>
1212
       <entry><literal>900150983cd24fb0 d6963f7d28e17f72</literal></entry>
1213 1214
      </row>

1215
      <row>
1216
       <entry><literal><function>pg_client_encoding</function>()</literal></entry>
1217 1218
       <entry><type>name</type></entry>
       <entry>
1219
        Current client encoding name
1220 1221 1222 1223 1224 1225
       </entry>
       <entry><literal>pg_client_encoding()</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
      </row>

      <row>
P
Peter Eisentraut 已提交
1226
       <entry><literal><function>quote_ident</function>(<parameter>string</parameter> text)</literal><indexterm><primary>quote_ident</></></entry>
1227 1228
       <entry><type>text</type></entry>
       <entry>
1229
        Return the given string suitably quoted to be used as an identifier
1230 1231 1232 1233
        in an <acronym>SQL</acronym> statement string.
        Quotes are added only if necessary (i.e., if the string contains
        non-identifier characters or would be case-folded).
        Embedded quotes are properly doubled.
1234
       </entry>
1235 1236
       <entry><literal>quote_ident('Foo bar')</literal></entry>
       <entry><literal>"Foo bar"</literal></entry>
1237 1238 1239
      </row>

      <row>
P
Peter Eisentraut 已提交
1240
       <entry><literal><function>quote_literal</function>(<parameter>string</parameter> text)</literal><indexterm><primary>quote_literal</></></entry>
1241 1242
       <entry><type>text</type></entry>
       <entry>
1243
        Return the given string suitably quoted to be used as a string literal
1244 1245
        in an <acronym>SQL</acronym> statement string.
        Embedded quotes and backslashes are properly doubled.
1246
       </entry>
1247
       <entry><literal>quote_literal( 'O\'Reilly')</literal></entry>
1248 1249 1250 1251
       <entry><literal>'O''Reilly'</literal></entry>
      </row>

      <row>
1252
       <entry><literal><function>repeat</function>(<parameter>string</parameter> <type>text</type>, <parameter>number</parameter> <type>integer</type>)</literal></entry>
1253
       <entry><type>text</type></entry>
1254 1255
       <entry>Repeat <parameter>string</parameter> the specified
       <parameter>number</parameter> of times</entry>
1256 1257 1258 1259 1260
       <entry><literal>repeat('Pg', 4)</literal></entry>
       <entry><literal>PgPgPgPg</literal></entry>
      </row>

      <row>
1261
       <entry><literal><function>replace</function>(<parameter>string</parameter> <type>text</type>,
1262
       <parameter>from</parameter> <type>text</type>,
1263
       <parameter>to</parameter> <type>text</type>)</literal></entry>
1264 1265
       <entry><type>text</type></entry>
       <entry>Replace all occurrences in <parameter>string</parameter> of substring
1266
        <parameter>from</parameter> with substring <parameter>to</parameter>.
1267
       </entry>
1268
       <entry><literal>replace( 'abcdefabcdef', 'cd', 'XX')</literal></entry>
1269 1270 1271 1272 1273
       <entry><literal>abXXefabXXef</literal></entry>
      </row>

      <row>
       <entry>
1274
        <literal><function>rpad</function>(<parameter>string</parameter> <type>text</type>,
1275
        <parameter>length</parameter> <type>integer</type>
1276
        <optional>, <parameter>fill</parameter> <type>text</type></optional>)</literal>
1277 1278 1279
       </entry>
       <entry><type>text</type></entry>
       <entry>
1280
        Fill up the <parameter>string</parameter> to length
1281 1282 1283 1284 1285 1286 1287 1288 1289 1290
        <parameter>length</parameter> by appending the characters
        <parameter>fill</parameter> (a space by default).  If the
        <parameter>string</parameter> is already longer than
        <parameter>length</parameter> then it is truncated.
       </entry>
       <entry><literal>rpad('hi', 5, 'xy')</literal></entry>
       <entry><literal>hixyx</literal></entry>
      </row>

      <row>
1291 1292 1293
       <entry><literal><function>rtrim</function>(<parameter>string</parameter> <type>text</type>
        <optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal>
       </entry>
1294 1295
       <entry><type>text</type></entry>
       <entry>
1296
        Remove the longest string containing only characters from
1297
        <parameter>characters</parameter> (a space by default) from the end of
1298
        <parameter>string</parameter>.
1299
       </entry>
1300
       <entry><literal>rtrim('trimxxxx', 'x')</literal></entry>
1301 1302 1303 1304
       <entry><literal>trim</literal></entry>
      </row>

      <row>
1305
       <entry><literal><function>split_part</function>(<parameter>string</parameter> <type>text</type>,
1306
       <parameter>delimiter</parameter> <type>text</type>,
1307
       <parameter>field</parameter> <type>integer</type>)</literal></entry>
1308 1309
       <entry><type>text</type></entry>
       <entry>Split <parameter>string</parameter> on <parameter>delimiter</parameter>
1310
        and return the given field (counting from one)
1311
       </entry>
1312
       <entry><literal>split_part( 'abc~@~def~@~ghi', '~@~', 2)</literal></entry>
1313 1314 1315 1316
       <entry><literal>def</literal></entry>
      </row>

      <row>
1317
       <entry><literal><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</literal></entry>
1318 1319
       <entry><type>text</type></entry>
       <entry>
1320
        Location of specified substring (same as
1321 1322 1323 1324
        <literal>position(<parameter>substring</parameter> in
         <parameter>string</parameter>)</literal>, but note the reversed
        argument order)
       </entry>
1325
       <entry><literal>strpos('high', 'ig')</literal></entry>
1326 1327 1328 1329
       <entry><literal>2</literal></entry>
      </row>

      <row>
1330
       <entry><literal><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</literal></entry>
1331 1332
       <entry><type>text</type></entry>
       <entry>
1333
        Extract substring (same as
1334 1335 1336 1337 1338 1339 1340
        <literal>substring(<parameter>string</parameter> from <parameter>from</parameter> for <parameter>count</parameter>)</literal>)
       </entry>
       <entry><literal>substr('alphabet', 3, 2)</literal></entry>
       <entry><literal>ph</literal></entry>
      </row>

      <row>
1341 1342
       <entry><literal><function>to_ascii</function>(<type>text</type>
        <optional>, <parameter>encoding</parameter></optional>)</literal></entry>
1343
       <entry><type>text</type></entry>
1344 1345

       <entry>
N
Neil Conway 已提交
1346
       Convert <parameter>text</parameter> to <acronym>ASCII</acronym> from another encoding
1347 1348 1349
       <footnote>
        <para>
         The <function>to_ascii</function> function supports conversion from
T
Tom Lane 已提交
1350 1351
         <literal>LATIN1</>, <literal>LATIN2</>, <literal>LATIN9</>,
         and <literal>WIN1250</> encodings only.
1352 1353 1354 1355
        </para>
       </footnote>
       </entry>

1356 1357 1358 1359 1360
       <entry><literal>to_ascii('Karel')</literal></entry>
       <entry><literal>Karel</literal></entry>
      </row>

      <row>
1361 1362
       <entry><literal><function>to_hex</function>(<parameter>number</parameter> <type>integer</type>
       or <type>bigint</type>)</literal></entry>
1363 1364
       <entry><type>text</type></entry>
       <entry>Convert <parameter>number</parameter> to its equivalent hexadecimal
1365
        representation
1366
       </entry>
1367 1368
       <entry><literal>to_hex(2147483647)</literal></entry>
       <entry><literal>7fffffff</literal></entry>
1369 1370 1371 1372
      </row>

      <row>
       <entry>
1373
        <literal><function>translate</function>(<parameter>string</parameter>
1374 1375
        <type>text</type>,
        <parameter>from</parameter> <type>text</type>,
1376
        <parameter>to</parameter> <type>text</type>)</literal>
1377 1378 1379 1380 1381 1382 1383 1384 1385 1386 1387 1388 1389 1390 1391 1392 1393
       </entry>
       <entry><type>text</type></entry>
       <entry>
        Any character in <parameter>string</parameter> that matches a
        character in the <parameter>from</parameter> set is replaced by
        the corresponding character in the <parameter>to</parameter>
        set.
       </entry>
       <entry><literal>translate('12345', '14', 'ax')</literal></entry>
       <entry><literal>a23x5</literal></entry>
      </row>       
      
     </tbody>
    </tgroup>
   </table>


T
Tatsuo Ishii 已提交
1394
   <table id="conversion-names">
1395
    <title>Built-in Conversions</title>
T
Tatsuo Ishii 已提交
1396 1397 1398
    <tgroup cols="3">
     <thead>
      <row>
1399 1400 1401 1402 1403 1404 1405 1406 1407 1408 1409 1410 1411 1412
       <entry>Conversion Name
        <footnote>
         <para>
          The conversion names follow a standard naming scheme: The
          official name of the source encoding with all
          non-alphanumeric characters replaced by underscores followed
          by <literal>_to_</literal> followed by the equally processed
          destination encoding name. Therefore the names might deviate
          from the customary encoding names.
         </para>
        </footnote>
       </entry>
       <entry>Source Encoding</entry>
       <entry>Destination Encoding</entry>
T
Tatsuo Ishii 已提交
1413 1414
      </row>
     </thead>
1415

T
Tatsuo Ishii 已提交
1416 1417
     <tbody>
      <row>
P
Peter Eisentraut 已提交
1418 1419 1420
       <entry><literal>ascii_to_mic</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1421 1422 1423
      </row>

      <row>
1424
       <entry><literal>ascii_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1425
       <entry><literal>SQL_ASCII</literal></entry>
1426
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1427 1428 1429
      </row>

      <row>
P
Peter Eisentraut 已提交
1430 1431 1432
       <entry><literal>big5_to_euc_tw</literal></entry>
       <entry><literal>BIG5</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1433 1434 1435
      </row>

      <row>
P
Peter Eisentraut 已提交
1436 1437 1438
       <entry><literal>big5_to_mic</literal></entry>
       <entry><literal>BIG5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1439 1440 1441
      </row>

      <row>
1442
       <entry><literal>big5_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1443
       <entry><literal>BIG5</literal></entry>
1444
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1445 1446 1447
      </row>

      <row>
P
Peter Eisentraut 已提交
1448 1449 1450
       <entry><literal>euc_cn_to_mic</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1451 1452 1453
      </row>

      <row>
1454
       <entry><literal>euc_cn_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1455
       <entry><literal>EUC_CN</literal></entry>
1456
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1457 1458 1459
      </row>

      <row>
P
Peter Eisentraut 已提交
1460 1461 1462
       <entry><literal>euc_jp_to_mic</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1463 1464 1465
      </row>

      <row>
P
Peter Eisentraut 已提交
1466 1467 1468
       <entry><literal>euc_jp_to_sjis</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1469 1470 1471
      </row>

      <row>
1472
       <entry><literal>euc_jp_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1473
       <entry><literal>EUC_JP</literal></entry>
1474
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1475 1476 1477
      </row>

      <row>
P
Peter Eisentraut 已提交
1478 1479 1480
       <entry><literal>euc_kr_to_mic</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1481 1482 1483
      </row>

      <row>
1484
       <entry><literal>euc_kr_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1485
       <entry><literal>EUC_KR</literal></entry>
1486
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1487 1488 1489
      </row>

      <row>
P
Peter Eisentraut 已提交
1490 1491 1492
       <entry><literal>euc_tw_to_big5</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1493 1494 1495
      </row>

      <row>
P
Peter Eisentraut 已提交
1496 1497 1498
       <entry><literal>euc_tw_to_mic</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1499 1500 1501
      </row>

      <row>
1502
       <entry><literal>euc_tw_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1503
       <entry><literal>EUC_TW</literal></entry>
1504
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1505 1506 1507
      </row>

      <row>
1508
       <entry><literal>gb18030_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1509
       <entry><literal>GB18030</literal></entry>
1510
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1511 1512 1513
      </row>

      <row>
1514
       <entry><literal>gbk_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1515
       <entry><literal>GBK</literal></entry>
1516
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1517 1518 1519
      </row>

      <row>
1520
       <entry><literal>iso_8859_10_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1521
       <entry><literal>LATIN6</literal></entry>
1522
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1523 1524 1525
      </row>

      <row>
1526
       <entry><literal>iso_8859_13_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1527
       <entry><literal>LATIN7</literal></entry>
1528
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1529 1530 1531
      </row>

      <row>
1532
       <entry><literal>iso_8859_14_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1533
       <entry><literal>LATIN8</literal></entry>
1534
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1535 1536 1537
      </row>

      <row>
1538
       <entry><literal>iso_8859_15_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1539
       <entry><literal>LATIN9</literal></entry>
1540
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1541 1542 1543
      </row>

      <row>
1544
       <entry><literal>iso_8859_16_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1545
       <entry><literal>LATIN10</literal></entry>
1546
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1547 1548 1549
      </row>

      <row>
P
Peter Eisentraut 已提交
1550 1551 1552
       <entry><literal>iso_8859_1_to_mic</literal></entry>
       <entry><literal>LATIN1</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1553 1554 1555
      </row>

      <row>
1556
       <entry><literal>iso_8859_1_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1557
       <entry><literal>LATIN1</literal></entry>
1558
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1559 1560 1561
      </row>

      <row>
P
Peter Eisentraut 已提交
1562 1563 1564
       <entry><literal>iso_8859_2_to_mic</literal></entry>
       <entry><literal>LATIN2</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1565 1566 1567
      </row>

      <row>
1568
       <entry><literal>iso_8859_2_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1569
       <entry><literal>LATIN2</literal></entry>
1570
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1571 1572 1573
      </row>

      <row>
1574
       <entry><literal>iso_8859_2_to_windows_1250</literal></entry>
P
Peter Eisentraut 已提交
1575 1576
       <entry><literal>LATIN2</literal></entry>
       <entry><literal>WIN1250</literal></entry>
1577 1578 1579
      </row>

      <row>
P
Peter Eisentraut 已提交
1580 1581 1582
       <entry><literal>iso_8859_3_to_mic</literal></entry>
       <entry><literal>LATIN3</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1583 1584 1585
      </row>

      <row>
1586
       <entry><literal>iso_8859_3_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1587
       <entry><literal>LATIN3</literal></entry>
1588
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1589 1590 1591
      </row>

      <row>
P
Peter Eisentraut 已提交
1592 1593 1594
       <entry><literal>iso_8859_4_to_mic</literal></entry>
       <entry><literal>LATIN4</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1595 1596 1597
      </row>

      <row>
1598
       <entry><literal>iso_8859_4_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1599
       <entry><literal>LATIN4</literal></entry>
1600
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1601 1602 1603
      </row>

      <row>
1604
       <entry><literal>iso_8859_5_to_koi8_r</literal></entry>
P
Peter Eisentraut 已提交
1605 1606
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>KOI8</literal></entry>
1607 1608 1609
      </row>

      <row>
P
Peter Eisentraut 已提交
1610 1611 1612
       <entry><literal>iso_8859_5_to_mic</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1613 1614 1615
      </row>

      <row>
1616
       <entry><literal>iso_8859_5_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1617
       <entry><literal>ISO_8859_5</literal></entry>
1618
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1619 1620 1621
      </row>

      <row>
1622
       <entry><literal>iso_8859_5_to_windows_1251</literal></entry>
P
Peter Eisentraut 已提交
1623
       <entry><literal>ISO_8859_5</literal></entry>
1624
       <entry><literal>WIN1251</literal></entry>
1625 1626 1627
      </row>

      <row>
1628
       <entry><literal>iso_8859_5_to_windows_866</literal></entry>
P
Peter Eisentraut 已提交
1629
       <entry><literal>ISO_8859_5</literal></entry>
1630
       <entry><literal>WIN866</literal></entry>
1631 1632 1633
      </row>

      <row>
1634
       <entry><literal>iso_8859_6_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1635
       <entry><literal>ISO_8859_6</literal></entry>
1636
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1637 1638 1639
      </row>

      <row>
1640
       <entry><literal>iso_8859_7_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1641
       <entry><literal>ISO_8859_7</literal></entry>
1642
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1643 1644 1645
      </row>

      <row>
1646
       <entry><literal>iso_8859_8_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1647
       <entry><literal>ISO_8859_8</literal></entry>
1648
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1649 1650 1651
      </row>

      <row>
1652
       <entry><literal>iso_8859_9_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1653
       <entry><literal>LATIN5</literal></entry>
1654
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1655 1656 1657
      </row>

      <row>
1658
       <entry><literal>johab_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1659
       <entry><literal>JOHAB</literal></entry>
1660
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1661 1662
      </row>

1663
      <row>
1664
       <entry><literal>koi8_r_to_iso_8859_5</literal></entry>
P
Peter Eisentraut 已提交
1665 1666
       <entry><literal>KOI8</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1667 1668 1669
      </row>

      <row>
1670
       <entry><literal>koi8_r_to_mic</literal></entry>
P
Peter Eisentraut 已提交
1671 1672
       <entry><literal>KOI8</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
1673 1674 1675
      </row>

      <row>
1676
       <entry><literal>koi8_r_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1677
       <entry><literal>KOI8</literal></entry>
1678
       <entry><literal>UTF8</literal></entry>
1679 1680 1681
      </row>

      <row>
1682
       <entry><literal>koi8_r_to_windows_1251</literal></entry>
P
Peter Eisentraut 已提交
1683
       <entry><literal>KOI8</literal></entry>
1684
       <entry><literal>WIN1251</literal></entry>
1685 1686 1687
      </row>

      <row>
1688
       <entry><literal>koi8_r_to_windows_866</literal></entry>
P
Peter Eisentraut 已提交
1689
       <entry><literal>KOI8</literal></entry>
1690
       <entry><literal>WIN866</literal></entry>
1691 1692 1693
      </row>

      <row>
P
Peter Eisentraut 已提交
1694 1695 1696
       <entry><literal>mic_to_ascii</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>SQL_ASCII</literal></entry>
1697 1698
      </row>

T
Tatsuo Ishii 已提交
1699
      <row>
P
Peter Eisentraut 已提交
1700 1701 1702
       <entry><literal>mic_to_big5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1703 1704
      </row>

1705
      <row>
P
Peter Eisentraut 已提交
1706 1707 1708
       <entry><literal>mic_to_euc_cn</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_CN</literal></entry>
1709 1710
      </row>

T
Tatsuo Ishii 已提交
1711
      <row>
P
Peter Eisentraut 已提交
1712 1713 1714
       <entry><literal>mic_to_euc_jp</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1715 1716
      </row>

1717
      <row>
P
Peter Eisentraut 已提交
1718 1719 1720
       <entry><literal>mic_to_euc_kr</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_KR</literal></entry>
1721 1722
      </row>

T
Tatsuo Ishii 已提交
1723
      <row>
P
Peter Eisentraut 已提交
1724 1725 1726
       <entry><literal>mic_to_euc_tw</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1727 1728
      </row>

1729
      <row>
P
Peter Eisentraut 已提交
1730 1731 1732
       <entry><literal>mic_to_iso_8859_1</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN1</literal></entry>
1733 1734 1735
      </row>

      <row>
P
Peter Eisentraut 已提交
1736 1737 1738
       <entry><literal>mic_to_iso_8859_2</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN2</literal></entry>
1739 1740 1741
      </row>

      <row>
P
Peter Eisentraut 已提交
1742 1743 1744
       <entry><literal>mic_to_iso_8859_3</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN3</literal></entry>
1745 1746 1747
      </row>

      <row>
P
Peter Eisentraut 已提交
1748 1749 1750
       <entry><literal>mic_to_iso_8859_4</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>LATIN4</literal></entry>
1751 1752 1753
      </row>

      <row>
P
Peter Eisentraut 已提交
1754 1755 1756
       <entry><literal>mic_to_iso_8859_5</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>ISO_8859_5</literal></entry>
1757 1758 1759
      </row>

      <row>
1760
       <entry><literal>mic_to_koi8_r</literal></entry>
P
Peter Eisentraut 已提交
1761 1762
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>KOI8</literal></entry>
1763 1764
      </row>

T
Tatsuo Ishii 已提交
1765
      <row>
P
Peter Eisentraut 已提交
1766 1767 1768
       <entry><literal>mic_to_sjis</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1769 1770
      </row>

1771
      <row>
1772
       <entry><literal>mic_to_windows_1250</literal></entry>
P
Peter Eisentraut 已提交
1773 1774
       <entry><literal>MULE_INTERNAL</literal></entry>
       <entry><literal>WIN1250</literal></entry>
1775 1776 1777
      </row>

      <row>
1778
       <entry><literal>mic_to_windows_1251</literal></entry>
P
Peter Eisentraut 已提交
1779
       <entry><literal>MULE_INTERNAL</literal></entry>
1780
       <entry><literal>WIN1251</literal></entry>
1781 1782 1783
      </row>

      <row>
1784
       <entry><literal>mic_to_windows_866</literal></entry>
P
Peter Eisentraut 已提交
1785
       <entry><literal>MULE_INTERNAL</literal></entry>
1786
       <entry><literal>WIN866</literal></entry>
1787 1788
      </row>

T
Tatsuo Ishii 已提交
1789
      <row>
P
Peter Eisentraut 已提交
1790 1791 1792
       <entry><literal>sjis_to_euc_jp</literal></entry>
       <entry><literal>SJIS</literal></entry>
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1793 1794 1795
      </row>

      <row>
P
Peter Eisentraut 已提交
1796 1797 1798
       <entry><literal>sjis_to_mic</literal></entry>
       <entry><literal>SJIS</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
T
Tatsuo Ishii 已提交
1799 1800 1801
      </row>

      <row>
1802
       <entry><literal>sjis_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1803
       <entry><literal>SJIS</literal></entry>
1804
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1805 1806 1807
      </row>

      <row>
1808 1809 1810
       <entry><literal>tcvn_to_utf8</literal></entry>
       <entry><literal>WIN1258</literal></entry>
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1811 1812 1813
      </row>

      <row>
1814
       <entry><literal>uhc_to_utf8</literal></entry>
P
Peter Eisentraut 已提交
1815
       <entry><literal>UHC</literal></entry>
1816
       <entry><literal>UTF8</literal></entry>
T
Tatsuo Ishii 已提交
1817 1818 1819
      </row>

      <row>
1820 1821
       <entry><literal>utf8_to_ascii</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1822
       <entry><literal>SQL_ASCII</literal></entry>
T
Tatsuo Ishii 已提交
1823 1824 1825
      </row>

      <row>
1826 1827
       <entry><literal>utf8_to_big5</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1828
       <entry><literal>BIG5</literal></entry>
T
Tatsuo Ishii 已提交
1829 1830 1831
      </row>

      <row>
1832 1833
       <entry><literal>utf8_to_euc_cn</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1834
       <entry><literal>EUC_CN</literal></entry>
T
Tatsuo Ishii 已提交
1835 1836 1837
      </row>

      <row>
1838 1839
       <entry><literal>utf8_to_euc_jp</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1840
       <entry><literal>EUC_JP</literal></entry>
T
Tatsuo Ishii 已提交
1841 1842 1843
      </row>

      <row>
1844 1845
       <entry><literal>utf8_to_euc_kr</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1846
       <entry><literal>EUC_KR</literal></entry>
T
Tatsuo Ishii 已提交
1847 1848 1849
      </row>

      <row>
1850 1851
       <entry><literal>utf8_to_euc_tw</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1852
       <entry><literal>EUC_TW</literal></entry>
T
Tatsuo Ishii 已提交
1853 1854 1855
      </row>

      <row>
1856 1857
       <entry><literal>utf8_to_gb18030</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1858
       <entry><literal>GB18030</literal></entry>
T
Tatsuo Ishii 已提交
1859 1860 1861
      </row>

      <row>
1862 1863
       <entry><literal>utf8_to_gbk</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1864
       <entry><literal>GBK</literal></entry>
T
Tatsuo Ishii 已提交
1865 1866 1867
      </row>

      <row>
1868 1869
       <entry><literal>utf8_to_iso_8859_1</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1870
       <entry><literal>LATIN1</literal></entry>
T
Tatsuo Ishii 已提交
1871 1872 1873
      </row>

      <row>
1874 1875
       <entry><literal>utf8_to_iso_8859_10</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1876
       <entry><literal>LATIN6</literal></entry>
T
Tatsuo Ishii 已提交
1877 1878 1879
      </row>

      <row>
1880 1881
       <entry><literal>utf8_to_iso_8859_13</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1882
       <entry><literal>LATIN7</literal></entry>
T
Tatsuo Ishii 已提交
1883 1884 1885
      </row>

      <row>
1886 1887
       <entry><literal>utf8_to_iso_8859_14</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1888
       <entry><literal>LATIN8</literal></entry>
T
Tatsuo Ishii 已提交
1889 1890 1891
      </row>

      <row>
1892 1893
       <entry><literal>utf8_to_iso_8859_15</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1894
       <entry><literal>LATIN9</literal></entry>
T
Tatsuo Ishii 已提交
1895 1896 1897
      </row>

      <row>
1898 1899
       <entry><literal>utf8_to_iso_8859_16</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1900
       <entry><literal>LATIN10</literal></entry>
T
Tatsuo Ishii 已提交
1901 1902 1903
      </row>

      <row>
1904 1905
       <entry><literal>utf8_to_iso_8859_2</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1906
       <entry><literal>LATIN2</literal></entry>
T
Tatsuo Ishii 已提交
1907 1908 1909
      </row>

      <row>
1910 1911
       <entry><literal>utf8_to_iso_8859_3</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1912
       <entry><literal>LATIN3</literal></entry>
T
Tatsuo Ishii 已提交
1913 1914 1915
      </row>

      <row>
1916 1917
       <entry><literal>utf8_to_iso_8859_4</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1918
       <entry><literal>LATIN4</literal></entry>
T
Tatsuo Ishii 已提交
1919 1920 1921
      </row>

      <row>
1922 1923
       <entry><literal>utf8_to_iso_8859_5</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1924
       <entry><literal>ISO_8859_5</literal></entry>
T
Tatsuo Ishii 已提交
1925 1926 1927
      </row>

      <row>
1928 1929
       <entry><literal>utf8_to_iso_8859_6</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1930
       <entry><literal>ISO_8859_6</literal></entry>
T
Tatsuo Ishii 已提交
1931 1932 1933
      </row>

      <row>
1934 1935
       <entry><literal>utf8_to_iso_8859_7</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1936
       <entry><literal>ISO_8859_7</literal></entry>
T
Tatsuo Ishii 已提交
1937 1938 1939
      </row>

      <row>
1940 1941
       <entry><literal>utf8_to_iso_8859_8</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1942
       <entry><literal>ISO_8859_8</literal></entry>
T
Tatsuo Ishii 已提交
1943 1944 1945
      </row>

      <row>
1946 1947
       <entry><literal>utf8_to_iso_8859_9</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1948
       <entry><literal>LATIN5</literal></entry>
T
Tatsuo Ishii 已提交
1949 1950 1951
      </row>

      <row>
1952 1953
       <entry><literal>utf8_to_johab</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1954
       <entry><literal>JOHAB</literal></entry>
T
Tatsuo Ishii 已提交
1955 1956 1957
      </row>

      <row>
1958 1959
       <entry><literal>utf8_to_koi8_r</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1960
       <entry><literal>KOI8</literal></entry>
1961 1962 1963
      </row>

      <row>
1964 1965
       <entry><literal>utf8_to_sjis</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1966
       <entry><literal>SJIS</literal></entry>
T
Tatsuo Ishii 已提交
1967 1968 1969
      </row>

      <row>
1970 1971 1972
       <entry><literal>utf8_to_tcvn</literal></entry>
       <entry><literal>UTF8</literal></entry>
       <entry><literal>WIN1258</literal></entry>
T
Tatsuo Ishii 已提交
1973 1974 1975
      </row>

      <row>
1976 1977
       <entry><literal>utf8_to_uhc</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1978
       <entry><literal>UHC</literal></entry>
T
Tatsuo Ishii 已提交
1979 1980
      </row>

1981
      <row>
1982 1983
       <entry><literal>utf8_to_windows_1250</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
1984
       <entry><literal>WIN1250</literal></entry>
1985 1986 1987
      </row>

      <row>
1988 1989 1990
       <entry><literal>utf8_to_windows_1251</literal></entry>
       <entry><literal>UTF8</literal></entry>
       <entry><literal>WIN1251</literal></entry>
1991 1992
      </row>

1993 1994 1995 1996 1997 1998
      <row>
       <entry><literal>utf8_to_windows_1252</literal></entry>
       <entry><literal>UTF8</literal></entry>
       <entry><literal>WIN1252</literal></entry>
      </row>

1999
      <row>
2000 2001
       <entry><literal>utf8_to_windows_1256</literal></entry>
       <entry><literal>UTF8</literal></entry>
P
Peter Eisentraut 已提交
2002
       <entry><literal>WIN1256</literal></entry>
2003 2004 2005
      </row>

      <row>
2006 2007 2008
       <entry><literal>utf8_to_windows_866</literal></entry>
       <entry><literal>UTF8</literal></entry>
       <entry><literal>WIN866</literal></entry>
2009 2010 2011
      </row>

      <row>
2012 2013
       <entry><literal>utf8_to_windows_874</literal></entry>
       <entry><literal>UTF8</literal></entry>
2014
       <entry><literal>WIN874</literal></entry>
2015
      </row>
2016

2017
      <row>
2018
       <entry><literal>windows_1250_to_iso_8859_2</literal></entry>
2019 2020
       <entry><literal>WIN1250</literal></entry>
       <entry><literal>LATIN2</literal></entry>
2021
      </row>
2022

2023
      <row>
2024
       <entry><literal>windows_1250_to_mic</literal></entry>
2025 2026
       <entry><literal>WIN1250</literal></entry>
       <entry><literal>MULE_INTERNAL</literal></entry>
2027 2028 2029
      </row>

      <row>
2030
       <entry><literal>windows_1250_to_utf8</literal></entry>
2031
       <entry><literal>WIN1250</literal></entry>
2032
       <entry><literal>UTF8</literal></entry>
2033 2034
      </row>

2035
      <row>
2036
       <entry><literal>windows_1251_to_iso_8859_5</literal></entry>
2037
       <entry><literal>WIN1251</literal></entry>
2038
       <entry><literal>ISO_8859_5</literal></entry>
2039
      </row>
2040

2041
      <row>
2042
       <entry><literal>windows_1251_to_koi8_r</literal></entry>
2043
       <entry><literal>WIN1251</literal></entry>
2044
       <entry><literal>KOI8</literal></entry>
2045 2046
      </row>

2047
      <row>
2048
       <entry><literal>windows_1251_to_mic</literal></entry>
2049
       <entry><literal>WIN1251</literal></entry>
2050
       <entry><literal>MULE_INTERNAL</literal></entry>
2051
      </row>
2052

2053
      <row>
2054 2055 2056
       <entry><literal>windows_1251_to_utf8</literal></entry>
       <entry><literal>WIN1251</literal></entry>
       <entry><literal>UTF8</literal></entry>
2057
      </row>
2058

2059
      <row>
2060
       <entry><literal>windows_1251_to_windows_866</literal></entry>
2061 2062
       <entry><literal>WIN1251</literal></entry>
       <entry><literal>WIN866</literal></entry>
2063 2064
      </row>

2065 2066 2067 2068 2069 2070
      <row>
       <entry><literal>windows_1252_to_utf8</literal></entry>
       <entry><literal>WIN1252</literal></entry>
       <entry><literal>UTF8</literal></entry>
      </row>

2071
      <row>
2072
       <entry><literal>windows_1256_to_utf8</literal></entry>
2073
       <entry><literal>WIN1256</literal></entry>
2074
       <entry><literal>UTF8</literal></entry>
2075
      </row>
2076

2077
      <row>
2078
       <entry><literal>windows_866_to_iso_8859_5</literal></entry>
2079
       <entry><literal>WIN866</literal></entry>
2080
       <entry><literal>ISO_8859_5</literal></entry>
2081
      </row>
2082

2083
      <row>
2084
       <entry><literal>windows_866_to_koi8_r</literal></entry>
2085
       <entry><literal>WIN866</literal></entry>
2086
       <entry><literal>KOI8</literal></entry>
2087
      </row>
2088

2089
      <row>
2090
       <entry><literal>windows_866_to_mic</literal></entry>
2091
       <entry><literal>WIN866</literal></entry>
2092
       <entry><literal>MULE_INTERNAL</literal></entry>
2093 2094
      </row>

2095
      <row>
2096 2097 2098
       <entry><literal>windows_866_to_utf8</literal></entry>
       <entry><literal>WIN866</literal></entry>
       <entry><literal>UTF8</literal></entry>
2099 2100 2101
      </row>

      <row>
2102
       <entry><literal>windows_866_to_windows_1251</literal></entry>
2103
       <entry><literal>WIN866</literal></entry>
2104 2105 2106 2107
       <entry><literal>WIN</literal></entry>
      </row>

      <row>
2108
       <entry><literal>windows_874_to_utf8</literal></entry>
2109
       <entry><literal>WIN874</literal></entry>
2110
       <entry><literal>UTF8</literal></entry>
2111 2112
      </row>

2113 2114 2115
     </tbody>
    </tgroup>
   </table>
2116

2117
  </sect1>
2118

2119

2120 2121
  <sect1 id="functions-binarystring">
   <title>Binary String Functions and Operators</title>
2122

P
Peter Eisentraut 已提交
2123 2124 2125 2126 2127
   <indexterm zone="functions-binarystring">
    <primary>binary data</primary>
    <secondary>functions</secondary>
   </indexterm>

2128 2129
   <para>
    This section describes functions and operators for examining and
2130
    manipulating values of type <type>bytea</type>.
2131
   </para>
2132

2133 2134 2135
   <para>
    <acronym>SQL</acronym> defines some string functions with a
    special syntax where 
2136
    certain key words rather than commas are used to separate the
2137 2138 2139 2140 2141 2142
    arguments.  Details are in
    <xref linkend="functions-binarystring-sql">.
    Some functions are also implemented using the regular syntax for
    function invocation.
    (See <xref linkend="functions-binarystring-other">.)
   </para>
2143

2144 2145 2146 2147 2148 2149 2150 2151 2152 2153 2154 2155
   <table id="functions-binarystring-sql">
    <title><acronym>SQL</acronym> Binary String Functions and Operators</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>  
      </row>
     </thead>
2156

2157 2158
     <tbody>
      <row>
2159 2160
       <entry><literal><parameter>string</parameter> <literal>||</literal>
        <parameter>string</parameter></literal></entry>
2161 2162
       <entry> <type>bytea</type> </entry>
       <entry>
2163
        String concatenation
2164
        <indexterm>
P
Peter Eisentraut 已提交
2165
         <primary>binary string</primary>
2166 2167 2168
         <secondary>concatenation</secondary>
        </indexterm>
       </entry>
2169 2170
       <entry><literal>'\\\\Post'::bytea || '\\047gres\\000'::bytea</literal></entry>
       <entry><literal>\\Post'gres\000</literal></entry>
2171
      </row>
2172

2173
      <row>
2174
       <entry><literal><function>octet_length</function>(<parameter>string</parameter>)</literal></entry>
2175
       <entry><type>integer</type></entry>
2176
       <entry>Number of bytes in binary string</entry>
2177
       <entry><literal>octet_length( 'jo\\000se'::bytea)</literal></entry>
2178 2179
       <entry><literal>5</literal></entry>
      </row>
2180

2181
      <row>
2182
       <entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
2183
       <entry><type>integer</type></entry>
2184
       <entry>Location of specified substring</entry>
2185 2186 2187
      <entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
       <entry><literal>3</literal></entry>
      </row>
2188

2189
      <row>
2190
       <entry><literal><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</literal></entry>
2191 2192
       <entry><type>bytea</type></entry>
       <entry>
2193
        Extract substring
2194 2195 2196 2197 2198 2199 2200
        <indexterm>
         <primary>substring</primary>
        </indexterm>
       </entry>
       <entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
       <entry><literal>h\000o</literal></entry>
      </row>
2201

2202 2203
      <row>
       <entry>
2204 2205 2206
        <literal><function>trim</function>(<optional>both</optional>
        <parameter>bytes</parameter> from
        <parameter>string</parameter>)</literal>
2207 2208 2209
       </entry>
       <entry><type>bytea</type></entry>
       <entry>
2210 2211 2212
        Remove the longest string containing only the bytes in
        <parameter>bytes</parameter> from the start
        and end of <parameter>string</parameter>
2213 2214 2215 2216
       </entry>
       <entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
       <entry><literal>Tom</literal></entry>
      </row>
2217 2218 2219 2220 2221 2222 2223 2224 2225 2226 2227 2228 2229 2230 2231

      <row>
       <entry><function>get_byte</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>
        Extract byte from string.
        <indexterm>
         <primary>get_byte</primary>
        </indexterm>
       </entry>
       <entry><literal>get_byte('Th\\000omas'::bytea, 4)</literal></entry>
       <entry><literal>109</literal></entry>
      </row>

      <row>
2232 2233
       <entry><function>set_byte</function>(<parameter>string</parameter>,
       <parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2234 2235 2236 2237 2238 2239 2240 2241 2242 2243 2244 2245 2246 2247 2248 2249 2250 2251 2252 2253 2254 2255 2256 2257 2258
       <entry><type>bytea</type></entry>
       <entry>
        Set byte in string.
        <indexterm>
         <primary>set_byte</primary>
        </indexterm>
       </entry>
       <entry><literal>set_byte('Th\\000omas'::bytea, 4, 64)</literal></entry>
       <entry><literal>Th\000o@as</literal></entry>
      </row>

      <row>
       <entry><function>get_bit</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
       <entry><type>integer</type></entry>
       <entry>
        Extract bit from string.
        <indexterm>
         <primary>get_bit</primary>
        </indexterm>
       </entry>
       <entry><literal>get_bit('Th\\000omas'::bytea, 45)</literal></entry>
       <entry><literal>1</literal></entry>
      </row>

      <row>
2259 2260
       <entry><function>set_bit</function>(<parameter>string</parameter>,
       <parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2261 2262 2263 2264 2265 2266 2267 2268 2269 2270
       <entry><type>bytea</type></entry>
       <entry>
        Set bit in string.
        <indexterm>
         <primary>set_bit</primary>
        </indexterm>
       </entry>
       <entry><literal>set_bit('Th\\000omas'::bytea, 45, 0)</literal></entry>
       <entry><literal>Th\000omAs</literal></entry>
      </row>
2271 2272 2273
     </tbody>
    </tgroup>
   </table>
2274

2275
   <para>
2276 2277 2278 2279 2280
    Additional binary string manipulation functions are available and
    are listed in <xref linkend="functions-binarystring-other">.  Some
    of them are used internally to implement the
    <acronym>SQL</acronym>-standard string functions listed in <xref
    linkend="functions-binarystring-sql">.
2281
   </para>
2282

2283 2284 2285 2286 2287 2288 2289 2290 2291 2292 2293 2294
   <table id="functions-binarystring-other">
    <title>Other Binary String Functions</title>
    <tgroup cols="5">
     <thead>
      <row>
       <entry>Function</entry>
       <entry>Return Type</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>
2295

2296 2297
     <tbody>
      <row>
2298
       <entry><literal><function>btrim</function>(<parameter>string</parameter>
N
Neil Conway 已提交
2299
        <type>bytea</type>, <parameter>bytes</parameter> <type>bytea</type>)</literal></entry>
2300 2301
       <entry><type>bytea</type></entry>
       <entry>
2302 2303
        Remove the longest string consisting only of bytes
        in <parameter>bytes</parameter> from the start and end of
2304
        <parameter>string</parameter>.
2305
      </entry>
2306
      <entry><literal>btrim('\\000trim\\000'::bytea, '\\000'::bytea)</literal></entry>
2307 2308 2309 2310
      <entry><literal>trim</literal></entry>
     </row>

     <row>
2311
      <entry><literal><function>length</function>(<parameter>string</parameter>)</literal></entry>
2312 2313
      <entry><type>integer</type></entry>
      <entry>
2314
       Length of binary string
2315
       <indexterm>
P
Peter Eisentraut 已提交
2316
        <primary>binary string</primary>
2317
        <secondary>length</secondary>
2318 2319 2320
       </indexterm>
       <indexterm>
        <primary>length</primary>
2321 2322
        <secondary sortas="binary string">of a binary string</secondary>
        <see>binary strings, length</see>
2323 2324
       </indexterm>
      </entry>
2325 2326
      <entry><literal>length('jo\\000se'::bytea)</literal></entry>
      <entry><literal>5</literal></entry>
2327 2328 2329 2330
     </row>

     <row>
      <entry>
2331 2332
       <literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
              <parameter>type</parameter> <type>text</type>)</literal>
2333
      </entry>
2334
      <entry><type>bytea</type></entry>
2335
      <entry>
2336 2337
       Decode binary string from <parameter>string</parameter> previously 
       encoded with <literal>encode</>.  Parameter type is same as in <literal>encode</>.
2338
      </entry>
2339
      <entry><literal>decode('123\\000456', 'escape')</literal></entry>
2340 2341 2342 2343 2344
      <entry><literal>123\000456</literal></entry>
     </row>       

     <row>
      <entry>
2345 2346
       <literal><function>encode</function>(<parameter>string</parameter> <type>bytea</type>,
              <parameter>type</parameter> <type>text</type>)</literal>
2347
      </entry>
2348
      <entry><type>text</type></entry>
2349
      <entry>
2350 2351
       Encode binary string to <acronym>ASCII</acronym>-only representation.  Supported
       types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
2352
      </entry>
2353
      <entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
2354 2355 2356 2357 2358 2359 2360 2361 2362 2363
      <entry><literal>123\000456</literal></entry>
     </row>       

    </tbody>
   </tgroup>
  </table>

 </sect1>


2364 2365 2366 2367 2368 2369 2370 2371 2372 2373 2374 2375 2376 2377 2378 2379 2380 2381 2382 2383 2384 2385 2386 2387 2388 2389 2390 2391 2392 2393 2394 2395 2396 2397 2398 2399 2400 2401 2402 2403 2404 2405 2406 2407 2408 2409 2410 2411 2412 2413 2414 2415 2416 2417 2418 2419 2420 2421 2422 2423 2424 2425 2426 2427 2428 2429 2430 2431 2432 2433 2434 2435 2436 2437 2438 2439 2440 2441 2442 2443 2444 2445 2446 2447 2448 2449 2450 2451 2452 2453 2454 2455 2456 2457 2458 2459 2460 2461 2462 2463 2464 2465 2466 2467 2468 2469 2470 2471 2472 2473 2474 2475 2476
  <sect1 id="functions-bitstring">
   <title>Bit String Functions and Operators</title>

   <indexterm zone="functions-bitstring">
    <primary>bit strings</primary>
    <secondary>functions</secondary>
   </indexterm>

   <para>
    This section describes functions and operators for examining and
    manipulating bit strings, that is values of the types
    <type>bit</type> and <type>bit varying</type>.  Aside from the
    usual comparison operators, the operators
    shown in <xref linkend="functions-bit-string-op-table"> can be used.
    Bit string operands of <literal>&amp;</literal>, <literal>|</literal>,
    and <literal>#</literal> must be of equal length.  When bit
    shifting, the original length of the string is preserved, as shown
    in the examples.
   </para>

   <table id="functions-bit-string-op-table">
    <title>Bit String Operators</title>

    <tgroup cols="4">
     <thead>
      <row>
       <entry>Operator</entry>
       <entry>Description</entry>
       <entry>Example</entry>
       <entry>Result</entry>
      </row>
     </thead>

     <tbody>
      <row>
       <entry> <literal>||</literal> </entry>
       <entry>concatenation</entry>
       <entry><literal>B'10001' || B'011'</literal></entry>
       <entry><literal>10001011</literal></entry>
      </row>

      <row>
       <entry> <literal>&amp;</literal> </entry>
       <entry>bitwise AND</entry>
       <entry><literal>B'10001' &amp; B'01101'</literal></entry>
       <entry><literal>00001</literal></entry>
      </row>

      <row>
       <entry> <literal>|</literal> </entry>
       <entry>bitwise OR</entry>
       <entry><literal>B'10001' | B'01101'</literal></entry>
       <entry><literal>11101</literal></entry>
      </row>

      <row>
       <entry> <literal>#</literal> </entry>
       <entry>bitwise XOR</entry>
       <entry><literal>B'10001' # B'01101'</literal></entry>
       <entry><literal>11100</literal></entry>
      </row>

      <row>
       <entry> <literal>~</literal> </entry>
       <entry>bitwise NOT</entry>
       <entry><literal>~ B'10001'</literal></entry>
       <entry><literal>01110</literal></entry>
      </row>

      <row>
       <entry> <literal>&lt;&lt;</literal> </entry>
       <entry>bitwise shift left</entry>
       <entry><literal>B'10001' &lt;&lt; 3</literal></entry>
       <entry><literal>01000</literal></entry>
      </row>

      <row>
       <entry> <literal>&gt;&gt;</literal> </entry>
       <entry>bitwise shift right</entry>
       <entry><literal>B'10001' &gt;&gt; 2</literal></entry>
       <entry><literal>00100</literal></entry>
      </row>
     </tbody>
    </tgroup>
   </table>

   <para>
    The following <acronym>SQL</acronym>-standard functions work on bit
    strings as well as character strings:
    <literal><function>length</function></literal>,
    <literal><function>bit_length</function></literal>,
    <literal><function>octet_length</function></literal>,
    <literal><function>position</function></literal>,
    <literal><function>substring</function></literal>.
   </para>

   <para>
    In addition, it is possible to cast integral values to and from type
    <type>bit</>.
    Some examples:
<programlisting>
44::bit(10)                    <lineannotation>0000101100</lineannotation>
44::bit(3)                     <lineannotation>100</lineannotation>
cast(-44 as bit(12))           <lineannotation>111111010100</lineannotation>
'1110'::bit(4)::integer        <lineannotation>14</lineannotation>
</programlisting>
    Note that casting to just <quote>bit</> means casting to
    <literal>bit(1)</>, and so it will deliver only the least significant
    bit of the integer.
   </para>

    <note>
     <para>
2477
      Prior to <productname>PostgreSQL</productname> 8.0, casting an
2478 2479 2480 2481 2482 2483 2484 2485 2486 2487
      integer to <type>bit(n)</> would copy the leftmost <literal>n</>
      bits of the integer, whereas now it copies the rightmost <literal>n</>
      bits.  Also, casting an integer to a bit string width wider than
      the integer itself will sign-extend on the left.
     </para>
    </note>

  </sect1>


2488 2489 2490
 <sect1 id="functions-matching">
  <title>Pattern Matching</title>

2491 2492 2493 2494
  <indexterm zone="functions-matching">
   <primary>pattern matching</primary>
  </indexterm>

2495
   <para>
2496 2497 2498
    There are three separate approaches to pattern matching provided
    by <productname>PostgreSQL</productname>: the traditional
    <acronym>SQL</acronym> <function>LIKE</function> operator, the
T
Tom Lane 已提交
2499
    more recent <function>SIMILAR TO</function> operator (added in
2500
    SQL:1999), and <acronym>POSIX</acronym>-style regular expressions.
2501
    Additionally, a pattern matching function,
2502
    <function>substring</function>, is available, using either
T
Tom Lane 已提交
2503
    <function>SIMILAR TO</function>-style or POSIX-style regular
2504
    expressions.
2505 2506 2507 2508 2509 2510 2511 2512
   </para>

   <tip>
    <para>
     If you have pattern matching needs that go beyond this,
     consider writing a user-defined function in Perl or Tcl.
    </para>
   </tip>
2513

2514
  <sect2 id="functions-like">
2515
   <title><function>LIKE</function></title>
2516

P
Peter Eisentraut 已提交
2517 2518
   <indexterm zone="functions-like">
    <primary>LIKE</primary>
2519 2520
   </indexterm>

2521
<synopsis>
2522 2523
<replaceable>string</replaceable> LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
<replaceable>string</replaceable> NOT LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2524
</synopsis>
2525

2526 2527 2528 2529 2530 2531 2532 2533 2534 2535 2536
    <para>
     Every <replaceable>pattern</replaceable> defines a set of strings.
     The <function>LIKE</function> expression returns true if the
     <replaceable>string</replaceable> is contained in the set of
     strings represented by <replaceable>pattern</replaceable>.  (As
     expected, the <function>NOT LIKE</function> expression returns
     false if <function>LIKE</function> returns true, and vice versa.
     An equivalent expression is
     <literal>NOT (<replaceable>string</replaceable> LIKE
      <replaceable>pattern</replaceable>)</literal>.)
    </para>
2537 2538

    <para>
2539 2540 2541 2542 2543 2544 2545 2546 2547
     If <replaceable>pattern</replaceable> does not contain percent
     signs or underscore, then the pattern only represents the string
     itself; in that case <function>LIKE</function> acts like the
     equals operator.  An underscore (<literal>_</literal>) in
     <replaceable>pattern</replaceable> stands for (matches) any single
     character; a percent sign (<literal>%</literal>) matches any string
     of zero or more characters.
    </para>

2548 2549 2550
   <para>
    Some examples:
<programlisting>
2551 2552 2553 2554
'abc' LIKE 'abc'    <lineannotation>true</lineannotation>
'abc' LIKE 'a%'     <lineannotation>true</lineannotation>
'abc' LIKE '_b_'    <lineannotation>true</lineannotation>
'abc' LIKE 'c'      <lineannotation>false</lineannotation>
2555 2556 2557
</programlisting>
   </para>
   
T
Thomas G. Lockhart 已提交
2558
   <para>
2559
    <function>LIKE</function> pattern matches always cover the entire
T
Tom Lane 已提交
2560
    string.  To match a sequence anywhere within a string, the
2561
    pattern must therefore start and end with a percent sign.
T
Thomas G. Lockhart 已提交
2562
   </para>
2563 2564

   <para>
2565 2566 2567 2568
    To match a literal underscore or percent sign without matching
    other characters, the respective character in
    <replaceable>pattern</replaceable> must be 
    preceded by the escape character.  The default escape
2569
    character is the backslash but a different one may be selected by
2570 2571 2572 2573 2574 2575 2576
    using the <literal>ESCAPE</literal> clause.  To match the escape
    character itself, write two escape characters.
   </para>

   <para>
    Note that the backslash already has a special meaning in string
    literals, so to write a pattern constant that contains a backslash
2577
    you must write two backslashes in an SQL statement.  Thus, writing a pattern
2578
    that actually matches a literal backslash means writing four backslashes
2579 2580
    in the statement.  You can avoid this by selecting a different escape
    character with <literal>ESCAPE</literal>; then a backslash is not special
2581
    to <function>LIKE</function> anymore. (But it is still special to the string
2582 2583 2584 2585 2586
    literal parser, so you still need two of them.)
   </para>

   <para>
    It's also possible to select no escape character by writing
2587
    <literal>ESCAPE ''</literal>.  This effectively disables the
B
Bruce Momjian 已提交
2588 2589
    escape mechanism, which makes it impossible to turn off the
    special meaning of underscore and percent signs in the pattern.
2590
   </para>
2591 2592

   <para>
2593
    The key word <token>ILIKE</token> can be used instead of
T
Tom Lane 已提交
2594
    <token>LIKE</token> to make the match case-insensitive according
2595
    to the active locale.  This is not in the <acronym>SQL</acronym> standard but is a
2596
    <productname>PostgreSQL</productname> extension.
T
Thomas G. Lockhart 已提交
2597
   </para>
2598

2599 2600
   <para>
    The operator <literal>~~</literal> is equivalent to
2601 2602 2603
    <function>LIKE</function>, and <literal>~~*</literal> corresponds to
    <function>ILIKE</function>.  There are also
    <literal>!~~</literal> and <literal>!~~*</literal> operators that
2604
    represent <function>NOT LIKE</function> and <function>NOT
2605
    ILIKE</function>, respectively.  All of these operators are
2606
    <productname>PostgreSQL</productname>-specific.
2607 2608
   </para>
  </sect2>
2609

2610

2611 2612
  <sect2 id="functions-similarto-regexp">
   <title><function>SIMILAR TO</function> Regular Expressions</title>
2613

2614
   <indexterm zone="functions-similarto-regexp">
P
Peter Eisentraut 已提交
2615
    <primary>regular expression</primary>
2616
    <!-- <seealso>pattern matching</seealso> breaks index build -->
2617 2618 2619
   </indexterm>

   <indexterm>
P
Peter Eisentraut 已提交
2620
    <primary>SIMILAR TO</primary>
2621 2622 2623 2624 2625 2626
   </indexterm>

   <indexterm>
    <primary>substring</primary>
   </indexterm>

2627
<synopsis>
2628 2629
<replaceable>string</replaceable> SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
<replaceable>string</replaceable> NOT SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2630
</synopsis>
2631 2632

    <para>
2633 2634 2635 2636 2637 2638 2639
     The <function>SIMILAR TO</function> operator returns true or
     false depending on whether its pattern matches the given string.
     It is much like <function>LIKE</function>, except that it
     interprets the pattern using the SQL standard's definition of a
     regular expression.  SQL regular expressions are a curious cross
     between <function>LIKE</function> notation and common regular
     expression notation.
2640 2641 2642 2643 2644 2645 2646 2647 2648
    </para>

    <para>
     Like <function>LIKE</function>, the  <function>SIMILAR TO</function>
     operator succeeds only if its pattern matches the entire string;
     this is unlike common regular expression practice, wherein the pattern
     may match any part of the string.
     Also like
     <function>LIKE</function>, <function>SIMILAR TO</function> uses
2649 2650 2651
     <literal>_</> and <literal>%</> as wildcard characters denoting
     any single character and any string, respectively (these are
     comparable to <literal>.</> and <literal>.*</> in POSIX regular
2652 2653 2654 2655 2656 2657 2658 2659 2660 2661 2662 2663 2664 2665 2666 2667 2668 2669 2670 2671 2672 2673 2674 2675 2676 2677 2678 2679 2680 2681 2682 2683 2684 2685 2686 2687 2688 2689 2690 2691 2692
     expressions).
    </para>

    <para>
     In addition to these facilities borrowed from <function>LIKE</function>,
     <function>SIMILAR TO</function> supports these pattern-matching
     metacharacters borrowed from POSIX regular expressions:

    <itemizedlist>
     <listitem>
      <para>
       <literal>|</literal> denotes alternation (either of two alternatives).
      </para>
     </listitem>
     <listitem>
      <para>
       <literal>*</literal> denotes repetition of the previous item zero
       or more times.
      </para>
     </listitem>
     <listitem>
      <para>
       <literal>+</literal> denotes repetition of the previous item one
       or more times.
      </para>
     </listitem>
     <listitem>
      <para>
       Parentheses <literal>()</literal> may be used to group items into
       a single logical item.
      </para>
     </listitem>
     <listitem>
      <para>
       A bracket expression <literal>[...]</literal> specifies a character
       class, just as in POSIX regular expressions.
      </para>
     </listitem>
    </itemizedlist>

     Notice that bounded repetition (<literal>?</> and <literal>{...}</>)
2693
     are not provided, though they exist in POSIX.  Also, the dot (<literal>.</>)
2694 2695 2696 2697 2698 2699 2700 2701 2702
     is not a metacharacter.
    </para>

    <para>
     As with <function>LIKE</>, a backslash disables the special meaning
     of any of these metacharacters; or a different escape character can
     be specified with <literal>ESCAPE</>.
    </para>

2703 2704 2705
   <para>
    Some examples:
<programlisting>
2706 2707 2708 2709
'abc' SIMILAR TO 'abc'      <lineannotation>true</lineannotation>
'abc' SIMILAR TO 'a'        <lineannotation>false</lineannotation>
'abc' SIMILAR TO '%(b|d)%'  <lineannotation>true</lineannotation>
'abc' SIMILAR TO '(b|c)%'   <lineannotation>false</lineannotation>
2710 2711
</programlisting>
   </para>
2712 2713

    <para>
2714 2715 2716 2717
     The <function>substring</> function with three parameters,
     <function>substring(<parameter>string</parameter> from
     <replaceable>pattern</replaceable> for
     <replaceable>escape-character</replaceable>)</function>, provides
2718
     extraction of a substring that matches an SQL
2719 2720 2721
     regular expression pattern.  As with <literal>SIMILAR TO</>, the
     specified pattern must match to the entire data string, else the
     function fails and returns null.  To indicate the part of the
2722 2723
     pattern that should be returned on success, the pattern must contain
     two occurrences of the escape character followed by a double quote
2724 2725
     (<literal>"</>).  The text matching the portion of the pattern
     between these markers is returned.
2726 2727
    </para>

2728 2729 2730
   <para>
    Some examples:
<programlisting>
2731 2732
substring('foobar' from '%#"o_b#"%' for '#')   <lineannotation>oob</lineannotation>
substring('foobar' from '#"o_b#"%' for '#')    <lineannotation>NULL</lineannotation>
2733 2734
</programlisting>
   </para>
2735 2736 2737 2738 2739 2740
  </sect2>

  <sect2 id="functions-posix-regexp">
   <title><acronym>POSIX</acronym> Regular Expressions</title>

   <indexterm zone="functions-posix-regexp">
P
Peter Eisentraut 已提交
2741
    <primary>regular expression</primary>
2742 2743 2744
    <seealso>pattern matching</seealso>
   </indexterm>

2745 2746 2747 2748 2749 2750
   <para>
    <xref linkend="functions-posix-table"> lists the available
    operators for pattern matching using POSIX regular expressions.
   </para>

   <table id="functions-posix-table">
2751 2752 2753 2754 2755 2756 2757 2758 2759 2760 2761
    <title>Regular Expression Match Operators</title>

    <tgroup cols="3">
     <thead>
      <row>
       <entry>Operator</entry>
       <entry>Description</entry>
       <entry>Example</entry>
      </row>
     </thead>

2762 2763 2764 2765 2766 2767
      <tbody>
       <row>
        <entry> <literal>~</literal> </entry>
        <entry>Matches regular expression, case sensitive</entry>
        <entry><literal>'thomas' ~ '.*thomas.*'</literal></entry>
       </row>
2768

2769 2770 2771 2772 2773
       <row>
        <entry> <literal>~*</literal> </entry>
        <entry>Matches regular expression, case insensitive</entry>
        <entry><literal>'thomas' ~* '.*Thomas.*'</literal></entry>
       </row>
2774

2775 2776 2777 2778 2779 2780 2781 2782 2783 2784 2785 2786 2787 2788 2789 2790 2791 2792
       <row>
        <entry> <literal>!~</literal> </entry>
        <entry>Does not match regular expression, case sensitive</entry>
        <entry><literal>'thomas' !~ '.*Thomas.*'</literal></entry>
       </row>

       <row>
        <entry> <literal>!~*</literal> </entry>
        <entry>Does not match regular expression, case insensitive</entry>
        <entry><literal>'thomas' !~* '.*vadim.*'</literal></entry>
       </row>
      </tbody>
     </tgroup>
    </table>

    <para>
     <acronym>POSIX</acronym> regular expressions provide a more
     powerful means for 
2793 2794
     pattern matching than the <function>LIKE</function> and
     <function>SIMILAR TO</> operators.
2795 2796 2797 2798 2799 2800 2801 2802 2803 2804 2805 2806
     Many Unix tools such as <command>egrep</command>,
     <command>sed</command>, or <command>awk</command> use a pattern
     matching language that is similar to the one described here.
    </para>

    <para>
     A regular expression is a character sequence that is an
     abbreviated definition of a set of strings (a <firstterm>regular
      set</firstterm>).  A string is said to match a regular expression
     if it is a member of the regular set described by the regular
     expression.  As with <function>LIKE</function>, pattern characters
     match string characters exactly unless they are special characters
2807
     in the regular expression language &mdash; but regular expressions use
2808 2809 2810 2811 2812 2813
     different special characters than <function>LIKE</function> does.
     Unlike <function>LIKE</function> patterns, a
     regular expression is allowed to match anywhere within a string, unless
     the regular expression is explicitly anchored to the beginning or
     end of the string.
    </para>
2814

2815 2816 2817
   <para>
    Some examples:
<programlisting>
2818 2819 2820 2821
'abc' ~ 'abc'    <lineannotation>true</lineannotation>
'abc' ~ '^a'     <lineannotation>true</lineannotation>
'abc' ~ '(b|d)'  <lineannotation>true</lineannotation>
'abc' ~ '^(b|c)' <lineannotation>false</lineannotation>
2822 2823
</programlisting>
   </para>
2824 2825

    <para>
2826 2827
     The <function>substring</> function with two parameters,
     <function>substring(<parameter>string</parameter> from
2828 2829
     <replaceable>pattern</replaceable>)</function>, provides extraction of a substring
     that matches a POSIX regular expression pattern.  It returns null if
2830 2831 2832 2833
     there is no match, otherwise the portion of the text that matched the
     pattern.  But if the pattern contains any parentheses, the portion
     of the text that matched the first parenthesized subexpression (the
     one whose left parenthesis comes first) is
T
Tom Lane 已提交
2834
     returned.  You can put parentheses around the whole expression
2835
     if you want to use parentheses within it without triggering this
T
Tom Lane 已提交
2836 2837 2838
     exception.  If you need parentheses in the pattern before the
     subexpression you want to extract, see the non-capturing parentheses
     described below.
2839 2840
    </para>

2841 2842 2843
   <para>
    Some examples:
<programlisting>
2844 2845
substring('foobar' from 'o.b')     <lineannotation>oob</lineannotation>
substring('foobar' from 'o(.)b')   <lineannotation>o</lineannotation>
2846 2847
</programlisting>
   </para>
2848

2849 2850 2851 2852 2853 2854 2855 2856 2857 2858 2859 2860
   <para>
    <productname>PostgreSQL</productname>'s regular expressions are implemented
    using a package written by Henry Spencer.  Much of
    the description of regular expressions below is copied verbatim from his
    manual entry.
   </para>

<!-- derived from the re_syntax.n man page -->

   <sect3 id="posix-syntax-details">
    <title>Regular Expression Details</title>

T
Thomas G. Lockhart 已提交
2861
   <para>
P
Peter Eisentraut 已提交
2862
    Regular expressions (<acronym>RE</acronym>s), as defined in
2863 2864 2865 2866 2867 2868 2869 2870 2871 2872 2873
    <acronym>POSIX</acronym> 1003.2, come in two forms:
    <firstterm>extended</> <acronym>RE</acronym>s or <acronym>ERE</>s
    (roughly those of <command>egrep</command>), and
    <firstterm>basic</> <acronym>RE</acronym>s or <acronym>BRE</>s
    (roughly those of <command>ed</command>).
    <productname>PostgreSQL</productname> supports both forms, and
    also implements some extensions
    that are not in the POSIX standard, but have become widely used anyway
    due to their availability in programming languages such as Perl and Tcl.
    <acronym>RE</acronym>s using these non-POSIX extensions are called
    <firstterm>advanced</> <acronym>RE</acronym>s or <acronym>ARE</>s
2874 2875 2876 2877 2878
    in this documentation.  AREs are almost an exact superset of EREs,
    but BREs have several notational incompatibilities (as well as being
    much more limited).
    We first describe the ARE and ERE forms, noting features that apply
    only to AREs, and then describe how BREs differ.
T
Thomas G. Lockhart 已提交
2879
   </para>
2880

2881 2882
   <note>
    <para>
2883 2884 2885 2886 2887 2888
     The form of regular expressions accepted by
     <productname>PostgreSQL</> can be chosen by setting the <xref
     linkend="guc-regex-flavor"> run-time parameter.  The usual
     setting is <literal>advanced</>, but one might choose
     <literal>extended</> for maximum backwards compatibility with
     pre-7.4 releases of <productname>PostgreSQL</>.
2889 2890 2891
    </para>
   </note>

T
Thomas G. Lockhart 已提交
2892
   <para>
2893
    A regular expression is defined as one or more
2894 2895 2896
    <firstterm>branches</firstterm>, separated by
    <literal>|</literal>.  It matches anything that matches one of the
    branches.
T
Thomas G. Lockhart 已提交
2897
   </para>
2898

2899
   <para>
2900 2901 2902 2903
    A branch is zero or more <firstterm>quantified atoms</> or
    <firstterm>constraints</>, concatenated.
    It matches a match for the first, followed by a match for the second, etc;
    an empty branch matches the empty string.
2904
   </para>
2905

B
Bruce Momjian 已提交
2906
   <para>
2907 2908 2909 2910 2911 2912 2913 2914
    A quantified atom is an <firstterm>atom</> possibly followed
    by a single <firstterm>quantifier</>.
    Without a quantifier, it matches a match for the atom.
    With a quantifier, it can match some number of matches of the atom.
    An <firstterm>atom</firstterm> can be any of the possibilities
    shown in <xref linkend="posix-atoms-table">.
    The possible quantifiers and their meanings are shown in
    <xref linkend="posix-quantifiers-table">.
B
Bruce Momjian 已提交
2915
   </para>
2916

2917
   <para>
2918 2919 2920 2921 2922 2923 2924 2925 2926 2927 2928 2929 2930 2931 2932 2933 2934 2935 2936 2937 2938 2939 2940 2941 2942 2943 2944 2945 2946 2947 2948 2949 2950 2951 2952 2953 2954 2955 2956 2957 2958 2959 2960 2961 2962 2963 2964 2965 2966 2967 2968 2969 2970 2971 2972 2973 2974 2975 2976 2977 2978 2979 2980 2981 2982 2983 2984 2985 2986 2987 2988 2989 2990 2991 2992 2993 2994 2995 2996 2997 2998
    A <firstterm>constraint</> matches an empty string, but matches only when
    specific conditions are met.  A constraint can be used where an atom
    could be used, except it may not be followed by a quantifier.
    The simple constraints are shown in
    <xref linkend="posix-constraints-table">;
    some more constraints are described later.
   </para>


   <table id="posix-atoms-table">
    <title>Regular Expression Atoms</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Atom</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>(</><replaceable>re</><literal>)</> </entry>
       <entry> (where <replaceable>re</> is any regular expression)
       matches a match for
       <replaceable>re</>, with the match noted for possible reporting </entry>
       </row>

       <row>
       <entry> <literal>(?:</><replaceable>re</><literal>)</> </entry>
       <entry> as above, but the match is not noted for reporting
       (a <quote>non-capturing</> set of parentheses)
       (AREs only) </entry>
       </row>

       <row>
       <entry> <literal>.</> </entry>
       <entry> matches any single character </entry>
       </row>

       <row>
       <entry> <literal>[</><replaceable>chars</><literal>]</> </entry>
       <entry> a <firstterm>bracket expression</>,
       matching any one of the <replaceable>chars</> (see
       <xref linkend="posix-bracket-expressions"> for more detail) </entry>
       </row>

       <row>
       <entry> <literal>\</><replaceable>k</> </entry>
       <entry> (where <replaceable>k</> is a non-alphanumeric character)
       matches that character taken as an ordinary character,
       e.g. <literal>\\</> matches a backslash character </entry>
       </row>

       <row>
       <entry> <literal>\</><replaceable>c</> </entry>
       <entry> where <replaceable>c</> is alphanumeric
       (possibly followed by other characters)
       is an <firstterm>escape</>, see <xref linkend="posix-escape-sequences">
       (AREs only; in EREs and BREs, this matches <replaceable>c</>) </entry>
       </row>

       <row>
       <entry> <literal>{</> </entry>
       <entry> when followed by a character other than a digit,
       matches the left-brace character <literal>{</>;
       when followed by a digit, it is the beginning of a
       <replaceable>bound</> (see below) </entry>
       </row>

       <row>
       <entry> <replaceable>x</> </entry>
       <entry> where <replaceable>x</> is a single character with no other
       significance, matches that character </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    An RE may not end with <literal>\</>.
2999 3000 3001 3002
   </para>

   <note>
    <para>
3003 3004 3005
     Remember that the backslash (<literal>\</literal>) already has a special
     meaning in <productname>PostgreSQL</> string literals.
     To write a pattern constant that contains a backslash,
3006
     you must write two backslashes in the statement.
3007
    </para>
3008 3009 3010 3011 3012 3013 3014 3015 3016 3017 3018 3019 3020 3021 3022 3023 3024 3025 3026 3027 3028 3029 3030 3031 3032 3033 3034 3035 3036 3037 3038 3039 3040 3041 3042 3043 3044 3045 3046 3047 3048 3049 3050 3051 3052 3053 3054 3055 3056 3057 3058 3059 3060 3061 3062 3063 3064 3065 3066 3067 3068 3069 3070 3071 3072 3073 3074 3075 3076 3077 3078 3079 3080 3081 3082 3083 3084 3085 3086 3087 3088 3089 3090
   </note>

   <table id="posix-quantifiers-table">
    <title>Regular Expression Quantifiers</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Quantifier</entry>
       <entry>Matches</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>*</> </entry>
       <entry> a sequence of 0 or more matches of the atom </entry>
       </row>

       <row>
       <entry> <literal>+</> </entry>
       <entry> a sequence of 1 or more matches of the atom </entry>
       </row>

       <row>
       <entry> <literal>?</> </entry>
       <entry> a sequence of 0 or 1 matches of the atom </entry>
       </row>

       <row>
       <entry> <literal>{</><replaceable>m</><literal>}</> </entry>
       <entry> a sequence of exactly <replaceable>m</> matches of the atom </entry>
       </row>

       <row>
       <entry> <literal>{</><replaceable>m</><literal>,}</> </entry>
       <entry> a sequence of <replaceable>m</> or more matches of the atom </entry>
       </row>

       <row>
       <entry>
       <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</> </entry>
       <entry> a sequence of <replaceable>m</> through <replaceable>n</>
       (inclusive) matches of the atom; <replaceable>m</> may not exceed
       <replaceable>n</> </entry>
       </row>

       <row>
       <entry> <literal>*?</> </entry>
       <entry> non-greedy version of <literal>*</> </entry>
       </row>

       <row>
       <entry> <literal>+?</> </entry>
       <entry> non-greedy version of <literal>+</> </entry>
       </row>

       <row>
       <entry> <literal>??</> </entry>
       <entry> non-greedy version of <literal>?</> </entry>
       </row>

       <row>
       <entry> <literal>{</><replaceable>m</><literal>}?</> </entry>
       <entry> non-greedy version of <literal>{</><replaceable>m</><literal>}</> </entry>
       </row>

       <row>
       <entry> <literal>{</><replaceable>m</><literal>,}?</> </entry>
       <entry> non-greedy version of <literal>{</><replaceable>m</><literal>,}</> </entry>
       </row>

       <row>
       <entry>
       <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</> </entry>
       <entry> non-greedy version of <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</> </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    The forms using <literal>{</><replaceable>...</><literal>}</>
T
Tom Lane 已提交
3091
    are known as <firstterm>bounds</>.
3092 3093 3094 3095 3096 3097 3098 3099 3100 3101 3102 3103 3104 3105 3106 3107
    The numbers <replaceable>m</> and <replaceable>n</> within a bound are
    unsigned decimal integers with permissible values from 0 to 255 inclusive.
   </para>

    <para>
     <firstterm>Non-greedy</> quantifiers (available in AREs only) match the
     same possibilities as their corresponding normal (<firstterm>greedy</>)
     counterparts, but prefer the smallest number rather than the largest
     number of matches.
     See <xref linkend="posix-matching-rules"> for more detail.
   </para>

   <note>
    <para>
     A quantifier cannot immediately follow another quantifier.
     A quantifier cannot
3108 3109 3110 3111
     begin an expression or subexpression or follow
     <literal>^</literal> or <literal>|</literal>.
    </para>
   </note>
3112

3113 3114 3115 3116 3117 3118 3119 3120 3121 3122 3123 3124 3125 3126 3127 3128 3129 3130 3131 3132 3133 3134 3135 3136 3137 3138 3139 3140 3141 3142 3143 3144 3145 3146 3147 3148 3149 3150 3151
   <table id="posix-constraints-table">
    <title>Regular Expression Constraints</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Constraint</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>^</> </entry>
       <entry> matches at the beginning of the string </entry>
       </row>

       <row>
       <entry> <literal>$</> </entry>
       <entry> matches at the end of the string </entry>
       </row>

       <row>
       <entry> <literal>(?=</><replaceable>re</><literal>)</> </entry>
       <entry> <firstterm>positive lookahead</> matches at any point
       where a substring matching <replaceable>re</> begins
       (AREs only) </entry>
       </row>

       <row>
       <entry> <literal>(?!</><replaceable>re</><literal>)</> </entry>
       <entry> <firstterm>negative lookahead</> matches at any point
       where no substring matching <replaceable>re</> begins
       (AREs only) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

3152
   <para>
3153 3154 3155
    Lookahead constraints may not contain <firstterm>back references</>
    (see <xref linkend="posix-escape-sequences">),
    and all parentheses within them are considered non-capturing.
3156
   </para>
3157 3158 3159 3160
   </sect3>

   <sect3 id="posix-bracket-expressions">
    <title>Bracket Expressions</title>
3161

T
Thomas G. Lockhart 已提交
3162
   <para>
3163 3164 3165 3166
    A <firstterm>bracket expression</firstterm> is a list of
    characters enclosed in <literal>[]</literal>.  It normally matches
    any single character from the list (but see below).  If the list
    begins with <literal>^</literal>, it matches any single character
3167 3168
    <emphasis>not</> from the rest of the list.
    If two characters
3169 3170 3171 3172 3173 3174
    in the list are separated by <literal>-</literal>, this is
    shorthand for the full range of characters between those two
    (inclusive) in the collating sequence,
    e.g. <literal>[0-9]</literal> in <acronym>ASCII</acronym> matches
    any decimal digit.  It is illegal for two ranges to share an
    endpoint, e.g.  <literal>a-c-e</literal>.  Ranges are very
3175
    collating-sequence-dependent, so portable programs should avoid
3176
    relying on them.
T
Thomas G. Lockhart 已提交
3177
   </para>
3178

T
Thomas G. Lockhart 已提交
3179
   <para>
3180 3181 3182 3183 3184 3185
    To include a literal <literal>]</literal> in the list, make it the
    first character (following a possible <literal>^</literal>).  To
    include a literal <literal>-</literal>, make it the first or last
    character, or the second endpoint of a range.  To use a literal
    <literal>-</literal> as the first endpoint of a range, enclose it
    in <literal>[.</literal> and <literal>.]</literal> to make it a
3186 3187 3188 3189 3190 3191 3192
    collating element (see below).  With the exception of these characters,
    some combinations using <literal>[</literal>
    (see next paragraphs), and escapes (AREs only), all other special
    characters lose their special significance within a bracket expression.
    In particular, <literal>\</literal> is not special when following
    ERE or BRE rules, though it is special (as introducing an escape)
    in AREs.
3193 3194 3195 3196
   </para>

   <para>
    Within a bracket expression, a collating element (a character, a
3197
    multiple-character sequence that collates as if it were a single
3198 3199 3200 3201
    character, or a collating-sequence name for either) enclosed in
    <literal>[.</literal> and <literal>.]</literal> stands for the
    sequence of characters of that collating element.  The sequence is
    a single element of the bracket expression's list.  A bracket
3202
    expression containing a multiple-character collating element can thus
3203 3204 3205 3206 3207 3208
    match more than one character, e.g. if the collating sequence
    includes a <literal>ch</literal> collating element, then the RE
    <literal>[[.ch.]]*c</literal> matches the first five characters of
    <literal>chchcc</literal>.
   </para>

3209 3210 3211 3212 3213 3214 3215
   <note>
    <para>
     <productname>PostgreSQL</> currently has no multi-character collating
     elements. This information describes possible future behavior.
    </para>
   </note>

3216 3217 3218 3219 3220 3221 3222 3223 3224 3225 3226 3227 3228 3229 3230 3231 3232 3233 3234 3235 3236 3237 3238 3239 3240 3241 3242 3243 3244 3245 3246 3247 3248
   <para>
    Within a bracket expression, a collating element enclosed in
    <literal>[=</literal> and <literal>=]</literal> is an equivalence
    class, standing for the sequences of characters of all collating
    elements equivalent to that one, including itself.  (If there are
    no other equivalent collating elements, the treatment is as if the
    enclosing delimiters were <literal>[.</literal> and
    <literal>.]</literal>.)  For example, if <literal>o</literal> and
    <literal>^</literal> are the members of an equivalence class, then
    <literal>[[=o=]]</literal>, <literal>[[=^=]]</literal>, and
    <literal>[o^]</literal> are all synonymous.  An equivalence class
    may not be an endpoint of a range.
   </para>

   <para>
    Within a bracket expression, the name of a character class
    enclosed in <literal>[:</literal> and <literal>:]</literal> stands
    for the list of all characters belonging to that class.  Standard
    character class names are: <literal>alnum</literal>,
    <literal>alpha</literal>, <literal>blank</literal>,
    <literal>cntrl</literal>, <literal>digit</literal>,
    <literal>graph</literal>, <literal>lower</literal>,
    <literal>print</literal>, <literal>punct</literal>,
    <literal>space</literal>, <literal>upper</literal>,
    <literal>xdigit</literal>.  These stand for the character classes
    defined in
    <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
    A locale may provide others.  A character class may not be used as
    an endpoint of a range.
   </para>

   <para>
    There are two special cases of bracket expressions:  the bracket
3249
    expressions <literal>[[:&lt;:]]</literal> and
3250 3251
    <literal>[[:&gt;:]]</literal> are constraints,
    matching empty strings at the beginning
3252
    and end of a word respectively.  A word is defined as a sequence
3253 3254 3255
    of word characters that is neither preceded nor followed by word
    characters.  A word character is an <literal>alnum</> character (as
    defined by
3256 3257
    <citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
    or an underscore.  This is an extension, compatible with but not
3258 3259 3260 3261 3262 3263 3264 3265 3266 3267 3268 3269 3270 3271 3272 3273 3274 3275 3276 3277 3278 3279 3280 3281 3282 3283 3284 3285 3286 3287 3288 3289 3290 3291 3292 3293 3294 3295 3296 3297 3298 3299 3300 3301 3302 3303 3304 3305 3306 3307 3308 3309 3310 3311 3312 3313 3314
    specified by <acronym>POSIX</acronym> 1003.2, and should be used with
    caution in software intended to be portable to other systems.
    The constraint escapes described below are usually preferable (they
    are no more standard, but are certainly easier to type).
   </para>
   </sect3>

   <sect3 id="posix-escape-sequences">
    <title>Regular Expression Escapes</title>

   <para>
    <firstterm>Escapes</> are special sequences beginning with <literal>\</>
    followed by an alphanumeric character. Escapes come in several varieties:
    character entry, class shorthands, constraint escapes, and back references.
    A <literal>\</> followed by an alphanumeric character but not constituting
    a valid escape is illegal in AREs.
    In EREs, there are no escapes: outside a bracket expression,
    a <literal>\</> followed by an alphanumeric character merely stands for
    that character as an ordinary character, and inside a bracket expression,
    <literal>\</> is an ordinary character.
    (The latter is the one actual incompatibility between EREs and AREs.)
   </para>

   <para>
    <firstterm>Character-entry escapes</> exist to make it easier to specify
    non-printing and otherwise inconvenient characters in REs.  They are
    shown in <xref linkend="posix-character-entry-escapes-table">.
   </para>

   <para>
    <firstterm>Class-shorthand escapes</> provide shorthands for certain
    commonly-used character classes.  They are
    shown in <xref linkend="posix-class-shorthand-escapes-table">.
   </para>

   <para>
    A <firstterm>constraint escape</> is a constraint,
    matching the empty string if specific conditions are met,
    written as an escape.  They are
    shown in <xref linkend="posix-constraint-escapes-table">.
   </para>

   <para>
    A <firstterm>back reference</> (<literal>\</><replaceable>n</>) matches the
    same string matched by the previous parenthesized subexpression specified
    by the number <replaceable>n</>
    (see <xref linkend="posix-constraint-backref-table">).  For example,
    <literal>([bc])\1</> matches <literal>bb</> or <literal>cc</>
    but not <literal>bc</> or <literal>cb</>.
    The subexpression must entirely precede the back reference in the RE.
    Subexpressions are numbered in the order of their leading parentheses.
    Non-capturing parentheses do not define subexpressions.
   </para>

   <note>
    <para>
     Keep in mind that an escape's leading <literal>\</> will need to be
3315 3316 3317 3318
     doubled when entering the pattern as an SQL string constant.  For example:
<programlisting>
'123' ~ '^\\d{3}' <lineannotation>true</lineannotation>
</programlisting>
3319 3320 3321 3322 3323 3324 3325 3326 3327 3328 3329 3330 3331 3332 3333 3334 3335 3336 3337 3338 3339 3340 3341 3342 3343 3344 3345 3346 3347 3348 3349 3350 3351 3352 3353 3354 3355 3356 3357 3358 3359 3360 3361 3362 3363 3364 3365
    </para>
   </note>

   <table id="posix-character-entry-escapes-table">
    <title>Regular Expression Character-Entry Escapes</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Escape</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>\a</> </entry>
       <entry> alert (bell) character, as in C </entry>
       </row>

       <row>
       <entry> <literal>\b</> </entry>
       <entry> backspace, as in C </entry>
       </row>

       <row>
       <entry> <literal>\B</> </entry>
       <entry> synonym for <literal>\</> to help reduce the need for backslash
       doubling </entry>
       </row>

       <row>
       <entry> <literal>\c</><replaceable>X</> </entry>
       <entry> (where <replaceable>X</> is any character) the character whose
       low-order 5 bits are the same as those of
       <replaceable>X</>, and whose other bits are all zero </entry>
       </row>

       <row>
       <entry> <literal>\e</> </entry>
       <entry> the character whose collating-sequence name
       is <literal>ESC</>,
       or failing that, the character with octal value 033 </entry>
       </row>

       <row>
       <entry> <literal>\f</> </entry>
3366
       <entry> form feed, as in C </entry>
3367 3368 3369 3370 3371 3372 3373 3374 3375 3376 3377 3378 3379 3380 3381 3382 3383 3384 3385 3386
       </row>

       <row>
       <entry> <literal>\n</> </entry>
       <entry> newline, as in C </entry>
       </row>

       <row>
       <entry> <literal>\r</> </entry>
       <entry> carriage return, as in C </entry>
       </row>

       <row>
       <entry> <literal>\t</> </entry>
       <entry> horizontal tab, as in C </entry>
       </row>

       <row>
       <entry> <literal>\u</><replaceable>wxyz</> </entry>
       <entry> (where <replaceable>wxyz</> is exactly four hexadecimal digits)
3387
       the UTF16 (Unicode, 16-bit) character <literal>U+</><replaceable>wxyz</>
3388 3389 3390 3391 3392 3393 3394 3395 3396 3397 3398 3399 3400 3401 3402 3403 3404 3405 3406 3407 3408 3409 3410 3411 3412 3413 3414 3415 3416 3417 3418 3419 3420 3421 3422 3423 3424 3425 3426 3427 3428 3429 3430 3431 3432 3433 3434 3435 3436 3437 3438 3439 3440 3441 3442 3443 3444 3445 3446 3447 3448 3449 3450 3451 3452 3453 3454 3455 3456 3457 3458 3459 3460 3461 3462 3463 3464 3465 3466 3467 3468 3469 3470 3471 3472 3473 3474 3475 3476 3477 3478 3479 3480 3481 3482 3483 3484 3485 3486 3487 3488 3489 3490 3491 3492 3493 3494 3495 3496 3497 3498 3499 3500 3501 3502 3503 3504
       in the local byte ordering </entry>
       </row>

       <row>
       <entry> <literal>\U</><replaceable>stuvwxyz</> </entry>
       <entry> (where <replaceable>stuvwxyz</> is exactly eight hexadecimal
       digits)
       reserved for a somewhat-hypothetical Unicode extension to 32 bits
       </entry> 
       </row>

       <row>
       <entry> <literal>\v</> </entry>
       <entry> vertical tab, as in C </entry>
       </row>

       <row>
       <entry> <literal>\x</><replaceable>hhh</> </entry>
       <entry> (where <replaceable>hhh</> is any sequence of hexadecimal
       digits)
       the character whose hexadecimal value is
       <literal>0x</><replaceable>hhh</>
       (a single character no matter how many hexadecimal digits are used)
       </entry>
       </row>

       <row>
       <entry> <literal>\0</> </entry>
       <entry> the character whose value is <literal>0</> </entry>
       </row>

       <row>
       <entry> <literal>\</><replaceable>xy</> </entry>
       <entry> (where <replaceable>xy</> is exactly two octal digits,
       and is not a <firstterm>back reference</>)
       the character whose octal value is
       <literal>0</><replaceable>xy</> </entry>
       </row>

       <row>
       <entry> <literal>\</><replaceable>xyz</> </entry>
       <entry> (where <replaceable>xyz</> is exactly three octal digits,
       and is not a <firstterm>back reference</>)
       the character whose octal value is
       <literal>0</><replaceable>xyz</> </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    Hexadecimal digits are <literal>0</>-<literal>9</>,
    <literal>a</>-<literal>f</>, and <literal>A</>-<literal>F</>.
    Octal digits are <literal>0</>-<literal>7</>.
   </para>

   <para>
    The character-entry escapes are always taken as ordinary characters.
    For example, <literal>\135</> is <literal>]</> in ASCII, but
    <literal>\135</> does not terminate a bracket expression.
   </para>

   <table id="posix-class-shorthand-escapes-table">
    <title>Regular Expression Class-Shorthand Escapes</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Escape</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>\d</> </entry>
       <entry> <literal>[[:digit:]]</> </entry>
       </row>

       <row>
       <entry> <literal>\s</> </entry>
       <entry> <literal>[[:space:]]</> </entry>
       </row>

       <row>
       <entry> <literal>\w</> </entry>
       <entry> <literal>[[:alnum:]_]</>
       (note underscore is included) </entry>
       </row>

       <row>
       <entry> <literal>\D</> </entry>
       <entry> <literal>[^[:digit:]]</> </entry>
       </row>

       <row>
       <entry> <literal>\S</> </entry>
       <entry> <literal>[^[:space:]]</> </entry>
       </row>

       <row>
       <entry> <literal>\W</> </entry>
       <entry> <literal>[^[:alnum:]_]</>
       (note underscore is included) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    Within bracket expressions, <literal>\d</>, <literal>\s</>,
    and <literal>\w</> lose their outer brackets,
    and <literal>\D</>, <literal>\S</>, and <literal>\W</> are illegal.
    (So, for example, <literal>[a-c\d]</> is equivalent to
    <literal>[a-c[:digit:]]</>.
    Also, <literal>[a-c\D]</>, which is equivalent to
    <literal>[a-c^[:digit:]]</>, is illegal.)
3505 3506
   </para>

3507 3508 3509 3510 3511 3512 3513 3514 3515 3516 3517 3518 3519 3520 3521 3522 3523 3524 3525 3526 3527 3528 3529 3530 3531 3532 3533 3534 3535 3536 3537 3538 3539 3540 3541 3542 3543 3544 3545 3546 3547 3548 3549 3550 3551 3552 3553 3554 3555 3556 3557 3558 3559 3560 3561 3562 3563 3564 3565 3566 3567 3568 3569 3570 3571 3572 3573 3574 3575 3576 3577 3578 3579 3580 3581 3582 3583 3584 3585 3586 3587 3588 3589 3590 3591 3592 3593 3594 3595 3596 3597 3598 3599 3600 3601 3602 3603 3604 3605 3606 3607 3608 3609 3610 3611
   <table id="posix-constraint-escapes-table">
    <title>Regular Expression Constraint Escapes</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Escape</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>\A</> </entry>
       <entry> matches only at the beginning of the string
       (see <xref linkend="posix-matching-rules"> for how this differs from
       <literal>^</>) </entry>
       </row>

       <row>
       <entry> <literal>\m</> </entry>
       <entry> matches only at the beginning of a word </entry>
       </row>

       <row>
       <entry> <literal>\M</> </entry>
       <entry> matches only at the end of a word </entry>
       </row>

       <row>
       <entry> <literal>\y</> </entry>
       <entry> matches only at the beginning or end of a word </entry>
       </row>

       <row>
       <entry> <literal>\Y</> </entry>
       <entry> matches only at a point that is not the beginning or end of a
       word </entry>
       </row>

       <row>
       <entry> <literal>\Z</> </entry>
       <entry> matches only at the end of the string
       (see <xref linkend="posix-matching-rules"> for how this differs from
       <literal>$</>) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    A word is defined as in the specification of
    <literal>[[:&lt;:]]</> and <literal>[[:&gt;:]]</> above.
    Constraint escapes are illegal within bracket expressions.
   </para>

   <table id="posix-constraint-backref-table">
    <title>Regular Expression Back References</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Escape</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>\</><replaceable>m</> </entry>
       <entry> (where <replaceable>m</> is a nonzero digit)
       a back reference to the <replaceable>m</>'th subexpression </entry>
       </row>

       <row>
       <entry> <literal>\</><replaceable>mnn</> </entry>
       <entry> (where <replaceable>m</> is a nonzero digit, and
       <replaceable>nn</> is some more digits, and the decimal value
       <replaceable>mnn</> is not greater than the number of closing capturing
       parentheses seen so far) 
       a back reference to the <replaceable>mnn</>'th subexpression </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <note>
    <para>
     There is an inherent historical ambiguity between octal character-entry 
     escapes and back references, which is resolved by heuristics,
     as hinted at above.
     A leading zero always indicates an octal escape.
     A single non-zero digit, not followed by another digit,
     is always taken as a back reference.
     A multi-digit sequence not starting with a zero is taken as a back 
     reference if it comes after a suitable subexpression
     (i.e. the number is in the legal range for a back reference),
     and otherwise is taken as octal.
    </para>
   </note>
   </sect3>

   <sect3 id="posix-metasyntax">
    <title>Regular Expression Metasyntax</title>

3612
   <para>
3613 3614
    In addition to the main syntax described above, there are some special
    forms and miscellaneous syntactic facilities available.
3615 3616 3617
   </para>

   <para>
3618
    Normally the flavor of RE being used is determined by
3619
    <varname>regex_flavor</>.
3620
    However, this can be overridden by a <firstterm>director</> prefix.
T
Tom Lane 已提交
3621 3622 3623 3624
    If an RE begins with <literal>***:</>,
    the rest of the RE is taken as an ARE regardless of
    <varname>regex_flavor</>.
    If an RE begins with <literal>***=</>,
3625 3626
    the rest of the RE is taken to be a literal string,
    with all characters considered ordinary characters.
3627 3628 3629
   </para>

   <para>
3630 3631 3632 3633
    An ARE may begin with <firstterm>embedded options</>:
    a sequence <literal>(?</><replaceable>xyz</><literal>)</>
    (where <replaceable>xyz</> is one or more alphabetic characters)
    specifies options affecting the rest of the RE.
3634 3635
    These options override any previously determined options (including
    both the RE flavor and case sensitivity).
3636 3637 3638 3639 3640 3641 3642 3643 3644 3645 3646 3647 3648 3649 3650 3651 3652 3653 3654 3655 3656 3657 3658
    The available option letters are
    shown in <xref linkend="posix-embedded-options-table">.
   </para>

   <table id="posix-embedded-options-table">
    <title>ARE Embedded-Option Letters</title>

    <tgroup cols="2">
     <thead>
      <row>
       <entry>Option</entry>
       <entry>Description</entry>
      </row>
     </thead>

      <tbody>
       <row>
       <entry> <literal>b</> </entry>
       <entry> rest of RE is a BRE </entry>
       </row>

       <row>
       <entry> <literal>c</> </entry>
3659
       <entry> case-sensitive matching (overrides operator type) </entry>
3660 3661 3662 3663 3664 3665 3666 3667 3668 3669
       </row>

       <row>
       <entry> <literal>e</> </entry>
       <entry> rest of RE is an ERE </entry>
       </row>

       <row>
       <entry> <literal>i</> </entry>
       <entry> case-insensitive matching (see
3670
       <xref linkend="posix-matching-rules">) (overrides operator type) </entry>
3671 3672 3673 3674 3675 3676 3677 3678 3679 3680 3681 3682 3683 3684 3685 3686 3687 3688 3689 3690 3691 3692 3693 3694 3695 3696 3697
       </row>

       <row>
       <entry> <literal>m</> </entry>
       <entry> historical synonym for <literal>n</> </entry>
       </row>

       <row>
       <entry> <literal>n</> </entry>
       <entry> newline-sensitive matching (see
       <xref linkend="posix-matching-rules">) </entry>
       </row>

       <row>
       <entry> <literal>p</> </entry>
       <entry> partial newline-sensitive matching (see
       <xref linkend="posix-matching-rules">) </entry>
       </row>

       <row>
       <entry> <literal>q</> </entry>
       <entry> rest of RE is a literal (<quote>quoted</>) string, all ordinary
       characters </entry>
       </row>

       <row>
       <entry> <literal>s</> </entry>
3698
       <entry> non-newline-sensitive matching (default) </entry>
3699 3700 3701 3702
       </row>

       <row>
       <entry> <literal>t</> </entry>
3703
       <entry> tight syntax (default; see below) </entry>
3704 3705 3706 3707 3708 3709 3710 3711 3712 3713 3714 3715 3716 3717 3718 3719 3720 3721
       </row>

       <row>
       <entry> <literal>w</> </entry>
       <entry> inverse partial newline-sensitive (<quote>weird</>) matching
       (see <xref linkend="posix-matching-rules">) </entry>
       </row>

       <row>
       <entry> <literal>x</> </entry>
       <entry> expanded syntax (see below) </entry>
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
    Embedded options take effect at the <literal>)</> terminating the sequence.
T
Tom Lane 已提交
3722 3723
    They may appear only at the start of an ARE (after the
    <literal>***:</> director if any).
3724 3725 3726 3727 3728 3729 3730 3731 3732 3733 3734 3735 3736 3737 3738 3739 3740 3741 3742 3743 3744 3745 3746 3747 3748 3749 3750
   </para>

   <para>
    In addition to the usual (<firstterm>tight</>) RE syntax, in which all
    characters are significant, there is an <firstterm>expanded</> syntax,
    available by specifying the embedded <literal>x</> option.
    In the expanded syntax,
    white-space characters in the RE are ignored, as are
    all characters between a <literal>#</>
    and the following newline (or the end of the RE).  This
    permits paragraphing and commenting a complex RE.
    There are three exceptions to that basic rule:

    <itemizedlist>
     <listitem>
      <para>
       a white-space character or <literal>#</> preceded by <literal>\</> is
       retained
      </para>
     </listitem>
     <listitem>
      <para>
       white space or <literal>#</> within a bracket expression is retained
      </para>
     </listitem>
     <listitem>
      <para>
T
Tom Lane 已提交
3751 3752
       white space and comments cannot appear within multi-character symbols,
       such as <literal>(?:</>
3753 3754 3755 3756
      </para>
     </listitem>
    </itemizedlist>

T
Tom Lane 已提交
3757
    For this purpose, white-space characters are blank, tab, newline, and
3758 3759 3760 3761 3762 3763 3764 3765 3766 3767 3768 3769 3770 3771 3772 3773 3774 3775 3776 3777 3778 3779 3780 3781 3782 3783 3784 3785 3786
    any character that belongs to the <replaceable>space</> character class.
   </para>

   <para>
    Finally, in an ARE, outside bracket expressions, the sequence
    <literal>(?#</><replaceable>ttt</><literal>)</>
    (where <replaceable>ttt</> is any text not containing a <literal>)</>)
    is a comment, completely ignored.
    Again, this is not allowed between the characters of
    multi-character symbols, like <literal>(?:</>.
    Such comments are more a historical artifact than a useful facility,
    and their use is deprecated; use the expanded syntax instead.
   </para>

   <para>
    <emphasis>None</> of these metasyntax extensions is available if
    an initial <literal>***=</> director
    has specified that the user's input be treated as a literal string
    rather than as an RE.
   </para>
   </sect3>

   <sect3 id="posix-matching-rules">
    <title>Regular Expression Matching Rules</title>

   <para>
    In the event that an RE could match more than one substring of a given
    string, the RE matches the one starting earliest in the string.
    If the RE could match more than one substring starting at that point,
3787 3788 3789
    either the longest possible match or the shortest possible match will
    be taken, depending on whether the RE is <firstterm>greedy</> or
    <firstterm>non-greedy</>.
3790 3791 3792
   </para>

   <para>
3793 3794 3795 3796 3797 3798 3799 3800 3801 3802 3803 3804 3805 3806 3807 3808 3809 3810 3811 3812 3813 3814 3815 3816 3817 3818 3819 3820 3821 3822 3823 3824 3825 3826 3827 3828 3829 3830 3831 3832 3833 3834 3835 3836 3837 3838 3839 3840 3841 3842 3843 3844 3845 3846 3847 3848 3849 3850 3851 3852 3853 3854 3855 3856 3857 3858 3859 3860 3861 3862 3863 3864 3865 3866 3867 3868 3869 3870 3871 3872 3873 3874 3875 3876
    Whether an RE is greedy or not is determined by the following rules:
    <itemizedlist>
     <listitem>
      <para>
       Most atoms, and all constraints, have no greediness attribute (because
       they cannot match variable amounts of text anyway).
      </para>
     </listitem>
     <listitem>
      <para>
       Adding parentheses around an RE does not change its greediness.
      </para>
     </listitem>
     <listitem>
      <para>
       A quantified atom with a fixed-repetition quantifier
       (<literal>{</><replaceable>m</><literal>}</>
       or
       <literal>{</><replaceable>m</><literal>}?</>)
       has the same greediness (possibly none) as the atom itself.
      </para>
     </listitem>
     <listitem>
      <para>
       A quantified atom with other normal quantifiers (including
       <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</>
       with <replaceable>m</> equal to <replaceable>n</>)
       is greedy (prefers longest match).
      </para>
     </listitem>
     <listitem>
      <para>
       A quantified atom with a non-greedy quantifier (including
       <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</>
       with <replaceable>m</> equal to <replaceable>n</>)
       is non-greedy (prefers shortest match).
      </para>
     </listitem>
     <listitem>
      <para>
       A branch &mdash; that is, an RE that has no top-level
       <literal>|</> operator &mdash; has the same greediness as the first
       quantified atom in it that has a greediness attribute.
      </para>
     </listitem>
     <listitem>
      <para>
       An RE consisting of two or more branches connected by the
       <literal>|</> operator is always greedy.
      </para>
     </listitem>
    </itemizedlist>
   </para>

   <para>
    The above rules associate greediness attributes not only with individual
    quantified atoms, but with branches and entire REs that contain quantified
    atoms.  What that means is that the matching is done in such a way that
    the branch, or whole RE, matches the longest or shortest possible
    substring <emphasis>as a whole</>.  Once the length of the entire match
    is determined, the part of it that matches any particular subexpression
    is determined on the basis of the greediness attribute of that
    subexpression, with subexpressions starting earlier in the RE taking
    priority over ones starting later.
   </para>

   <para>
    An example of what this means:
<screen>
SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');
<lineannotation>Result: </lineannotation><computeroutput>123</computeroutput>
SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
    In the first case, the RE as a whole is greedy because <literal>Y*</>
    is greedy.  It can match beginning at the <literal>Y</>, and it matches
    the longest possible string starting there, i.e., <literal>Y123</>.
    The output is the parenthesized part of that, or <literal>123</>.
    In the second case, the RE as a whole is non-greedy because <literal>Y*?</>
    is non-greedy.  It can match beginning at the <literal>Y</>, and it matches
    the shortest possible string starting there, i.e., <literal>Y1</>.
    The subexpression <literal>[0-9]{1,3}</> is greedy but it cannot change
    the decision as to the overall match length; so it is forced to match
    just <literal>1</>.
3877 3878 3879
   </para>

   <para>
3880 3881 3882 3883 3884
    In short, when an RE contains both greedy and non-greedy subexpressions,
    the total match length is either as long as possible or as short as
    possible, according to the attribute assigned to the whole RE.  The
    attributes assigned to the subexpressions only affect how much of that
    match they are allowed to <quote>eat</> relative to each other.
3885 3886 3887 3888
   </para>

   <para>
    The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
3889
    can be used to force greediness or non-greediness, respectively,
3890 3891 3892 3893 3894 3895 3896 3897 3898 3899 3900 3901 3902 3903 3904 3905 3906 3907 3908 3909 3910 3911 3912 3913 3914
    on a subexpression or a whole RE.
   </para>

   <para>
    Match lengths are measured in characters, not collating elements.
    An empty string is considered longer than no match at all.
    For example:
    <literal>bb*</>
    matches the three middle characters of <literal>abbbc</>;
    <literal>(week|wee)(night|knights)</>
    matches all ten characters of <literal>weeknights</>;
    when <literal>(.*).*</>
    is matched against <literal>abc</> the parenthesized subexpression
    matches all three characters; and when
    <literal>(a*)*</> is matched against <literal>bc</>
    both the whole RE and the parenthesized
    subexpression match an empty string.
   </para>

   <para>
    If case-independent matching is specified,
    the effect is much as if all case distinctions had vanished from the
    alphabet.
    When an alphabetic that exists in multiple cases appears as an
    ordinary character outside a bracket expression, it is effectively
3915
    transformed into a bracket expression containing both cases,
3916 3917 3918 3919 3920 3921 3922 3923 3924 3925 3926 3927 3928 3929 3930 3931 3932 3933 3934 3935 3936 3937 3938 3939 3940 3941 3942 3943 3944 3945 3946 3947 3948 3949 3950 3951 3952 3953 3954 3955 3956 3957 3958 3959 3960 3961 3962 3963 3964 3965 3966 3967 3968 3969 3970 3971 3972 3973 3974 3975 3976 3977 3978 3979 3980 3981 3982 3983 3984 3985 3986
    e.g. <literal>x</> becomes <literal>[xX]</>.
    When it appears inside a bracket expression, all case counterparts
    of it are added to the bracket expression, e.g.
    <literal>[x]</> becomes <literal>[xX]</>
    and <literal>[^x]</> becomes <literal>[^xX]</>.
   </para>

   <para>
    If newline-sensitive matching is specified, <literal>.</>
    and bracket expressions using <literal>^</>
    will never match the newline character
    (so that matches will never cross newlines unless the RE
    explicitly arranges it)
    and <literal>^</>and <literal>$</>
    will match the empty string after and before a newline
    respectively, in addition to matching at beginning and end of string
    respectively.
    But the ARE escapes <literal>\A</> and <literal>\Z</>
    continue to match beginning or end of string <emphasis>only</>.
   </para>

   <para>
    If partial newline-sensitive matching is specified,
    this affects <literal>.</> and bracket expressions
    as with newline-sensitive matching, but not <literal>^</>
    and <literal>$</>.
   </para>

   <para>
    If inverse partial newline-sensitive matching is specified,
    this affects <literal>^</> and <literal>$</>
    as with newline-sensitive matching, but not <literal>.</>
    and bracket expressions.
    This isn't very useful but is provided for symmetry.
   </para>
   </sect3>

   <sect3 id="posix-limits-compatibility">
    <title>Limits and Compatibility</title>

   <para>
    No particular limit is imposed on the length of REs in this
    implementation.  However,
    programs intended to be highly portable should not employ REs longer
    than 256 bytes,
    as a POSIX-compliant implementation can refuse to accept such REs.
   </para>

   <para>
    The only feature of AREs that is actually incompatible with
    POSIX EREs is that <literal>\</> does not lose its special
    significance inside bracket expressions.
    All other ARE features use syntax which is illegal or has
    undefined or unspecified effects in POSIX EREs;
    the <literal>***</> syntax of directors likewise is outside the POSIX
    syntax for both BREs and EREs.
   </para>

   <para>
    Many of the ARE extensions are borrowed from Perl, but some have
    been changed to clean them up, and a few Perl extensions are not present.
    Incompatibilities of note include <literal>\b</>, <literal>\B</>,
    the lack of special treatment for a trailing newline,
    the addition of complemented bracket expressions to the things
    affected by newline-sensitive matching,
    the restrictions on parentheses and back references in lookahead
    constraints, and the longest/shortest-match (rather than first-match)
    matching semantics.
   </para>

   <para>
3987
    Two significant incompatibilities exist between AREs and the ERE syntax
3988 3989 3990 3991 3992 3993 3994 3995 3996 3997 3998 3999 4000 4001 4002 4003 4004 4005 4006 4007
    recognized by pre-7.4 releases of <productname>PostgreSQL</>:

    <itemizedlist>
     <listitem>
      <para>
       In AREs, <literal>\</> followed by an alphanumeric character is either
       an escape or an error, while in previous releases, it was just another
       way of writing the alphanumeric.
       This should not be much of a problem because there was no reason to
       write such a sequence in earlier releases.
      </para>
     </listitem>
     <listitem>
      <para>
       In AREs, <literal>\</> remains a special character within
       <literal>[]</>, so a literal <literal>\</> within a bracket
       expression must be written <literal>\\</>.
      </para>
     </listitem>
    </itemizedlist>
4008 4009 4010

    While these differences are unlikely to create a problem for most
    applications, you can avoid them if necessary by
4011
    setting <varname>regex_flavor</> to <literal>extended</>.
4012 4013 4014 4015 4016
   </para>
   </sect3>

   <sect3 id="posix-basic-regexes">
    <title>Basic Regular Expressions</title>
4017

4018 4019 4020 4021 4022 4023 4024 4025 4026 4027 4028 4029 4030 4031 4032 4033 4034 4035 4036 4037 4038 4039 4040 4041 4042 4043 4044 4045 4046 4047
   <para>
    BREs differ from EREs in several respects.
    <literal>|</>, <literal>+</>, and <literal>?</>
    are ordinary characters and there is no equivalent
    for their functionality.
    The delimiters for bounds are
    <literal>\{</> and <literal>\}</>,
    with <literal>{</> and <literal>}</>
    by themselves ordinary characters.
    The parentheses for nested subexpressions are
    <literal>\(</> and <literal>\)</>,
    with <literal>(</> and <literal>)</> by themselves ordinary characters.
    <literal>^</> is an ordinary character except at the beginning of the
    RE or the beginning of a parenthesized subexpression,
    <literal>$</> is an ordinary character except at the end of the
    RE or the end of a parenthesized subexpression,
    and <literal>*</> is an ordinary character if it appears at the beginning
    of the RE or the beginning of a parenthesized subexpression
    (after a possible leading <literal>^</>).
    Finally, single-digit back references are available, and
    <literal>\&lt;</> and <literal>\&gt;</>
    are synonyms for
    <literal>[[:&lt;:]]</> and <literal>[[:&gt;:]]</>
    respectively; no other escapes are available.
   </para>
   </sect3>

<!-- end re_syntax.n man page -->

  </sect2>
4048 4049 4050 4051
 </sect1>


  <sect1 id="functions-formatting">
4052
   <title>Data Type Formatting Functions</title>
4053

4054 4055 4056 4057
   <indexterm zone="functions-formatting">
    <primary>formatting</primary>
   </indexterm>

P
Peter Eisentraut 已提交
4058 4059 4060 4061
   <indexterm zone="functions-formatting">
    <primary>to_char</primary>
   </indexterm>

4062
   <para>
4063
    The <productname>PostgreSQL</productname> formatting functions
4064 4065
    provide a powerful set of tools for converting various data types
    (date/time, integer, floating point, numeric) to formatted strings
4066
    and for converting from formatted strings to specific data types.
4067
    <xref linkend="functions-formatting-table"> lists them.
4068
    These functions all follow a common calling convention: the first
4069
    argument is the value to be formatted and the second argument is a
4070
    template that defines the output or input format.
4071 4072
   </para>

4073
    <table id="functions-formatting-table">
4074 4075 4076 4077
     <title>Formatting Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
4078 4079 4080 4081
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
4082 4083 4084 4085
       </row>
      </thead>
      <tbody>
       <row>
4086 4087 4088 4089
        <entry><literal><function>to_char</function>(<type>timestamp</type>, <type>text</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>convert time stamp to string</entry>
        <entry><literal>to_char(current_timestamp, 'HH12:MI:SS')</literal></entry>
4090
       </row>
B
Bruce Momjian 已提交
4091
       <row>
4092 4093 4094 4095
        <entry><literal><function>to_char</function>(<type>interval</type>, <type>text</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>convert interval to string</entry>
        <entry><literal>to_char(interval '15h&nbsp;2m&nbsp;12s', 'HH24:MI:SS')</literal></entry>
B
Bruce Momjian 已提交
4096
       </row>
4097
       <row>
4098 4099 4100 4101
        <entry><literal><function>to_char</function>(<type>int</type>, <type>text</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>convert integer to string</entry>
        <entry><literal>to_char(125, '999')</literal></entry>
4102 4103
       </row>
       <row>
4104
        <entry><literal><function>to_char</function>(<type>double precision</type>,
4105
        <type>text</type>)</literal></entry>
4106 4107 4108
        <entry><type>text</type></entry>
        <entry>convert real/double precision to string</entry>
        <entry><literal>to_char(125.8::real, '999D9')</literal></entry>
4109 4110
       </row>
       <row>
4111 4112 4113 4114
        <entry><literal><function>to_char</function>(<type>numeric</type>, <type>text</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>convert numeric to string</entry>
        <entry><literal>to_char(-125.8, '999D99S')</literal></entry>
4115 4116
       </row>
       <row>
4117 4118 4119 4120
        <entry><literal><function>to_date</function>(<type>text</type>, <type>text</type>)</literal></entry>
        <entry><type>date</type></entry>
        <entry>convert string to date</entry>
        <entry><literal>to_date('05&nbsp;Dec&nbsp;2000', 'DD&nbsp;Mon&nbsp;YYYY')</literal></entry>
4121 4122
       </row>
       <row>
4123 4124 4125 4126
        <entry><literal><function>to_timestamp</function>(<type>text</type>, <type>text</type>)</literal></entry>
        <entry><type>timestamp with time zone</type></entry>
        <entry>convert string to time stamp</entry>
        <entry><literal>to_timestamp('05&nbsp;Dec&nbsp;2000', 'DD&nbsp;Mon&nbsp;YYYY')</literal></entry>
4127 4128
       </row>
       <row>
4129 4130 4131 4132
        <entry><literal><function>to_number</function>(<type>text</type>, <type>text</type>)</literal></entry>
        <entry><type>numeric</type></entry>
        <entry>convert string to numeric</entry>
        <entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
4133 4134 4135 4136 4137
       </row>
      </tbody>
     </tgroup>
    </table>

4138
   <para>
4139 4140
    Warning: <literal><function>to_char</function>(<type>interval</type>, <type>text</type>)</literal> 
    is deprecated and should not be used in newly-written code. It will be removed in the next version.
4141 4142
   </para>

4143
   <para>
4144
    In an output template string (for <function>to_char</>), there are certain patterns that are
4145 4146
    recognized and replaced with appropriately-formatted data from the value
    to be formatted.  Any text that is not a template pattern is simply
4147
    copied verbatim.  Similarly, in an input template string (for anything but <function>to_char</>), template patterns
4148 4149 4150 4151
    identify the parts of the input data string to be looked at and the
    values to be found there.
   </para>

4152 4153 4154 4155 4156 4157
  <para>
   <xref linkend="functions-formatting-datetime-table"> shows the
   template patterns available for formatting date and time values.
  </para>

    <table id="functions-formatting-datetime-table">
4158
     <title>Template Patterns for Date/Time Formatting</title>
4159 4160 4161
     <tgroup cols="2">
      <thead>
       <row>
4162 4163
        <entry>Pattern</entry>
        <entry>Description</entry>
4164 4165 4166 4167
       </row>
      </thead>
      <tbody>
       <row>
4168 4169
        <entry><literal>HH</literal></entry>
        <entry>hour of day (01-12)</entry>
4170 4171
       </row>
       <row>
4172 4173
        <entry><literal>HH12</literal></entry>
        <entry>hour of day (01-12)</entry>
4174
       </row>       
4175
       <row>
4176 4177
        <entry><literal>HH24</literal></entry>
        <entry>hour of day (00-23)</entry>
4178
       </row>       
4179
       <row>
4180 4181
        <entry><literal>MI</literal></entry>
        <entry>minute (00-59)</entry>
4182 4183
       </row>   
       <row>
4184 4185
        <entry><literal>SS</literal></entry>
        <entry>second (00-59)</entry>
4186
       </row>
B
Bruce Momjian 已提交
4187
       <row>
4188 4189
        <entry><literal>MS</literal></entry>
        <entry>millisecond (000-999)</entry>
B
Bruce Momjian 已提交
4190 4191
       </row>
       <row>
4192 4193
        <entry><literal>US</literal></entry>
        <entry>microsecond (000000-999999)</entry>
B
Bruce Momjian 已提交
4194
       </row>
4195
       <row>
4196 4197
        <entry><literal>SSSS</literal></entry>
        <entry>seconds past midnight (0-86399)</entry>
4198
       </row>
B
Bruce Momjian 已提交
4199
       <row>
4200 4201 4202
        <entry><literal>AM</literal> or <literal>A.M.</literal> or
        <literal>PM</literal> or <literal>P.M.</literal></entry>
        <entry>meridian indicator (uppercase)</entry>
B
Bruce Momjian 已提交
4203 4204
       </row>
       <row>
4205 4206 4207
        <entry><literal>am</literal> or <literal>a.m.</literal> or
        <literal>pm</literal> or <literal>p.m.</literal></entry>
        <entry>meridian indicator (lowercase)</entry>
B
Bruce Momjian 已提交
4208
       </row>
4209
       <row>
4210 4211
        <entry><literal>Y,YYY</literal></entry>
        <entry>year (4 and more digits) with comma</entry>
4212 4213
       </row>
       <row>
4214 4215
        <entry><literal>YYYY</literal></entry>
        <entry>year (4 and more digits)</entry>
4216 4217
       </row>
       <row>
4218 4219
        <entry><literal>YYY</literal></entry>
        <entry>last 3 digits of year</entry>
4220 4221
       </row>
       <row>
4222 4223
        <entry><literal>YY</literal></entry>
        <entry>last 2 digits of year</entry>
4224 4225
       </row>
       <row>
4226 4227
        <entry><literal>Y</literal></entry>
        <entry>last digit of year</entry>
4228
       </row>
4229
       <row>
4230 4231
        <entry><literal>IYYY</literal></entry>
        <entry>ISO year (4 and more digits)</entry>
4232 4233
       </row>
       <row>
4234 4235
        <entry><literal>IYY</literal></entry>
        <entry>last 3 digits of ISO year</entry>
4236 4237
       </row>
       <row>
4238 4239
        <entry><literal>IY</literal></entry>
        <entry>last 2 digits of ISO year</entry>
4240 4241
       </row>
       <row>
4242 4243
        <entry><literal>I</literal></entry>
        <entry>last digits of ISO year</entry>
4244
       </row>
B
Bruce Momjian 已提交
4245
       <row>
4246 4247 4248
        <entry><literal>BC</literal> or <literal>B.C.</literal> or
        <literal>AD</literal> or <literal>A.D.</literal></entry>
        <entry>era indicator (uppercase)</entry>
B
Bruce Momjian 已提交
4249 4250
       </row>
       <row>
4251 4252 4253
        <entry><literal>bc</literal> or <literal>b.c.</literal> or
        <literal>ad</literal> or <literal>a.d.</literal></entry>
        <entry>era indicator (lowercase)</entry>
B
Bruce Momjian 已提交
4254
       </row>
4255
       <row>
4256 4257
        <entry><literal>MONTH</literal></entry>
        <entry>full uppercase month name (blank-padded to 9 chars)</entry>
4258 4259
       </row>
       <row>
4260 4261
        <entry><literal>Month</literal></entry>
        <entry>full mixed-case month name (blank-padded to 9 chars)</entry>
4262 4263
       </row>
       <row>
4264 4265
        <entry><literal>month</literal></entry>
        <entry>full lowercase month name (blank-padded to 9 chars)</entry>
4266 4267
       </row>
       <row>
4268 4269
        <entry><literal>MON</literal></entry>
        <entry>abbreviated uppercase month name (3 chars)</entry>
4270 4271
       </row>
       <row>
4272 4273
        <entry><literal>Mon</literal></entry>
        <entry>abbreviated mixed-case month name (3 chars)</entry>
4274 4275
       </row>
       <row>
4276 4277
        <entry><literal>mon</literal></entry>
        <entry>abbreviated lowercase month name (3 chars)</entry>
4278 4279
       </row>
       <row>
4280 4281
        <entry><literal>MM</literal></entry>
        <entry>month number (01-12)</entry>
4282 4283
       </row>
       <row>
4284 4285
        <entry><literal>DAY</literal></entry>
        <entry>full uppercase day name (blank-padded to 9 chars)</entry>
4286 4287
       </row>
       <row>
4288 4289
        <entry><literal>Day</literal></entry>
        <entry>full mixed-case day name (blank-padded to 9 chars)</entry>
4290 4291
       </row>
       <row>
4292 4293
        <entry><literal>day</literal></entry>
        <entry>full lowercase day name (blank-padded to 9 chars)</entry>
4294 4295
       </row>
       <row>
4296 4297
        <entry><literal>DY</literal></entry>
        <entry>abbreviated uppercase day name (3 chars)</entry>
4298 4299
       </row>
       <row>
4300 4301
        <entry><literal>Dy</literal></entry>
        <entry>abbreviated mixed-case day name (3 chars)</entry>
4302 4303
       </row>
       <row>
4304 4305
        <entry><literal>dy</literal></entry>
        <entry>abbreviated lowercase day name (3 chars)</entry>
4306 4307
       </row>
       <row>
4308 4309
        <entry><literal>DDD</literal></entry>
        <entry>day of year (001-366)</entry>
4310 4311
       </row>
       <row>
4312 4313
        <entry><literal>DD</literal></entry>
        <entry>day of month (01-31)</entry>
4314 4315
       </row>
       <row>
4316 4317
        <entry><literal>D</literal></entry>
        <entry>day of week (1-7; Sunday is 1)</entry>
4318 4319
       </row>
       <row>
4320 4321
        <entry><literal>W</literal></entry>
        <entry>week of month (1-5) (The first week starts on the first day of the month.)</entry>
4322 4323
       </row> 
       <row>
4324 4325
        <entry><literal>WW</literal></entry>
        <entry>week number of year (1-53) (The first week starts on the first day of the year.)</entry>
4326
       </row>
B
Bruce Momjian 已提交
4327
       <row>
4328 4329
        <entry><literal>IW</literal></entry>
        <entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
B
Bruce Momjian 已提交
4330
       </row>
4331
       <row>
4332 4333
        <entry><literal>CC</literal></entry>
        <entry>century (2 digits)</entry>
4334 4335
       </row>
       <row>
4336 4337
        <entry><literal>J</literal></entry>
        <entry>Julian Day (days since January 1, 4712 BC)</entry>
4338 4339
       </row>
       <row>
4340 4341
        <entry><literal>Q</literal></entry>
        <entry>quarter</entry>
4342 4343
       </row>
       <row>
4344 4345
        <entry><literal>RM</literal></entry>
        <entry>month in Roman numerals (I-XII; I=January) (uppercase)</entry>
B
Bruce Momjian 已提交
4346 4347
       </row>
       <row>
4348 4349
        <entry><literal>rm</literal></entry>
        <entry>month in Roman numerals (i-xii; i=January) (lowercase)</entry>
4350
       </row>
4351
       <row>
4352 4353
        <entry><literal>TZ</literal></entry>
        <entry>time-zone name (uppercase)</entry>
4354 4355
       </row>
       <row>
4356 4357
        <entry><literal>tz</literal></entry>
        <entry>time-zone name (lowercase)</entry>
4358
       </row>
4359 4360 4361 4362 4363
      </tbody>
     </tgroup>
    </table>

   <para>
4364
    Certain modifiers may be applied to any template pattern to alter its
4365 4366 4367
    behavior.  For example, <literal>FMMonth</literal>
    is the <literal>Month</literal> pattern with the
    <literal>FM</literal> modifier.
4368 4369
    <xref linkend="functions-formatting-datetimemod-table"> shows the
    modifier patterns for date/time formatting.
4370 4371
   </para>

4372
    <table id="functions-formatting-datetimemod-table">
4373
     <title>Template Pattern Modifiers for Date/Time Formatting</title>
4374 4375 4376
     <tgroup cols="3">
      <thead>
       <row>
4377 4378 4379
        <entry>Modifier</entry>
        <entry>Description</entry>
        <entry>Example</entry>
4380 4381 4382 4383
       </row>
      </thead>
      <tbody>
       <row>
4384 4385 4386
        <entry><literal>FM</literal> prefix</entry>
        <entry>fill mode (suppress padding blanks and zeroes)</entry>
        <entry><literal>FMMonth</literal></entry>
4387 4388
       </row>
       <row>
4389 4390 4391 4392
        <entry><literal>TH</literal> suffix</entry>
        <entry>uppercase ordinal number suffix</entry>
        <entry><literal>DDTH</literal></entry>
       </row>   
4393
       <row>
4394 4395 4396
        <entry><literal>th</literal> suffix</entry>
        <entry>lowercase ordinal number suffix</entry>
        <entry><literal>DDth</literal></entry>
4397 4398
       </row>
       <row>
4399 4400 4401 4402
        <entry><literal>FX</literal> prefix</entry>
        <entry>fixed format global option (see usage notes)</entry>
        <entry><literal>FX&nbsp;Month&nbsp;DD&nbsp;Day</literal></entry>
       </row>   
4403
       <row>
4404 4405 4406
        <entry><literal>SP</literal> suffix</entry>
        <entry>spell mode (not yet implemented)</entry>
        <entry><literal>DDSP</literal></entry>
4407 4408 4409 4410 4411 4412
       </row>       
      </tbody>
     </tgroup>
    </table>

   <para>
T
Tom Lane 已提交
4413
    Usage notes for date/time formatting:
4414 4415

    <itemizedlist>
4416 4417
     <listitem>
      <para>
4418
       <literal>FM</literal> suppresses leading zeroes and trailing blanks
4419 4420 4421 4422 4423
       that would otherwise be added to make the output of a pattern be
       fixed-width.
      </para>
     </listitem>

4424 4425 4426
     <listitem>
      <para>
       <function>to_timestamp</function> and <function>to_date</function>
4427
       skip multiple blank spaces in the input string if the <literal>FX</literal> option 
4428
       is not used. <literal>FX</literal> must be specified as the first item
4429 4430 4431 4432
       in the template.  For example 
       <literal>to_timestamp('2000&nbsp;&nbsp;&nbsp;&nbsp;JUN', 'YYYY MON')</literal> is correct, but
       <literal>to_timestamp('2000&nbsp;&nbsp;&nbsp;&nbsp;JUN', 'FXYYYY MON')</literal> returns an error,
       because <function>to_timestamp</function> expects one space only.
4433 4434 4435 4436 4437
      </para>
     </listitem>

     <listitem>
      <para>
4438
       Ordinary text is allowed in <function>to_char</function>
4439 4440
       templates and will be output literally.  You can put a substring
       in double quotes to force it to be interpreted as literal text
4441
       even if it contains pattern key words.  For example, in
4442
       <literal>'"Hello Year "YYYY'</literal>, the <literal>YYYY</literal>
4443
       will be replaced by the year data, but the single <literal>Y</literal> in <literal>Year</literal>
4444
       will not be.
4445 4446 4447 4448 4449
      </para>
     </listitem>

     <listitem>
      <para>
4450 4451 4452
       If you want to have a double quote in the output you must
       precede it with a backslash, for example <literal>'\\"YYYY
       Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
4453 4454
       (Two backslashes are necessary because the backslash already
       has a special meaning in a string constant.)
4455 4456
      </para>
     </listitem>
4457 4458 4459

     <listitem>
      <para>
4460 4461
       The <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
       <type>date</type> has a restriction if you use a year with more than 4 digits. You must
P
Peter Eisentraut 已提交
4462
       use some non-digit character or template after <literal>YYYY</literal>,
4463
       otherwise the year is always interpreted as 4 digits. For example
4464
       (with the year 20000):
P
Peter Eisentraut 已提交
4465
       <literal>to_date('200001131', 'YYYYMMDD')</literal> will be 
4466
       interpreted as a 4-digit year; instead use a non-digit 
P
Peter Eisentraut 已提交
4467 4468 4469
       separator after the year, like
       <literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
       <literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
4470
      </para>
4471 4472 4473 4474 4475 4476 4477 4478 4479 4480 4481
     </listitem>

     <listitem>
      <para>
       In conversions from string to <type>timestamp</type> or
       <type>date</type>, the <literal>CC</literal> field is ignored if there
       is a <literal>YYY</literal>, <literal>YYYY</literal> or
       <literal>Y,YYY</literal> field. If <literal>CC</literal> is used with
       <literal>YY</literal> or <literal>Y</literal> then the year is computed
       as <literal>(CC-1)*100+YY</literal>.
      </para>
4482
     </listitem>
B
Bruce Momjian 已提交
4483 4484 4485

     <listitem>
      <para>
4486 4487
       Millisecond (<literal>MS</literal>) and microsecond (<literal>US</literal>)
       values in a conversion from string to <type>timestamp</type> are used as part of the
P
Peter Eisentraut 已提交
4488
       seconds after the decimal point. For example 
B
Bruce Momjian 已提交
4489
       <literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
4490
       but 300, because the conversion counts it as 12 + 0.3 seconds.
4491 4492
       This means for the format <literal>SS:MS</literal>, the input values
       <literal>12:3</literal>, <literal>12:30</literal>, and <literal>12:300</literal> specify the
P
Peter Eisentraut 已提交
4493
       same number of milliseconds. To get three milliseconds, one must use
4494
       <literal>12:003</literal>, which the conversion counts as
P
Peter Eisentraut 已提交
4495 4496 4497 4498 4499
       12 + 0.003 = 12.003 seconds.
      </para>

      <para>
       Here is a more 
B
Bruce Momjian 已提交
4500
       complex example: 
4501
       <literal>to_timestamp('15:12:02.020.001230', 'HH:MI:SS.MS.US')</literal>
P
Peter Eisentraut 已提交
4502 4503
       is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
       1230 microseconds = 2.021230 seconds. 
B
Bruce Momjian 已提交
4504 4505
      </para>
     </listitem>
4506 4507 4508 4509 4510 4511 4512

     <listitem>
      <para><function>to_char</function>'s day of the week numbering
        (see the 'D' formatting pattern) is different from that of the 
        <function>extract</function> function.
      </para>
     </listitem>
4513
    </itemizedlist>
4514
   </para>
4515

4516 4517 4518 4519 4520 4521
  <para>
   <xref linkend="functions-formatting-numeric-table"> shows the
   template patterns available for formatting numeric values.
  </para>

    <table id="functions-formatting-numeric-table">
4522
     <title>Template Patterns for Numeric Formatting</title>
4523 4524 4525
     <tgroup cols="2">
      <thead>
       <row>
4526 4527
        <entry>Pattern</entry>
        <entry>Description</entry>
4528 4529 4530 4531
       </row>
      </thead>
      <tbody>
       <row>
4532 4533
        <entry><literal>9</literal></entry>
        <entry>value with the specified number of digits</entry>
4534 4535
       </row>
       <row>
4536 4537
        <entry><literal>0</literal></entry>
        <entry>value with leading zeros</entry>
4538 4539
       </row>
       <row>
4540 4541
        <entry><literal>.</literal> (period)</entry>
        <entry>decimal point</entry>
4542 4543
       </row>       
       <row>
4544 4545
        <entry><literal>,</literal> (comma)</entry>
        <entry>group (thousand) separator</entry>
4546 4547
       </row>
       <row>
4548 4549
        <entry><literal>PR</literal></entry>
        <entry>negative value in angle brackets</entry>
4550 4551
       </row>
       <row>
4552 4553
        <entry><literal>S</literal></entry>
        <entry>sign anchored to number (uses locale)</entry>
4554 4555
       </row>
       <row>
4556 4557
        <entry><literal>L</literal></entry>
        <entry>currency symbol (uses locale)</entry>
4558 4559
       </row>
       <row>
4560 4561
        <entry><literal>D</literal></entry>
        <entry>decimal point (uses locale)</entry>
4562 4563
       </row>
       <row>
4564 4565
        <entry><literal>G</literal></entry>
        <entry>group separator (uses locale)</entry>
4566 4567
       </row>
       <row>
4568 4569
        <entry><literal>MI</literal></entry>
        <entry>minus sign in specified position (if number &lt; 0)</entry>
4570 4571
       </row>
       <row>
4572 4573
        <entry><literal>PL</literal></entry>
        <entry>plus sign in specified position (if number &gt; 0)</entry>
4574 4575
       </row>
       <row>
4576 4577
        <entry><literal>SG</literal></entry>
        <entry>plus/minus sign in specified position</entry>
4578 4579
       </row>
       <row>
4580 4581
        <entry><literal>RN</literal></entry>
        <entry>roman numeral (input between 1 and 3999)</entry>
4582 4583
       </row>
       <row>
4584 4585
        <entry><literal>TH</literal> or <literal>th</literal></entry>
        <entry>ordinal number suffix</entry>
4586 4587
       </row>
       <row>
4588 4589
        <entry><literal>V</literal></entry>
        <entry>shift specified number of digits (see notes)</entry>
4590 4591
       </row>
       <row>
4592 4593
        <entry><literal>EEEE</literal></entry>
        <entry>scientific notation (not implemented yet)</entry>
4594 4595 4596 4597 4598 4599
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
T
Tom Lane 已提交
4600
    Usage notes for numeric formatting:
4601 4602 4603 4604

    <itemizedlist>
     <listitem>
      <para>
4605
       A sign formatted using <literal>SG</literal>, <literal>PL</literal>, or
4606
       <literal>MI</literal> is not anchored to
4607
       the number; for example,
4608 4609
       <literal>to_char(-12, 'S9999')</literal> produces <literal>'&nbsp;&nbsp;-12'</literal>,
       but <literal>to_char(-12, 'MI9999')</literal> produces <literal>'-&nbsp;&nbsp;12'</literal>.
4610 4611
       The Oracle implementation does not allow the use of
       <literal>MI</literal> ahead of <literal>9</literal>, but rather
4612
       requires that <literal>9</literal> precede
4613 4614 4615 4616 4617 4618
       <literal>MI</literal>.
      </para>
     </listitem>

     <listitem>
      <para>
4619
       <literal>9</literal> results in a value with the same number of 
4620
       digits as there are <literal>9</literal>s. If a digit is
4621
       not available it outputs a space.
4622 4623 4624 4625 4626
      </para>
     </listitem>

     <listitem>
      <para>
4627
       <literal>TH</literal> does not convert values less than zero
4628
       and does not convert fractional numbers.
4629 4630 4631 4632 4633
      </para>
     </listitem>

     <listitem>
      <para>
4634
       <literal>PL</literal>, <literal>SG</literal>, and
4635
       <literal>TH</literal> are <productname>PostgreSQL</productname>
4636
       extensions. 
4637 4638 4639 4640 4641 4642 4643 4644 4645 4646 4647
      </para>
     </listitem>

     <listitem>
      <para>
       <literal>V</literal> effectively
       multiplies the input values by
       <literal>10^<replaceable>n</replaceable></literal>, where
       <replaceable>n</replaceable> is the number of digits following
       <literal>V</literal>. 
       <function>to_char</function> does not support the use of
4648 4649
       <literal>V</literal> combined with a decimal point.
       (E.g., <literal>99.9V99</literal> is not allowed.)
4650 4651 4652
      </para>
     </listitem>
    </itemizedlist>
4653
   </para>   
4654

4655 4656 4657 4658 4659 4660
  <para>
   <xref linkend="functions-formatting-examples-table"> shows some
   examples of the use of the <function>to_char</function> function.
  </para>

    <table id="functions-formatting-examples-table">
4661
     <title><function>to_char</function> Examples</title>
4662 4663 4664
     <tgroup cols="2">
      <thead>
       <row>
4665 4666
        <entry>Expression</entry>
        <entry>Result</entry>
4667 4668 4669 4670
       </row>
      </thead>
      <tbody>
       <row>
4671 4672
        <entry><literal>to_char(current_timestamp, 'Day,&nbsp;DD&nbsp;&nbsp;HH12:MI:SS')</literal></entry>
        <entry><literal>'Tuesday&nbsp;&nbsp;,&nbsp;06&nbsp;&nbsp;05:39:18'</literal></entry>
4673
       </row>
4674
       <row>
4675 4676
        <entry><literal>to_char(current_timestamp, 'FMDay,&nbsp;FMDD&nbsp;&nbsp;HH12:MI:SS')</literal></entry>
        <entry><literal>'Tuesday,&nbsp;6&nbsp;&nbsp;05:39:18'</literal></entry>
4677 4678
       </row>          
       <row>
4679
        <entry><literal>to_char(-0.1, '99.99')</literal></entry>
B
Bruce Momjian 已提交
4680
        <entry><literal>'&nbsp;&nbsp;-.10'</literal></entry>
4681 4682
       </row>
       <row>
4683
        <entry><literal>to_char(-0.1, 'FM9.99')</literal></entry>
4684
        <entry><literal>'-.1'</literal></entry>
4685 4686
       </row>
       <row>
4687 4688
        <entry><literal>to_char(0.1, '0.9')</literal></entry>
        <entry><literal>'&nbsp;0.1'</literal></entry>
4689 4690
       </row>
       <row>
4691 4692
        <entry><literal>to_char(12, '9990999.9')</literal></entry>
        <entry><literal>'&nbsp;&nbsp;&nbsp;&nbsp;0012.0'</literal></entry>
4693 4694
       </row>
       <row>
4695
        <entry><literal>to_char(12, 'FM9990999.9')</literal></entry>
B
Bruce Momjian 已提交
4696
        <entry><literal>'0012.'</literal></entry>
4697 4698
       </row>
       <row>
4699 4700
        <entry><literal>to_char(485, '999')</literal></entry>
        <entry><literal>'&nbsp;485'</literal></entry>
4701 4702
       </row>
       <row>
4703
        <entry><literal>to_char(-485, '999')</literal></entry>
4704
        <entry><literal>'-485'</literal></entry>
4705 4706
       </row>
       <row>
4707 4708
        <entry><literal>to_char(485, '9&nbsp;9&nbsp;9')</literal></entry>
        <entry><literal>'&nbsp;4&nbsp;8&nbsp;5'</literal></entry>
4709 4710
       </row>
       <row>
4711 4712
        <entry><literal>to_char(1485, '9,999')</literal></entry>
        <entry><literal>'&nbsp;1,485'</literal></entry>
4713 4714
       </row>
       <row>
4715 4716
        <entry><literal>to_char(1485, '9G999')</literal></entry>
        <entry><literal>'&nbsp;1&nbsp;485'</literal></entry>
4717 4718
       </row>
       <row>
4719
        <entry><literal>to_char(148.5, '999.999')</literal></entry>
B
Bruce Momjian 已提交
4720
        <entry><literal>'&nbsp;148.500'</literal></entry>
4721
       </row>
B
Bruce Momjian 已提交
4722 4723 4724 4725 4726 4727 4728 4729
       <row>
        <entry><literal>to_char(148.5, 'FM999.999')</literal></entry>
        <entry><literal>'148.5'</literal></entry>
       </row>
       <row>
        <entry><literal>to_char(148.5, 'FM999.990')</literal></entry>
        <entry><literal>'148.500'</literal></entry>
       </row>
4730
       <row>
4731
        <entry><literal>to_char(148.5, '999D999')</literal></entry>
4732
        <entry><literal>'&nbsp;148,500'</literal></entry>        
4733 4734
       </row>
       <row>
4735 4736
        <entry><literal>to_char(3148.5, '9G999D999')</literal></entry>
        <entry><literal>'&nbsp;3&nbsp;148,500'</literal></entry>
4737 4738
       </row>
       <row>
4739
        <entry><literal>to_char(-485, '999S')</literal></entry>
4740
        <entry><literal>'485-'</literal></entry>
4741
       </row>
4742
       <row>            
4743
        <entry><literal>to_char(-485, '999MI')</literal></entry>
4744
        <entry><literal>'485-'</literal></entry>        
4745 4746
       </row>
       <row>
4747
        <entry><literal>to_char(485, '999MI')</literal></entry>
4748
        <entry><literal>'485&nbsp;'</literal></entry>           
B
Bruce Momjian 已提交
4749 4750 4751
       </row>
       <row>
        <entry><literal>to_char(485, 'FM999MI')</literal></entry>
4752
        <entry><literal>'485'</literal></entry>         
4753 4754
       </row>
       <row>
4755
        <entry><literal>to_char(485, 'PL999')</literal></entry>
4756
        <entry><literal>'+485'</literal></entry>        
4757
       </row>
4758
       <row>            
4759
        <entry><literal>to_char(485, 'SG999')</literal></entry>
4760
        <entry><literal>'+485'</literal></entry>        
4761 4762
       </row>
       <row>
4763
        <entry><literal>to_char(-485, 'SG999')</literal></entry>
4764
        <entry><literal>'-485'</literal></entry>        
4765 4766
       </row>
       <row>
4767
        <entry><literal>to_char(-485, '9SG99')</literal></entry>
4768
        <entry><literal>'4-85'</literal></entry>        
4769 4770
       </row>
       <row>
4771
        <entry><literal>to_char(-485, '999PR')</literal></entry>
4772
        <entry><literal>'&lt;485&gt;'</literal></entry>         
4773 4774
       </row>
       <row>
4775
        <entry><literal>to_char(485, 'L999')</literal></entry>
4776
        <entry><literal>'DM&nbsp;485</literal></entry>   
4777 4778
       </row>
       <row>
4779
        <entry><literal>to_char(485, 'RN')</literal></entry>            
4780
        <entry><literal>'&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;CDLXXXV'</literal></entry>
4781 4782
       </row>
       <row>
4783
        <entry><literal>to_char(485, 'FMRN')</literal></entry>  
4784
        <entry><literal>'CDLXXXV'</literal></entry>
4785 4786
       </row>
       <row>
4787
        <entry><literal>to_char(5.2, 'FMRN')</literal></entry>
4788
        <entry><literal>'V'</literal></entry>           
4789 4790
       </row>
       <row>
4791
        <entry><literal>to_char(482, '999th')</literal></entry>
4792
        <entry><literal>'&nbsp;482nd'</literal></entry>                         
4793 4794
       </row>
       <row>
4795 4796
        <entry><literal>to_char(485, '"Good&nbsp;number:"999')</literal></entry>
        <entry><literal>'Good&nbsp;number:&nbsp;485'</literal></entry>
4797 4798
       </row>
       <row>
4799 4800
        <entry><literal>to_char(485.8, '"Pre:"999"&nbsp;Post:"&nbsp;.999')</literal></entry>
        <entry><literal>'Pre:&nbsp;485&nbsp;Post:&nbsp;.800'</literal></entry>
4801 4802
       </row>
       <row>
4803
        <entry><literal>to_char(12, '99V999')</literal></entry>         
4804
        <entry><literal>'&nbsp;12000'</literal></entry>
4805 4806
       </row>
       <row>
4807 4808
        <entry><literal>to_char(12.4, '99V999')</literal></entry>
        <entry><literal>'&nbsp;12400'</literal></entry>
4809
       </row>
4810
       <row>            
4811
        <entry><literal>to_char(12.45, '99V9')</literal></entry>
4812
        <entry><literal>'&nbsp;125'</literal></entry>
4813 4814 4815 4816
       </row>
      </tbody>
     </tgroup>
    </table>
4817

4818 4819 4820
  </sect1>


4821
  <sect1 id="functions-datetime">
4822
   <title>Date/Time Functions and Operators</title>
4823

4824 4825 4826 4827 4828 4829 4830 4831
  <para>
   <xref linkend="functions-datetime-table"> shows the available
   functions for date/time value processing, with details appearing in
   the following subsections.  <xref
   linkend="operators-datetime-table"> illustrates the behaviors of
   the basic arithmetic operators (<literal>+</literal>,
   <literal>*</literal>, etc.).  For formatting functions, refer to
   <xref linkend="functions-formatting">.  You should be familiar with
4832 4833
   the background information on date/time data types from <xref
   linkend="datatype-datetime">.
4834 4835 4836
  </para>

  <para>
4837 4838 4839
   All the functions and operators described below that take <type>time</type> or <type>timestamp</type>
   inputs actually come in two variants: one that takes <type>time with time zone</type> or <type>timestamp
   with time zone</type>, and one that takes <type>time without time zone</type> or <type>timestamp without time zone</type>.
4840 4841 4842 4843
   For brevity, these variants are not shown separately.  Also, the
   <literal>+</> and <literal>*</> operators come in commutative pairs (for
   example both date + integer and integer + date); we show only one of each
   such pair.
4844
  </para>
4845 4846 4847 4848

    <table id="operators-datetime-table">
     <title>Date/Time Operators</title>

4849
     <tgroup cols="3">
4850 4851
      <thead>
       <row>
4852
        <entry>Operator</entry>
4853 4854 4855 4856 4857 4858 4859
        <entry>Example</entry>
        <entry>Result</entry>
       </row>
      </thead>

      <tbody>
       <row>
4860
        <entry> <literal>+</literal> </entry>
4861 4862
        <entry><literal>date '2001-09-28' + integer '7'</literal></entry>
        <entry><literal>date '2001-10-05'</literal></entry>
4863 4864 4865
       </row>

       <row>
4866
        <entry> <literal>+</literal> </entry>
4867 4868
        <entry><literal>date '2001-09-28' + interval '1 hour'</literal></entry>
        <entry><literal>timestamp '2001-09-28 01:00'</literal></entry>
4869 4870
       </row>

4871 4872 4873 4874 4875 4876 4877 4878 4879 4880 4881 4882 4883 4884 4885 4886 4887 4888
       <row>
        <entry> <literal>+</literal> </entry>
        <entry><literal>date '2001-09-28' + time '03:00'</literal></entry>
        <entry><literal>timestamp '2001-09-28 03:00'</literal></entry>
       </row>

       <row>
        <entry> <literal>+</literal> </entry>
        <entry><literal>interval '1 day' + interval '1 hour'</literal></entry>
        <entry><literal>interval '1 day 01:00'</literal></entry>
       </row>

       <row>
        <entry> <literal>+</literal> </entry>
        <entry><literal>timestamp '2001-09-28 01:00' + interval '23 hours'</literal></entry>
        <entry><literal>timestamp '2001-09-29 00:00'</literal></entry>
       </row>

4889
       <row>
4890
        <entry> <literal>+</literal> </entry>
4891 4892
        <entry><literal>time '01:00' + interval '3 hours'</literal></entry>
        <entry><literal>time '04:00'</literal></entry>
4893 4894 4895
       </row>

       <row>
4896
        <entry> <literal>-</literal> </entry>
4897 4898 4899 4900 4901 4902 4903 4904 4905 4906 4907 4908 4909 4910
        <entry><literal>- interval '23 hours'</literal></entry>
        <entry><literal>interval '-23:00'</literal></entry>
       </row>

       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>date '2001-10-01' - date '2001-09-28'</literal></entry>
        <entry><literal>integer '3'</literal></entry>
       </row>

       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>date '2001-10-01' - integer '7'</literal></entry>
        <entry><literal>date '2001-09-24'</literal></entry>
4911 4912 4913
       </row>

       <row>
4914
        <entry> <literal>-</literal> </entry>
4915 4916
        <entry><literal>date '2001-09-28' - interval '1 hour'</literal></entry>
        <entry><literal>timestamp '2001-09-27 23:00'</literal></entry>
4917 4918
       </row>

4919 4920 4921 4922 4923 4924
       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>time '05:00' - time '03:00'</literal></entry>
        <entry><literal>interval '02:00'</literal></entry>
       </row>

4925
       <row>
4926
        <entry> <literal>-</literal> </entry>
4927 4928
        <entry><literal>time '05:00' - interval '2 hours'</literal></entry>
        <entry><literal>time '03:00'</literal></entry>
4929 4930
       </row>

4931 4932 4933 4934 4935 4936 4937 4938 4939 4940 4941 4942 4943 4944 4945 4946 4947 4948
       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>timestamp '2001-09-28 23:00' - interval '23 hours'</literal></entry>
        <entry><literal>timestamp '2001-09-28 00:00'</literal></entry>
       </row>

       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>interval '1 day' - interval '1 hour'</literal></entry>
        <entry><literal>interval '23:00'</literal></entry>
       </row>

       <row>
        <entry> <literal>-</literal> </entry>
        <entry><literal>timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'</literal></entry>
        <entry><literal>interval '1 day 15:00'</literal></entry>
       </row>

4949 4950
       <row>
        <entry> <literal>*</literal> </entry>
4951 4952
        <entry><literal>interval '1 hour' * double precision '3.5'</literal></entry>
        <entry><literal>interval '03:30'</literal></entry>
4953 4954 4955 4956
       </row>

       <row>
        <entry> <literal>/</literal> </entry>
4957 4958
        <entry><literal>interval '1 hour' / double precision '1.5'</literal></entry>
        <entry><literal>interval '00:40'</literal></entry>
4959 4960 4961 4962 4963
       </row>
      </tbody>
     </tgroup>
    </table>

4964
    <table id="functions-datetime-table">
4965
     <title>Date/Time Functions</title>
4966
     <tgroup cols="5">
4967 4968
      <thead>
       <row>
4969 4970 4971 4972 4973
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
        <entry>Result</entry>
4974 4975
       </row>
      </thead>
4976

4977 4978
      <tbody>
       <row>
4979 4980 4981 4982 4983 4984
        <entry><literal><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</literal></entry>
        <entry><type>interval</type></entry>
        <entry>Subtract arguments, producing a <quote>symbolic</> result that
        uses years and months</entry>
        <entry><literal>age(timestamp '2001-04-10', timestamp '1957-06-13')</literal></entry>
        <entry><literal>43 years 9 mons 27 days</literal></entry>
4985
       </row>
4986

4987
       <row>
4988 4989 4990 4991 4992
        <entry><literal><function>age</function>(<type>timestamp</type>)</literal></entry>
        <entry><type>interval</type></entry>
        <entry>Subtract from <function>current_date</function></entry>
        <entry><literal>age(timestamp '1957-06-13')</literal></entry>
        <entry><literal>43 years 8 mons 3 days</literal></entry>
4993 4994 4995
       </row>

       <row>
4996 4997 4998 4999 5000 5001
        <entry><literal><function>current_date</function></literal></entry>
        <entry><type>date</type></entry>
        <entry>Today's date; see <xref linkend="functions-datetime-current">
        </entry>
        <entry></entry>
        <entry></entry>
5002
       </row>
5003 5004

       <row>
5005
        <entry><literal><function>current_time</function></literal></entry>
5006
        <entry><type>time with time zone</type></entry>
5007
        <entry>Time of day; see <xref linkend="functions-datetime-current">
5008 5009 5010
        </entry>
        <entry></entry>
        <entry></entry>
5011 5012
       </row>

5013
       <row>
5014 5015 5016 5017 5018 5019
        <entry><literal><function>current_timestamp</function></literal></entry>
        <entry><type>timestamp with time zone</type></entry>
        <entry>Date and time; see <xref linkend="functions-datetime-current">
        </entry>
        <entry></entry>
        <entry></entry>
5020 5021 5022
       </row>

       <row>
5023 5024 5025 5026
        <entry><literal><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>Get subfield (equivalent to
         <function>extract</function>); see <xref linkend="functions-datetime-extract">
5027
        </entry>
5028 5029
        <entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
        <entry><literal>20</literal></entry>
5030
       </row>
5031

5032
       <row>
5033 5034 5035 5036
        <entry><literal><function>date_part</function>(<type>text</type>, <type>interval</type>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>Get subfield (equivalent to
         <function>extract</function>); see <xref linkend="functions-datetime-extract">
5037
        </entry>
5038 5039
        <entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
        <entry><literal>3</literal></entry>
5040
       </row>
5041

5042
       <row>
5043 5044 5045
        <entry><literal><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</literal></entry>
        <entry><type>timestamp</type></entry>
        <entry>Truncate to specified precision; see also <xref
5046
                                                        linkend="functions-datetime-trunc">
5047
        </entry>
5048 5049
        <entry><literal>date_trunc('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
        <entry><literal>2001-02-16 20:00:00</literal></entry>
5050
       </row>
5051

5052
       <row>
5053
        <entry><literal><function>extract</function>(<parameter>field</parameter> from
5054
         <type>timestamp</type>)</literal></entry>
5055 5056
        <entry><type>double precision</type></entry>
        <entry>Get subfield; see <xref linkend="functions-datetime-extract">
5057
        </entry>
5058 5059
        <entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
        <entry><literal>20</literal></entry>
5060
       </row>
5061 5062

       <row>
5063
        <entry><literal><function>extract</function>(<parameter>field</parameter> from
5064
         <type>interval</type>)</literal></entry>
5065 5066
        <entry><type>double precision</type></entry>
        <entry>Get subfield; see <xref linkend="functions-datetime-extract">
5067
        </entry>
5068 5069
        <entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
        <entry><literal>3</literal></entry>
5070 5071
       </row>

5072
       <row>
5073 5074 5075 5076 5077
        <entry><literal><function>isfinite</function>(<type>timestamp</type>)</literal></entry>
        <entry><type>boolean</type></entry>
        <entry>Test for finite time stamp (not equal to infinity)</entry>
        <entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
        <entry><literal>true</literal></entry>
5078
       </row>
5079

5080
       <row>
5081 5082 5083 5084 5085
        <entry><literal><function>isfinite</function>(<type>interval</type>)</literal></entry>
        <entry><type>boolean</type></entry>
        <entry>Test for finite interval</entry>
        <entry><literal>isfinite(interval '4 hours')</literal></entry>
        <entry><literal>true</literal></entry>
5086
       </row>
5087

5088
       <row>
5089
        <entry><literal><function>localtime</function></literal></entry>
5090
        <entry><type>time</type></entry>
5091
        <entry>Time of day; see <xref linkend="functions-datetime-current">
5092 5093 5094 5095 5096 5097
        </entry>
        <entry></entry>
        <entry></entry>
       </row>

       <row>
5098
        <entry><literal><function>localtimestamp</function></literal></entry>
5099
        <entry><type>timestamp</type></entry>
5100
        <entry>Date and time; see <xref linkend="functions-datetime-current">
5101 5102 5103 5104 5105
        </entry>
        <entry></entry>
        <entry></entry>
       </row>

5106
       <row>
5107 5108 5109 5110
        <entry><literal><function>now</function>()</literal></entry>
        <entry><type>timestamp with time zone</type></entry>
        <entry>Current date and time (equivalent to
         <function>current_timestamp</function>); see <xref
5111
                                                     linkend="functions-datetime-current">
5112 5113 5114
        </entry>
        <entry></entry>
        <entry></entry>
5115
       </row>
5116

5117
       <row>
5118 5119 5120
        <entry><literal><function>timeofday()</function></literal></entry>
        <entry><type>text</type></entry>
        <entry>Current date and time; see <xref
5121
                                         linkend="functions-datetime-current">
5122 5123 5124
        </entry>
        <entry></entry>
        <entry></entry>
5125 5126
       </row>

5127 5128 5129
      </tbody>
     </tgroup>
    </table>
5130

5131
   <para>
5132
    In addition to these functions, the SQL <literal>OVERLAPS</> operator is
5133 5134 5135 5136 5137 5138 5139
    supported:
<synopsis>
( <replaceable>start1</replaceable>, <replaceable>end1</replaceable> ) OVERLAPS ( <replaceable>start2</replaceable>, <replaceable>end2</replaceable> )
( <replaceable>start1</replaceable>, <replaceable>length1</replaceable> ) OVERLAPS ( <replaceable>start2</replaceable>, <replaceable>length2</replaceable> )
</synopsis>
    This expression yields true when two time periods (defined by their
    endpoints) overlap, false when they do not overlap.  The endpoints
5140 5141
    can be specified as pairs of dates, times, or time stamps; or as
    a date, time, or time stamp followed by an interval.
5142 5143 5144 5145 5146 5147 5148 5149 5150 5151 5152
   </para>

<screen>
SELECT (DATE '2001-02-16', DATE '2001-12-21') OVERLAPS
       (DATE '2001-10-30', DATE '2002-10-30');
<lineannotation>Result: </lineannotation><computeroutput>true</computeroutput>
SELECT (DATE '2001-02-16', INTERVAL '100 days') OVERLAPS
       (DATE '2001-10-30', DATE '2002-10-30');
<lineannotation>Result: </lineannotation><computeroutput>false</computeroutput>
</screen>

5153 5154 5155 5156 5157 5158 5159 5160
  <sect2 id="functions-datetime-extract">
   <title><function>EXTRACT</function>, <function>date_part</function></title>

<synopsis>
EXTRACT (<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
</synopsis>

   <para>
5161
    The <function>extract</function> function retrieves subfields
T
Tom Lane 已提交
5162 5163 5164 5165
    such as year or hour from date/time values.
    <replaceable>source</replaceable> must be a value expression of
    type <type>timestamp</type>, <type>time</type>, or <type>interval</type>.
    (Expressions of type <type>date</type> will
5166
    be cast to <type>timestamp</type> and can therefore be used as
5167 5168
    well.)  <replaceable>field</replaceable> is an identifier or
    string that selects what field to extract from the source value.
5169 5170
    The <function>extract</function> function returns values of type
    <type>double precision</type>.
5171
    The following are valid field names:
5172 5173 5174 5175

    <!-- alphabetical -->
    <variablelist>
     <varlistentry>
5176
      <term><literal>century</literal></term>
5177 5178
      <listitem>
       <para>
5179
        The century
5180 5181 5182
       </para>

<screen>
5183
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
5184
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
5185 5186
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
5187 5188 5189
</screen>

       <para>
5190 5191 5192 5193 5194 5195 5196
        The first century starts at 0001-01-01 00:00:00 AD, although
        they did not know it at the time. This definition applies to all
        Gregorian calendar countries. There is no century number 0,
        you go from -1 to 1.

        If you disagree with this, please write your complaint to:
        Pope, Cathedral Saint-Peter of Roma, Vatican.
5197 5198 5199
       </para>

       <para>
5200 5201 5202
        <productname>PostgreSQL</productname> releases before 8.0 did not
        follow the conventional numbering of centuries, but just returned
        the year field divided by 100.
5203 5204 5205 5206 5207
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
5208
      <term><literal>day</literal></term>
5209 5210 5211 5212 5213 5214 5215 5216 5217 5218 5219 5220 5221
      <listitem>
       <para>
        The day (of the month) field (1 - 31)
       </para>

<screen>
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5222
      <term><literal>decade</literal></term>
5223 5224 5225 5226 5227 5228 5229 5230 5231 5232 5233 5234 5235
      <listitem>
       <para>
        The year field divided by 10
       </para>

<screen>
SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>200</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5236
      <term><literal>dow</literal></term>
5237 5238 5239 5240 5241 5242 5243 5244 5245 5246
      <listitem>
       <para>
        The day of the week (0 - 6; Sunday is 0) (for
        <type>timestamp</type> values only)
       </para>

<screen>
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>5</computeroutput>
</screen>
5247 5248 5249 5250 5251
       <para>
        Note that <function>extract</function>'s day of the week numbering is 
        different from that of the <function>to_char</function> function.
       </para>

5252 5253 5254 5255
      </listitem>
     </varlistentry>

     <varlistentry>
5256
      <term><literal>doy</literal></term>
5257 5258 5259 5260
      <listitem>
       <para>
        The day of the year (1 - 365/366) (for <type>timestamp</type> values only)
       </para>
5261

5262 5263 5264 5265 5266 5267 5268 5269
<screen>
SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>47</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5270
      <term><literal>epoch</literal></term>
5271 5272 5273
      <listitem>
       <para>
        For <type>date</type> and <type>timestamp</type> values, the
5274
        number of seconds since 1970-01-01 00:00:00-00 (can be negative);
5275
        for <type>interval</type> values, the total number
5276 5277 5278 5279
        of seconds in the interval
       </para>

<screen>
5280 5281
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');
<lineannotation>Result: </lineannotation><computeroutput>982384720</computeroutput>
5282 5283 5284

SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
<lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
5285 5286 5287 5288 5289 5290 5291 5292
</screen>

       <para>
        Here is how you can convert an epoch value back to a time
        stamp:
       </para>

<screen>
5293
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720 * INTERVAL '1 second';
5294 5295 5296 5297 5298
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5299
      <term><literal>hour</literal></term>
5300 5301 5302 5303 5304 5305 5306 5307 5308 5309 5310 5311 5312
      <listitem>
       <para>
        The hour field (0 - 23)
       </para>

<screen>
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5313
      <term><literal>microseconds</literal></term>
5314 5315 5316 5317 5318 5319 5320 5321 5322 5323 5324 5325 5326 5327
      <listitem>
       <para>
        The seconds field, including fractional parts, multiplied by 1
        000 000.  Note that this includes full seconds.
       </para>

<screen>
SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28500000</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5328
      <term><literal>millennium</literal></term>
5329 5330
      <listitem>
       <para>
5331
        The millennium
5332 5333 5334 5335
       </para>

<screen>
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
5336
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
5337 5338 5339
</screen>

       <para>
5340 5341 5342 5343 5344 5345 5346 5347
        Years in the 1900s are in the second millennium.
        The third millennium starts January 1, 2001.
       </para>

       <para>
        <productname>PostgreSQL</productname> releases before 8.0 did not
        follow the conventional numbering of millennia, but just returned
        the year field divided by 1000.
5348 5349 5350 5351 5352
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
5353
      <term><literal>milliseconds</literal></term>
5354 5355 5356 5357 5358 5359 5360 5361 5362 5363 5364 5365 5366 5367
      <listitem>
       <para>
        The seconds field, including fractional parts, multiplied by
        1000.  Note that this includes full seconds.
       </para>

<screen>
SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28500</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5368
      <term><literal>minute</literal></term>
5369 5370 5371 5372 5373 5374 5375 5376 5377 5378 5379 5380 5381
      <listitem>
       <para>
        The minutes field (0 - 59)
       </para>

<screen>
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>38</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5382
      <term><literal>month</literal></term>
5383 5384 5385 5386 5387 5388 5389 5390 5391 5392 5393 5394 5395 5396 5397 5398 5399 5400 5401 5402 5403
      <listitem>
       <para>
        For <type>timestamp</type> values, the number of the month
        within the year (1 - 12) ; for <type>interval</type> values
        the number of months, modulo 12 (0 - 11)
       </para>

<screen>
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>

SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5404
      <term><literal>quarter</literal></term>
5405 5406 5407 5408 5409 5410 5411 5412 5413 5414 5415 5416 5417 5418
      <listitem>
       <para>
        The quarter of the year (1 - 4) that the day is in (for
        <type>timestamp</type> values only)
       </para>

<screen>
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5419
      <term><literal>second</literal></term>
5420 5421 5422 5423 5424 5425 5426 5427 5428 5429 5430 5431 5432 5433 5434 5435
      <listitem>
       <para>
        The seconds field, including fractional parts (0 -
        59<footnote><simpara>60 if leap seconds are
        implemented by the operating system</simpara></footnote>)
       </para>

<screen>
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>

SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
<lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
</screen>
      </listitem>
     </varlistentry>
5436 5437 5438 5439
     <varlistentry>
      <term><literal>timezone</literal></term>
      <listitem>
       <para>
5440
        The time zone offset from UTC, measured in seconds.  Positive values
5441 5442
        correspond to time zones east of UTC, negative values to
        zones west of UTC.
5443 5444 5445 5446 5447 5448 5449 5450
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><literal>timezone_hour</literal></term>
      <listitem>
       <para>
5451
        The hour component of the time zone offset
5452 5453 5454 5455 5456 5457 5458 5459
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
      <term><literal>timezone_minute</literal></term>
      <listitem>
       <para>
5460
        The minute component of the time zone offset
5461 5462 5463
       </para>
      </listitem>
     </varlistentry>
5464 5465

     <varlistentry>
5466
      <term><literal>week</literal></term>
5467 5468
      <listitem>
       <para>
5469
        The number of the week of the year that the day is in.  By definition
5470
        (<acronym>ISO</acronym> 8601), the first week of a year
5471
        contains January 4 of that year.  (The <acronym>ISO</acronym>-8601
5472
        week starts on Monday.)  In other words, the first Thursday of
5473
        a year is in week 1 of that year. (for <type>timestamp</type> values only)
5474
       </para>
5475 5476 5477 5478 5479 5480
       <para>
        Because of this, it is possible for early January dates to be part of the 
        52nd or 53rd week of the previous year.  For example, <literal>2005-01-01</>
        is part of the 53rd week of year 2004, and <literal>2006-01-01</> is part of 
        the 52nd week of year 2005.
       </para>
5481 5482 5483 5484 5485 5486 5487 5488 5489

<screen>
SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>7</computeroutput>
</screen>
      </listitem>
     </varlistentry>

     <varlistentry>
5490
      <term><literal>year</literal></term>
5491 5492
      <listitem>
       <para>
5493 5494
        The year field.  Keep in mind there is no <literal>0 AD</>, so subtracting 
        <literal>BC</> years from <literal>AD</> years should be done with care.
5495 5496 5497 5498 5499 5500 5501 5502 5503 5504 5505
       </para>

<screen>
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>2001</computeroutput>
</screen>
      </listitem>
     </varlistentry>

    </variablelist>

5506 5507 5508
   </para>

   <para>
5509 5510 5511
    The <function>extract</function> function is primarily intended
    for computational processing.  For formatting date/time values for
    display, see <xref linkend="functions-formatting">.
5512
   </para>
5513 5514

   <para>
5515 5516
    The <function>date_part</function> function is modeled on the traditional
    <productname>Ingres</productname> equivalent to the
5517
    <acronym>SQL</acronym>-standard function <function>extract</function>:
5518 5519 5520
<synopsis>
date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
</synopsis>
5521
    Note that here the <replaceable>field</replaceable> parameter needs to
5522
    be a string value, not a name.  The valid field names for
5523 5524 5525 5526 5527 5528 5529 5530
    <function>date_part</function> are the same as for
    <function>extract</function>.
   </para>

<screen>
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>

T
Tatsuo Ishii 已提交
5531
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
5532 5533 5534 5535 5536 5537 5538 5539 5540 5541 5542 5543 5544 5545 5546 5547 5548 5549
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
</screen>

  </sect2>

  <sect2 id="functions-datetime-trunc">
   <title><function>date_trunc</function></title>

   <para>
    The function <function>date_trunc</function> is conceptually
    similar to the <function>trunc</function> function for numbers.
   </para>

   <para>
<synopsis>
date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
</synopsis>
    <replaceable>source</replaceable> is a value expression of type
5550 5551 5552 5553
    <type>timestamp</type> or <type>interval</>.
    (Values of type <type>date</type> and
    <type>time</type> are cast automatically, to <type>timestamp</type> or
    <type>interval</> respectively.)
5554
    <replaceable>field</replaceable> selects to which precision to
5555 5556 5557
    truncate the input value.  The return value is of type
    <type>timestamp</type> or <type>interval</>
    with all fields that are less significant than the
5558 5559 5560 5561 5562 5563
    selected one set to zero (or one, for day and month).
   </para>

   <para>
    Valid values for <replaceable>field</replaceable> are:
    <simplelist>
5564 5565 5566 5567 5568 5569
     <member><literal>microseconds</literal></member>
     <member><literal>milliseconds</literal></member>
     <member><literal>second</literal></member>
     <member><literal>minute</literal></member>
     <member><literal>hour</literal></member>
     <member><literal>day</literal></member>
5570
     <member><literal>week</literal></member>
5571 5572 5573 5574 5575
     <member><literal>month</literal></member>
     <member><literal>year</literal></member>
     <member><literal>decade</literal></member>
     <member><literal>century</literal></member>
     <member><literal>millennium</literal></member>
5576 5577 5578
    </simplelist>
   </para>

5579 5580
   <para>
    Examples:
5581 5582
<screen>
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
5583
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
5584 5585

SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
5586
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
5587
</screen>
5588
   </para>
5589 5590
  </sect2>

5591
  <sect2 id="functions-datetime-zoneconvert">
5592
   <title><literal>AT TIME ZONE</literal></title>
5593 5594

   <indexterm>
5595
    <primary>time zone</primary>
5596 5597 5598 5599
    <secondary>conversion</secondary>
   </indexterm>

   <para>
5600 5601 5602 5603
    The <literal>AT TIME ZONE</literal> construct allows conversions
    of time stamps to different time zones.  <xref
    linkend="functions-datetime-zoneconvert-table"> shows its
    variants.
5604 5605 5606
   </para>

    <table id="functions-datetime-zoneconvert-table">
5607
     <title><literal>AT TIME ZONE</literal> Variants</title>
5608 5609 5610
     <tgroup cols="3">
      <thead>
       <row>
5611 5612 5613
        <entry>Expression</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
5614 5615 5616 5617 5618
       </row>
      </thead>

      <tbody>
       <row>
5619 5620 5621 5622 5623
        <entry>
         <literal><type>timestamp without time zone</type> AT TIME ZONE <replaceable>zone</></literal>
        </entry>
        <entry><type>timestamp with time zone</type></entry>
        <entry>Convert local time in given time zone to UTC</entry>
5624 5625 5626
       </row>

       <row>
5627 5628 5629 5630 5631
        <entry>
         <literal><type>timestamp with time zone</type> AT TIME ZONE <replaceable>zone</></literal>
        </entry>
        <entry><type>timestamp without time zone</type></entry>
        <entry>Convert UTC to local time in given time zone</entry>
5632 5633 5634
       </row>

       <row>
5635 5636 5637 5638 5639
        <entry>
         <literal><type>time with time zone</type> AT TIME ZONE <replaceable>zone</></literal>
        </entry>
        <entry><type>time with time zone</type></entry>
        <entry>Convert local time across time zones</entry>
5640 5641 5642 5643 5644 5645
       </row>
      </tbody>
     </tgroup>
    </table>

   <para>
5646
    In these expressions, the desired time zone <replaceable>zone</> can be
5647 5648
    specified either as a text string (e.g., <literal>'PST'</literal>)
    or as an interval (e.g., <literal>INTERVAL '-08:00'</literal>).
5649 5650 5651 5652 5653
    In the text case, the available zone names are those shown in
    <xref linkend="datetime-timezone-input-table">.  (It would be useful
    to support the more general names shown in
    <xref linkend="datetime-timezone-set-table">, but this is not yet
    implemented.)
5654 5655 5656
   </para>

   <para>
5657
    Examples (supposing that the local time zone is <literal>PST8PDT</>):
5658 5659 5660 5661 5662 5663 5664
<screen>
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST';
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>

SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST';
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
</screen>
5665 5666 5667 5668
    The first example takes a zone-less time stamp and interprets it as MST time
    (UTC-7) to produce a UTC time stamp, which is then rotated to PST (UTC-8)
    for display.  The second example takes a time stamp specified in EST
    (UTC-5) and converts it to local time in MST (UTC-7).
5669 5670 5671
   </para>

   <para>
5672 5673 5674 5675
    The function <literal><function>timezone</function>(<replaceable>zone</>,
    <replaceable>timestamp</>)</literal> is equivalent to the SQL-conforming construct
    <literal><replaceable>timestamp</> AT TIME ZONE
    <replaceable>zone</></literal>. 
5676 5677 5678
   </para>
  </sect2>

5679 5680 5681
  <sect2 id="functions-datetime-current">
   <title>Current Date/Time</title>

5682 5683 5684 5685 5686 5687 5688 5689 5690 5691
   <indexterm>
    <primary>date</primary>
    <secondary>current</secondary>
   </indexterm>

   <indexterm>
    <primary>time</primary>
    <secondary>current</secondary>
   </indexterm>

5692
   <para>
5693 5694
    The following functions are available to obtain the current date and/or
    time:
5695
<synopsis>
5696
CURRENT_DATE
5697
CURRENT_TIME
5698
CURRENT_TIMESTAMP
5699 5700
CURRENT_TIME ( <replaceable>precision</replaceable> )
CURRENT_TIMESTAMP ( <replaceable>precision</replaceable> )
5701 5702 5703 5704
LOCALTIME
LOCALTIMESTAMP
LOCALTIME ( <replaceable>precision</replaceable> )
LOCALTIMESTAMP ( <replaceable>precision</replaceable> )
5705
</synopsis>
5706 5707 5708 5709 5710 5711 5712 5713 5714 5715
    </para>

    <para>
     <function>CURRENT_TIME</function> and
     <function>CURRENT_TIMESTAMP</function> deliver values with time zone;
     <function>LOCALTIME</function> and
     <function>LOCALTIMESTAMP</function> deliver values without time zone.
    </para>

    <para>
5716 5717 5718 5719 5720 5721
     <function>CURRENT_TIME</function>,
     <function>CURRENT_TIMESTAMP</function>,
     <function>LOCALTIME</function>, and
     <function>LOCALTIMESTAMP</function>
     can optionally be given
     a precision parameter, which causes the result to be rounded
5722
     to that many fractional digits in the seconds field.  Without a precision parameter,
5723
     the result is given to the full available precision.
5724 5725
    </para>

5726 5727 5728 5729 5730 5731 5732
    <note>
     <para>
      Prior to <productname>PostgreSQL</productname> 7.2, the precision
      parameters were unimplemented, and the result was always given
      in integer seconds.
     </para>
    </note>
5733

5734 5735 5736
   <para>
    Some examples:
<screen>
5737
SELECT CURRENT_TIME;
5738
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
5739 5740

SELECT CURRENT_DATE;
5741
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23</computeroutput>
5742 5743

SELECT CURRENT_TIMESTAMP;
5744
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.662522-05</computeroutput>
5745 5746

SELECT CURRENT_TIMESTAMP(2);
5747
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.66-05</computeroutput>
5748 5749

SELECT LOCALTIMESTAMP;
5750
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.662522</computeroutput>
5751
</screen>
5752
   </para>
5753 5754 5755

   <para>
    The function <function>now()</function> is the traditional
5756
    <productname>PostgreSQL</productname> equivalent to
5757
    <function>CURRENT_TIMESTAMP</function>.
5758 5759 5760
   </para>

   <para>
5761
    There is also the function <function>timeofday()</function>, which for historical
5762
    reasons returns a <type>text</type> string rather than a <type>timestamp</type> value:
5763 5764
<screen>
SELECT timeofday();
5765
<lineannotation>Result: </lineannotation><computeroutput>Sat Feb 17 19:07:32.000126 2001 EST</computeroutput>
5766
</screen>
5767
   </para>
5768 5769

   <para>
5770
    It is important to know that
5771 5772
    <function>CURRENT_TIMESTAMP</function> and related functions return
    the start time of the current transaction; their values do not
5773 5774 5775 5776
    change during the transaction. This is considered a feature:
    the intent is to allow a single transaction to have a consistent
    notion of the <quote>current</quote> time, so that multiple
    modifications within the same transaction bear the same
5777
    time stamp. <function>timeofday()</function>
5778
    returns the wall-clock time and does advance during transactions.
5779 5780
   </para>

B
Bruce Momjian 已提交
5781
   <note>
5782 5783
    <para>
     Other database systems may advance these values more
B
Bruce Momjian 已提交
5784 5785
     frequently.
    </para>
5786 5787
   </note>

5788
   <para>
5789
    All the date/time data types also accept the special literal value
5790
    <literal>now</literal> to specify the current date and time.  Thus,
5791
    the following three all return the same result:
5792 5793 5794
<programlisting>
SELECT CURRENT_TIMESTAMP;
SELECT now();
5795
SELECT TIMESTAMP 'now';  -- incorrect for use with DEFAULT
5796
</programlisting>
5797 5798
   </para>

T
Tom Lane 已提交
5799
    <tip>
5800
     <para>
5801 5802
      You do not want to use the third form when specifying a <literal>DEFAULT</>
      clause while creating a table.  The system will convert <literal>now</literal>
5803
      to a <type>timestamp</type> as soon as the constant is parsed, so that when
5804
      the default value is needed,
5805 5806
      the time of the table creation would be used!  The first two
      forms will not be evaluated until the default value is used,
5807 5808
      because they are function calls.  Thus they will give the desired
      behavior of defaulting to the time of row insertion.
5809
     </para>
T
Tom Lane 已提交
5810
    </tip>
5811 5812
  </sect2>
 </sect1>
5813 5814 5815

  
 <sect1 id="functions-geometry">
5816
  <title>Geometric Functions and Operators</title>
5817 5818

   <para>
5819 5820 5821
    The geometric types <type>point</type>, <type>box</type>,
    <type>lseg</type>, <type>line</type>, <type>path</type>,
    <type>polygon</type>, and <type>circle</type> have a large set of
5822 5823 5824 5825
    native support functions and operators, shown in <xref
    linkend="functions-geometry-op-table">, <xref
    linkend="functions-geometry-func-table">, and <xref
    linkend="functions-geometry-conv-table">.
5826 5827
   </para>

5828
   <table id="functions-geometry-op-table">
5829 5830 5831 5832
     <title>Geometric Operators</title>
     <tgroup cols="3">
      <thead>
       <row>
5833 5834 5835
        <entry>Operator</entry>
        <entry>Description</entry>
        <entry>Example</entry>
5836 5837 5838 5839
       </row>
      </thead>
      <tbody>
       <row>
5840 5841 5842
        <entry> <literal>+</literal> </entry>
        <entry>Translation</entry>
        <entry><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></entry>
5843 5844
       </row>
       <row>
5845 5846 5847
        <entry> <literal>-</literal> </entry>
        <entry>Translation</entry>
        <entry><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></entry>
5848 5849
       </row>
       <row>
5850 5851 5852
        <entry> <literal>*</literal> </entry>
        <entry>Scaling/rotation</entry>
        <entry><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></entry>
5853 5854
       </row>
       <row>
5855 5856 5857
        <entry> <literal>/</literal> </entry>
        <entry>Scaling/rotation</entry>
        <entry><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></entry>
5858 5859
       </row>
       <row>
5860 5861 5862
        <entry> <literal>#</literal> </entry>
        <entry>Point or box of intersection</entry>
        <entry><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></entry>
5863 5864
       </row>
       <row>
5865 5866 5867
        <entry> <literal>#</literal> </entry>
        <entry>Number of points in path or polygon</entry>
        <entry><literal># '((1,0),(0,1),(-1,0))'</literal></entry>
5868
       </row>
5869
       <row>
5870 5871 5872
        <entry> <literal>@-@</literal> </entry>
        <entry>Length or circumference</entry>
        <entry><literal>@-@ path '((0,0),(1,0))'</literal></entry>
5873 5874
       </row>
       <row>
5875 5876 5877
        <entry> <literal>@@</literal> </entry>
        <entry>Center</entry>
        <entry><literal>@@ circle '((0,0),10)'</literal></entry>
5878
       </row>
5879
       <row>
5880 5881 5882
        <entry> <literal>##</literal> </entry>
        <entry>Closest point to first operand on second operand</entry>
        <entry><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></entry>
5883
       </row>
5884
       <row>
5885 5886 5887
        <entry> <literal>&lt;-&gt;</literal> </entry>
        <entry>Distance between</entry>
        <entry><literal>circle '((0,0),1)' &lt;-&gt; circle '((5,0),1)'</literal></entry>
5888
       </row>
5889
       <row>
5890 5891 5892
        <entry> <literal>&amp;&amp;</literal> </entry>
        <entry>Overlaps?</entry>
        <entry><literal>box '((0,0),(1,1))' &amp;&amp; box '((0,0),(2,2))'</literal></entry>
5893 5894
       </row>
       <row>
5895 5896 5897
        <entry> <literal>&amp;&lt;</literal> </entry>
        <entry>Does not extend to the right of?</entry>
        <entry><literal>box '((0,0),(1,1))' &amp;&lt; box '((0,0),(2,2))'</literal></entry>
5898 5899
       </row>
       <row>
5900 5901 5902
        <entry> <literal>&amp;&gt;</literal> </entry>
        <entry>Does not extend to the left of?</entry>
        <entry><literal>box '((0,0),(3,3))' &amp;&gt; box '((0,0),(2,2))'</literal></entry>
5903 5904
       </row>
       <row>
5905 5906 5907
        <entry> <literal>&lt;&lt;</literal> </entry>
        <entry>Is left of?</entry>
        <entry><literal>circle '((0,0),1)' &lt;&lt; circle '((5,0),1)'</literal></entry>
5908 5909
       </row>
       <row>
5910 5911 5912
        <entry> <literal>&gt;&gt;</literal> </entry>
        <entry>Is right of?</entry>
        <entry><literal>circle '((5,0),1)' &gt;&gt; circle '((0,0),1)'</literal></entry>
5913
       </row>
5914
       <row>
5915 5916 5917
        <entry> <literal>&lt;^</literal> </entry>
        <entry>Is below?</entry>
        <entry><literal>circle '((0,0),1)' &lt;^ circle '((0,5),1)'</literal></entry>
5918
       </row>
5919
       <row>
5920 5921
        <entry> <literal>&gt;^</literal> </entry>
        <entry>Is above?</entry>
5922
        <entry><literal>circle '((0,5),1)' &gt;^ circle '((0,0),1)'</literal></entry>
5923 5924
       </row>
       <row>
5925 5926 5927
        <entry> <literal>?#</literal> </entry>
        <entry>Intersects?</entry>
        <entry><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></entry>
5928 5929
       </row>
       <row>
5930 5931 5932
        <entry> <literal>?-</literal> </entry>
        <entry>Is horizontal?</entry>
        <entry><literal>?- lseg '((-1,0),(1,0))'</literal></entry>
5933 5934
       </row>
       <row>
5935 5936 5937
        <entry> <literal>?-</literal> </entry>
        <entry>Are horizontally aligned?</entry>
        <entry><literal>point '(1,0)' ?- point '(0,0)'</literal></entry>
5938 5939
       </row>
       <row>
5940 5941 5942
        <entry> <literal>?|</literal> </entry>
        <entry>Is vertical?</entry>
        <entry><literal>?| lseg '((-1,0),(1,0))'</literal></entry>
5943 5944
       </row>
       <row>
5945 5946 5947
        <entry> <literal>?|</literal> </entry>
        <entry>Are vertically aligned?</entry>
        <entry><literal>point '(0,1)' ?| point '(0,0)'</literal></entry>
5948
       </row>
5949
       <row>
5950 5951 5952
        <entry> <literal>?-|</literal> </entry>
        <entry>Is perpendicular?</entry>
        <entry><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></entry>
5953
       </row>
5954
       <row>
5955 5956 5957
        <entry> <literal>?||</literal> </entry>
        <entry>Are parallel?</entry>
        <entry><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></entry>
5958 5959
       </row>
       <row>
5960 5961 5962
        <entry> <literal>~</literal> </entry>
        <entry>Contains?</entry>
        <entry><literal>circle '((0,0),2)' ~ point '(1,1)'</literal></entry>
5963 5964
       </row>
       <row>
5965 5966 5967
        <entry> <literal>@</literal> </entry>
        <entry>Contained in or on?</entry>
        <entry><literal>point '(1,1)' @ circle '((0,0),2)'</literal></entry>
5968 5969
       </row>
       <row>
5970 5971 5972
        <entry> <literal>~=</literal> </entry>
        <entry>Same as?</entry>
        <entry><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></entry>
5973 5974 5975 5976
       </row>
      </tbody>
     </tgroup>
   </table>
5977

5978 5979

   <table id="functions-geometry-func-table">
T
Thomas G. Lockhart 已提交
5980 5981 5982 5983
     <title>Geometric Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
5984 5985 5986 5987
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
T
Thomas G. Lockhart 已提交
5988 5989 5990 5991
       </row>
      </thead>
      <tbody>
       <row>
5992 5993 5994 5995
        <entry><literal><function>area</function>(<replaceable>object</>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>area</entry>
        <entry><literal>area(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
5996 5997
       </row>
       <row>
5998 5999 6000 6001
        <entry><literal><function>box_intersect</function>(<type>box</>, <type>box</>)</literal></entry>
        <entry><type>box</type></entry>
        <entry>intersection box</entry>
        <entry><literal>box_intersect(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
6002 6003
       </row>
       <row>
6004 6005 6006 6007
        <entry><literal><function>center</function>(<replaceable>object</>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>center</entry>
        <entry><literal>center(box '((0,0),(1,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
6008 6009
       </row>
       <row>
6010 6011 6012 6013
        <entry><literal><function>diameter</function>(<type>circle</>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>diameter of circle</entry>
        <entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6014 6015
       </row>
       <row>
6016 6017 6018 6019
        <entry><literal><function>height</function>(<type>box</>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>vertical size of box</entry>
        <entry><literal>height(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
6020 6021
       </row>
       <row>
6022 6023 6024 6025
        <entry><literal><function>isclosed</function>(<type>path</>)</literal></entry>
        <entry><type>boolean</type></entry>
        <entry>a closed path?</entry>
        <entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6026 6027
       </row>
       <row>
6028 6029 6030 6031
        <entry><literal><function>isopen</function>(<type>path</>)</literal></entry>
        <entry><type>boolean</type></entry>
        <entry>an open path?</entry>
        <entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
6032 6033
       </row>
       <row>
6034 6035 6036 6037
        <entry><literal><function>length</function>(<replaceable>object</>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>length</entry>
        <entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6038
       </row>
6039
       <row>
6040 6041 6042 6043
        <entry><literal><function>npoints</function>(<type>path</>)</literal></entry>
        <entry><type>integer</type></entry>
        <entry>number of points</entry>
        <entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
6044 6045
       </row>
       <row>
6046 6047 6048 6049
        <entry><literal><function>npoints</function>(<type>polygon</>)</literal></entry>
        <entry><type>integer</type></entry>
        <entry>number of points</entry>
        <entry><literal>npoints(polygon '((1,1),(0,0))')</literal></entry>
6050
       </row>
T
Thomas G. Lockhart 已提交
6051
       <row>
6052 6053 6054 6055
        <entry><literal><function>pclose</function>(<type>path</>)</literal></entry>
        <entry><type>path</type></entry>
        <entry>convert path to closed</entry>
        <entry><literal>pclose(path '[(0,0),(1,1),(2,0)]')</literal></entry>
T
Thomas G. Lockhart 已提交
6056
       </row>
6057 6058
<![IGNORE[
<!-- Not defined by this name. Implements the intersection operator '#' -->
T
Thomas G. Lockhart 已提交
6059
       <row>
6060 6061 6062 6063
        <entry><literal><function>point</function>(<type>lseg</>, <type>lseg</>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>intersection</entry>
        <entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
6064
       </row>
6065
]]>
T
Thomas G. Lockhart 已提交
6066
       <row>
6067 6068 6069 6070
        <entry><literal><function>popen</function>(<type>path</>)</literal></entry>
        <entry><type>path</type></entry>
        <entry>convert path to open</entry>
        <entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6071 6072
       </row>
       <row>
6073 6074 6075 6076
        <entry><literal><function>radius</function>(<type>circle</type>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>radius of circle</entry>
        <entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6077 6078
       </row>
       <row>
6079 6080 6081 6082
        <entry><literal><function>width</function>(<type>box</>)</literal></entry>
        <entry><type>double precision</type></entry>
        <entry>horizontal size of box</entry>
        <entry><literal>width(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
6083 6084 6085
       </row>
      </tbody>
     </tgroup>
6086
   </table>
6087

6088

6089
   <table id="functions-geometry-conv-table">
T
Thomas G. Lockhart 已提交
6090 6091 6092 6093
     <title>Geometric Type Conversion Functions</title>
     <tgroup cols="4">
      <thead>
       <row>
6094 6095 6096 6097
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
T
Thomas G. Lockhart 已提交
6098 6099 6100 6101
       </row>
      </thead>
      <tbody>
       <row>
6102 6103 6104 6105
        <entry><literal><function>box</function>(<type>circle</type>)</literal></entry>
        <entry><type>box</type></entry>
        <entry>circle to box</entry>
        <entry><literal>box(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6106 6107
       </row>
       <row>
6108 6109 6110 6111
        <entry><literal><function>box</function>(<type>point</type>, <type>point</type>)</literal></entry>
        <entry><type>box</type></entry>
        <entry>points to box</entry>
        <entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
T
Thomas G. Lockhart 已提交
6112 6113
       </row>
       <row>
6114 6115 6116 6117
        <entry><literal><function>box</function>(<type>polygon</type>)</literal></entry>
        <entry><type>box</type></entry>
        <entry>polygon to box</entry>
        <entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6118 6119
       </row>
       <row>
6120 6121 6122 6123
        <entry><literal><function>circle</function>(<type>box</type>)</literal></entry>
        <entry><type>circle</type></entry>
        <entry>box to circle</entry>
        <entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
6124 6125
       </row>
       <row>
6126 6127
        <entry><literal><function>circle</function>(<type>point</type>, <type>double precision</type>)</literal></entry>
        <entry><type>circle</type></entry>
6128
        <entry>center and radius to circle</entry>
6129
        <entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
T
Thomas G. Lockhart 已提交
6130 6131
       </row>
       <row>
6132 6133 6134 6135
        <entry><literal><function>lseg</function>(<type>box</type>)</literal></entry>
        <entry><type>lseg</type></entry>
        <entry>box diagonal to line segment</entry>
        <entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6136 6137
       </row>
       <row>
6138 6139 6140 6141
        <entry><literal><function>lseg</function>(<type>point</type>, <type>point</type>)</literal></entry>
        <entry><type>lseg</type></entry>
        <entry>points to line segment</entry>
        <entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6142 6143
       </row>
       <row>
6144 6145 6146 6147
        <entry><literal><function>path</function>(<type>polygon</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>polygon to path</entry>
        <entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6148
       </row>
6149 6150 6151 6152 6153 6154 6155 6156 6157 6158 6159 6160 6161
       <row>
        <entry><literal><function>point</function>(<type>double
         precision</type>, <type>double precision</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>construct point</entry>
        <entry><literal>point(23.4, -44.5)</literal></entry>
       </row>
       <row>
        <entry><literal><function>point</function>(<type>box</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>center of box</entry>
        <entry><literal>point(box '((-1,0),(1,0))')</literal></entry>
       </row>
T
Thomas G. Lockhart 已提交
6162
       <row>
6163 6164 6165 6166
        <entry><literal><function>point</function>(<type>circle</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>center of circle</entry>
        <entry><literal>point(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6167
       </row>
6168 6169 6170 6171 6172 6173
       <row>
        <entry><literal><function>point</function>(<type>lseg</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>center of lseg</entry>
        <entry><literal>point(lseg '((-1,0),(1,0))')</literal></entry>
       </row>
T
Thomas G. Lockhart 已提交
6174
       <row>
6175 6176 6177 6178
        <entry><literal><function>point</function>(<type>lseg</type>, <type>lseg</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>intersection</entry>
        <entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
T
Thomas G. Lockhart 已提交
6179 6180
       </row>
       <row>
6181 6182 6183 6184
        <entry><literal><function>point</function>(<type>polygon</type>)</literal></entry>
        <entry><type>point</type></entry>
        <entry>center of polygon</entry>
        <entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6185 6186
       </row>
       <row>
6187 6188 6189 6190
        <entry><literal><function>polygon</function>(<type>box</type>)</literal></entry>
        <entry><type>polygon</type></entry>
        <entry>box to 4-point polygon</entry>
        <entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
T
Thomas G. Lockhart 已提交
6191 6192
       </row>
       <row>
6193 6194 6195 6196
        <entry><literal><function>polygon</function>(<type>circle</type>)</literal></entry>
        <entry><type>polygon</type></entry>
        <entry>circle to 12-point polygon</entry>
        <entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6197 6198
       </row>
       <row>
6199 6200 6201 6202
        <entry><literal><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</literal></entry>
        <entry><type>polygon</type></entry>
        <entry>circle to <replaceable class="parameter">npts</replaceable>-point polygon</entry>
        <entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
T
Thomas G. Lockhart 已提交
6203 6204
       </row>
       <row>
6205 6206 6207 6208
        <entry><literal><function>polygon</function>(<type>path</type>)</literal></entry>
        <entry><type>polygon</type></entry>
        <entry>path to polygon</entry>
        <entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
T
Thomas G. Lockhart 已提交
6209 6210 6211
       </row>
      </tbody>
     </tgroup>
6212 6213
   </table>

6214 6215
    <para>
     It is possible to access the two component numbers of a <type>point</>
6216
     as though it were an array with indices 0 and 1.  For example, if
6217
     <literal>t.p</> is a <type>point</> column then
6218
     <literal>SELECT p[0] FROM t</> retrieves the X coordinate and
6219
     <literal>UPDATE t SET p[1] = ...</> changes the Y coordinate.
6220 6221
     In the same way, a value of type <type>box</> or <type>lseg</> may be treated
     as an array of two <type>point</> values.
6222 6223
    </para>

6224 6225 6226 6227 6228 6229 6230 6231 6232 6233 6234 6235 6236 6237 6238 6239
    <para>
     The <function>area</function> function works for the types
     <type>box</type>, <type>circle</type>, and <type>path</type>.
     The <function>area</function> function only works on the
     <type>path</type> data type if the points in the
     <type>path</type> are non-intersecting.  For example, the
     <type>path</type>
     <literal>'((0,0),(0,1),(2,1),(2,2),(1,2),(1,0),(0,0))'::PATH</literal>
     won't work, however, the following visually identical
     <type>path</type>
     <literal>'((0,0),(0,1),(1,1),(1,2),(2,2),(2,1),(1,1),(1,0),(0,0))'::PATH</literal>
     will work.  If the concept of an intersecting versus
     non-intersecting <type>path</type> is confusing, draw both of the
     above <type>path</type>s side by side on a piece of graph paper.
    </para>

6240
  </sect1>
6241

6242

6243
 <sect1 id="functions-net">
6244
  <title>Network Address Functions and Operators</title>
6245

6246 6247
  <para>
   <xref linkend="cidr-inet-operators-table"> shows the operators
6248
   available for the <type>cidr</type> and <type>inet</type> types.
6249
   The operators <literal>&lt;&lt;</literal>,
6250 6251
   <literal>&lt;&lt;=</literal>, <literal>&gt;&gt;</literal>, and
   <literal>&gt;&gt;=</literal> test for subnet inclusion.  They
6252 6253 6254 6255
   consider only the network parts of the two addresses, ignoring any
   host part, and determine whether one network part is identical to
   or a subnet of the other.
  </para>
6256

6257
    <table id="cidr-inet-operators-table">
6258 6259 6260 6261
     <title><type>cidr</type> and <type>inet</type> Operators</title>
     <tgroup cols="3">
      <thead>
       <row>
6262 6263 6264
        <entry>Operator</entry>
        <entry>Description</entry>
        <entry>Example</entry>
6265 6266 6267 6268
       </row>
      </thead>
      <tbody>
       <row>
6269 6270 6271
        <entry> <literal>&lt;</literal> </entry>
        <entry>is less than</entry>
        <entry><literal>inet '192.168.1.5' &lt; inet '192.168.1.6'</literal></entry>
6272 6273
       </row>
       <row>
6274 6275 6276
        <entry> <literal>&lt;=</literal> </entry>
        <entry>is less than or equal</entry>
        <entry><literal>inet '192.168.1.5' &lt;= inet '192.168.1.5'</literal></entry>
6277 6278
       </row>
       <row>
6279 6280 6281
        <entry> <literal>=</literal> </entry>
        <entry>equals</entry>
        <entry><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></entry>
6282 6283
       </row>
       <row>
6284 6285 6286
        <entry> <literal>&gt;=</literal> </entry>
        <entry>is greater or equal</entry>
        <entry><literal>inet '192.168.1.5' &gt;= inet '192.168.1.5'</literal></entry>
6287 6288
       </row>
       <row>
6289 6290 6291
        <entry> <literal>&gt;</literal> </entry>
        <entry>is greater than</entry>
        <entry><literal>inet '192.168.1.5' &gt; inet '192.168.1.4'</literal></entry>
6292 6293
       </row>
       <row>
6294 6295 6296
        <entry> <literal>&lt;&gt;</literal> </entry>
        <entry>is not equal</entry>
        <entry><literal>inet '192.168.1.5' &lt;&gt; inet '192.168.1.4'</literal></entry>
6297 6298
       </row>
       <row>
6299 6300 6301
        <entry> <literal>&lt;&lt;</literal> </entry>
        <entry>is contained within</entry>
        <entry><literal>inet '192.168.1.5' &lt;&lt; inet '192.168.1/24'</literal></entry>
6302 6303
       </row>
       <row>
6304 6305 6306
        <entry> <literal>&lt;&lt;=</literal> </entry>
        <entry>is contained within or equals</entry>
        <entry><literal>inet '192.168.1/24' &lt;&lt;= inet '192.168.1/24'</literal></entry>
6307 6308
       </row>
       <row>
6309 6310 6311
        <entry> <literal>&gt;&gt;</literal> </entry>
        <entry>contains</entry>
        <entry><literal>inet '192.168.1/24' &gt;&gt; inet '192.168.1.5'</literal></entry>
6312 6313
       </row>
       <row>
6314 6315 6316
        <entry> <literal>&gt;&gt;=</literal> </entry>
        <entry>contains or equals</entry>
        <entry><literal>inet '192.168.1/24' &gt;&gt;= inet '192.168.1/24'</literal></entry>
6317 6318 6319 6320
       </row>
      </tbody>
     </tgroup>
    </table>
6321

6322 6323
  <para>
   <xref linkend="cidr-inet-functions-table"> shows the functions
6324 6325 6326
   available for use with the <type>cidr</type> and <type>inet</type>
   types.  The <function>host</function>,
   <function>text</function>, and <function>abbrev</function>
6327
   functions are primarily intended to offer alternative display
6328 6329 6330
   formats. You can cast a text value to <type>inet</> using normal casting
   syntax: <literal>inet(<replaceable>expression</>)</literal> or
   <literal><replaceable>colname</>::inet</literal>.
6331
  </para>
6332

6333
    <table id="cidr-inet-functions-table">
6334
     <title><type>cidr</type> and <type>inet</type> Functions</title>
6335
     <tgroup cols="5">
T
Thomas G. Lockhart 已提交
6336 6337
      <thead>
       <row>
6338 6339 6340 6341 6342
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
        <entry>Result</entry>
T
Thomas G. Lockhart 已提交
6343 6344 6345 6346
       </row>
      </thead>
      <tbody>
       <row>
6347 6348 6349 6350 6351
        <entry><literal><function>broadcast</function>(<type>inet</type>)</literal></entry>
        <entry><type>inet</type></entry>
        <entry>broadcast address for network</entry>
        <entry><literal>broadcast('192.168.1.5/24')</literal></entry>
        <entry><literal>192.168.1.255/24</literal></entry>
T
Thomas G. Lockhart 已提交
6352 6353
       </row>
       <row>
6354 6355 6356 6357 6358
        <entry><literal><function>host</function>(<type>inet</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>extract IP address as text</entry>
        <entry><literal>host('192.168.1.5/24')</literal></entry>
        <entry><literal>192.168.1.5</literal></entry>
T
Thomas G. Lockhart 已提交
6359 6360
       </row>
       <row>
6361 6362 6363 6364 6365
        <entry><literal><function>masklen</function>(<type>inet</type>)</literal></entry>
        <entry><type>integer</type></entry>
        <entry>extract netmask length</entry>
        <entry><literal>masklen('192.168.1.5/24')</literal></entry>
        <entry><literal>24</literal></entry>
T
Thomas G. Lockhart 已提交
6366
       </row>
B
Bruce Momjian 已提交
6367
       <row>
6368 6369 6370 6371 6372
        <entry><literal><function>set_masklen</function>(<type>inet</type>, <type>integer</type>)</literal></entry>
        <entry><type>inet</type></entry>
        <entry>set netmask length for <type>inet</type> value</entry>
        <entry><literal>set_masklen('192.168.1.5/24', 16)</literal></entry>
        <entry><literal>192.168.1.5/16</literal></entry>
B
Bruce Momjian 已提交
6373
       </row>
T
Thomas G. Lockhart 已提交
6374
       <row>
6375 6376 6377 6378 6379
        <entry><literal><function>netmask</function>(<type>inet</type>)</literal></entry>
        <entry><type>inet</type></entry>
        <entry>construct netmask for network</entry>
        <entry><literal>netmask('192.168.1.5/24')</literal></entry>
        <entry><literal>255.255.255.0</literal></entry>
6380
       </row>
B
Bruce Momjian 已提交
6381
       <row>
6382 6383 6384 6385 6386
        <entry><literal><function>hostmask</function>(<type>inet</type>)</literal></entry>
        <entry><type>inet</type></entry>
        <entry>construct host mask for network</entry>
        <entry><literal>hostmask('192.168.23.20/30')</literal></entry>
        <entry><literal>0.0.0.3</literal></entry>
B
Bruce Momjian 已提交
6387
       </row>
6388
       <row>
6389 6390 6391 6392 6393
        <entry><literal><function>network</function>(<type>inet</type>)</literal></entry>
        <entry><type>cidr</type></entry>
        <entry>extract network part of address</entry>
        <entry><literal>network('192.168.1.5/24')</literal></entry>
        <entry><literal>192.168.1.0/24</literal></entry>
6394 6395
       </row>
       <row>
6396 6397 6398 6399 6400
        <entry><literal><function>text</function>(<type>inet</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>extract IP address and netmask length as text</entry>
        <entry><literal>text(inet '192.168.1.5')</literal></entry>
        <entry><literal>192.168.1.5/32</literal></entry>
6401
       </row>
6402
       <row>
6403 6404 6405 6406 6407
        <entry><literal><function>abbrev</function>(<type>inet</type>)</literal></entry>
        <entry><type>text</type></entry>
        <entry>abbreviated display format as text</entry>
        <entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
        <entry><literal>10.1/16</literal></entry>
6408
       </row>
6409 6410 6411 6412 6413 6414 6415
       <row>
    <entry><literal><function>family</function>(<type>inet</type>)</literal></entry>
    <entry><type>integer</type></entry>
    <entry>extract family of address; <literal>4</literal> for IPv4,
    <literal>6</literal> for IPv6</entry>
    <entry><literal>family('::1')</literal></entry>
    <entry><literal>6</literal></entry>
6416
       </row>
6417 6418 6419 6420
      </tbody>
     </tgroup>
    </table>

6421 6422
  <para>
   <xref linkend="macaddr-functions-table"> shows the functions
6423 6424 6425
   available for use with the <type>macaddr</type> type.  The function
   <literal><function>trunc</function>(<type>macaddr</type>)</literal> returns a MAC
   address with the last 3 bytes set to zero.  This can be used to
6426 6427 6428 6429 6430
   associate the remaining prefix with a manufacturer.  The directory
   <filename>contrib/mac</filename> in the source distribution
   contains some utilities to create and maintain such an association
   table.
  </para>
6431

6432
    <table id="macaddr-functions-table">
6433
     <title><type>macaddr</type> Functions</title>
6434 6435 6436
     <tgroup cols="5">
      <thead>
       <row>
6437 6438 6439 6440 6441
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
        <entry>Result</entry>
T
Thomas G. Lockhart 已提交
6442
       </row>
6443 6444
      </thead>
      <tbody>
6445
       <row>
6446 6447 6448 6449 6450
        <entry><literal><function>trunc</function>(<type>macaddr</type>)</literal></entry>
        <entry><type>macaddr</type></entry>
        <entry>set last 3 bytes to zero</entry>
        <entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
        <entry><literal>12:34:56:00:00:00</literal></entry>
6451
       </row>
T
Thomas G. Lockhart 已提交
6452 6453 6454
      </tbody>
     </tgroup>
    </table>
6455

6456
   <para>
6457 6458
    The <type>macaddr</type> type also supports the standard relational
    operators (<literal>&gt;</literal>, <literal>&lt;=</literal>, etc.) for
6459 6460 6461
    lexicographical ordering.
   </para>

T
Thomas G. Lockhart 已提交
6462
  </sect1>
6463

6464

6465
 <sect1 id="functions-sequence">
6466
  <title>Sequence Manipulation Functions</title>
6467 6468

  <indexterm>
P
Peter Eisentraut 已提交
6469
   <primary>sequence</primary>
6470 6471 6472 6473 6474 6475 6476 6477 6478 6479 6480
  </indexterm>
  <indexterm>
   <primary>nextval</primary>
  </indexterm>
  <indexterm>
   <primary>currval</primary>
  </indexterm>
  <indexterm>
   <primary>setval</primary>
  </indexterm>

6481 6482 6483 6484 6485 6486 6487 6488 6489 6490 6491 6492 6493
  <para>
   This section describes <productname>PostgreSQL</productname>'s functions
   for operating on <firstterm>sequence objects</firstterm>.
   Sequence objects (also called sequence generators or
   just sequences) are special single-row tables created with
   <command>CREATE SEQUENCE</command>.  A sequence object is usually used to
   generate unique identifiers for rows of a table.  The sequence functions,
   listed in <xref linkend="functions-sequence-table">,
   provide simple, multiuser-safe methods for obtaining successive
   sequence values from sequence objects.
  </para>

   <table id="functions-sequence-table">
6494
    <title>Sequence Functions</title>
6495 6496
    <tgroup cols="3">
     <thead>
6497
      <row><entry>Function</entry> <entry>Return Type</entry> <entry>Description</entry></row>
6498 6499 6500 6501
     </thead>

     <tbody>
      <row>
6502 6503 6504
        <entry><literal><function>nextval</function>(<type>text</type>)</literal></entry>
        <entry><type>bigint</type></entry>
        <entry>Advance sequence and return new value</entry>
6505 6506
      </row>
      <row>
6507 6508 6509
        <entry><literal><function>currval</function>(<type>text</type>)</literal></entry>
        <entry><type>bigint</type></entry>
        <entry>Return value most recently obtained with <function>nextval</function></entry>
6510 6511
      </row>
      <row>
6512 6513 6514
        <entry><literal><function>setval</function>(<type>text</type>, <type>bigint</type>)</literal></entry>
        <entry><type>bigint</type></entry>
        <entry>Set sequence's current value</entry>
6515 6516
      </row>
      <row>
6517 6518 6519
        <entry><literal><function>setval</function>(<type>text</type>, <type>bigint</type>, <type>boolean</type>)</literal></entry>
        <entry><type>bigint</type></entry>
        <entry>Set sequence's current value and <literal>is_called</literal> flag</entry>
6520 6521 6522 6523 6524 6525
      </row>
     </tbody>
    </tgroup>
   </table>

  <para>
6526 6527 6528 6529
   For largely historical reasons, the sequence to be operated on by a
   sequence-function call is specified by a text-string argument.  To
   achieve some compatibility with the handling of ordinary
   <acronym>SQL</acronym> names, the sequence functions convert their
6530
   argument to lowercase unless the string is double-quoted.  Thus
6531
<programlisting>
6532 6533 6534
nextval('foo')      <lineannotation>operates on sequence <literal>foo</literal></>
nextval('FOO')      <lineannotation>operates on sequence <literal>foo</literal></>
nextval('"Foo"')    <lineannotation>operates on sequence <literal>Foo</literal></>
6535 6536 6537
</programlisting>
   The sequence name can be schema-qualified if necessary:
<programlisting>
6538 6539 6540
nextval('myschema.foo')     <lineannotation>operates on <literal>myschema.foo</literal></>
nextval('"myschema".foo')   <lineannotation>same as above</lineannotation>
nextval('foo')              <lineannotation>searches search path for <literal>foo</literal></>
6541 6542 6543 6544 6545 6546 6547 6548 6549 6550
</programlisting>
   Of course, the text argument can be the result of an expression,
   not only a simple literal, which is occasionally useful.
  </para>

  <para>
   The available sequence functions are:

    <variablelist>
     <varlistentry>
6551
      <term><function>nextval</function></term>
6552 6553 6554
      <listitem>
       <para>
        Advance the sequence object to its next value and return that
6555 6556 6557
        value.  This is done atomically: even if multiple sessions
        execute <function>nextval</function> concurrently, each will safely receive
        a distinct sequence value.
6558 6559 6560 6561 6562
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
6563
      <term><function>currval</function></term>
6564 6565
      <listitem>
       <para>
6566
        Return the value most recently obtained by <function>nextval</function>
6567 6568 6569 6570 6571 6572
        for this sequence in the current session.  (An error is
        reported if <function>nextval</function> has never been called for this
        sequence in this session.)  Notice that because this is returning
        a session-local value, it gives a predictable answer whether or not
        other sessions have executed <function>nextval</function> since the
        current session did.
6573 6574 6575 6576 6577
       </para>
      </listitem>
     </varlistentry>

     <varlistentry>
6578
      <term><function>setval</function></term>
6579 6580 6581
      <listitem>
       <para>
        Reset the sequence object's counter value.  The two-parameter
6582 6583 6584 6585 6586 6587 6588 6589 6590
        form sets the sequence's <literal>last_value</literal> field to the specified
        value and sets its <literal>is_called</literal> field to <literal>true</literal>,
        meaning that the next <function>nextval</function> will advance the sequence
        before returning a value.  In the three-parameter form,
        <literal>is_called</literal> may be set either <literal>true</literal> or
        <literal>false</literal>.  If it's set to <literal>false</literal>,
        the next <function>nextval</function> will return exactly the specified
        value, and sequence advancement commences with the following
        <function>nextval</function>.  For example,
6591 6592

<screen>
6593
SELECT setval('foo', 42);           <lineannotation>Next <function>nextval</> will return 43</lineannotation>
6594
SELECT setval('foo', 42, true);     <lineannotation>Same as above</lineannotation>
6595
SELECT setval('foo', 42, false);    <lineannotation>Next <function>nextval</> will return 42</lineannotation>
6596 6597
</screen>

6598
        The result returned by <function>setval</function> is just the value of its
6599
        second argument.
6600 6601 6602 6603 6604 6605 6606 6607 6608
       </para>
      </listitem>
     </varlistentry>
    </variablelist>
  </para>

  <important>
   <para>
    To avoid blocking of concurrent transactions that obtain numbers from the
6609
    same sequence, a <function>nextval</function> operation is never rolled back;
6610
    that is, once a value has been fetched it is considered used, even if the
6611
    transaction that did the <function>nextval</function> later aborts.  This means
6612
    that aborted transactions may leave unused <quote>holes</quote> in the
6613
    sequence of assigned values.  <function>setval</function> operations are never
6614 6615 6616 6617 6618 6619
    rolled back, either.
   </para>
  </important>

  <para>
   If a sequence object has been created with default parameters,
6620 6621
   <function>nextval</function> calls on it will return successive values
   beginning with 1.  Other behaviors can be obtained by using
6622
   special parameters in the <xref linkend="SQL-CREATESEQUENCE"> command;
6623 6624 6625 6626 6627 6628
   see its command reference page for more information.
  </para>

 </sect1>


6629 6630 6631
 <sect1 id="functions-conditional">
  <title>Conditional Expressions</title>

6632
  <indexterm>
P
Peter Eisentraut 已提交
6633
   <primary>CASE</primary>
6634 6635 6636
  </indexterm>

  <indexterm>
P
Peter Eisentraut 已提交
6637
   <primary>conditional expression</primary>
6638 6639
  </indexterm>

6640
  <para>
6641
   This section describes the <acronym>SQL</acronym>-compliant conditional expressions
6642
   available in <productname>PostgreSQL</productname>.
6643 6644 6645 6646 6647 6648 6649 6650 6651 6652
  </para>

  <tip>
   <para>
    If your needs go beyond the capabilities of these conditional
    expressions you might want to consider writing a stored procedure
    in a more expressive programming language.
   </para>
  </tip>

6653
  <sect2>
6654 6655 6656 6657 6658 6659
   <title><literal>CASE</></title>

  <para>
   The <acronym>SQL</acronym> <token>CASE</token> expression is a
   generic conditional expression, similar to if/else statements in
   other languages:
6660 6661 6662 6663 6664 6665 6666 6667

<synopsis>
CASE WHEN <replaceable>condition</replaceable> THEN <replaceable>result</replaceable>
     <optional>WHEN ...</optional>
     <optional>ELSE <replaceable>result</replaceable></optional>
END
</synopsis>

6668
   <token>CASE</token> clauses can be used wherever
6669
   an expression is valid.  <replaceable>condition</replaceable> is an
6670
   expression that returns a <type>boolean</type> result.  If the result is true
6671 6672
   then the value of the <token>CASE</token> expression is the
   <replaceable>result</replaceable> that follows the condition.  If the result is false any
6673 6674 6675 6676 6677
   subsequent <token>WHEN</token> clauses are searched in the same
   manner.  If no <token>WHEN</token>
   <replaceable>condition</replaceable> is true then the value of the
   case expression is the <replaceable>result</replaceable> in the
   <token>ELSE</token> clause.  If the <token>ELSE</token> clause is
P
Peter Eisentraut 已提交
6678
   omitted and no condition matches, the result is null.
6679 6680 6681 6682 6683
  </para>

   <para>
    An example:
<screen>
6684 6685
SELECT * FROM test;

6686 6687 6688 6689 6690
 a
---
 1
 2
 3
6691 6692 6693 6694 6695 6696 6697 6698 6699


SELECT a,
       CASE WHEN a=1 THEN 'one'
            WHEN a=2 THEN 'two'
            ELSE 'other'
       END
    FROM test;

6700 6701 6702 6703 6704 6705 6706 6707 6708
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</screen>
   </para>

  <para>
6709
   The data types of all the <replaceable>result</replaceable>
6710
   expressions must be convertible to a single output type.
6711
   See <xref linkend="typeconv-union-case"> for more detail.
6712 6713
  </para>

6714 6715 6716 6717
  <para>
   The following <quote>simple</quote> <token>CASE</token> expression is a
   specialized variant of the general form above:

6718 6719 6720 6721 6722 6723 6724 6725
<synopsis>
CASE <replaceable>expression</replaceable>
    WHEN <replaceable>value</replaceable> THEN <replaceable>result</replaceable>
    <optional>WHEN ...</optional>
    <optional>ELSE <replaceable>result</replaceable></optional>
END
</synopsis>

6726
   The
6727
   <replaceable>expression</replaceable> is computed and compared to
6728
   all the <replaceable>value</replaceable> specifications in the
6729 6730
   <token>WHEN</token> clauses until one is found that is equal.  If
   no match is found, the <replaceable>result</replaceable> in the
P
Peter Eisentraut 已提交
6731
   <token>ELSE</token> clause (or a null value) is returned.  This is similar
6732 6733 6734 6735 6736 6737 6738
   to the <function>switch</function> statement in C.
  </para>

   <para>
    The example above can be written using the simple
    <token>CASE</token> syntax:
<screen>
6739 6740 6741 6742 6743 6744 6745
SELECT a,
       CASE a WHEN 1 THEN 'one'
              WHEN 2 THEN 'two'
              ELSE 'other'
       END
    FROM test;

6746 6747 6748 6749 6750 6751
 a | case
---+-------
 1 | one
 2 | two
 3 | other
</screen>
6752 6753 6754 6755 6756 6757 6758 6759 6760 6761
   </para>

   <para>
    A <token>CASE</token> expression does not evaluate any subexpressions
    that are not needed to determine the result.  For example, this is a
    possible way of avoiding a division-by-zero failure:
<programlisting>
SELECT ... WHERE CASE WHEN x &lt;&gt; 0 THEN y/x &gt; 1.5 ELSE false END;
</programlisting>
   </para>
6762
  </sect2>
6763

6764
  <sect2>
6765
   <title><literal>COALESCE</></title>
6766

P
Peter Eisentraut 已提交
6767 6768 6769 6770
  <indexterm>
   <primary>COALESCE</primary>
  </indexterm>

6771
<synopsis>
6772
<function>COALESCE</function>(<replaceable>value</replaceable> <optional>, ...</optional>)
6773 6774 6775 6776
</synopsis>

  <para>
   The <function>COALESCE</function> function returns the first of its
6777 6778
   arguments that is not null.  Null is returned only if all arguments
   are null.  This is often useful to substitute a
P
Peter Eisentraut 已提交
6779
   default value for null values when data is retrieved for display,
6780 6781 6782 6783 6784
   for example:
<programlisting>
SELECT COALESCE(description, short_description, '(none)') ...
</programlisting>
  </para>
6785 6786 6787 6788 6789 6790 6791

   <para>
    Like a <token>CASE</token> expression, <function>COALESCE</function> will
    not evaluate arguments that are not needed to determine the result;
    that is, arguments to the right of the first non-null argument are
    not evaluated.
   </para>
6792
  </sect2>
6793

6794
  <sect2>
6795
   <title><literal>NULLIF</></title>
6796

6797
  <indexterm>
6798
   <primary>NULLIF</primary>
6799 6800
  </indexterm>

6801
<synopsis>
6802
<function>NULLIF</function>(<replaceable>value1</replaceable>, <replaceable>value2</replaceable>)
6803 6804 6805
</synopsis>

  <para>
P
Peter Eisentraut 已提交
6806
   The <function>NULLIF</function> function returns a null value if and only
6807 6808 6809 6810 6811 6812 6813 6814 6815
   if <replaceable>value1</replaceable> and
   <replaceable>value2</replaceable> are equal.  Otherwise it returns
   <replaceable>value1</replaceable>.  This can be used to perform the
   inverse operation of the <function>COALESCE</function> example
   given above:
<programlisting>
SELECT NULLIF(value, '(none)') ...
</programlisting>
  </para>
6816

6817
  </sect2>
6818

6819 6820 6821
 </sect1>


6822 6823
 <sect1 id="functions-array">
  <title>Array Functions and Operators</title>
6824

6825
  <para>
6826 6827
   <xref linkend="array-operators-table"> shows the operators
   available for <type>array</type> types.
6828 6829
  </para>

6830 6831 6832 6833 6834
    <table id="array-operators-table">
     <title><type>array</type> Operators</title>
     <tgroup cols="4">
      <thead>
       <row>
6835 6836 6837 6838
        <entry>Operator</entry>
        <entry>Description</entry>
        <entry>Example</entry>
        <entry>Result</entry>
6839 6840 6841 6842
       </row>
      </thead>
      <tbody>
       <row>
6843 6844 6845 6846
        <entry> <literal>=</literal> </entry>
        <entry>equal</entry>
        <entry><literal>ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]</literal></entry>
        <entry><literal>t</literal></entry>
6847
       </row>
6848

6849
       <row>
6850 6851 6852 6853
        <entry> <literal>&lt;&gt;</literal> </entry>
        <entry>not equal</entry>
        <entry><literal>ARRAY[1,2,3] &lt;&gt; ARRAY[1,2,4]</literal></entry>
        <entry><literal>t</literal></entry>
6854
       </row>
6855

6856
       <row>
6857 6858 6859 6860
        <entry> <literal>&lt;</literal> </entry>
        <entry>less than</entry>
        <entry><literal>ARRAY[1,2,3] &lt; ARRAY[1,2,4]</literal></entry>
        <entry><literal>t</literal></entry>
6861
       </row>
6862

6863
       <row>
6864 6865 6866 6867
        <entry> <literal>&gt;</literal> </entry>
        <entry>greater than</entry>
        <entry><literal>ARRAY[1,4,3] &gt; ARRAY[1,2,4]</literal></entry>
        <entry><literal>t</literal></entry>
6868
       </row>
6869

6870
       <row>
6871 6872 6873 6874
        <entry> <literal>&lt;=</literal> </entry>
        <entry>less than or equal</entry>
        <entry><literal>ARRAY[1,2,3] &lt;= ARRAY[1,2,3]</literal></entry>
        <entry><literal>t</literal></entry>
6875
       </row>
6876

6877
       <row>
6878 6879 6880 6881
        <entry> <literal>&gt;=</literal> </entry>
        <entry>greater than or equal</entry>
        <entry><literal>ARRAY[1,4,3] &gt;= ARRAY[1,4,3]</literal></entry>
        <entry><literal>t</literal></entry>
6882
       </row>
6883

6884
       <row>
6885 6886 6887 6888
        <entry> <literal>||</literal> </entry>
        <entry>array-to-array concatenation</entry>
        <entry><literal>ARRAY[1,2,3] || ARRAY[4,5,6]</literal></entry>
        <entry><literal>{1,2,3,4,5,6}</literal></entry>
6889
       </row>
6890

6891
       <row>
6892 6893 6894 6895
        <entry> <literal>||</literal> </entry>
        <entry>array-to-array concatenation</entry>
        <entry><literal>ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]</literal></entry>
        <entry><literal>{{1,2,3},{4,5,6},{7,8,9}}</literal></entry>
6896
       </row>
6897

6898
       <row>
6899 6900 6901 6902
        <entry> <literal>||</literal> </entry>
        <entry>element-to-array concatenation</entry>
        <entry><literal>3 || ARRAY[4,5,6]</literal></entry>
        <entry><literal>{3,4,5,6}</literal></entry>
6903
       </row>
6904

6905
       <row>
6906 6907 6908 6909
        <entry> <literal>||</literal> </entry>
        <entry>array-to-element concatenation</entry>
        <entry><literal>ARRAY[4,5,6] || 7</literal></entry>
        <entry><literal>{4,5,6,7}</literal></entry>
6910 6911 6912 6913
       </row>
      </tbody>
     </tgroup>
    </table>
6914

6915
  <para>
6916 6917
   See <xref linkend="arrays"> for more details about array operator
   behavior.
6918 6919
  </para>

6920 6921 6922
  <para>
   <xref linkend="array-functions-table"> shows the functions
   available for use with array types. See <xref linkend="arrays">
T
Tom Lane 已提交
6923
   for more discussion and examples of the use of these functions.
6924
  </para>
6925

6926 6927 6928 6929 6930
    <table id="array-functions-table">
     <title><type>array</type> Functions</title>
     <tgroup cols="5">
      <thead>
       <row>
6931 6932 6933 6934 6935
        <entry>Function</entry>
        <entry>Return Type</entry>
        <entry>Description</entry>
        <entry>Example</entry>
        <entry>Result</entry>
6936 6937 6938 6939
       </row>
      </thead>
      <tbody>
       <row>
6940
        <entry>
6941 6942 6943 6944 6945
     <literal>
      <function>array_cat</function>
      (<type>anyarray</type>, <type>anyarray</type>)
     </literal>
    </entry>
6946 6947 6948 6949
        <entry><type>anyarray</type></entry>
        <entry>concatenate two arrays</entry>
        <entry><literal>array_cat(ARRAY[1,2,3], ARRAY[4,5])</literal></entry>
        <entry><literal>{1,2,3,4,5}</literal></entry>
6950 6951
       </row>
       <row>
6952
        <entry>
6953 6954 6955 6956 6957
     <literal>
      <function>array_append</function>
      (<type>anyarray</type>, <type>anyelement</type>)
     </literal>
    </entry>
6958 6959 6960 6961
        <entry><type>anyarray</type></entry>
        <entry>append an element to the end of an array</entry>
        <entry><literal>array_append(ARRAY[1,2], 3)</literal></entry>
        <entry><literal>{1,2,3}</literal></entry>
6962 6963
       </row>
       <row>
6964
        <entry>
6965 6966 6967 6968 6969
     <literal>
      <function>array_prepend</function>
      (<type>anyelement</type>, <type>anyarray</type>)
     </literal>
    </entry>
6970 6971 6972 6973
        <entry><type>anyarray</type></entry>
        <entry>append an element to the beginning of an array</entry>
        <entry><literal>array_prepend(1, ARRAY[2,3])</literal></entry>
        <entry><literal>{1,2,3}</literal></entry>
6974 6975
       </row>
       <row>
6976
        <entry>
6977 6978 6979 6980 6981
     <literal>
      <function>array_dims</function>
      (<type>anyarray</type>)
     </literal>
    </entry>
6982 6983 6984 6985
        <entry><type>text</type></entry>
        <entry>returns a text representation of array's dimensions</entry>
        <entry><literal>array_dims(array[[1,2,3], [4,5,6]])</literal></entry>
        <entry><literal>[1:2][1:3]</literal></entry>
6986 6987
       </row>
       <row>
6988
        <entry>
6989 6990 6991 6992 6993
     <literal>
      <function>array_lower</function>
      (<type>anyarray</type>, <type>integer</type>)
     </literal>
    </entry>
6994 6995 6996 6997
        <entry><type>integer</type></entry>
        <entry>returns lower bound of the requested array dimension</entry>
        <entry><literal>array_lower(array_prepend(0, ARRAY[1,2,3]), 1)</literal></entry>
        <entry><literal>0</literal></entry>
6998 6999
       </row>
       <row>
7000
        <entry>
7001 7002 7003 7004 7005
     <literal>
      <function>array_upper</function>
      (<type>anyarray</type>, <type>integer</type>)
     </literal>
    </entry>
7006 7007 7008 7009
        <entry><type>integer</type></entry>
        <entry>returns upper bound of the requested array dimension</entry>
        <entry><literal>array_upper(ARRAY[1,2,3,4], 1)</literal></entry>
        <entry><literal>4</literal></entry>
7010 7011
       </row>
       <row>
7012
        <entry>
7013 7014 7015 7016 7017
     <literal>
      <function>array_to_string</function>
      (<type>anyarray</type>, <type>text</type>)
     </literal>
    </entry>
7018 7019 7020 7021
        <entry><type>text</type></entry>
        <entry>concatenates array elements using provided delimiter</entry>
        <entry><literal>array_to_string(array[1, 2, 3], '~^~')</literal></entry>
        <entry><literal>1~^~2~^~3</literal></entry>
7022 7023
       </row>
       <row>
7024
        <entry>
7025 7026 7027 7028 7029
     <literal>
      <function>string_to_array</function>
      (<type>text</type>, <type>text</type>)
     </literal>
    </entry>
7030 7031 7032 7033
        <entry><type>text[]</type></entry>
        <entry>splits string into array elements using provided delimiter</entry>
        <entry><literal>string_to_array( 'xx~^~yy~^~zz', '~^~')</literal></entry>
        <entry><literal>{xx,yy,zz}</literal></entry>
7034 7035 7036 7037 7038
       </row>
      </tbody>
     </tgroup>
    </table>
  </sect1>
7039

7040 7041 7042 7043 7044 7045 7046 7047 7048 7049 7050 7051 7052 7053 7054 7055 7056 7057 7058 7059 7060 7061 7062 7063 7064 7065 7066 7067 7068 7069 7070 7071 7072 7073 7074 7075 7076 7077 7078 7079 7080 7081 7082 7083 7084 7085 7086 7087 7088 7089 7090 7091 7092 7093 7094 7095 7096 7097 7098 7099 7100 7101 7102 7103 7104 7105 7106 7107 7108 7109 7110 7111 7112 7113 7114 7115 7116 7117 7118 7119 7120 7121 7122 7123 7124 7125 7126 7127 7128 7129 7130 7131 7132 7133 7134 7135 7136 7137 7138 7139 7140 7141 7142 7143 7144 7145 7146 7147 7148 7149 7150 7151 7152 7153 7154 7155 7156 7157 7158 7159 7160 7161 7162 7163 7164 7165 7166 7167 7168 7169 7170 7171 7172 7173
 <sect1 id="functions-aggregate">
  <title>Aggregate Functions</title>

  <indexterm zone="functions-aggregate">
   <primary>aggregate function</primary>
   <secondary>built-in</secondary>
  </indexterm>

  <para>
   <firstterm>Aggregate functions</firstterm> compute a single result
   value from a set of input values.  <xref
   linkend="functions-aggregate-table"> shows the built-in aggregate
   functions.  The special syntax considerations for aggregate
   functions are explained in <xref linkend="syntax-aggregates">.
   Consult <xref linkend="tutorial-agg"> for additional introductory
   information.
  </para>

  <table id="functions-aggregate-table">
   <title>Aggregate Functions</title>

   <tgroup cols="4">
    <thead>
     <row>
      <entry>Function</entry>
      <entry>Argument Type</entry>
      <entry>Return Type</entry>
      <entry>Description</entry>
     </row>
    </thead>

    <tbody>
     <row>
      <entry>
       <indexterm>
        <primary>average</primary>
       </indexterm>
       <function>avg(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, <type>numeric</type>, or <type>interval</type>
      </entry>
      <entry>
       <type>numeric</type> for any integer type argument,
       <type>double precision</type> for a floating-point argument,
       otherwise the same as the argument data type
      </entry>
      <entry>the average (arithmetic mean) of all input values</entry>
     </row>

     <row>
      <entry>
       <indexterm>
        <primary>bit_and</primary>
       </indexterm>
       <function>bit_and(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
       <type>bit</type>
      </entry>
      <entry>
        same as argument data type
      </entry>
      <entry>the bitwise AND of all non-null input values, or null if none</entry>
     </row>

     <row>
      <entry>
       <indexterm>
        <primary>bit_or</primary>
       </indexterm>
       <function>bit_or(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
       <type>bit</type>
      </entry>
      <entry>
        same as argument data type
      </entry>
      <entry>the bitwise OR of all non-null input values, or null if none</entry>
     </row>

     <row>
      <entry>
       <indexterm>
        <primary>bool_and</primary>
       </indexterm>
       <function>bool_and(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>true if all input values are true, otherwise false</entry>
     </row>

     <row>
      <entry>
       <indexterm>
        <primary>bool_or</primary>
       </indexterm>
       <function>bool_or(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>true if at least one input value is true, otherwise false</entry>
     </row>

     <row>
      <entry><function>count(*)</function></entry>
      <entry></entry>
      <entry><type>bigint</type></entry>
      <entry>number of input values</entry>
     </row>

     <row>
      <entry><function>count(<replaceable class="parameter">expression</replaceable>)</function></entry>
      <entry>any</entry>
      <entry><type>bigint</type></entry>
      <entry>
       number of input values for which the value of <replaceable
       class="parameter">expression</replaceable> is not null
      </entry>
     </row>
7174

7175 7176 7177 7178 7179 7180 7181 7182 7183 7184 7185 7186 7187 7188 7189
     <row>
      <entry>
       <indexterm>
        <primary>every</primary>
       </indexterm>
       <function>every(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>
       <type>bool</type>
      </entry>
      <entry>equivalent to <function>bool_and</function></entry>
     </row>
7190

7191 7192
     <row>
      <entry><function>max(<replaceable class="parameter">expression</replaceable>)</function></entry>
7193
      <entry>any array, numeric, string, or date/time type</entry>
7194 7195 7196 7197 7198 7199 7200
      <entry>same as argument type</entry>
      <entry>
       maximum value of <replaceable
       class="parameter">expression</replaceable> across all input
       values
      </entry>
     </row>
7201

7202 7203
     <row>
      <entry><function>min(<replaceable class="parameter">expression</replaceable>)</function></entry>
7204
      <entry>any array, numeric, string, or date/time type</entry>
7205 7206 7207 7208 7209 7210 7211
      <entry>same as argument type</entry>
      <entry>
       minimum value of <replaceable
       class="parameter">expression</replaceable> across all input
       values
      </entry>
     </row>
7212

7213 7214 7215 7216 7217 7218 7219 7220 7221 7222 7223 7224 7225 7226 7227 7228 7229 7230
     <row>
      <entry>
       <indexterm>
        <primary>standard deviation</primary>
       </indexterm>
       <function>stddev(<replaceable class="parameter">expression</replaceable>)</function>
      </entry>
      <entry>
       <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, or <type>numeric</type>
      </entry>
      <entry>
       <type>double precision</type> for floating-point arguments,
       otherwise <type>numeric</type>
      </entry>
      <entry>sample standard deviation of the input values</entry>
     </row>
7231

7232 7233 7234 7235 7236 7237 7238 7239 7240 7241 7242 7243 7244 7245 7246 7247 7248
     <row>
      <entry><function>sum(<replaceable class="parameter">expression</replaceable>)</function></entry>
      <entry>
       <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, <type>numeric</type>, or
       <type>interval</type>
      </entry>
      <entry>
       <type>bigint</type> for <type>smallint</type> or
       <type>integer</type> arguments, <type>numeric</type> for
       <type>bigint</type> arguments, <type>double precision</type>
       for floating-point arguments, otherwise the same as the
       argument data type
      </entry>
      <entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
     </row>
7249

7250 7251 7252 7253 7254 7255 7256 7257 7258 7259 7260 7261 7262 7263 7264 7265 7266 7267
     <row>
      <entry>
       <indexterm>
        <primary>variance</primary>
       </indexterm>
       <function>variance</function>(<replaceable class="parameter">expression</replaceable>)
      </entry>
      <entry>
       <type>smallint</type>, <type>integer</type>,
       <type>bigint</type>, <type>real</type>, <type>double
       precision</type>, or <type>numeric</type>
      </entry>
      <entry>
       <type>double precision</type> for floating-point arguments,
       otherwise <type>numeric</type>
      </entry>
      <entry>sample variance of the input values (square of the sample standard deviation)</entry>
     </row>
7268

7269 7270 7271
    </tbody>
   </tgroup>
  </table>
P
Peter Eisentraut 已提交
7272

7273
  <para>
7274 7275 7276
   It should be noted that except for <function>count</function>,
   these functions return a null value when no rows are selected.  In
   particular, <function>sum</function> of no rows returns null, not
T
Tom Lane 已提交
7277
   zero as one might expect.  The <function>coalesce</function> function may be
7278
   used to substitute zero for null when necessary.
7279 7280
  </para>

7281 7282 7283 7284 7285 7286 7287 7288 7289 7290 7291 7292 7293 7294 7295 7296 7297 7298 7299 7300 7301 7302
  <note>
    <indexterm>
      <primary>ANY</primary>
    </indexterm>
    <indexterm>
      <primary>SOME</primary>
    </indexterm>
    <para>
      Boolean aggregates <function>bool_and</function> and 
      <function>bool_or</function> correspond to standard SQL aggregates
      <function>every</function> and <function>any</function> or
      <function>some</function>. 
      As for <function>any</function> and <function>some</function>, 
      it seems that there is an ambiguity built into the standard syntax:
<programlisting>
SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
</programlisting>
      Here <function>ANY</function> can be considered both as leading
      to a subquery or as an aggregate if the select expression returns 1 row.
      Thus the standard name cannot be given to these aggregates.
    </para>
  </note>
7303

7304
  <note>
7305
   <para>
7306 7307 7308 7309 7310 7311 7312
    Users accustomed to working with other SQL database management
    systems may be surprised by the performance characteristics of
    certain aggregate functions in
    <productname>PostgreSQL</productname> when the aggregate is
    applied to the entire table (in other words, no
    <literal>WHERE</literal> clause is specified). In particular, a
    query like
7313
<programlisting>
7314
SELECT min(col) FROM sometable;
7315
</programlisting>
7316 7317 7318 7319 7320 7321 7322
    will be executed by <productname>PostgreSQL</productname> using a
    sequential scan of the entire table. Other database systems may
    optimize queries of this form to use an index on the column, if
    one is available. Similarly, the aggregate functions
    <function>max()</function> and <function>count()</function> always
    require a sequential scan if applied to the entire table in
    <productname>PostgreSQL</productname>.
7323 7324
   </para>

7325
   <para>
7326 7327 7328 7329 7330 7331 7332
    <productname>PostgreSQL</productname> cannot easily implement this
    optimization because it also allows for user-defined aggregate
    queries. Since <function>min()</function>,
    <function>max()</function>, and <function>count()</function> are
    defined using a generic API for aggregate functions, there is no
    provision for special-casing the execution of these functions
    under certain circumstances.
7333 7334 7335
   </para>

   <para>
7336 7337 7338 7339 7340
    Fortunately, there is a simple workaround for
    <function>min()</function> and <function>max()</function>. The
    query shown below is equivalent to the query above, except that it
    can take advantage of a B-tree index if there is one present on
    the column in question.
7341
<programlisting>
7342
SELECT col FROM sometable ORDER BY col ASC LIMIT 1;
7343
</programlisting>
7344 7345
    A similar query (obtained by substituting <literal>DESC</literal>
    for <literal>ASC</literal> in the query above) can be used in the
N
Neil Conway 已提交
7346
    place of <function>max()</function>.
7347 7348 7349
   </para>

   <para>
N
Neil Conway 已提交
7350 7351
    Unfortunately, there is no similarly trivial workaround that can
    be used to improve the performance of <function>count()</function>
7352
    when applied to the entire table.
7353
   </para>
7354
  </note>
7355

7356
 </sect1>
7357

B
Bruce Momjian 已提交
7358

7359 7360
 <sect1 id="functions-subquery">
  <title>Subquery Expressions</title>
7361

7362 7363 7364
  <indexterm>
   <primary>EXISTS</primary>
  </indexterm>
7365

7366 7367 7368
  <indexterm>
   <primary>IN</primary>
  </indexterm>
7369

7370 7371 7372
  <indexterm>
   <primary>NOT IN</primary>
  </indexterm>
7373

7374 7375 7376
  <indexterm>
   <primary>ANY</primary>
  </indexterm>
7377

7378 7379 7380
  <indexterm>
   <primary>ALL</primary>
  </indexterm>
7381

7382 7383 7384 7385 7386 7387 7388 7389 7390 7391 7392 7393 7394 7395 7396 7397 7398 7399 7400 7401 7402 7403 7404 7405 7406 7407 7408 7409 7410 7411
  <indexterm>
   <primary>SOME</primary>
  </indexterm>

  <indexterm>
   <primary>subquery</primary>
  </indexterm>

  <para>
   This section describes the <acronym>SQL</acronym>-compliant subquery
   expressions available in <productname>PostgreSQL</productname>.
   All of the expression forms documented in this section return
   Boolean (true/false) results.
  </para>

  <sect2>
   <title><literal>EXISTS</literal></title>

<synopsis>
EXISTS ( <replaceable>subquery</replaceable> )
</synopsis>

  <para>
   The argument of <token>EXISTS</token> is an arbitrary <command>SELECT</> statement,
   or <firstterm>subquery</firstterm>.  The
   subquery is evaluated to determine whether it returns any rows.
   If it returns at least one row, the result of <token>EXISTS</token> is
   <quote>true</>; if the subquery returns no rows, the result of <token>EXISTS</token> 
   is <quote>false</>.
  </para>
7412

7413 7414 7415 7416
  <para>
   The subquery can refer to variables from the surrounding query,
   which will act as constants during any one evaluation of the subquery.
  </para>
7417

7418 7419 7420 7421 7422 7423 7424
  <para>
   The subquery will generally only be executed far enough to determine
   whether at least one row is returned, not all the way to completion.
   It is unwise to write a subquery that has any side effects (such as
   calling sequence functions); whether the side effects occur or not
   may be difficult to predict.
  </para>
7425

7426 7427 7428 7429 7430 7431 7432 7433
  <para>
   Since the result depends only on whether any rows are returned,
   and not on the contents of those rows, the output list of the
   subquery is normally uninteresting.  A common coding convention is
   to write all <literal>EXISTS</> tests in the form
   <literal>EXISTS(SELECT 1 WHERE ...)</literal>.  There are exceptions to
   this rule however, such as subqueries that use <token>INTERSECT</token>.
  </para>
7434

7435 7436 7437 7438 7439 7440 7441 7442 7443 7444
  <para>
   This simple example is like an inner join on <literal>col2</>, but
   it produces at most one output row for each <literal>tab1</> row,
   even if there are multiple matching <literal>tab2</> rows:
<screen>
SELECT col1 FROM tab1
    WHERE EXISTS(SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
</screen>
  </para>
  </sect2>
7445

7446 7447
  <sect2>
   <title><literal>IN</literal></title>
7448

7449 7450 7451
<synopsis>
<replaceable>expression</replaceable> IN (<replaceable>subquery</replaceable>)
</synopsis>
7452

7453 7454 7455 7456 7457 7458 7459 7460
  <para>
   The right-hand side is a parenthesized
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result.
   The result of <token>IN</token> is <quote>true</> if any equal subquery row is found.
   The result is <quote>false</> if no equal row is found (including the special
   case where the subquery returns no rows).
  </para>
7461

7462 7463 7464 7465 7466 7467 7468
  <para>
   Note that if the left-hand expression yields null, or if there are
   no equal right-hand values and at least one right-hand row yields
   null, the result of the <token>IN</token> construct will be null, not false.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>
7469

7470 7471 7472 7473 7474 7475 7476 7477
  <para>
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
   be evaluated completely.
  </para>

<synopsis>
<replaceable>row_constructor</replaceable> IN (<replaceable>subquery</replaceable>)
</synopsis>
J
Joe Conway 已提交
7478

7479
  <para>
7480 7481 7482 7483 7484 7485 7486 7487 7488
   The left-hand side of this form of <token>IN</token> is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The right-hand side is a parenthesized
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand row.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result.
   The result of <token>IN</token> is <quote>true</> if any equal subquery row is found.
   The result is <quote>false</> if no equal row is found (including the special
   case where the subquery returns no rows).
7489 7490
  </para>

7491 7492 7493 7494 7495 7496 7497 7498 7499 7500
  <para>
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (null).
   If all the row results are either unequal or null, with at least one null,
   then the result of <token>IN</token> is null.
  </para>
  </sect2>
7501

7502
  <sect2>
7503
   <title><literal>NOT IN</literal></title>
7504 7505 7506 7507

<synopsis>
<replaceable>expression</replaceable> NOT IN (<replaceable>subquery</replaceable>)
</synopsis>
7508

7509
  <para>
7510 7511 7512 7513 7514 7515
   The right-hand side is a parenthesized
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result.
   The result of <token>NOT IN</token> is <quote>true</> if only unequal subquery rows
   are found (including the special case where the subquery returns no rows).
   The result is <quote>false</> if any equal row is found.
7516 7517 7518
  </para>

  <para>
7519 7520 7521 7522 7523 7524 7525 7526 7527 7528 7529 7530 7531 7532 7533 7534 7535 7536 7537 7538 7539 7540 7541 7542 7543 7544
   Note that if the left-hand expression yields null, or if there are
   no equal right-hand values and at least one right-hand row yields
   null, the result of the <token>NOT IN</token> construct will be null, not true.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>

  <para>
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
   be evaluated completely.
  </para>

<synopsis>
<replaceable>row_constructor</replaceable> NOT IN (<replaceable>subquery</replaceable>)
</synopsis>

  <para>
   The left-hand side of this form of <token>NOT IN</token> is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The right-hand side is a parenthesized
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand row.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result.
   The result of <token>NOT IN</token> is <quote>true</> if only unequal subquery rows
   are found (including the special case where the subquery returns no rows).
   The result is <quote>false</> if any equal row is found.
7545 7546
  </para>

J
Joe Conway 已提交
7547
  <para>
7548 7549 7550 7551 7552 7553 7554
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (null).
   If all the row results are either unequal or null, with at least one null,
   then the result of <token>NOT IN</token> is null.
J
Joe Conway 已提交
7555
  </para>
7556
  </sect2>
J
Joe Conway 已提交
7557

7558 7559
  <sect2>
   <title><literal>ANY</literal>/<literal>SOME</literal></title>
7560

7561 7562 7563 7564
<synopsis>
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ANY (<replaceable>subquery</replaceable>)
<replaceable>expression</replaceable> <replaceable>operator</replaceable> SOME (<replaceable>subquery</replaceable>)
</synopsis>
7565

7566 7567 7568 7569 7570 7571 7572 7573 7574 7575
  <para>
   The right-hand side is a parenthesized
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result using the
   given <replaceable>operator</replaceable>, which must yield a Boolean
   result.
   The result of <token>ANY</token> is <quote>true</> if any true result is obtained.
   The result is <quote>false</> if no true result is found (including the special
   case where the subquery returns no rows).
  </para>
P
Peter Eisentraut 已提交
7576

7577 7578 7579 7580
  <para>
   <token>SOME</token> is a synonym for <token>ANY</token>.
   <token>IN</token> is equivalent to <literal>= ANY</literal>.
  </para>
7581

7582 7583 7584 7585 7586 7587 7588
  <para>
   Note that if there are no successes and at least one right-hand row yields
   null for the operator's result, the result of the <token>ANY</token> construct
   will be null, not false.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>
7589

7590 7591 7592 7593
  <para>
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
   be evaluated completely.
  </para>
7594

7595 7596 7597 7598
<synopsis>
<replaceable>row_constructor</replaceable> <replaceable>operator</> ANY (<replaceable>subquery</replaceable>)
<replaceable>row_constructor</replaceable> <replaceable>operator</> SOME (<replaceable>subquery</replaceable>)
</synopsis>
7599

7600 7601 7602 7603 7604 7605 7606 7607 7608 7609 7610 7611 7612 7613 7614
  <para>
   The left-hand side of this form of <token>ANY</token> is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The right-hand side is a parenthesized
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand row.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result,
   using the given <replaceable>operator</replaceable>.  Presently,
   only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise <token>ANY</token> constructs.
   The result of <token>ANY</token> is <quote>true</> if any equal or unequal row is
   found, respectively.
   The result is <quote>false</> if no such row is found (including the special
   case where the subquery returns no rows).
  </para>
7615

7616 7617 7618 7619 7620 7621 7622 7623 7624 7625
  <para>
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (null).
   If there is at least one null row result, then the result of <token>ANY</token>
   cannot be false; it will be true or null. 
  </para>
  </sect2>
7626

7627 7628
  <sect2>
   <title><literal>ALL</literal></title>
7629

7630 7631 7632
<synopsis>
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
</synopsis>
7633

7634 7635 7636 7637 7638 7639 7640 7641 7642 7643
  <para>
   The right-hand side is a parenthesized
   subquery, which must return exactly one column.  The left-hand expression
   is evaluated and compared to each row of the subquery result using the
   given <replaceable>operator</replaceable>, which must yield a Boolean
   result.
   The result of <token>ALL</token> is <quote>true</> if all rows yield true
   (including the special case where the subquery returns no rows).
   The result is <quote>false</> if any false result is found.
  </para>
7644

7645 7646 7647
  <para>
   <token>NOT IN</token> is equivalent to <literal>&lt;&gt; ALL</literal>.
  </para>
7648

7649 7650 7651 7652 7653 7654 7655
  <para>
   Note that if there are no failures but at least one right-hand row yields
   null for the operator's result, the result of the <token>ALL</token> construct
   will be null, not true.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>
7656

7657 7658 7659 7660
  <para>
   As with <token>EXISTS</token>, it's unwise to assume that the subquery will
   be evaluated completely.
  </para>
7661

7662 7663 7664
<synopsis>
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
</synopsis>
7665 7666

  <para>
7667 7668 7669 7670 7671 7672 7673 7674 7675 7676 7677 7678 7679 7680
   The left-hand side of this form of <token>ALL</token> is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The right-hand side is a parenthesized
   subquery, which must return exactly as many columns as there are
   expressions in the left-hand row.  The left-hand expressions are
   evaluated and compared row-wise to each row of the subquery result,
   using the given <replaceable>operator</replaceable>.  Presently,
   only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise <token>ALL</token> queries.
   The result of <token>ALL</token> is <quote>true</> if all subquery rows are equal
   or unequal, respectively (including the special
   case where the subquery returns no rows).
   The result is <quote>false</> if any row is found to be unequal or equal,
   respectively.
7681 7682
  </para>

7683 7684 7685 7686 7687 7688 7689 7690 7691 7692 7693 7694 7695 7696
  <para>
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of that row comparison is unknown (null).
   If there is at least one null row result, then the result of <token>ALL</token>
   cannot be true; it will be false or null. 
  </para>
  </sect2>

  <sect2>
   <title>Row-wise Comparison</title>

7697
   <indexterm zone="functions-subquery">
7698
    <primary>comparison</primary>
7699
    <secondary>subquery result row</secondary>
7700 7701 7702 7703 7704
   </indexterm>

<synopsis>
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> (<replaceable>subquery</replaceable>)
</synopsis>
7705

7706 7707 7708 7709 7710 7711 7712 7713 7714 7715 7716 7717
  <para>
   The left-hand side is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The right-hand side is a parenthesized subquery, which must return exactly
   as many columns as there are expressions in the left-hand row. Furthermore,
   the subquery cannot return more than one row.  (If it returns zero rows,
   the result is taken to be null.)  The left-hand side is evaluated and
   compared row-wise to the single subquery result row.
   Presently, only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise comparisons.
   The result is <quote>true</> if the two rows are equal or unequal, respectively.
  </para>
7718

7719 7720 7721 7722 7723 7724 7725 7726 7727
  <para>
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of the row comparison is unknown (null).
  </para>
  </sect2>
 </sect1>
7728 7729


7730 7731
 <sect1 id="functions-comparisons">
  <title>Row and Array Comparisons</title>
7732

7733 7734 7735
  <indexterm>
   <primary>IN</primary>
  </indexterm>
7736

7737 7738 7739
  <indexterm>
   <primary>NOT IN</primary>
  </indexterm>
7740

7741 7742 7743
  <indexterm>
   <primary>ANY</primary>
  </indexterm>
7744

7745 7746 7747
  <indexterm>
   <primary>ALL</primary>
  </indexterm>
7748

7749 7750 7751
  <indexterm>
   <primary>SOME</primary>
  </indexterm>
7752

7753 7754 7755 7756 7757 7758 7759 7760 7761 7762 7763 7764 7765 7766 7767 7768 7769
  <indexterm>
   <primary>comparison</primary>
   <secondary>row-wise</secondary>
  </indexterm>

  <indexterm>
   <primary>IS DISTINCT FROM</primary>
  </indexterm>

  <indexterm>
   <primary>IS NULL</primary>
  </indexterm>

  <indexterm>
   <primary>IS NOT NULL</primary>
  </indexterm>

7770
  <para>
7771 7772 7773 7774 7775 7776 7777 7778 7779
   This section describes several specialized constructs for making
   multiple comparisons between groups of values.  These forms are
   syntactically related to the subquery forms of the previous section,
   but do not involve subqueries.
   The forms involving array subexpressions are
   <productname>PostgreSQL</productname> extensions; the rest are
   <acronym>SQL</acronym>-compliant.
   All of the expression forms documented in this section return
   Boolean (true/false) results.
7780 7781
  </para>

7782 7783
  <sect2>
   <title><literal>IN</literal></title>
7784

7785 7786 7787 7788 7789 7790 7791 7792 7793
<synopsis>
<replaceable>expression</replaceable> IN (<replaceable>value</replaceable><optional>, ...</optional>)
</synopsis>

  <para>
   The right-hand side is a parenthesized list
   of scalar expressions.  The result is <quote>true</> if the left-hand expression's
   result is equal to any of the right-hand expressions.  This is a shorthand
   notation for
7794

7795 7796 7797 7798 7799 7800 7801 7802
<synopsis>
<replaceable>expression</replaceable> = <replaceable>value1</replaceable>
OR
<replaceable>expression</replaceable> = <replaceable>value2</replaceable>
OR
...
</synopsis>
  </para>
7803

7804 7805 7806 7807 7808 7809 7810 7811 7812 7813 7814 7815 7816 7817 7818
  <para>
   Note that if the left-hand expression yields null, or if there are
   no equal right-hand values and at least one right-hand expression yields
   null, the result of the <token>IN</token> construct will be null, not false.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>
  </sect2>

  <sect2>
   <title><literal>NOT IN</literal></title>

<synopsis>
<replaceable>expression</replaceable> NOT IN (<replaceable>value</replaceable><optional>, ...</optional>)
</synopsis>
P
Peter Eisentraut 已提交
7819

7820
  <para>
7821 7822 7823 7824 7825 7826 7827 7828 7829 7830 7831 7832
   The right-hand side is a parenthesized list
   of scalar expressions.  The result is <quote>true</quote> if the left-hand expression's
   result is unequal to all of the right-hand expressions.  This is a shorthand
   notation for

<synopsis>
<replaceable>expression</replaceable> &lt;&gt; <replaceable>value1</replaceable>
AND
<replaceable>expression</replaceable> &lt;&gt; <replaceable>value2</replaceable>
AND
...
</synopsis>
7833
  </para>
7834

7835 7836 7837 7838 7839 7840 7841 7842
  <para>
   Note that if the left-hand expression yields null, or if there are
   no equal right-hand values and at least one right-hand expression yields
   null, the result of the <token>NOT IN</token> construct will be null, not true
   as one might naively expect.
   This is in accordance with SQL's normal rules for Boolean combinations
   of null values.
  </para>
7843

7844 7845 7846 7847 7848 7849 7850 7851 7852
  <tip>
  <para>
   <literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
   cases.  However, null values are much more likely to trip up the novice when
   working with <token>NOT IN</token> than when working with <token>IN</token>.
   It's best to express your condition positively if possible.
  </para>
  </tip>
  </sect2>
7853

7854 7855
  <sect2>
   <title><literal>ANY</literal>/<literal>SOME</literal> (array)</title>
7856

7857 7858 7859 7860
<synopsis>
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ANY (<replaceable>array expression</replaceable>)
<replaceable>expression</replaceable> <replaceable>operator</replaceable> SOME (<replaceable>array expression</replaceable>)
</synopsis>
B
Bruce Momjian 已提交
7861

7862 7863 7864 7865 7866 7867 7868 7869 7870 7871 7872
  <para>
   The right-hand side is a parenthesized expression, which must yield an
   array value.
   The left-hand expression
   is evaluated and compared to each element of the array using the
   given <replaceable>operator</replaceable>, which must yield a Boolean
   result.
   The result of <token>ANY</token> is <quote>true</> if any true result is obtained.
   The result is <quote>false</> if no true result is found (including the special
   case where the array has zero elements).
  </para>
B
Bruce Momjian 已提交
7873

7874 7875 7876 7877
  <para>
   <token>SOME</token> is a synonym for <token>ANY</token>.
  </para>
  </sect2>
B
Bruce Momjian 已提交
7878

7879 7880
  <sect2>
   <title><literal>ALL</literal> (array)</title>
B
Bruce Momjian 已提交
7881

7882 7883 7884
<synopsis>
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>array expression</replaceable>)
</synopsis>
7885

7886 7887 7888 7889 7890 7891 7892 7893 7894 7895 7896 7897 7898 7899 7900 7901 7902 7903 7904 7905 7906 7907 7908 7909 7910 7911 7912 7913 7914 7915 7916 7917 7918 7919 7920 7921 7922 7923 7924 7925 7926 7927 7928 7929 7930 7931 7932 7933 7934 7935 7936 7937 7938 7939
  <para>
   The right-hand side is a parenthesized expression, which must yield an
   array value.
   The left-hand expression
   is evaluated and compared to each element of the array using the
   given <replaceable>operator</replaceable>, which must yield a Boolean
   result.
   The result of <token>ALL</token> is <quote>true</> if all comparisons yield true
   (including the special case where the array has zero elements).
   The result is <quote>false</> if any false result is found.
  </para>
  </sect2>

  <sect2>
   <title>Row-wise Comparison</title>

<synopsis>
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> <replaceable>row_constructor</replaceable>
</synopsis>

  <para>
   Each side is a row constructor,
   as described in <xref linkend="sql-syntax-row-constructors">.
   The two row values must have the same number of fields.
   Each side is evaluated and they are compared row-wise.
   Presently, only <literal>=</literal> and <literal>&lt;&gt;</literal> operators are allowed
   in row-wise comparisons.
   The result is <quote>true</> if the two rows are equal or unequal, respectively.
  </para>

  <para>
   As usual, null values in the rows are combined per
   the normal rules of SQL Boolean expressions.  Two rows are considered
   equal if all their corresponding members are non-null and equal; the rows
   are unequal if any corresponding members are non-null and unequal;
   otherwise the result of the row comparison is unknown (null).
  </para>

<synopsis>
<replaceable>row_constructor</replaceable> IS DISTINCT FROM <replaceable>row_constructor</replaceable>
</synopsis>

  <para>
   This construct is similar to a <literal>&lt;&gt;</literal> row comparison,
   but it does not yield null for null inputs.  Instead, any null value is
   considered unequal to (distinct from) any non-null value, and any two
   nulls are considered equal (not distinct).  Thus the result will always
   be either true or false, never null.
  </para>

<synopsis>
<replaceable>row_constructor</replaceable> IS NULL
<replaceable>row_constructor</replaceable> IS NOT NULL
</synopsis>
7940

7941 7942 7943 7944
  <para>
   These constructs test a row value for null or not null.  A row value
   is considered not null if it has at least one field that is not null.
  </para>
B
Bruce Momjian 已提交
7945

7946 7947
  </sect2>
 </sect1>
7948

7949 7950 7951 7952 7953 7954 7955 7956 7957 7958 7959 7960 7961
 <sect1 id="functions-srf">
  <title>Set Returning Functions</title>

  <indexterm zone="functions-srf">
   <primary>set returning functions</primary>
   <secondary>functions</secondary>
  </indexterm>

  <para>
   This section describes functions that possibly return more than one row.
   Currently the only functions in this class are series generating functions,
   as detailed in <xref linkend="functions-srf-series">.
  </para>
7962

7963 7964 7965 7966
  <table id="functions-srf-series">
   <title>Series Generating Functions</title>
   <tgroup cols="4">
    <thead>
7967
     <row>
7968 7969 7970 7971
      <entry>Function</entry>
      <entry>Argument Type</entry>
      <entry>Return Type</entry>
      <entry>Description</entry>
7972
     </row>
7973
    </thead>
7974

7975
    <tbody>
7976
     <row>
7977 7978 7979
      <entry><literal><function>generate_series</function>(<parameter>start</parameter>, <parameter>stop</parameter>)</literal></entry>
      <entry><type>int</type> or <type>bigint</type></entry>
      <entry><type>setof int</type> or <type>setof bigint</type> (same as argument type)</entry>
7980
      <entry>
7981 7982
       Generate a series of values, from <parameter>start</parameter> to <parameter>stop</parameter>
       with a step size of one.
7983 7984 7985 7986
      </entry>
     </row>

     <row>
7987 7988 7989
      <entry><literal><function>generate_series</function>(<parameter>start</parameter>, <parameter>stop</parameter>, <parameter>step</parameter>)</literal></entry>
      <entry><type>int</type> or <type>bigint</type></entry>
      <entry><type>setof int</type> or <type>setof bigint</type> (same as argument type)</entry>
7990
      <entry>
7991 7992
       Generate a series of values, from <parameter>start</parameter> to <parameter>stop</parameter>
       with a step size of <parameter>step</parameter>.
7993 7994 7995 7996 7997 7998
      </entry>
     </row>

    </tbody>
   </tgroup>
  </table>
7999

8000
  <para>
8001 8002 8003 8004 8005 8006
   When <parameter>step</parameter> is positive, zero rows are returned if
   <parameter>start</parameter> is greater than <parameter>stop</parameter>.
   Conversely, when <parameter>step</parameter> is negative, zero rows are
   returned if <parameter>start</parameter> is less than <parameter>stop</parameter>.
   Zero rows are also returned for <literal>NULL</literal> inputs. It is an error
   for <parameter>step</parameter> to be zero. Some examples follow:
B
Bruce Momjian 已提交
8007
<programlisting>
8008 8009 8010 8011 8012 8013 8014
select * from generate_series(2,4);
 generate_series
-----------------
               2
               3
               4
(3 rows)
B
Bruce Momjian 已提交
8015

8016 8017 8018 8019 8020 8021 8022
select * from generate_series(5,1,-2);
 generate_series
-----------------
               5
               3
               1
(3 rows)
8023

8024 8025 8026 8027
select * from generate_series(4,3);
 generate_series
-----------------
(0 rows)
8028

8029 8030 8031 8032 8033 8034 8035
select current_date + s.a as dates from generate_series(0,14,7) as s(a);
   dates
------------
 2004-02-05
 2004-02-12
 2004-02-19
(3 rows)
8036
</programlisting>
8037
  </para>
8038
 </sect1>
8039

8040 8041
 <sect1 id="functions-info">
  <title>System Information Functions</title>
8042

8043 8044 8045 8046
  <para>
   <xref linkend="functions-info-session-table"> shows several
   functions that extract session and system information.
  </para>
8047

8048 8049 8050 8051 8052 8053
   <table id="functions-info-session-table">
    <title>Session Information Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>
8054

8055 8056 8057 8058 8059 8060
     <tbody>
      <row>
       <entry><function>current_database()</function></entry>
       <entry><type>name</type></entry>
       <entry>name of current database</entry>
      </row>
8061

8062 8063 8064 8065 8066
      <row>
       <entry><function>current_schema()</function></entry>
       <entry><type>name</type></entry>
       <entry>name of current schema</entry>
      </row>
8067

8068 8069 8070 8071 8072
      <row>
       <entry><function>current_schemas(boolean)</function></entry>
       <entry><type>name[]</type></entry>
       <entry>names of schemas in search path optionally including implicit schemas</entry>
      </row>
8073

8074 8075 8076 8077 8078
      <row>
       <entry><function>current_user</function></entry>
       <entry><type>name</type></entry>
       <entry>user name of current execution context</entry>
      </row>
8079

8080 8081 8082 8083 8084
      <row>
       <entry><function>inet_client_addr()</function></entry>
       <entry><type>inet</type></entry>
       <entry>address of the remote connection</entry>
      </row>
8085

8086 8087 8088 8089 8090
      <row>
       <entry><function>inet_client_port()</function></entry>
       <entry><type>int4</type></entry>
       <entry>port of the remote connection</entry>
      </row>
8091

8092 8093 8094 8095 8096
      <row>
       <entry><function>inet_server_addr()</function></entry>
       <entry><type>inet</type></entry>
       <entry>address of the local connection</entry>
      </row>
8097

8098 8099 8100 8101 8102
      <row>
       <entry><function>inet_server_port()</function></entry>
       <entry><type>int4</type></entry>
       <entry>port of the local connection</entry>
      </row>
8103

8104 8105 8106 8107 8108
      <row>
       <entry><function>session_user</function></entry>
       <entry><type>name</type></entry>
       <entry>session user name</entry>
      </row>
8109

8110 8111 8112 8113 8114
      <row>
       <entry><function>user</function></entry>
       <entry><type>name</type></entry>
       <entry>equivalent to <function>current_user</function></entry>
      </row>
8115

8116 8117 8118 8119 8120 8121 8122 8123
      <row>
       <entry><function>version()</function></entry>
       <entry><type>text</type></entry>
       <entry>PostgreSQL version information</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8124

8125 8126 8127 8128
   <indexterm zone="functions-info">
    <primary>user</primary>
    <secondary>current</secondary>
   </indexterm>
8129

8130 8131 8132 8133
   <indexterm zone="functions-info">
    <primary>schema</primary>
    <secondary>current</secondary>
   </indexterm>
8134

8135 8136 8137 8138
   <indexterm zone="functions-info">
    <primary>search path</primary>
    <secondary>current</secondary>
   </indexterm>
8139

8140
   <para>
T
Tom Lane 已提交
8141 8142 8143 8144
    The <function>session_user</function> is normally the user who initiated
    the current database connection; but superusers can change this setting
    with <xref linkend="sql-set-session-authorization">.
    The <function>current_user</function> is the user identifier
8145 8146 8147 8148 8149 8150
    that is applicable for permission checking. Normally, it is equal
    to the session user, but it changes during the execution of
    functions with the attribute <literal>SECURITY DEFINER</literal>.
    In Unix parlance, the session user is the <quote>real user</quote> and
    the current user is the <quote>effective user</quote>.
   </para>
8151

8152 8153 8154 8155 8156 8157 8158
   <note>
    <para>
     <function>current_user</function>, <function>session_user</function>, and
     <function>user</function> have special syntactic status in <acronym>SQL</acronym>:
     they must be called without trailing parentheses.
    </para>
   </note>
8159

8160 8161 8162 8163 8164 8165 8166 8167 8168 8169
   <para>
    <function>current_schema</function> returns the name of the schema that is
    at the front of the search path (or a null value if the search path is
    empty).  This is the schema that will be used for any tables or
    other named objects that are created without specifying a target schema.
    <function>current_schemas(boolean)</function> returns an array of the names of all
    schemas presently in the search path.  The Boolean option determines whether or not
    implicitly included system schemas such as <literal>pg_catalog</> are included in the search 
    path returned.
   </para>
8170

8171 8172 8173 8174 8175 8176 8177 8178
   <note>
    <para>
     The search path may be altered at run time.  The command is:
<programlisting>
SET search_path TO <replaceable>schema</> <optional>, <replaceable>schema</>, ...</optional>
</programlisting>
    </para>
   </note>
8179

8180 8181 8182
   <indexterm zone="functions-info">
    <primary>inet_client_addr</primary>
   </indexterm>
8183

8184 8185 8186
   <indexterm zone="functions-info">
    <primary>inet_client_port</primary>
   </indexterm>
8187

8188 8189 8190
   <indexterm zone="functions-info">
    <primary>inet_server_addr</primary>
   </indexterm>
8191

8192 8193 8194
   <indexterm zone="functions-info">
    <primary>inet_server_port</primary>
   </indexterm>
8195

8196 8197 8198 8199 8200 8201 8202
   <para>
     <function>inet_client_addr</function> returns the IP address of the
     current client, and <function>inet_client_port</function> returns the
     port number.
     <function>inet_server_addr</function> returns the IP address on which
     the server accepted the current connection, and
     <function>inet_server_port</function> returns the port number.
T
Tom Lane 已提交
8203 8204
     All these functions return NULL if the current connection is via a
     Unix-domain socket.
8205
   </para>
8206

8207 8208 8209
   <indexterm zone="functions-info">
    <primary>version</primary>
   </indexterm>
8210

8211 8212 8213 8214
   <para>
    <function>version()</function> returns a string describing the
    <productname>PostgreSQL</productname> server's version.
   </para>
8215

8216 8217 8218 8219
  <indexterm>
   <primary>privilege</primary>
   <secondary>querying</secondary>
  </indexterm>
8220 8221

  <para>
8222 8223 8224 8225
   <xref linkend="functions-info-access-table"> lists functions that
   allow the user to query object access privileges programmatically.
   See <xref linkend="ddl-priv"> for more information about
   privileges.
8226 8227
  </para>

8228 8229 8230 8231 8232 8233
   <table id="functions-info-access-table">
    <title>Access Privilege Inquiry Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>
8234

8235 8236 8237 8238 8239 8240 8241 8242 8243 8244 8245 8246 8247 8248 8249 8250 8251 8252 8253 8254 8255 8256 8257 8258 8259 8260 8261 8262 8263 8264 8265 8266 8267 8268 8269 8270 8271 8272 8273 8274 8275 8276 8277 8278 8279 8280 8281 8282 8283 8284 8285 8286 8287 8288 8289 8290 8291 8292 8293 8294 8295 8296 8297 8298 8299 8300 8301 8302 8303 8304 8305 8306 8307 8308 8309 8310 8311 8312 8313 8314 8315 8316 8317 8318 8319 8320 8321 8322 8323 8324 8325 8326 8327 8328
     <tbody>
      <row>
       <entry><literal><function>has_table_privilege</function>(<parameter>user</parameter>,
                                  <parameter>table</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for table</entry>
      </row>
      <row>
       <entry><literal><function>has_table_privilege</function>(<parameter>table</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for table</entry>
      </row>
      <row>
       <entry><literal><function>has_database_privilege</function>(<parameter>user</parameter>,
                                  <parameter>database</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for database</entry>
      </row>
      <row>
       <entry><literal><function>has_database_privilege</function>(<parameter>database</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for database</entry>
      </row>
      <row>
       <entry><literal><function>has_function_privilege</function>(<parameter>user</parameter>,
                                  <parameter>function</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for function</entry>
      </row>
      <row>
       <entry><literal><function>has_function_privilege</function>(<parameter>function</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for function</entry>
      </row>
      <row>
       <entry><literal><function>has_language_privilege</function>(<parameter>user</parameter>,
                                  <parameter>language</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for language</entry>
      </row>
      <row>
       <entry><literal><function>has_language_privilege</function>(<parameter>language</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for language</entry>
      </row>
      <row>
       <entry><literal><function>has_schema_privilege</function>(<parameter>user</parameter>,
                                  <parameter>schema</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for schema</entry>
      </row>
      <row>
       <entry><literal><function>has_schema_privilege</function>(<parameter>schema</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for schema</entry>
      </row>
      <row>
       <entry><literal><function>has_tablespace_privilege</function>(<parameter>user</parameter>,
                                  <parameter>tablespace</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does user have privilege for tablespace</entry>
      </row>
      <row>
       <entry><literal><function>has_tablespace_privilege</function>(<parameter>tablespace</parameter>,
                                  <parameter>privilege</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>does current user have privilege for tablespace</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8329

8330 8331 8332 8333 8334 8335 8336 8337 8338 8339 8340 8341 8342 8343 8344 8345 8346 8347
   <indexterm zone="functions-info">
    <primary>has_table_privilege</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>has_database_privilege</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>has_function_privilege</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>has_language_privilege</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>has_schema_privilege</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>has_tablespace_privilege</primary>
   </indexterm>
8348

8349 8350 8351 8352 8353 8354 8355 8356 8357 8358 8359 8360 8361 8362 8363 8364 8365 8366 8367 8368 8369
   <para>
    <function>has_table_privilege</function> checks whether a user
    can access a table in a particular way.  The user can be
    specified by name or by ID
    (<literal>pg_user.usesysid</literal>), or if the argument is
    omitted
    <function>current_user</function> is assumed.  The table can be specified
    by name or by OID.  (Thus, there are actually six variants of
    <function>has_table_privilege</function>, which can be distinguished by
    the number and types of their arguments.)  When specifying by name,
    the name can be schema-qualified if necessary.
    The desired access privilege type
    is specified by a text string, which must evaluate to one of the
    values <literal>SELECT</literal>, <literal>INSERT</literal>, <literal>UPDATE</literal>,
    <literal>DELETE</literal>, <literal>RULE</literal>, <literal>REFERENCES</literal>, or
    <literal>TRIGGER</literal>.  (Case of the string is not significant, however.)
    An example is:
<programlisting>
SELECT has_table_privilege('myschema.mytable', 'select');
</programlisting>
   </para>
8370

8371 8372 8373 8374 8375 8376 8377 8378 8379 8380
   <para>
    <function>has_database_privilege</function> checks whether a user
    can access a database in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    The desired access privilege type must evaluate to
    <literal>CREATE</literal>,
    <literal>TEMPORARY</literal>, or
    <literal>TEMP</literal> (which is equivalent to
    <literal>TEMPORARY</literal>).
   </para>
8381

8382 8383 8384 8385 8386
   <para>
    <function>has_function_privilege</function> checks whether a user
    can access a function in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    When specifying a function by a text string rather than by OID,
8387 8388
    the allowed input is the same as for the <type>regprocedure</> data type
    (see <xref linkend="datatype-oid">).
8389 8390 8391 8392 8393 8394 8395
    The desired access privilege type must evaluate to
    <literal>EXECUTE</literal>.
    An example is:
<programlisting>
SELECT has_function_privilege('joeuser', 'myfunc(int, text)', 'execute');
</programlisting>
   </para>
8396

8397 8398 8399 8400 8401 8402 8403 8404 8405 8406 8407 8408 8409 8410 8411 8412 8413 8414 8415 8416 8417 8418 8419 8420
   <para>
    <function>has_language_privilege</function> checks whether a user
    can access a procedural language in a particular way.  The possibilities
    for its arguments are analogous to <function>has_table_privilege</function>.
    The desired access privilege type must evaluate to
    <literal>USAGE</literal>.
   </para>

   <para>
    <function>has_schema_privilege</function> checks whether a user
    can access a schema in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    The desired access privilege type must evaluate to
    <literal>CREATE</literal> or
    <literal>USAGE</literal>.
   </para>

   <para>
    <function>has_tablespace_privilege</function> checks whether a user
    can access a tablespace in a particular way.  The possibilities for its
    arguments are analogous to <function>has_table_privilege</function>.
    The desired access privilege type must evaluate to
    <literal>CREATE</literal>.
   </para>
8421 8422

  <para>
T
Tom Lane 已提交
8423
   To test whether a user holds a grant option on the privilege,
8424 8425
   append <literal> WITH GRANT OPTION</literal> to the privilege key
   word; for example <literal>'UPDATE WITH GRANT OPTION'</literal>.
8426 8427 8428
  </para>

  <para>
8429 8430 8431 8432 8433 8434 8435 8436 8437 8438 8439
   <xref linkend="functions-info-schema-table"> shows functions that
   determine whether a certain object is <firstterm>visible</> in the
   current schema search path.  A table is said to be visible if its
   containing schema is in the search path and no table of the same
   name appears earlier in the search path.  This is equivalent to the
   statement that the table can be referenced by name without explicit
   schema qualification.  For example, to list the names of all
   visible tables:
<programlisting>
SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);
</programlisting>
8440 8441
  </para>

8442 8443 8444 8445 8446 8447
   <table id="functions-info-schema-table">
    <title>Schema Visibility Inquiry Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>
8448

8449 8450 8451 8452 8453 8454 8455 8456 8457 8458 8459 8460 8461 8462 8463 8464 8465 8466 8467 8468 8469 8470 8471 8472 8473 8474 8475 8476 8477 8478 8479 8480 8481 8482 8483 8484 8485 8486 8487 8488
     <tbody>
      <row>
       <entry><literal><function>pg_table_is_visible</function>(<parameter>table_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is table visible in search path</entry>
      </row>
      <row>
       <entry><literal><function>pg_type_is_visible</function>(<parameter>type_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is type (or domain) visible in search path</entry>
      </row>
      <row>
       <entry><literal><function>pg_function_is_visible</function>(<parameter>function_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is function visible in search path</entry>
      </row>
      <row>
       <entry><literal><function>pg_operator_is_visible</function>(<parameter>operator_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is operator visible in search path</entry>
      </row>
      <row>
       <entry><literal><function>pg_opclass_is_visible</function>(<parameter>opclass_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is operator class visible in search path</entry>
      </row>
      <row>
       <entry><literal><function>pg_conversion_is_visible</function>(<parameter>conversion_oid</parameter>)</literal>
       </entry>
       <entry><type>boolean</type></entry>
       <entry>is conversion visible in search path</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8489

8490 8491 8492 8493 8494 8495 8496 8497 8498 8499 8500 8501 8502 8503 8504 8505 8506 8507
   <indexterm zone="functions-info">
    <primary>pg_table_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>pg_type_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>pg_function_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>pg_operator_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>pg_opclass_is_visible</primary>
   </indexterm>
   <indexterm zone="functions-info">
    <primary>pg_conversion_is_visible</primary>
   </indexterm>
8508

8509 8510 8511 8512 8513 8514 8515 8516 8517 8518 8519 8520 8521 8522 8523
   <para>
   <function>pg_table_is_visible</function> performs the check for
   tables (or views, or any other kind of <literal>pg_class</> entry).
   <function>pg_type_is_visible</function>,
   <function>pg_function_is_visible</function>,
   <function>pg_operator_is_visible</function>,
   <function>pg_opclass_is_visible</function>, and
   <function>pg_conversion_is_visible</function> perform the same sort of
   visibility check for types (and domains), functions, operators, operator classes
   and conversions, respectively.  For functions and operators, an object in
   the search path is visible if there is no object of the same name
   <emphasis>and argument data type(s)</> earlier in the path.  For
   operator classes, both name and associated index access method are
   considered.
   </para>
8524

8525 8526 8527 8528 8529 8530 8531 8532 8533
   <para>
    All these functions require object OIDs to identify the object to be
    checked.  If you want to test an object by name, it is convenient to use
    the OID alias types (<type>regclass</>, <type>regtype</>,
    <type>regprocedure</>, or <type>regoperator</>), for example
<programlisting>
SELECT pg_type_is_visible('myschema.widget'::regtype);
</programlisting>
    Note that it would not make much sense to test an unqualified name in
8534
    this way &mdash; if the name can be recognized at all, it must be visible.
8535
   </para>
8536

8537 8538 8539 8540
   <indexterm zone="functions-info">
    <primary>format_type</primary>
   </indexterm>

8541 8542 8543
   <indexterm zone="functions-info">
    <primary>pg_get_viewdef</primary>
   </indexterm>
8544

8545 8546 8547
   <indexterm zone="functions-info">
    <primary>pg_get_ruledef</primary>
   </indexterm>
8548

8549 8550 8551
   <indexterm zone="functions-info">
    <primary>pg_get_indexdef</primary>
   </indexterm>
8552

8553 8554 8555
   <indexterm zone="functions-info">
    <primary>pg_get_triggerdef</primary>
   </indexterm>
8556

8557 8558 8559
   <indexterm zone="functions-info">
    <primary>pg_get_constraintdef</primary>
   </indexterm>
8560

8561 8562
   <indexterm zone="functions-info">
    <primary>pg_get_expr</primary>
P
Peter Eisentraut 已提交
8563 8564
   </indexterm>

8565 8566 8567 8568 8569 8570 8571 8572 8573 8574 8575
   <indexterm zone="functions-info">
    <primary>pg_get_userbyid</primary>
   </indexterm>

   <indexterm zone="functions-info">
    <primary>pg_get_serial_sequence</primary>
   </indexterm>

   <indexterm zone="functions-info">
    <primary>pg_tablespace_databases</primary>
   </indexterm>
8576 8577

  <para>
8578 8579
   <xref linkend="functions-info-catalog-table"> lists functions that
   extract information from the system catalogs.
8580 8581
  </para>

8582 8583 8584 8585 8586 8587 8588 8589
   <table id="functions-info-catalog-table">
    <title>System Catalog Information Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>

     <tbody>
8590 8591 8592 8593 8594
      <row>
       <entry><literal><function>format_type</function>(<parameter>type_oid</parameter>, <parameter>typemod</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get SQL name of a data type</entry>
      </row>
8595 8596 8597 8598 8599 8600 8601 8602 8603 8604 8605 8606 8607 8608 8609 8610 8611 8612 8613 8614 8615 8616 8617 8618 8619 8620 8621 8622 8623 8624 8625 8626 8627 8628 8629 8630 8631 8632 8633 8634 8635 8636 8637 8638 8639 8640 8641 8642 8643 8644 8645 8646 8647 8648 8649 8650 8651 8652 8653 8654 8655 8656 8657 8658 8659 8660 8661 8662 8663 8664 8665 8666 8667 8668 8669 8670 8671
      <row>
       <entry><literal><function>pg_get_viewdef</function>(<parameter>view_name</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE VIEW</> command for view (<emphasis>deprecated</emphasis>)</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_viewdef</function>(<parameter>view_name</parameter>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE VIEW</> command for view (<emphasis>deprecated</emphasis>)</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_viewdef</function>(<parameter>view_oid</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE VIEW</> command for view</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_viewdef</function>(<parameter>view_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE VIEW</> command for view</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_ruledef</function>(<parameter>rule_oid</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE RULE</> command for rule</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_ruledef</function>(<parameter>rule_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE RULE</> command for rule</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_indexdef</function>(<parameter>index_oid</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE INDEX</> command for index</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_indexdef</function>(<parameter>index_oid</parameter>, <parameter>column_no</>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE INDEX</> command for index,
       or definition of just one index column when
       <parameter>column_no</> is not zero</entry>
      </row>
      <row>
       <entry><function>pg_get_triggerdef</function>(<parameter>trigger_oid</parameter>)</entry>
       <entry><type>text</type></entry>
       <entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_constraintdef</function>(<parameter>constraint_oid</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get definition of a constraint</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_constraintdef</function>(<parameter>constraint_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get definition of a constraint</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_expr</function>(<parameter>expr_text</parameter>, <parameter>relation_oid</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>decompile internal form of an expression, assuming that any Vars
       in it refer to the relation indicated by the second parameter</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_expr</function>(<parameter>expr_text</parameter>, <parameter>relation_oid</>, <parameter>pretty_bool</>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>decompile internal form of an expression, assuming that any Vars
       in it refer to the relation indicated by the second parameter</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_userbyid</function>(<parameter>userid</parameter>)</literal></entry>
       <entry><type>name</type></entry>
       <entry>get user name with given ID</entry>
      </row>
      <row>
       <entry><literal><function>pg_get_serial_sequence</function>(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</literal></entry>
       <entry><type>text</type></entry>
P
Peter Eisentraut 已提交
8672
       <entry>get name of the sequence that a <type>serial</type> or <type>bigserial</type> column
8673 8674 8675 8676 8677 8678 8679 8680 8681 8682 8683
       uses</entry>
      </row>
      <row>
       <entry><literal><function>pg_tablespace_databases</function>(<parameter>tablespace_oid</parameter>)</literal></entry>
       <entry><type>setof oid</type></entry>
       <entry>get set of database OIDs that have objects in the tablespace</entry>
      </row>
     </tbody>
    </tgroup>
   </table>

8684 8685 8686 8687 8688 8689
  <para>
   <function>format_type</function> returns the SQL name of a data type that
   is identified by its type OID and possibly a type modifier.  Pass NULL
   for the type modifier if no specific modifier is known.
  </para>

8690
  <para>
8691 8692 8693 8694 8695 8696 8697 8698 8699 8700 8701 8702 8703 8704 8705 8706 8707 8708 8709
   <function>pg_get_viewdef</function>,
   <function>pg_get_ruledef</function>,
   <function>pg_get_indexdef</function>,
   <function>pg_get_triggerdef</function>, and
   <function>pg_get_constraintdef</function> respectively
   reconstruct the creating command for a view, rule, index, trigger, or
   constraint.  (Note that this is a decompiled reconstruction, not
   the original text of the command.)
   <function>pg_get_expr</function> decompiles the internal form of an
   individual expression, such as the default value for a column.  It
   may be useful when examining the contents of system catalogs.
   Most of these functions come in two
   variants, one of which can optionally <quote>pretty-print</> the result.
   The pretty-printed format is more readable, but the default format is more
   likely to be
   interpreted the same way by future versions of <productname>PostgreSQL</>;
   avoid using pretty-printed output for dump purposes.
   Passing <literal>false</> for the pretty-print parameter yields the
   same result as the variant that does not have the parameter at all.
8710 8711 8712
  </para>

  <para>
8713 8714 8715 8716 8717 8718 8719 8720
   <function>pg_get_userbyid</function>
   extracts a user's name given a user ID number.
   <function>pg_get_serial_sequence</function>
   fetches the name of the sequence associated with a serial or
   bigserial column.  The name is suitably formatted
   for passing to the sequence functions (see <xref
   linkend="functions-sequence">).
   NULL is returned if the column does not have a sequence attached.
8721 8722 8723
  </para>

  <para>
8724 8725 8726 8727 8728 8729 8730 8731
  <function>pg_tablespace_databases</function> allows usage examination of a
  tablespace. It will return a set of OIDs of databases that have objects
  stored in the tablespace. If this function returns any row, the
  tablespace is not empty and cannot be dropped. To
  display the specific objects populating the tablespace, you will need
  to connect to the databases identified by 
  <function>pg_tablespace_databases</function> and query their
  <structname>pg_class</> catalogs.
8732 8733
  </para>

8734 8735 8736
   <indexterm zone="functions-info">
    <primary>obj_description</primary>
   </indexterm>
8737

8738 8739 8740
   <indexterm zone="functions-info">
    <primary>col_description</primary>
   </indexterm>
8741

8742 8743 8744 8745
   <indexterm zone="functions-info">
    <primary>comment</primary>
    <secondary sortas="database objects">about database objects</secondary>
   </indexterm>
8746

8747 8748 8749 8750 8751 8752 8753
   <para>
    The functions shown in <xref
    linkend="functions-info-comment-table"> extract comments
    previously stored with the <command>COMMENT</command> command.  A
    null value is returned if no comment could be found matching the
    specified parameters.
   </para>
8754

8755 8756 8757 8758 8759 8760
   <table id="functions-info-comment-table">
    <title>Comment Information Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>
8761

8762 8763 8764 8765 8766 8767 8768 8769 8770 8771 8772 8773 8774 8775 8776 8777 8778 8779 8780
     <tbody>
      <row>
       <entry><literal><function>obj_description</function>(<parameter>object_oid</parameter>, <parameter>catalog_name</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get comment for a database object</entry>
      </row>
      <row>
       <entry><literal><function>obj_description</function>(<parameter>object_oid</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get comment for a database object (<emphasis>deprecated</emphasis>)</entry>
      </row>
      <row>
       <entry><literal><function>col_description</function>(<parameter>table_oid</parameter>, <parameter>column_number</parameter>)</literal></entry>
       <entry><type>text</type></entry>
       <entry>get comment for a table column</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8781

8782 8783 8784 8785 8786 8787 8788 8789 8790 8791 8792
   <para>
    The two-parameter form of <function>obj_description</function> returns the
    comment for a database object specified by its OID and the name of the
    containing system catalog.  For example,
    <literal>obj_description(123456,'pg_class')</literal>
    would retrieve the comment for a table with OID 123456.
    The one-parameter form of <function>obj_description</function> requires only
    the object OID.  It is now deprecated since there is no guarantee that
    OIDs are unique across different system catalogs; therefore, the wrong
    comment could be returned.
   </para>
8793

8794 8795 8796 8797 8798 8799 8800
   <para>
    <function>col_description</function> returns the comment for a table column,
    which is specified by the OID of its table and its column number.
    <function>obj_description</function> cannot be used for table columns since
    columns do not have OIDs of their own.
   </para>
  </sect1>
8801

8802 8803
 <sect1 id="functions-admin">
  <title>System Administration Functions</title>
8804 8805

  <para>
8806 8807
   <xref linkend="functions-admin-set-table"> shows the functions
   available to query and alter run-time configuration parameters.
8808 8809
  </para>

8810 8811 8812 8813 8814 8815
   <table id="functions-admin-set-table">
    <title>Configuration Settings Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
     </thead>
8816

8817 8818 8819 8820 8821 8822 8823 8824 8825 8826 8827 8828 8829 8830 8831 8832 8833 8834 8835 8836
     <tbody>
      <row>
       <entry>
        <literal><function>current_setting</function>(<parameter>setting_name</parameter>)</literal>
       </entry>
       <entry><type>text</type></entry>
       <entry>current value of setting</entry>
      </row>
      <row>
       <entry>
        <literal><function>set_config(<parameter>setting_name</parameter>,
                             <parameter>new_value</parameter>,
                             <parameter>is_local</parameter>)</function></literal>
       </entry>
       <entry><type>text</type></entry>
       <entry>set parameter and return new value</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8837

8838 8839 8840
   <indexterm zone="functions-admin">
    <primary>SET</primary>
   </indexterm>
8841

8842 8843 8844
   <indexterm zone="functions-admin">
    <primary>SHOW</primary>
   </indexterm>
8845

8846 8847 8848 8849 8850
   <indexterm zone="functions-admin">
    <primary>configuration</primary>
    <secondary sortas="server">of the server</secondary>
    <tertiary>functions</tertiary>
   </indexterm>
8851

8852 8853 8854 8855 8856 8857 8858
   <para>
    The function <function>current_setting</function> yields the
    current value of the setting <parameter>setting_name</parameter>.
    It corresponds to the <acronym>SQL</acronym> command
    <command>SHOW</command>.  An example:
<programlisting>
SELECT current_setting('datestyle');
8859

8860 8861 8862 8863 8864 8865
 current_setting
-----------------
 ISO, MDY
(1 row)
</programlisting>
   </para>
8866

8867 8868 8869 8870 8871 8872 8873 8874 8875 8876 8877
   <para>
    <function>set_config</function> sets the parameter
    <parameter>setting_name</parameter> to
    <parameter>new_value</parameter>.  If
    <parameter>is_local</parameter> is <literal>true</literal>, the
    new value will only apply to the current transaction. If you want
    the new value to apply for the current session, use
    <literal>false</literal> instead. The function corresponds to the
    SQL command <command>SET</command>. An example:
<programlisting>
SELECT set_config('log_statement_stats', 'off', false);
8878

8879 8880 8881 8882 8883 8884
 set_config
------------
 off
(1 row)
</programlisting>
   </para>
8885

8886 8887 8888
   <indexterm zone="functions-admin">
    <primary>pg_cancel_backend</primary>
   </indexterm>
8889

8890 8891 8892 8893
   <indexterm zone="functions-admin">
    <primary>signal</primary>
    <secondary sortas="backend">backend processes</secondary>
   </indexterm>
8894

8895 8896 8897 8898 8899 8900
   <para>
    The function shown in <xref
    linkend="functions-admin-signal-table"> sends control signals to
    other server processes.  Use of this function is restricted
    to superusers.
   </para>
8901

8902 8903 8904 8905 8906 8907 8908
   <table id="functions-admin-signal-table">
    <title>Backend Signalling Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry>
      </row>
     </thead>
8909

8910 8911 8912
     <tbody>
      <row>
       <entry>
8913 8914
        <literal><function>pg_cancel_backend</function>(<parameter>pid</parameter>)</literal>
        </entry>
8915 8916 8917 8918 8919 8920
       <entry><type>int</type></entry>
       <entry>Cancel a backend's current query</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8921

8922 8923 8924 8925
   <para>
    This function returns 1 if successful, 0 if not successful.
    The process ID (<literal>pid</literal>) of an active backend can be found
    from the <structfield>procpid</structfield> column in the
P
Peter Eisentraut 已提交
8926
    <structname>pg_stat_activity</structname> view, or by listing the <command>postgres</command>
8927 8928
    processes on the server with <application>ps</>.
   </para>
8929

8930 8931 8932
   <indexterm zone="functions-admin">
    <primary>pg_start_backup</primary>
   </indexterm>
8933

8934 8935 8936
   <indexterm zone="functions-admin">
    <primary>pg_stop_backup</primary>
   </indexterm>
8937

8938 8939 8940
   <indexterm zone="functions-admin">
    <primary>backup</primary>
   </indexterm>
8941

8942 8943 8944 8945 8946
   <para>
    The functions shown in <xref
    linkend="functions-admin-backup-table"> assist in making on-line backups.
    Use of these functions is restricted to superusers.
   </para>
8947

8948 8949 8950 8951 8952 8953 8954
   <table id="functions-admin-backup-table">
    <title>Backup Control Functions</title>
    <tgroup cols="3">
     <thead>
      <row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry>
      </row>
     </thead>
8955

8956 8957 8958
     <tbody>
      <row>
       <entry>
8959 8960
        <literal><function>pg_start_backup</function>(<parameter>label_text</parameter>)</literal>
        </entry>
8961 8962 8963 8964 8965
       <entry><type>text</type></entry>
       <entry>Set up for performing on-line backup</entry>
      </row>
      <row>
       <entry>
8966 8967
        <literal><function>pg_stop_backup</function>()</literal>
        </entry>
8968 8969 8970 8971 8972 8973
       <entry><type>text</type></entry>
       <entry>Finish performing on-line backup</entry>
      </row>
     </tbody>
    </tgroup>
   </table>
8974

8975 8976 8977 8978 8979 8980 8981 8982 8983
   <para>
    <function>pg_start_backup</> accepts a single parameter which is an
    arbitrary user-defined label for the backup.  (Typically this would be
    the name under which the backup dump file will be stored.)  The function
    writes a backup label file into the database cluster's data directory,
    and then returns the backup's starting WAL offset as text.  (The user
    need not pay any attention to this result value, but it is provided in
    case it is of use.)
   </para>
8984

8985 8986 8987 8988 8989 8990 8991 8992 8993
   <para>
    <function>pg_stop_backup</> removes the label file created by
    <function>pg_start_backup</>, and instead creates a backup history file in
    the WAL archive area.  The history file includes the label given to
    <function>pg_start_backup</>, the starting and ending WAL offsets for
    the backup, and the starting and ending times of the backup.  The return
    value is the backup's ending WAL offset (which again may be of little
    interest).
   </para>
8994

8995 8996 8997 8998 8999
   <para>
    For details about proper usage of these functions, see
    <xref linkend="backup-online">.
   </para>
  </sect1>
9000
</chapter>
9001

9002 9003
<!-- Keep this comment at the end of the file
Local variables:
T
Thomas G. Lockhart 已提交
9004
mode:sgml
9005
sgml-omittag:nil
9006 9007 9008 9009 9010 9011 9012 9013
sgml-shorttag:t
sgml-minimize-attributes:nil
sgml-always-quote-attributes:t
sgml-indent-step:1
sgml-indent-data:t
sgml-parent-document:nil
sgml-default-dtd-file:"./reference.ced"
sgml-exposed-tags:nil
T
Thomas G. Lockhart 已提交
9014
sgml-local-catalogs:("/usr/lib/sgml/catalog")
9015 9016 9017
sgml-local-ecat-files:nil
End:
-->