2
$PostgreSQL: pgsql/doc/src/sgml/func.sgml,v 1.234.4.5 2005-04-01 14:25:39 momjian Exp $
3
PostgreSQL documentation
6
<chapter id="functions">
7
<title>Functions and Operators</title>
9
<indexterm zone="functions">
10
<primary>function</primary>
13
<indexterm zone="functions">
14
<primary>operator</primary>
18
<productname>PostgreSQL</productname> provides a large number of
19
functions and operators for the built-in data types. Users can also
20
define their own functions and operators, as described in
21
<xref linkend="server-programming">. The
22
<application>psql</application> commands <command>\df</command> and
23
<command>\do</command> can be used to show the list of all actually
24
available functions and operators, respectively.
28
If you are concerned about portability then take note that most of
29
the functions and operators described in this chapter, with the
30
exception of the most trivial arithmetic and comparison operators
31
and some explicitly marked functions, are not specified by the
32
<acronym>SQL</acronym> standard. Some of the extended functionality
33
is present in other <acronym>SQL</acronym> database management
34
systems, and in many cases this functionality is compatible and
35
consistent between the various implementations.
39
<sect1 id="functions-logical">
40
<title>Logical Operators</title>
42
<indexterm zone="functions-logical">
43
<primary>operator</primary>
44
<secondary>logical</secondary>
48
<primary>Boolean</primary>
49
<secondary>operators</secondary>
50
<see>operators, logical</see>
54
The usual logical operators are available:
57
<primary>AND (operator)</primary>
61
<primary>OR (operator)</primary>
65
<primary>NOT (operator)</primary>
69
<primary>conjunction</primary>
73
<primary>disjunction</primary>
77
<primary>negation</primary>
81
<member><literal>AND</></member>
82
<member><literal>OR</></member>
83
<member><literal>NOT</></member>
86
<acronym>SQL</acronym> uses a three-valued Boolean logic where the null value represents
87
<quote>unknown</quote>. Observe the following truth tables:
93
<entry><replaceable>a</replaceable></entry>
94
<entry><replaceable>b</replaceable></entry>
95
<entry><replaceable>a</replaceable> AND <replaceable>b</replaceable></entry>
96
<entry><replaceable>a</replaceable> OR <replaceable>b</replaceable></entry>
150
<entry><replaceable>a</replaceable></entry>
151
<entry>NOT <replaceable>a</replaceable></entry>
176
The operators <literal>AND</literal> and <literal>OR</literal> are
177
commutative, that is, you can switch the left and right operand
178
without affecting the result. But see <xref
179
linkend="syntax-express-eval"> for more information about the
180
order of evaluation of subexpressions.
184
<sect1 id="functions-comparison">
185
<title>Comparison Operators</title>
187
<indexterm zone="functions-comparison">
188
<primary>comparison</primary>
189
<secondary>operators</secondary>
193
The usual comparison operators are available, shown in <xref
194
linkend="functions-comparison-table">.
197
<table id="functions-comparison-table">
198
<title>Comparison Operators</title>
202
<entry>Operator</entry>
203
<entry>Description</entry>
209
<entry> <literal><</literal> </entry>
210
<entry>less than</entry>
214
<entry> <literal>></literal> </entry>
215
<entry>greater than</entry>
219
<entry> <literal><=</literal> </entry>
220
<entry>less than or equal to</entry>
224
<entry> <literal>>=</literal> </entry>
225
<entry>greater than or equal to</entry>
229
<entry> <literal>=</literal> </entry>
234
<entry> <literal><></literal> or <literal>!=</literal> </entry>
235
<entry>not equal</entry>
243
The <literal>!=</literal> operator is converted to
244
<literal><></literal> in the parser stage. It is not
245
possible to implement <literal>!=</literal> and
246
<literal><></literal> operators that do different things.
251
Comparison operators are available for all data types where this
252
makes sense. All comparison operators are binary operators that
253
return values of type <type>boolean</type>; expressions like
254
<literal>1 < 2 < 3</literal> are not valid (because there is
255
no <literal><</literal> operator to compare a Boolean value with
256
<literal>3</literal>).
261
<primary>BETWEEN</primary>
263
In addition to the comparison operators, the special
264
<token>BETWEEN</token> construct is available.<indexterm><primary>BETWEEN</primary></indexterm>
266
<replaceable>a</replaceable> BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
270
<replaceable>a</replaceable> >= <replaceable>x</replaceable> AND <replaceable>a</replaceable> <= <replaceable>y</replaceable>
274
<replaceable>a</replaceable> NOT BETWEEN <replaceable>x</replaceable> AND <replaceable>y</replaceable>
278
<replaceable>a</replaceable> < <replaceable>x</replaceable> OR <replaceable>a</replaceable> > <replaceable>y</replaceable>
280
There is no difference between the two respective forms apart from
281
the <acronym>CPU</acronym> cycles required to rewrite the first one
282
into the second one internally.
287
<primary>IS NULL</primary>
290
<primary>IS NOT NULL</primary>
293
<primary>ISNULL</primary>
296
<primary>NOTNULL</primary>
298
To check whether a value is or is not null, use the constructs
300
<replaceable>expression</replaceable> IS NULL
301
<replaceable>expression</replaceable> IS NOT NULL
303
or the equivalent, but nonstandard, constructs
305
<replaceable>expression</replaceable> ISNULL
306
<replaceable>expression</replaceable> NOTNULL
308
<indexterm><primary>null value</primary><secondary>comparing</secondary></indexterm>
312
Do <emphasis>not</emphasis> write
313
<literal><replaceable>expression</replaceable> = NULL</literal>
314
because <literal>NULL</> is not <quote>equal to</quote>
315
<literal>NULL</>. (The null value represents an unknown value,
316
and it is not known whether two unknown values are equal.) This
317
behavior conforms to the SQL standard.
322
Some applications may expect that
323
<literal><replaceable>expression</replaceable> = NULL</literal>
324
returns true if <replaceable>expression</replaceable> evaluates to
325
the null value. It is highly recommended that these applications
326
be modified to comply with the SQL standard. However, if that
327
cannot be done the <xref linkend="guc-transform-null-equals">
328
configuration variable is available. If it is enabled,
329
<productname>PostgreSQL</productname> will convert <literal>x =
330
NULL</literal> clauses to <literal>x IS NULL</literal>. This was
331
the default behavior in <productname>PostgreSQL</productname>
332
releases 6.5 through 7.1.
338
<primary>IS DISTINCT FROM</primary>
340
The ordinary comparison operators yield null (signifying <quote>unknown</>)
341
when either input is null. Another way to do comparisons is with the
342
<literal>IS DISTINCT FROM</literal> construct:
344
<replaceable>expression</replaceable> IS DISTINCT FROM <replaceable>expression</replaceable>
346
For non-null inputs this is the same as the <literal><></> operator.
347
However, when both inputs are null it will return false, and when just
348
one input is null it will return true. Thus it effectively acts as though
349
null were a normal data value, rather than <quote>unknown</>.
354
<primary>IS TRUE</primary>
357
<primary>IS NOT TRUE</primary>
360
<primary>IS FALSE</primary>
363
<primary>IS NOT FALSE</primary>
366
<primary>IS UNKNOWN</primary>
369
<primary>IS NOT UNKNOWN</primary>
371
Boolean values can also be tested using the constructs
373
<replaceable>expression</replaceable> IS TRUE
374
<replaceable>expression</replaceable> IS NOT TRUE
375
<replaceable>expression</replaceable> IS FALSE
376
<replaceable>expression</replaceable> IS NOT FALSE
377
<replaceable>expression</replaceable> IS UNKNOWN
378
<replaceable>expression</replaceable> IS NOT UNKNOWN
380
These will always return true or false, never a null value, even when the
382
A null input is treated as the logical value <quote>unknown</>.
383
Notice that <literal>IS UNKNOWN</> and <literal>IS NOT UNKNOWN</> are
384
effectively the same as <literal>IS NULL</literal> and
385
<literal>IS NOT NULL</literal>, respectively, except that the input
386
expression must be of Boolean type.
390
<sect1 id="functions-math">
391
<title>Mathematical Functions and Operators</title>
394
Mathematical operators are provided for many
395
<productname>PostgreSQL</productname> types. For types without
396
common mathematical conventions for all possible permutations
397
(e.g., date/time types) we
398
describe the actual behavior in subsequent sections.
402
<xref linkend="functions-math-op-table"> shows the available mathematical operators.
405
<table id="functions-math-op-table">
406
<title>Mathematical Operators</title>
411
<entry>Operator</entry>
412
<entry>Description</entry>
413
<entry>Example</entry>
414
<entry>Result</entry>
420
<entry> <literal>+</literal> </entry>
421
<entry>addition</entry>
422
<entry><literal>2 + 3</literal></entry>
423
<entry><literal>5</literal></entry>
427
<entry> <literal>-</literal> </entry>
428
<entry>subtraction</entry>
429
<entry><literal>2 - 3</literal></entry>
430
<entry><literal>-1</literal></entry>
434
<entry> <literal>*</literal> </entry>
435
<entry>multiplication</entry>
436
<entry><literal>2 * 3</literal></entry>
437
<entry><literal>6</literal></entry>
441
<entry> <literal>/</literal> </entry>
442
<entry>division (integer division truncates results)</entry>
443
<entry><literal>4 / 2</literal></entry>
444
<entry><literal>2</literal></entry>
448
<entry> <literal>%</literal> </entry>
449
<entry>modulo (remainder)</entry>
450
<entry><literal>5 % 4</literal></entry>
451
<entry><literal>1</literal></entry>
455
<entry> <literal>^</literal> </entry>
456
<entry>exponentiation</entry>
457
<entry><literal>2.0 ^ 3.0</literal></entry>
458
<entry><literal>8</literal></entry>
462
<entry> <literal>|/</literal> </entry>
463
<entry>square root</entry>
464
<entry><literal>|/ 25.0</literal></entry>
465
<entry><literal>5</literal></entry>
469
<entry> <literal>||/</literal> </entry>
470
<entry>cube root</entry>
471
<entry><literal>||/ 27.0</literal></entry>
472
<entry><literal>3</literal></entry>
476
<entry> <literal>!</literal> </entry>
477
<entry>factorial</entry>
478
<entry><literal>5 !</literal></entry>
479
<entry><literal>120</literal></entry>
483
<entry> <literal>!!</literal> </entry>
484
<entry>factorial (prefix operator)</entry>
485
<entry><literal>!! 5</literal></entry>
486
<entry><literal>120</literal></entry>
490
<entry> <literal>@</literal> </entry>
491
<entry>absolute value</entry>
492
<entry><literal>@ -5.0</literal></entry>
493
<entry><literal>5</literal></entry>
497
<entry> <literal>&</literal> </entry>
498
<entry>bitwise AND</entry>
499
<entry><literal>91 & 15</literal></entry>
500
<entry><literal>11</literal></entry>
504
<entry> <literal>|</literal> </entry>
505
<entry>bitwise OR</entry>
506
<entry><literal>32 | 3</literal></entry>
507
<entry><literal>35</literal></entry>
511
<entry> <literal>#</literal> </entry>
512
<entry>bitwise XOR</entry>
513
<entry><literal>17 # 5</literal></entry>
514
<entry><literal>20</literal></entry>
518
<entry> <literal>~</literal> </entry>
519
<entry>bitwise NOT</entry>
520
<entry><literal>~1</literal></entry>
521
<entry><literal>-2</literal></entry>
525
<entry> <literal><<</literal> </entry>
526
<entry>bitwise shift left</entry>
527
<entry><literal>1 << 4</literal></entry>
528
<entry><literal>16</literal></entry>
532
<entry> <literal>>></literal> </entry>
533
<entry>bitwise shift right</entry>
534
<entry><literal>8 >> 2</literal></entry>
535
<entry><literal>2</literal></entry>
543
The bitwise operators work only on integral data types, whereas
544
the others are available for all numeric data types. The bitwise
545
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">.
551
<xref linkend="functions-math-func-table"> shows the available
552
mathematical functions. In the table, <literal>dp</literal>
553
indicates <type>double precision</type>. Many of these functions
554
are provided in multiple forms with different argument types.
555
Except where noted, any given form of a function returns the same
556
data type as its argument.
557
The functions working with <type>double precision</type> data are mostly
558
implemented on top of the host system's C library; accuracy and behavior in
559
boundary cases may therefore vary depending on the host system.
562
<table id="functions-math-func-table">
563
<title>Mathematical Functions</title>
567
<entry>Function</entry>
568
<entry>Return Type</entry>
569
<entry>Description</entry>
570
<entry>Example</entry>
571
<entry>Result</entry>
577
<entry><literal><function>abs</>(<replaceable>x</replaceable>)</literal></entry>
578
<entry>(same as <replaceable>x</>)</entry>
579
<entry>absolute value</entry>
580
<entry><literal>abs(-17.4)</literal></entry>
581
<entry><literal>17.4</literal></entry>
585
<entry><literal><function>cbrt</function>(<type>dp</type>)</literal></entry>
586
<entry><type>dp</type></entry>
587
<entry>cube root</entry>
588
<entry><literal>cbrt(27.0)</literal></entry>
589
<entry><literal>3</literal></entry>
593
<entry><literal><function>ceil</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
594
<entry>(same as input)</entry>
595
<entry>smallest integer not less than argument</entry>
596
<entry><literal>ceil(-42.8)</literal></entry>
597
<entry><literal>-42</literal></entry>
601
<entry><literal><function>ceiling</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
602
<entry>(same as input)</entry>
603
<entry>smallest integer not less than argument (alias for <function>ceil</function>)</entry>
604
<entry><literal>ceiling(-95.3)</literal></entry>
605
<entry><literal>-95</literal></entry>
609
<entry><literal><function>degrees</function>(<type>dp</type>)</literal></entry>
610
<entry><type>dp</type></entry>
611
<entry>radians to degrees</entry>
612
<entry><literal>degrees(0.5)</literal></entry>
613
<entry><literal>28.6478897565412</literal></entry>
617
<entry><literal><function>exp</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
618
<entry>(same as input)</entry>
619
<entry>exponential</entry>
620
<entry><literal>exp(1.0)</literal></entry>
621
<entry><literal>2.71828182845905</literal></entry>
625
<entry><literal><function>floor</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
626
<entry>(same as input)</entry>
627
<entry>largest integer not greater than argument</entry>
628
<entry><literal>floor(-42.8)</literal></entry>
629
<entry><literal>-43</literal></entry>
633
<entry><literal><function>ln</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
634
<entry>(same as input)</entry>
635
<entry>natural logarithm</entry>
636
<entry><literal>ln(2.0)</literal></entry>
637
<entry><literal>0.693147180559945</literal></entry>
641
<entry><literal><function>log</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
642
<entry>(same as input)</entry>
643
<entry>base 10 logarithm</entry>
644
<entry><literal>log(100.0)</literal></entry>
645
<entry><literal>2</literal></entry>
649
<entry><literal><function>log</function>(<parameter>b</parameter> <type>numeric</type>,
650
<parameter>x</parameter> <type>numeric</type>)</literal></entry>
651
<entry><type>numeric</type></entry>
652
<entry>logarithm to base <parameter>b</parameter></entry>
653
<entry><literal>log(2.0, 64.0)</literal></entry>
654
<entry><literal>6.0000000000</literal></entry>
658
<entry><literal><function>mod</function>(<parameter>y</parameter>,
659
<parameter>x</parameter>)</literal></entry>
660
<entry>(same as argument types)</entry>
661
<entry>remainder of <parameter>y</parameter>/<parameter>x</parameter></entry>
662
<entry><literal>mod(9,4)</literal></entry>
663
<entry><literal>1</literal></entry>
667
<entry><literal><function>pi</function>()</literal></entry>
668
<entry><type>dp</type></entry>
669
<entry><quote>π</quote> constant</entry>
670
<entry><literal>pi()</literal></entry>
671
<entry><literal>3.14159265358979</literal></entry>
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>
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>
693
<entry><literal><function>radians</function>(<type>dp</type>)</literal></entry>
694
<entry><type>dp</type></entry>
695
<entry>degrees to radians</entry>
696
<entry><literal>radians(45.0)</literal></entry>
697
<entry><literal>0.785398163397448</literal></entry>
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
<entry><literal>random()</literal></entry>
709
<entry><literal><function>round</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
710
<entry>(same as input)</entry>
711
<entry>round to nearest integer</entry>
712
<entry><literal>round(42.4)</literal></entry>
713
<entry><literal>42</literal></entry>
717
<entry><literal><function>round</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</literal></entry>
718
<entry><type>numeric</type></entry>
719
<entry>round to <parameter>s</parameter> decimal places</entry>
720
<entry><literal>round(42.4382, 2)</literal></entry>
721
<entry><literal>42.44</literal></entry>
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>
733
<entry><literal><function>sign</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
734
<entry>(same as input)</entry>
735
<entry>sign of the argument (-1, 0, +1)</entry>
736
<entry><literal>sign(-8.4)</literal></entry>
737
<entry><literal>-1</literal></entry>
741
<entry><literal><function>sqrt</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
742
<entry>(same as input)</entry>
743
<entry>square root</entry>
744
<entry><literal>sqrt(2.0)</literal></entry>
745
<entry><literal>1.4142135623731</literal></entry>
749
<entry><literal><function>trunc</function>(<type>dp</type> or <type>numeric</type>)</literal></entry>
750
<entry>(same as input)</entry>
751
<entry>truncate toward zero</entry>
752
<entry><literal>trunc(42.8)</literal></entry>
753
<entry><literal>42</literal></entry>
757
<entry><literal><function>trunc</function>(<parameter>v</parameter> <type>numeric</type>, <parameter>s</parameter> <type>integer</type>)</literal></entry>
758
<entry><type>numeric</type></entry>
759
<entry>truncate to <parameter>s</parameter> decimal places</entry>
760
<entry><literal>trunc(42.4382, 2)</literal></entry>
761
<entry><literal>42.43</literal></entry>
765
<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>
766
<entry><type>integer</type></entry>
767
<entry>return the bucket to which <parameter>operand</> would
768
be assigned in an equidepth histogram with <parameter>count</>
769
buckets, an upper bound of <parameter>b1</>, and a lower bound
770
of <parameter>b2</></entry>
771
<entry><literal>width_bucket(5.35, 0.024, 10.06, 5)</literal></entry>
772
<entry><literal>3</literal></entry>
779
Finally, <xref linkend="functions-math-trig-table"> shows the
780
available trigonometric functions. All trigonometric functions
781
take arguments and return values of type <type>double
785
<table id="functions-math-trig-table">
786
<title>Trigonometric Functions</title>
791
<entry>Function</entry>
792
<entry>Description</entry>
798
<entry><literal><function>acos</function>(<replaceable>x</replaceable>)</literal></entry>
799
<entry>inverse cosine</entry>
803
<entry><literal><function>asin</function>(<replaceable>x</replaceable>)</literal></entry>
804
<entry>inverse sine</entry>
808
<entry><literal><function>atan</function>(<replaceable>x</replaceable>)</literal></entry>
809
<entry>inverse tangent</entry>
813
<entry><literal><function>atan2</function>(<replaceable>x</replaceable>,
814
<replaceable>y</replaceable>)</literal></entry>
815
<entry>inverse tangent of
816
<literal><replaceable>x</replaceable>/<replaceable>y</replaceable></literal></entry>
820
<entry><literal><function>cos</function>(<replaceable>x</replaceable>)</literal></entry>
821
<entry>cosine</entry>
825
<entry><literal><function>cot</function>(<replaceable>x</replaceable>)</literal></entry>
826
<entry>cotangent</entry>
830
<entry><literal><function>sin</function>(<replaceable>x</replaceable>)</literal></entry>
835
<entry><literal><function>tan</function>(<replaceable>x</replaceable>)</literal></entry>
836
<entry>tangent</entry>
845
<sect1 id="functions-string">
846
<title>String Functions and Operators</title>
849
This section describes functions and operators for examining and
850
manipulating string values. Strings in this context include values
851
of all the types <type>character</type>, <type>character
852
varying</type>, and <type>text</type>. Unless otherwise noted, all
853
of the functions listed below work on all of these types, but be
854
wary of potential effects of the automatic padding when using the
855
<type>character</type> type. Generally, the functions described
856
here also work on data of non-string types by converting that data
857
to a string representation first. Some functions also exist
858
natively for the bit-string types.
862
<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
arguments. Details are in <xref linkend="functions-string-sql">.
865
These functions are also implemented using the regular syntax for
866
function invocation. (See <xref linkend="functions-string-other">.)
869
<table id="functions-string-sql">
870
<title><acronym>SQL</acronym> String Functions and Operators</title>
874
<entry>Function</entry>
875
<entry>Return Type</entry>
876
<entry>Description</entry>
877
<entry>Example</entry>
878
<entry>Result</entry>
884
<entry><literal><parameter>string</parameter> <literal>||</literal>
885
<parameter>string</parameter></literal></entry>
886
<entry> <type>text</type> </entry>
890
<primary>character string</primary>
891
<secondary>concatenation</secondary>
894
<entry><literal>'Post' || 'greSQL'</literal></entry>
895
<entry><literal>PostgreSQL</literal></entry>
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
<entry><literal>bit_length('jose')</literal></entry>
903
<entry><literal>32</literal></entry>
907
<entry><literal><function>char_length</function>(<parameter>string</parameter>)</literal> or <literal><function>character_length</function>(<parameter>string</parameter>)</literal></entry>
908
<entry><type>integer</type></entry>
910
Number of characters in string
912
<primary>character string</primary>
913
<secondary>length</secondary>
916
<primary>length</primary>
917
<secondary sortas="character string">of a character string</secondary>
918
<see>character strings, length</see>
921
<entry><literal>char_length('jose')</literal></entry>
922
<entry><literal>4</literal></entry>
926
<entry><literal><function>convert</function>(<parameter>string</parameter>
927
using <parameter>conversion_name</parameter>)</literal></entry>
928
<entry><type>text</type></entry>
930
Change encoding using specified conversion name. Conversions
931
can be defined by <command>CREATE CONVERSION</command>. Also
932
there are some pre-defined conversion names. See <xref
933
linkend="conversion-names"> for available conversion
936
<entry><literal>convert('PostgreSQL' using iso_8859_1_to_utf_8)</literal></entry>
937
<entry><literal>'PostgreSQL'</literal> in Unicode (UTF-8) encoding</entry>
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
<entry><literal>lower('TOM')</literal></entry>
945
<entry><literal>tom</literal></entry>
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
<entry><literal>octet_length('jose')</literal></entry>
953
<entry><literal>4</literal></entry>
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
<entry><type>text</type></entry>
962
<primary>overlay</primary>
965
<entry><literal>overlay('Txxxxas' placing 'hom' from 2 for 4)</literal></entry>
966
<entry><literal>Thomas</literal></entry>
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
<entry><literal>position('om' in 'Thomas')</literal></entry>
974
<entry><literal>3</literal></entry>
978
<entry><literal><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</literal></entry>
979
<entry><type>text</type></entry>
983
<primary>substring</primary>
986
<entry><literal>substring('Thomas' from 2 for 3)</literal></entry>
987
<entry><literal>hom</literal></entry>
991
<entry><literal><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable>)</literal></entry>
992
<entry><type>text</type></entry>
994
Extract substring matching POSIX regular expression
996
<primary>substring</primary>
999
<entry><literal>substring('Thomas' from '...$')</literal></entry>
1000
<entry><literal>mas</literal></entry>
1004
<entry><literal><function>substring</function>(<parameter>string</parameter> from <replaceable>pattern</replaceable> for <replaceable>escape</replaceable>)</literal></entry>
1005
<entry><type>text</type></entry>
1007
Extract substring matching <acronym>SQL</acronym> regular
1010
<primary>substring</primary>
1013
<entry><literal>substring('Thomas' from '%#"o_a#"_' for '#')</literal></entry>
1014
<entry><literal>oma</literal></entry>
1019
<literal><function>trim</function>(<optional>leading | trailing | both</optional>
1020
<optional><parameter>characters</parameter></optional> from
1021
<parameter>string</parameter>)</literal>
1023
<entry><type>text</type></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>.
1029
<entry><literal>trim(both 'x' from 'xTomxx')</literal></entry>
1030
<entry><literal>Tom</literal></entry>
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
<entry><literal>upper('tom')</literal></entry>
1038
<entry><literal>TOM</literal></entry>
1045
Additional string manipulation functions are available and are
1046
listed in <xref linkend="functions-string-other">. Some of them are used internally to implement the
1047
<acronym>SQL</acronym>-standard string functions listed in <xref linkend="functions-string-sql">.
1050
<table id="functions-string-other">
1051
<title>Other String Functions</title>
1055
<entry>Function</entry>
1056
<entry>Return Type</entry>
1057
<entry>Description</entry>
1058
<entry>Example</entry>
1059
<entry>Result</entry>
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
<entry><literal>ascii('x')</literal></entry>
1069
<entry><literal>120</literal></entry>
1073
<entry><literal><function>btrim</function>(<parameter>string</parameter> <type>text</type>
1074
<optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal></entry>
1075
<entry><type>text</type></entry>
1077
Remove the longest string consisting only of characters
1078
in <parameter>characters</parameter> (a space by default)
1079
from the start and end of <parameter>string</parameter>.
1081
<entry><literal>btrim('xyxtrimyyx', 'xy')</literal></entry>
1082
<entry><literal>trim</literal></entry>
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
<entry><literal>chr(65)</literal></entry>
1090
<entry><literal>A</literal></entry>
1095
<literal><function>convert</function>(<parameter>string</parameter>
1097
<optional><parameter>src_encoding</parameter> <type>name</type>,</optional>
1098
<parameter>dest_encoding</parameter> <type>name</type>)</literal>
1100
<entry><type>text</type></entry>
1102
Convert string to <parameter>dest_encoding</parameter>.
1103
The original encoding is specified by
1104
<parameter>src_encoding</parameter>. If
1105
<parameter>src_encoding</parameter> is omitted, database
1106
encoding is assumed.
1108
<entry><literal>convert( 'text_in_unicode', 'UNICODE', 'LATIN1')</literal></entry>
1109
<entry><literal>text_in_unicode</literal> represented in ISO 8859-1 encoding</entry>
1114
<literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
1115
<parameter>type</parameter> <type>text</type>)</literal>
1117
<entry><type>bytea</type></entry>
1119
Decode binary data from <parameter>string</parameter> previously
1120
encoded with <function>encode</>. Parameter type is same as in <function>encode</>.
1122
<entry><literal>decode('MTIzAAE=', 'base64')</literal></entry>
1123
<entry><literal>123\000\001</literal></entry>
1128
<literal><function>encode</function>(<parameter>data</parameter> <type>bytea</type>,
1129
<parameter>type</parameter> <type>text</type>)</literal>
1131
<entry><type>text</type></entry>
1133
Encode binary data to <acronym>ASCII</acronym>-only representation. Supported
1134
types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
1136
<entry><literal>encode( '123\\000\\001', 'base64')</literal></entry>
1137
<entry><literal>MTIzAAE=</literal></entry>
1141
<entry><literal><function>initcap</function>(<type>text</type>)</literal></entry>
1142
<entry><type>text</type></entry>
1144
Convert the first letter of each word to uppercase and the
1145
rest to lowercase. Words are sequences of alphanumeric
1146
characters separated by non-alphanumeric characters.
1148
<entry><literal>initcap('hi THOMAS')</literal></entry>
1149
<entry><literal>Hi Thomas</literal></entry>
1153
<entry><literal><function>length</function>(<parameter>string</parameter> <type>text</type>)</literal></entry>
1154
<entry><type>integer</type></entry>
1156
Number of characters in <parameter>string</parameter>.
1158
<primary>character string</primary>
1159
<secondary>length</secondary>
1162
<primary>length</primary>
1163
<secondary sortas="character string">of a character string</secondary>
1164
<see>character strings, length</see>
1167
<entry><literal>length('jose')</literal></entry>
1168
<entry><literal>4</literal></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>
1177
<entry><type>text</type></entry>
1179
Fill up the <parameter>string</parameter> to length
1180
<parameter>length</parameter> by prepending the characters
1181
<parameter>fill</parameter> (a space by default). If the
1182
<parameter>string</parameter> is already longer than
1183
<parameter>length</parameter> then it is truncated (on the
1186
<entry><literal>lpad('hi', 5, 'xy')</literal></entry>
1187
<entry><literal>xyxhi</literal></entry>
1191
<entry><literal><function>ltrim</function>(<parameter>string</parameter> <type>text</type>
1192
<optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal>
1194
<entry><type>text</type></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>.
1200
<entry><literal>ltrim('zzzytrim', 'xyz')</literal></entry>
1201
<entry><literal>trim</literal></entry>
1205
<entry><literal><function>md5</function>(<parameter>string</parameter> <type>text</type>)</literal></entry>
1206
<entry><type>text</type></entry>
1208
Calculates the MD5 hash of <parameter>string</parameter>,
1209
returning the result in hexadecimal.
1211
<entry><literal>md5('abc')</literal></entry>
1212
<entry><literal>900150983cd24fb0 d6963f7d28e17f72</literal></entry>
1216
<entry><literal><function>pg_client_encoding</function>()</literal></entry>
1217
<entry><type>name</type></entry>
1219
Current client encoding name
1221
<entry><literal>pg_client_encoding()</literal></entry>
1222
<entry><literal>SQL_ASCII</literal></entry>
1226
<entry><literal><function>quote_ident</function>(<parameter>string</parameter> text)</literal><indexterm><primary>quote_ident</></></entry>
1227
<entry><type>text</type></entry>
1229
Return the given string suitably quoted to be used as an identifier
1230
in an <acronym>SQL</acronym> statement string.
1231
Quotes are added only if necessary (i.e., if the string contains
1232
non-identifier characters or would be case-folded).
1233
Embedded quotes are properly doubled.
1235
<entry><literal>quote_ident('Foo bar')</literal></entry>
1236
<entry><literal>"Foo bar"</literal></entry>
1240
<entry><literal><function>quote_literal</function>(<parameter>string</parameter> text)</literal><indexterm><primary>quote_literal</></></entry>
1241
<entry><type>text</type></entry>
1243
Return the given string suitably quoted to be used as a string literal
1244
in an <acronym>SQL</acronym> statement string.
1245
Embedded quotes and backslashes are properly doubled.
1247
<entry><literal>quote_literal( 'O\'Reilly')</literal></entry>
1248
<entry><literal>'O''Reilly'</literal></entry>
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
<entry>Repeat <parameter>string</parameter> the specified
1255
<parameter>number</parameter> of times</entry>
1256
<entry><literal>repeat('Pg', 4)</literal></entry>
1257
<entry><literal>PgPgPgPg</literal></entry>
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
<entry><type>text</type></entry>
1265
<entry>Replace all occurrences in <parameter>string</parameter> of substring
1266
<parameter>from</parameter> with substring <parameter>to</parameter>.
1268
<entry><literal>replace( 'abcdefabcdef', 'cd', 'XX')</literal></entry>
1269
<entry><literal>abXXefabXXef</literal></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>
1278
<entry><type>text</type></entry>
1280
Fill up the <parameter>string</parameter> to length
1281
<parameter>length</parameter> by appending the characters
1282
<parameter>fill</parameter> (a space by default). If the
1283
<parameter>string</parameter> is already longer than
1284
<parameter>length</parameter> then it is truncated.
1286
<entry><literal>rpad('hi', 5, 'xy')</literal></entry>
1287
<entry><literal>hixyx</literal></entry>
1291
<entry><literal><function>rtrim</function>(<parameter>string</parameter> <type>text</type>
1292
<optional>, <parameter>characters</parameter> <type>text</type></optional>)</literal>
1294
<entry><type>text</type></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>.
1300
<entry><literal>rtrim('trimxxxx', 'x')</literal></entry>
1301
<entry><literal>trim</literal></entry>
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
<entry><type>text</type></entry>
1309
<entry>Split <parameter>string</parameter> on <parameter>delimiter</parameter>
1310
and return the given field (counting from one)
1312
<entry><literal>split_part( 'abc~@~def~@~ghi', '~@~', 2)</literal></entry>
1313
<entry><literal>def</literal></entry>
1317
<entry><literal><function>strpos</function>(<parameter>string</parameter>, <parameter>substring</parameter>)</literal></entry>
1318
<entry><type>text</type></entry>
1320
Location of specified substring (same as
1321
<literal>position(<parameter>substring</parameter> in
1322
<parameter>string</parameter>)</literal>, but note the reversed
1325
<entry><literal>strpos('high', 'ig')</literal></entry>
1326
<entry><literal>2</literal></entry>
1330
<entry><literal><function>substr</function>(<parameter>string</parameter>, <parameter>from</parameter> <optional>, <parameter>count</parameter></optional>)</literal></entry>
1331
<entry><type>text</type></entry>
1333
Extract substring (same as
1334
<literal>substring(<parameter>string</parameter> from <parameter>from</parameter> for <parameter>count</parameter>)</literal>)
1336
<entry><literal>substr('alphabet', 3, 2)</literal></entry>
1337
<entry><literal>ph</literal></entry>
1341
<entry><literal><function>to_ascii</function>(<type>text</type>
1342
<optional>, <parameter>encoding</parameter></optional>)</literal></entry>
1343
<entry><type>text</type></entry>
1346
Convert <parameter>text</parameter> to <acronym>ASCII</acronym> from another encoding
1349
The <function>to_ascii</function> function supports conversion from
1350
<literal>LATIN1</>, <literal>LATIN2</>, <literal>LATIN9</>,
1351
and <literal>WIN1250</> encodings only.
1356
<entry><literal>to_ascii('Karel')</literal></entry>
1357
<entry><literal>Karel</literal></entry>
1361
<entry><literal><function>to_hex</function>(<parameter>number</parameter> <type>integer</type>
1362
or <type>bigint</type>)</literal></entry>
1363
<entry><type>text</type></entry>
1364
<entry>Convert <parameter>number</parameter> to its equivalent hexadecimal
1367
<entry><literal>to_hex(2147483647)</literal></entry>
1368
<entry><literal>7fffffff</literal></entry>
1373
<literal><function>translate</function>(<parameter>string</parameter>
1375
<parameter>from</parameter> <type>text</type>,
1376
<parameter>to</parameter> <type>text</type>)</literal>
1378
<entry><type>text</type></entry>
1380
Any character in <parameter>string</parameter> that matches a
1381
character in the <parameter>from</parameter> set is replaced by
1382
the corresponding character in the <parameter>to</parameter>
1385
<entry><literal>translate('12345', '14', 'ax')</literal></entry>
1386
<entry><literal>a23x5</literal></entry>
1394
<table id="conversion-names">
1395
<title>Built-in Conversions</title>
1399
<entry>Conversion Name
1402
The conversion names follow a standard naming scheme: The
1403
official name of the source encoding with all
1404
non-alphanumeric characters replaced by underscores followed
1405
by <literal>_to_</literal> followed by the equally processed
1406
destination encoding name. Therefore the names might deviate
1407
from the customary encoding names.
1411
<entry>Source Encoding</entry>
1412
<entry>Destination Encoding</entry>
1418
<entry><literal>ascii_to_mic</literal></entry>
1419
<entry><literal>SQL_ASCII</literal></entry>
1420
<entry><literal>MULE_INTERNAL</literal></entry>
1424
<entry><literal>ascii_to_utf_8</literal></entry>
1425
<entry><literal>SQL_ASCII</literal></entry>
1426
<entry><literal>UNICODE</literal></entry>
1430
<entry><literal>big5_to_euc_tw</literal></entry>
1431
<entry><literal>BIG5</literal></entry>
1432
<entry><literal>EUC_TW</literal></entry>
1436
<entry><literal>big5_to_mic</literal></entry>
1437
<entry><literal>BIG5</literal></entry>
1438
<entry><literal>MULE_INTERNAL</literal></entry>
1442
<entry><literal>big5_to_utf_8</literal></entry>
1443
<entry><literal>BIG5</literal></entry>
1444
<entry><literal>UNICODE</literal></entry>
1448
<entry><literal>euc_cn_to_mic</literal></entry>
1449
<entry><literal>EUC_CN</literal></entry>
1450
<entry><literal>MULE_INTERNAL</literal></entry>
1454
<entry><literal>euc_cn_to_utf_8</literal></entry>
1455
<entry><literal>EUC_CN</literal></entry>
1456
<entry><literal>UNICODE</literal></entry>
1460
<entry><literal>euc_jp_to_mic</literal></entry>
1461
<entry><literal>EUC_JP</literal></entry>
1462
<entry><literal>MULE_INTERNAL</literal></entry>
1466
<entry><literal>euc_jp_to_sjis</literal></entry>
1467
<entry><literal>EUC_JP</literal></entry>
1468
<entry><literal>SJIS</literal></entry>
1472
<entry><literal>euc_jp_to_utf_8</literal></entry>
1473
<entry><literal>EUC_JP</literal></entry>
1474
<entry><literal>UNICODE</literal></entry>
1478
<entry><literal>euc_kr_to_mic</literal></entry>
1479
<entry><literal>EUC_KR</literal></entry>
1480
<entry><literal>MULE_INTERNAL</literal></entry>
1484
<entry><literal>euc_kr_to_utf_8</literal></entry>
1485
<entry><literal>EUC_KR</literal></entry>
1486
<entry><literal>UNICODE</literal></entry>
1490
<entry><literal>euc_tw_to_big5</literal></entry>
1491
<entry><literal>EUC_TW</literal></entry>
1492
<entry><literal>BIG5</literal></entry>
1496
<entry><literal>euc_tw_to_mic</literal></entry>
1497
<entry><literal>EUC_TW</literal></entry>
1498
<entry><literal>MULE_INTERNAL</literal></entry>
1502
<entry><literal>euc_tw_to_utf_8</literal></entry>
1503
<entry><literal>EUC_TW</literal></entry>
1504
<entry><literal>UNICODE</literal></entry>
1508
<entry><literal>gb18030_to_utf_8</literal></entry>
1509
<entry><literal>GB18030</literal></entry>
1510
<entry><literal>UNICODE</literal></entry>
1514
<entry><literal>gbk_to_utf_8</literal></entry>
1515
<entry><literal>GBK</literal></entry>
1516
<entry><literal>UNICODE</literal></entry>
1520
<entry><literal>iso_8859_10_to_utf_8</literal></entry>
1521
<entry><literal>LATIN6</literal></entry>
1522
<entry><literal>UNICODE</literal></entry>
1526
<entry><literal>iso_8859_13_to_utf_8</literal></entry>
1527
<entry><literal>LATIN7</literal></entry>
1528
<entry><literal>UNICODE</literal></entry>
1532
<entry><literal>iso_8859_14_to_utf_8</literal></entry>
1533
<entry><literal>LATIN8</literal></entry>
1534
<entry><literal>UNICODE</literal></entry>
1538
<entry><literal>iso_8859_15_to_utf_8</literal></entry>
1539
<entry><literal>LATIN9</literal></entry>
1540
<entry><literal>UNICODE</literal></entry>
1544
<entry><literal>iso_8859_16_to_utf_8</literal></entry>
1545
<entry><literal>LATIN10</literal></entry>
1546
<entry><literal>UNICODE</literal></entry>
1550
<entry><literal>iso_8859_1_to_mic</literal></entry>
1551
<entry><literal>LATIN1</literal></entry>
1552
<entry><literal>MULE_INTERNAL</literal></entry>
1556
<entry><literal>iso_8859_1_to_utf_8</literal></entry>
1557
<entry><literal>LATIN1</literal></entry>
1558
<entry><literal>UNICODE</literal></entry>
1562
<entry><literal>iso_8859_2_to_mic</literal></entry>
1563
<entry><literal>LATIN2</literal></entry>
1564
<entry><literal>MULE_INTERNAL</literal></entry>
1568
<entry><literal>iso_8859_2_to_utf_8</literal></entry>
1569
<entry><literal>LATIN2</literal></entry>
1570
<entry><literal>UNICODE</literal></entry>
1574
<entry><literal>iso_8859_2_to_windows_1250</literal></entry>
1575
<entry><literal>LATIN2</literal></entry>
1576
<entry><literal>WIN1250</literal></entry>
1580
<entry><literal>iso_8859_3_to_mic</literal></entry>
1581
<entry><literal>LATIN3</literal></entry>
1582
<entry><literal>MULE_INTERNAL</literal></entry>
1586
<entry><literal>iso_8859_3_to_utf_8</literal></entry>
1587
<entry><literal>LATIN3</literal></entry>
1588
<entry><literal>UNICODE</literal></entry>
1592
<entry><literal>iso_8859_4_to_mic</literal></entry>
1593
<entry><literal>LATIN4</literal></entry>
1594
<entry><literal>MULE_INTERNAL</literal></entry>
1598
<entry><literal>iso_8859_4_to_utf_8</literal></entry>
1599
<entry><literal>LATIN4</literal></entry>
1600
<entry><literal>UNICODE</literal></entry>
1604
<entry><literal>iso_8859_5_to_koi8_r</literal></entry>
1605
<entry><literal>ISO_8859_5</literal></entry>
1606
<entry><literal>KOI8</literal></entry>
1610
<entry><literal>iso_8859_5_to_mic</literal></entry>
1611
<entry><literal>ISO_8859_5</literal></entry>
1612
<entry><literal>MULE_INTERNAL</literal></entry>
1616
<entry><literal>iso_8859_5_to_utf_8</literal></entry>
1617
<entry><literal>ISO_8859_5</literal></entry>
1618
<entry><literal>UNICODE</literal></entry>
1622
<entry><literal>iso_8859_5_to_windows_1251</literal></entry>
1623
<entry><literal>ISO_8859_5</literal></entry>
1624
<entry><literal>WIN</literal></entry>
1628
<entry><literal>iso_8859_5_to_windows_866</literal></entry>
1629
<entry><literal>ISO_8859_5</literal></entry>
1630
<entry><literal>ALT</literal></entry>
1634
<entry><literal>iso_8859_6_to_utf_8</literal></entry>
1635
<entry><literal>ISO_8859_6</literal></entry>
1636
<entry><literal>UNICODE</literal></entry>
1640
<entry><literal>iso_8859_7_to_utf_8</literal></entry>
1641
<entry><literal>ISO_8859_7</literal></entry>
1642
<entry><literal>UNICODE</literal></entry>
1646
<entry><literal>iso_8859_8_to_utf_8</literal></entry>
1647
<entry><literal>ISO_8859_8</literal></entry>
1648
<entry><literal>UNICODE</literal></entry>
1652
<entry><literal>iso_8859_9_to_utf_8</literal></entry>
1653
<entry><literal>LATIN5</literal></entry>
1654
<entry><literal>UNICODE</literal></entry>
1658
<entry><literal>johab_to_utf_8</literal></entry>
1659
<entry><literal>JOHAB</literal></entry>
1660
<entry><literal>UNICODE</literal></entry>
1664
<entry><literal>koi8_r_to_iso_8859_5</literal></entry>
1665
<entry><literal>KOI8</literal></entry>
1666
<entry><literal>ISO_8859_5</literal></entry>
1670
<entry><literal>koi8_r_to_mic</literal></entry>
1671
<entry><literal>KOI8</literal></entry>
1672
<entry><literal>MULE_INTERNAL</literal></entry>
1676
<entry><literal>koi8_r_to_utf_8</literal></entry>
1677
<entry><literal>KOI8</literal></entry>
1678
<entry><literal>UNICODE</literal></entry>
1682
<entry><literal>koi8_r_to_windows_1251</literal></entry>
1683
<entry><literal>KOI8</literal></entry>
1684
<entry><literal>WIN</literal></entry>
1688
<entry><literal>koi8_r_to_windows_866</literal></entry>
1689
<entry><literal>KOI8</literal></entry>
1690
<entry><literal>ALT</literal></entry>
1694
<entry><literal>mic_to_ascii</literal></entry>
1695
<entry><literal>MULE_INTERNAL</literal></entry>
1696
<entry><literal>SQL_ASCII</literal></entry>
1700
<entry><literal>mic_to_big5</literal></entry>
1701
<entry><literal>MULE_INTERNAL</literal></entry>
1702
<entry><literal>BIG5</literal></entry>
1706
<entry><literal>mic_to_euc_cn</literal></entry>
1707
<entry><literal>MULE_INTERNAL</literal></entry>
1708
<entry><literal>EUC_CN</literal></entry>
1712
<entry><literal>mic_to_euc_jp</literal></entry>
1713
<entry><literal>MULE_INTERNAL</literal></entry>
1714
<entry><literal>EUC_JP</literal></entry>
1718
<entry><literal>mic_to_euc_kr</literal></entry>
1719
<entry><literal>MULE_INTERNAL</literal></entry>
1720
<entry><literal>EUC_KR</literal></entry>
1724
<entry><literal>mic_to_euc_tw</literal></entry>
1725
<entry><literal>MULE_INTERNAL</literal></entry>
1726
<entry><literal>EUC_TW</literal></entry>
1730
<entry><literal>mic_to_iso_8859_1</literal></entry>
1731
<entry><literal>MULE_INTERNAL</literal></entry>
1732
<entry><literal>LATIN1</literal></entry>
1736
<entry><literal>mic_to_iso_8859_2</literal></entry>
1737
<entry><literal>MULE_INTERNAL</literal></entry>
1738
<entry><literal>LATIN2</literal></entry>
1742
<entry><literal>mic_to_iso_8859_3</literal></entry>
1743
<entry><literal>MULE_INTERNAL</literal></entry>
1744
<entry><literal>LATIN3</literal></entry>
1748
<entry><literal>mic_to_iso_8859_4</literal></entry>
1749
<entry><literal>MULE_INTERNAL</literal></entry>
1750
<entry><literal>LATIN4</literal></entry>
1754
<entry><literal>mic_to_iso_8859_5</literal></entry>
1755
<entry><literal>MULE_INTERNAL</literal></entry>
1756
<entry><literal>ISO_8859_5</literal></entry>
1760
<entry><literal>mic_to_koi8_r</literal></entry>
1761
<entry><literal>MULE_INTERNAL</literal></entry>
1762
<entry><literal>KOI8</literal></entry>
1766
<entry><literal>mic_to_sjis</literal></entry>
1767
<entry><literal>MULE_INTERNAL</literal></entry>
1768
<entry><literal>SJIS</literal></entry>
1772
<entry><literal>mic_to_windows_1250</literal></entry>
1773
<entry><literal>MULE_INTERNAL</literal></entry>
1774
<entry><literal>WIN1250</literal></entry>
1778
<entry><literal>mic_to_windows_1251</literal></entry>
1779
<entry><literal>MULE_INTERNAL</literal></entry>
1780
<entry><literal>WIN</literal></entry>
1784
<entry><literal>mic_to_windows_866</literal></entry>
1785
<entry><literal>MULE_INTERNAL</literal></entry>
1786
<entry><literal>ALT</literal></entry>
1790
<entry><literal>sjis_to_euc_jp</literal></entry>
1791
<entry><literal>SJIS</literal></entry>
1792
<entry><literal>EUC_JP</literal></entry>
1796
<entry><literal>sjis_to_mic</literal></entry>
1797
<entry><literal>SJIS</literal></entry>
1798
<entry><literal>MULE_INTERNAL</literal></entry>
1802
<entry><literal>sjis_to_utf_8</literal></entry>
1803
<entry><literal>SJIS</literal></entry>
1804
<entry><literal>UNICODE</literal></entry>
1808
<entry><literal>tcvn_to_utf_8</literal></entry>
1809
<entry><literal>TCVN</literal></entry>
1810
<entry><literal>UNICODE</literal></entry>
1814
<entry><literal>uhc_to_utf_8</literal></entry>
1815
<entry><literal>UHC</literal></entry>
1816
<entry><literal>UNICODE</literal></entry>
1820
<entry><literal>utf_8_to_ascii</literal></entry>
1821
<entry><literal>UNICODE</literal></entry>
1822
<entry><literal>SQL_ASCII</literal></entry>
1826
<entry><literal>utf_8_to_big5</literal></entry>
1827
<entry><literal>UNICODE</literal></entry>
1828
<entry><literal>BIG5</literal></entry>
1832
<entry><literal>utf_8_to_euc_cn</literal></entry>
1833
<entry><literal>UNICODE</literal></entry>
1834
<entry><literal>EUC_CN</literal></entry>
1838
<entry><literal>utf_8_to_euc_jp</literal></entry>
1839
<entry><literal>UNICODE</literal></entry>
1840
<entry><literal>EUC_JP</literal></entry>
1844
<entry><literal>utf_8_to_euc_kr</literal></entry>
1845
<entry><literal>UNICODE</literal></entry>
1846
<entry><literal>EUC_KR</literal></entry>
1850
<entry><literal>utf_8_to_euc_tw</literal></entry>
1851
<entry><literal>UNICODE</literal></entry>
1852
<entry><literal>EUC_TW</literal></entry>
1856
<entry><literal>utf_8_to_gb18030</literal></entry>
1857
<entry><literal>UNICODE</literal></entry>
1858
<entry><literal>GB18030</literal></entry>
1862
<entry><literal>utf_8_to_gbk</literal></entry>
1863
<entry><literal>UNICODE</literal></entry>
1864
<entry><literal>GBK</literal></entry>
1868
<entry><literal>utf_8_to_iso_8859_1</literal></entry>
1869
<entry><literal>UNICODE</literal></entry>
1870
<entry><literal>LATIN1</literal></entry>
1874
<entry><literal>utf_8_to_iso_8859_10</literal></entry>
1875
<entry><literal>UNICODE</literal></entry>
1876
<entry><literal>LATIN6</literal></entry>
1880
<entry><literal>utf_8_to_iso_8859_13</literal></entry>
1881
<entry><literal>UNICODE</literal></entry>
1882
<entry><literal>LATIN7</literal></entry>
1886
<entry><literal>utf_8_to_iso_8859_14</literal></entry>
1887
<entry><literal>UNICODE</literal></entry>
1888
<entry><literal>LATIN8</literal></entry>
1892
<entry><literal>utf_8_to_iso_8859_15</literal></entry>
1893
<entry><literal>UNICODE</literal></entry>
1894
<entry><literal>LATIN9</literal></entry>
1898
<entry><literal>utf_8_to_iso_8859_16</literal></entry>
1899
<entry><literal>UNICODE</literal></entry>
1900
<entry><literal>LATIN10</literal></entry>
1904
<entry><literal>utf_8_to_iso_8859_2</literal></entry>
1905
<entry><literal>UNICODE</literal></entry>
1906
<entry><literal>LATIN2</literal></entry>
1910
<entry><literal>utf_8_to_iso_8859_3</literal></entry>
1911
<entry><literal>UNICODE</literal></entry>
1912
<entry><literal>LATIN3</literal></entry>
1916
<entry><literal>utf_8_to_iso_8859_4</literal></entry>
1917
<entry><literal>UNICODE</literal></entry>
1918
<entry><literal>LATIN4</literal></entry>
1922
<entry><literal>utf_8_to_iso_8859_5</literal></entry>
1923
<entry><literal>UNICODE</literal></entry>
1924
<entry><literal>ISO_8859_5</literal></entry>
1928
<entry><literal>utf_8_to_iso_8859_6</literal></entry>
1929
<entry><literal>UNICODE</literal></entry>
1930
<entry><literal>ISO_8859_6</literal></entry>
1934
<entry><literal>utf_8_to_iso_8859_7</literal></entry>
1935
<entry><literal>UNICODE</literal></entry>
1936
<entry><literal>ISO_8859_7</literal></entry>
1940
<entry><literal>utf_8_to_iso_8859_8</literal></entry>
1941
<entry><literal>UNICODE</literal></entry>
1942
<entry><literal>ISO_8859_8</literal></entry>
1946
<entry><literal>utf_8_to_iso_8859_9</literal></entry>
1947
<entry><literal>UNICODE</literal></entry>
1948
<entry><literal>LATIN5</literal></entry>
1952
<entry><literal>utf_8_to_johab</literal></entry>
1953
<entry><literal>UNICODE</literal></entry>
1954
<entry><literal>JOHAB</literal></entry>
1958
<entry><literal>utf_8_to_koi8_r</literal></entry>
1959
<entry><literal>UNICODE</literal></entry>
1960
<entry><literal>KOI8</literal></entry>
1964
<entry><literal>utf_8_to_sjis</literal></entry>
1965
<entry><literal>UNICODE</literal></entry>
1966
<entry><literal>SJIS</literal></entry>
1970
<entry><literal>utf_8_to_tcvn</literal></entry>
1971
<entry><literal>UNICODE</literal></entry>
1972
<entry><literal>TCVN</literal></entry>
1976
<entry><literal>utf_8_to_uhc</literal></entry>
1977
<entry><literal>UNICODE</literal></entry>
1978
<entry><literal>UHC</literal></entry>
1982
<entry><literal>utf_8_to_windows_1250</literal></entry>
1983
<entry><literal>UNICODE</literal></entry>
1984
<entry><literal>WIN1250</literal></entry>
1988
<entry><literal>utf_8_to_windows_1251</literal></entry>
1989
<entry><literal>UNICODE</literal></entry>
1990
<entry><literal>WIN</literal></entry>
1994
<entry><literal>utf_8_to_windows_1256</literal></entry>
1995
<entry><literal>UNICODE</literal></entry>
1996
<entry><literal>WIN1256</literal></entry>
2000
<entry><literal>utf_8_to_windows_866</literal></entry>
2001
<entry><literal>UNICODE</literal></entry>
2002
<entry><literal>ALT</literal></entry>
2006
<entry><literal>utf_8_to_windows_874</literal></entry>
2007
<entry><literal>UNICODE</literal></entry>
2008
<entry><literal>WIN874</literal></entry>
2012
<entry><literal>windows_1250_to_iso_8859_2</literal></entry>
2013
<entry><literal>WIN1250</literal></entry>
2014
<entry><literal>LATIN2</literal></entry>
2018
<entry><literal>windows_1250_to_mic</literal></entry>
2019
<entry><literal>WIN1250</literal></entry>
2020
<entry><literal>MULE_INTERNAL</literal></entry>
2024
<entry><literal>windows_1250_to_utf_8</literal></entry>
2025
<entry><literal>WIN1250</literal></entry>
2026
<entry><literal>UNICODE</literal></entry>
2030
<entry><literal>windows_1251_to_iso_8859_5</literal></entry>
2031
<entry><literal>WIN</literal></entry>
2032
<entry><literal>ISO_8859_5</literal></entry>
2036
<entry><literal>windows_1251_to_koi8_r</literal></entry>
2037
<entry><literal>WIN</literal></entry>
2038
<entry><literal>KOI8</literal></entry>
2042
<entry><literal>windows_1251_to_mic</literal></entry>
2043
<entry><literal>WIN</literal></entry>
2044
<entry><literal>MULE_INTERNAL</literal></entry>
2048
<entry><literal>windows_1251_to_utf_8</literal></entry>
2049
<entry><literal>WIN</literal></entry>
2050
<entry><literal>UNICODE</literal></entry>
2054
<entry><literal>windows_1251_to_windows_866</literal></entry>
2055
<entry><literal>WIN</literal></entry>
2056
<entry><literal>ALT</literal></entry>
2060
<entry><literal>windows_1256_to_utf_8</literal></entry>
2061
<entry><literal>WIN1256</literal></entry>
2062
<entry><literal>UNICODE</literal></entry>
2066
<entry><literal>windows_866_to_iso_8859_5</literal></entry>
2067
<entry><literal>ALT</literal></entry>
2068
<entry><literal>ISO_8859_5</literal></entry>
2072
<entry><literal>windows_866_to_koi8_r</literal></entry>
2073
<entry><literal>ALT</literal></entry>
2074
<entry><literal>KOI8</literal></entry>
2078
<entry><literal>windows_866_to_mic</literal></entry>
2079
<entry><literal>ALT</literal></entry>
2080
<entry><literal>MULE_INTERNAL</literal></entry>
2084
<entry><literal>windows_866_to_utf_8</literal></entry>
2085
<entry><literal>ALT</literal></entry>
2086
<entry><literal>UNICODE</literal></entry>
2090
<entry><literal>windows_866_to_windows_1251</literal></entry>
2091
<entry><literal>ALT</literal></entry>
2092
<entry><literal>WIN</literal></entry>
2096
<entry><literal>windows_874_to_utf_8</literal></entry>
2097
<entry><literal>WIN874</literal></entry>
2098
<entry><literal>UNICODE</literal></entry>
2108
<sect1 id="functions-binarystring">
2109
<title>Binary String Functions and Operators</title>
2111
<indexterm zone="functions-binarystring">
2112
<primary>binary data</primary>
2113
<secondary>functions</secondary>
2117
This section describes functions and operators for examining and
2118
manipulating values of type <type>bytea</type>.
2122
<acronym>SQL</acronym> defines some string functions with a
2123
special syntax where
2124
certain key words rather than commas are used to separate the
2125
arguments. Details are in
2126
<xref linkend="functions-binarystring-sql">.
2127
Some functions are also implemented using the regular syntax for
2128
function invocation.
2129
(See <xref linkend="functions-binarystring-other">.)
2132
<table id="functions-binarystring-sql">
2133
<title><acronym>SQL</acronym> Binary String Functions and Operators</title>
2137
<entry>Function</entry>
2138
<entry>Return Type</entry>
2139
<entry>Description</entry>
2140
<entry>Example</entry>
2141
<entry>Result</entry>
2147
<entry><literal><parameter>string</parameter> <literal>||</literal>
2148
<parameter>string</parameter></literal></entry>
2149
<entry> <type>bytea</type> </entry>
2151
String concatenation
2153
<primary>binary string</primary>
2154
<secondary>concatenation</secondary>
2157
<entry><literal>'\\\\Post'::bytea || '\\047gres\\000'::bytea</literal></entry>
2158
<entry><literal>\\Post'gres\000</literal></entry>
2162
<entry><literal><function>octet_length</function>(<parameter>string</parameter>)</literal></entry>
2163
<entry><type>integer</type></entry>
2164
<entry>Number of bytes in binary string</entry>
2165
<entry><literal>octet_length( 'jo\\000se'::bytea)</literal></entry>
2166
<entry><literal>5</literal></entry>
2170
<entry><literal><function>position</function>(<parameter>substring</parameter> in <parameter>string</parameter>)</literal></entry>
2171
<entry><type>integer</type></entry>
2172
<entry>Location of specified substring</entry>
2173
<entry><literal>position('\\000om'::bytea in 'Th\\000omas'::bytea)</literal></entry>
2174
<entry><literal>3</literal></entry>
2178
<entry><literal><function>substring</function>(<parameter>string</parameter> <optional>from <type>integer</type></optional> <optional>for <type>integer</type></optional>)</literal></entry>
2179
<entry><type>bytea</type></entry>
2183
<primary>substring</primary>
2186
<entry><literal>substring('Th\\000omas'::bytea from 2 for 3)</literal></entry>
2187
<entry><literal>h\000o</literal></entry>
2192
<literal><function>trim</function>(<optional>both</optional>
2193
<parameter>bytes</parameter> from
2194
<parameter>string</parameter>)</literal>
2196
<entry><type>bytea</type></entry>
2198
Remove the longest string containing only the bytes in
2199
<parameter>bytes</parameter> from the start
2200
and end of <parameter>string</parameter>
2202
<entry><literal>trim('\\000'::bytea from '\\000Tom\\000'::bytea)</literal></entry>
2203
<entry><literal>Tom</literal></entry>
2207
<entry><function>get_byte</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
2208
<entry><type>integer</type></entry>
2210
Extract byte from string.
2212
<primary>get_byte</primary>
2215
<entry><literal>get_byte('Th\\000omas'::bytea, 4)</literal></entry>
2216
<entry><literal>109</literal></entry>
2220
<entry><function>set_byte</function>(<parameter>string</parameter>,
2221
<parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2222
<entry><type>bytea</type></entry>
2226
<primary>set_byte</primary>
2229
<entry><literal>set_byte('Th\\000omas'::bytea, 4, 64)</literal></entry>
2230
<entry><literal>Th\000o@as</literal></entry>
2234
<entry><function>get_bit</function>(<parameter>string</parameter>, <parameter>offset</parameter>)</entry>
2235
<entry><type>integer</type></entry>
2237
Extract bit from string.
2239
<primary>get_bit</primary>
2242
<entry><literal>get_bit('Th\\000omas'::bytea, 45)</literal></entry>
2243
<entry><literal>1</literal></entry>
2247
<entry><function>set_bit</function>(<parameter>string</parameter>,
2248
<parameter>offset</parameter>, <parameter>newvalue</>)</entry>
2249
<entry><type>bytea</type></entry>
2253
<primary>set_bit</primary>
2256
<entry><literal>set_bit('Th\\000omas'::bytea, 45, 0)</literal></entry>
2257
<entry><literal>Th\000omAs</literal></entry>
2264
Additional binary string manipulation functions are available and
2265
are listed in <xref linkend="functions-binarystring-other">. Some
2266
of them are used internally to implement the
2267
<acronym>SQL</acronym>-standard string functions listed in <xref
2268
linkend="functions-binarystring-sql">.
2271
<table id="functions-binarystring-other">
2272
<title>Other Binary String Functions</title>
2276
<entry>Function</entry>
2277
<entry>Return Type</entry>
2278
<entry>Description</entry>
2279
<entry>Example</entry>
2280
<entry>Result</entry>
2286
<entry><literal><function>btrim</function>(<parameter>string</parameter>
2287
<type>bytea</type>, <parameter>bytes</parameter> <type>bytea</type>)</literal></entry>
2288
<entry><type>bytea</type></entry>
2290
Remove the longest string consisting only of bytes
2291
in <parameter>bytes</parameter> from the start and end of
2292
<parameter>string</parameter>.
2294
<entry><literal>btrim('\\000trim\\000'::bytea, '\\000'::bytea)</literal></entry>
2295
<entry><literal>trim</literal></entry>
2299
<entry><literal><function>length</function>(<parameter>string</parameter>)</literal></entry>
2300
<entry><type>integer</type></entry>
2302
Length of binary string
2304
<primary>binary string</primary>
2305
<secondary>length</secondary>
2308
<primary>length</primary>
2309
<secondary sortas="binary string">of a binary string</secondary>
2310
<see>binary strings, length</see>
2313
<entry><literal>length('jo\\000se'::bytea)</literal></entry>
2314
<entry><literal>5</literal></entry>
2319
<literal><function>decode</function>(<parameter>string</parameter> <type>text</type>,
2320
<parameter>type</parameter> <type>text</type>)</literal>
2322
<entry><type>bytea</type></entry>
2324
Decode binary string from <parameter>string</parameter> previously
2325
encoded with <literal>encode</>. Parameter type is same as in <literal>encode</>.
2327
<entry><literal>decode('123\\000456', 'escape')</literal></entry>
2328
<entry><literal>123\000456</literal></entry>
2333
<literal><function>encode</function>(<parameter>string</parameter> <type>bytea</type>,
2334
<parameter>type</parameter> <type>text</type>)</literal>
2336
<entry><type>text</type></entry>
2338
Encode binary string to <acronym>ASCII</acronym>-only representation. Supported
2339
types are: <literal>base64</>, <literal>hex</>, <literal>escape</>.
2341
<entry><literal>encode('123\\000456'::bytea, 'escape')</literal></entry>
2342
<entry><literal>123\000456</literal></entry>
2352
<sect1 id="functions-bitstring">
2353
<title>Bit String Functions and Operators</title>
2355
<indexterm zone="functions-bitstring">
2356
<primary>bit strings</primary>
2357
<secondary>functions</secondary>
2361
This section describes functions and operators for examining and
2362
manipulating bit strings, that is values of the types
2363
<type>bit</type> and <type>bit varying</type>. Aside from the
2364
usual comparison operators, the operators
2365
shown in <xref linkend="functions-bit-string-op-table"> can be used.
2366
Bit string operands of <literal>&</literal>, <literal>|</literal>,
2367
and <literal>#</literal> must be of equal length. When bit
2368
shifting, the original length of the string is preserved, as shown
2372
<table id="functions-bit-string-op-table">
2373
<title>Bit String Operators</title>
2378
<entry>Operator</entry>
2379
<entry>Description</entry>
2380
<entry>Example</entry>
2381
<entry>Result</entry>
2387
<entry> <literal>||</literal> </entry>
2388
<entry>concatenation</entry>
2389
<entry><literal>B'10001' || B'011'</literal></entry>
2390
<entry><literal>10001011</literal></entry>
2394
<entry> <literal>&</literal> </entry>
2395
<entry>bitwise AND</entry>
2396
<entry><literal>B'10001' & B'01101'</literal></entry>
2397
<entry><literal>00001</literal></entry>
2401
<entry> <literal>|</literal> </entry>
2402
<entry>bitwise OR</entry>
2403
<entry><literal>B'10001' | B'01101'</literal></entry>
2404
<entry><literal>11101</literal></entry>
2408
<entry> <literal>#</literal> </entry>
2409
<entry>bitwise XOR</entry>
2410
<entry><literal>B'10001' # B'01101'</literal></entry>
2411
<entry><literal>11100</literal></entry>
2415
<entry> <literal>~</literal> </entry>
2416
<entry>bitwise NOT</entry>
2417
<entry><literal>~ B'10001'</literal></entry>
2418
<entry><literal>01110</literal></entry>
2422
<entry> <literal><<</literal> </entry>
2423
<entry>bitwise shift left</entry>
2424
<entry><literal>B'10001' << 3</literal></entry>
2425
<entry><literal>01000</literal></entry>
2429
<entry> <literal>>></literal> </entry>
2430
<entry>bitwise shift right</entry>
2431
<entry><literal>B'10001' >> 2</literal></entry>
2432
<entry><literal>00100</literal></entry>
2439
The following <acronym>SQL</acronym>-standard functions work on bit
2440
strings as well as character strings:
2441
<literal><function>length</function></literal>,
2442
<literal><function>bit_length</function></literal>,
2443
<literal><function>octet_length</function></literal>,
2444
<literal><function>position</function></literal>,
2445
<literal><function>substring</function></literal>.
2449
In addition, it is possible to cast integral values to and from type
2453
44::bit(10) <lineannotation>0000101100</lineannotation>
2454
44::bit(3) <lineannotation>100</lineannotation>
2455
cast(-44 as bit(12)) <lineannotation>111111010100</lineannotation>
2456
'1110'::bit(4)::integer <lineannotation>14</lineannotation>
2458
Note that casting to just <quote>bit</> means casting to
2459
<literal>bit(1)</>, and so it will deliver only the least significant
2465
Prior to <productname>PostgreSQL</productname> 8.0, casting an
2466
integer to <type>bit(n)</> would copy the leftmost <literal>n</>
2467
bits of the integer, whereas now it copies the rightmost <literal>n</>
2468
bits. Also, casting an integer to a bit string width wider than
2469
the integer itself will sign-extend on the left.
2476
<sect1 id="functions-matching">
2477
<title>Pattern Matching</title>
2479
<indexterm zone="functions-matching">
2480
<primary>pattern matching</primary>
2484
There are three separate approaches to pattern matching provided
2485
by <productname>PostgreSQL</productname>: the traditional
2486
<acronym>SQL</acronym> <function>LIKE</function> operator, the
2487
more recent <function>SIMILAR TO</function> operator (added in
2488
SQL:1999), and <acronym>POSIX</acronym>-style regular expressions.
2489
Additionally, a pattern matching function,
2490
<function>substring</function>, is available, using either
2491
<function>SIMILAR TO</function>-style or POSIX-style regular
2497
If you have pattern matching needs that go beyond this,
2498
consider writing a user-defined function in Perl or Tcl.
2502
<sect2 id="functions-like">
2503
<title><function>LIKE</function></title>
2505
<indexterm zone="functions-like">
2506
<primary>LIKE</primary>
2510
<replaceable>string</replaceable> LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2511
<replaceable>string</replaceable> NOT LIKE <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2515
Every <replaceable>pattern</replaceable> defines a set of strings.
2516
The <function>LIKE</function> expression returns true if the
2517
<replaceable>string</replaceable> is contained in the set of
2518
strings represented by <replaceable>pattern</replaceable>. (As
2519
expected, the <function>NOT LIKE</function> expression returns
2520
false if <function>LIKE</function> returns true, and vice versa.
2521
An equivalent expression is
2522
<literal>NOT (<replaceable>string</replaceable> LIKE
2523
<replaceable>pattern</replaceable>)</literal>.)
2527
If <replaceable>pattern</replaceable> does not contain percent
2528
signs or underscore, then the pattern only represents the string
2529
itself; in that case <function>LIKE</function> acts like the
2530
equals operator. An underscore (<literal>_</literal>) in
2531
<replaceable>pattern</replaceable> stands for (matches) any single
2532
character; a percent sign (<literal>%</literal>) matches any string
2533
of zero or more characters.
2539
'abc' LIKE 'abc' <lineannotation>true</lineannotation>
2540
'abc' LIKE 'a%' <lineannotation>true</lineannotation>
2541
'abc' LIKE '_b_' <lineannotation>true</lineannotation>
2542
'abc' LIKE 'c' <lineannotation>false</lineannotation>
2547
<function>LIKE</function> pattern matches always cover the entire
2548
string. To match a sequence anywhere within a string, the
2549
pattern must therefore start and end with a percent sign.
2553
To match a literal underscore or percent sign without matching
2554
other characters, the respective character in
2555
<replaceable>pattern</replaceable> must be
2556
preceded by the escape character. The default escape
2557
character is the backslash but a different one may be selected by
2558
using the <literal>ESCAPE</literal> clause. To match the escape
2559
character itself, write two escape characters.
2563
Note that the backslash already has a special meaning in string
2564
literals, so to write a pattern constant that contains a backslash
2565
you must write two backslashes in an SQL statement. Thus, writing a pattern
2566
that actually matches a literal backslash means writing four backslashes
2567
in the statement. You can avoid this by selecting a different escape
2568
character with <literal>ESCAPE</literal>; then a backslash is not special
2569
to <function>LIKE</function> anymore. (But it is still special to the string
2570
literal parser, so you still need two of them.)
2574
It's also possible to select no escape character by writing
2575
<literal>ESCAPE ''</literal>. This effectively disables the
2576
escape mechanism, which makes it impossible to turn off the
2577
special meaning of underscore and percent signs in the pattern.
2581
The key word <token>ILIKE</token> can be used instead of
2582
<token>LIKE</token> to make the match case-insensitive according
2583
to the active locale. This is not in the <acronym>SQL</acronym> standard but is a
2584
<productname>PostgreSQL</productname> extension.
2588
The operator <literal>~~</literal> is equivalent to
2589
<function>LIKE</function>, and <literal>~~*</literal> corresponds to
2590
<function>ILIKE</function>. There are also
2591
<literal>!~~</literal> and <literal>!~~*</literal> operators that
2592
represent <function>NOT LIKE</function> and <function>NOT
2593
ILIKE</function>, respectively. All of these operators are
2594
<productname>PostgreSQL</productname>-specific.
2599
<sect2 id="functions-similarto-regexp">
2600
<title><function>SIMILAR TO</function> Regular Expressions</title>
2602
<indexterm zone="functions-similarto-regexp">
2603
<primary>regular expression</primary>
2604
<!-- <seealso>pattern matching</seealso> breaks index build -->
2608
<primary>SIMILAR TO</primary>
2612
<primary>substring</primary>
2616
<replaceable>string</replaceable> SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2617
<replaceable>string</replaceable> NOT SIMILAR TO <replaceable>pattern</replaceable> <optional>ESCAPE <replaceable>escape-character</replaceable></optional>
2621
The <function>SIMILAR TO</function> operator returns true or
2622
false depending on whether its pattern matches the given string.
2623
It is much like <function>LIKE</function>, except that it
2624
interprets the pattern using the SQL standard's definition of a
2625
regular expression. SQL regular expressions are a curious cross
2626
between <function>LIKE</function> notation and common regular
2627
expression notation.
2631
Like <function>LIKE</function>, the <function>SIMILAR TO</function>
2632
operator succeeds only if its pattern matches the entire string;
2633
this is unlike common regular expression practice, wherein the pattern
2634
may match any part of the string.
2636
<function>LIKE</function>, <function>SIMILAR TO</function> uses
2637
<literal>_</> and <literal>%</> as wildcard characters denoting
2638
any single character and any string, respectively (these are
2639
comparable to <literal>.</> and <literal>.*</> in POSIX regular
2644
In addition to these facilities borrowed from <function>LIKE</function>,
2645
<function>SIMILAR TO</function> supports these pattern-matching
2646
metacharacters borrowed from POSIX regular expressions:
2651
<literal>|</literal> denotes alternation (either of two alternatives).
2656
<literal>*</literal> denotes repetition of the previous item zero
2662
<literal>+</literal> denotes repetition of the previous item one
2668
Parentheses <literal>()</literal> may be used to group items into
2669
a single logical item.
2674
A bracket expression <literal>[...]</literal> specifies a character
2675
class, just as in POSIX regular expressions.
2680
Notice that bounded repetition (<literal>?</> and <literal>{...}</>)
2681
are not provided, though they exist in POSIX. Also, the dot (<literal>.</>)
2682
is not a metacharacter.
2686
As with <function>LIKE</>, a backslash disables the special meaning
2687
of any of these metacharacters; or a different escape character can
2688
be specified with <literal>ESCAPE</>.
2694
'abc' SIMILAR TO 'abc' <lineannotation>true</lineannotation>
2695
'abc' SIMILAR TO 'a' <lineannotation>false</lineannotation>
2696
'abc' SIMILAR TO '%(b|d)%' <lineannotation>true</lineannotation>
2697
'abc' SIMILAR TO '(b|c)%' <lineannotation>false</lineannotation>
2702
The <function>substring</> function with three parameters,
2703
<function>substring(<parameter>string</parameter> from
2704
<replaceable>pattern</replaceable> for
2705
<replaceable>escape-character</replaceable>)</function>, provides
2706
extraction of a substring that matches an SQL
2707
regular expression pattern. As with <literal>SIMILAR TO</>, the
2708
specified pattern must match to the entire data string, else the
2709
function fails and returns null. To indicate the part of the
2710
pattern that should be returned on success, the pattern must contain
2711
two occurrences of the escape character followed by a double quote
2712
(<literal>"</>). The text matching the portion of the pattern
2713
between these markers is returned.
2719
substring('foobar' from '%#"o_b#"%' for '#') <lineannotation>oob</lineannotation>
2720
substring('foobar' from '#"o_b#"%' for '#') <lineannotation>NULL</lineannotation>
2725
<sect2 id="functions-posix-regexp">
2726
<title><acronym>POSIX</acronym> Regular Expressions</title>
2728
<indexterm zone="functions-posix-regexp">
2729
<primary>regular expression</primary>
2730
<seealso>pattern matching</seealso>
2734
<xref linkend="functions-posix-table"> lists the available
2735
operators for pattern matching using POSIX regular expressions.
2738
<table id="functions-posix-table">
2739
<title>Regular Expression Match Operators</title>
2744
<entry>Operator</entry>
2745
<entry>Description</entry>
2746
<entry>Example</entry>
2752
<entry> <literal>~</literal> </entry>
2753
<entry>Matches regular expression, case sensitive</entry>
2754
<entry><literal>'thomas' ~ '.*thomas.*'</literal></entry>
2758
<entry> <literal>~*</literal> </entry>
2759
<entry>Matches regular expression, case insensitive</entry>
2760
<entry><literal>'thomas' ~* '.*Thomas.*'</literal></entry>
2764
<entry> <literal>!~</literal> </entry>
2765
<entry>Does not match regular expression, case sensitive</entry>
2766
<entry><literal>'thomas' !~ '.*Thomas.*'</literal></entry>
2770
<entry> <literal>!~*</literal> </entry>
2771
<entry>Does not match regular expression, case insensitive</entry>
2772
<entry><literal>'thomas' !~* '.*vadim.*'</literal></entry>
2779
<acronym>POSIX</acronym> regular expressions provide a more
2781
pattern matching than the <function>LIKE</function> and
2782
<function>SIMILAR TO</> operators.
2783
Many Unix tools such as <command>egrep</command>,
2784
<command>sed</command>, or <command>awk</command> use a pattern
2785
matching language that is similar to the one described here.
2789
A regular expression is a character sequence that is an
2790
abbreviated definition of a set of strings (a <firstterm>regular
2791
set</firstterm>). A string is said to match a regular expression
2792
if it is a member of the regular set described by the regular
2793
expression. As with <function>LIKE</function>, pattern characters
2794
match string characters exactly unless they are special characters
2795
in the regular expression language — but regular expressions use
2796
different special characters than <function>LIKE</function> does.
2797
Unlike <function>LIKE</function> patterns, a
2798
regular expression is allowed to match anywhere within a string, unless
2799
the regular expression is explicitly anchored to the beginning or
2806
'abc' ~ 'abc' <lineannotation>true</lineannotation>
2807
'abc' ~ '^a' <lineannotation>true</lineannotation>
2808
'abc' ~ '(b|d)' <lineannotation>true</lineannotation>
2809
'abc' ~ '^(b|c)' <lineannotation>false</lineannotation>
2814
The <function>substring</> function with two parameters,
2815
<function>substring(<parameter>string</parameter> from
2816
<replaceable>pattern</replaceable>)</function>, provides extraction of a substring
2817
that matches a POSIX regular expression pattern. It returns null if
2818
there is no match, otherwise the portion of the text that matched the
2819
pattern. But if the pattern contains any parentheses, the portion
2820
of the text that matched the first parenthesized subexpression (the
2821
one whose left parenthesis comes first) is
2822
returned. You can put parentheses around the whole expression
2823
if you want to use parentheses within it without triggering this
2824
exception. If you need parentheses in the pattern before the
2825
subexpression you want to extract, see the non-capturing parentheses
2832
substring('foobar' from 'o.b') <lineannotation>oob</lineannotation>
2833
substring('foobar' from 'o(.)b') <lineannotation>o</lineannotation>
2838
<productname>PostgreSQL</productname>'s regular expressions are implemented
2839
using a package written by Henry Spencer. Much of
2840
the description of regular expressions below is copied verbatim from his
2844
<!-- derived from the re_syntax.n man page -->
2846
<sect3 id="posix-syntax-details">
2847
<title>Regular Expression Details</title>
2850
Regular expressions (<acronym>RE</acronym>s), as defined in
2851
<acronym>POSIX</acronym> 1003.2, come in two forms:
2852
<firstterm>extended</> <acronym>RE</acronym>s or <acronym>ERE</>s
2853
(roughly those of <command>egrep</command>), and
2854
<firstterm>basic</> <acronym>RE</acronym>s or <acronym>BRE</>s
2855
(roughly those of <command>ed</command>).
2856
<productname>PostgreSQL</productname> supports both forms, and
2857
also implements some extensions
2858
that are not in the POSIX standard, but have become widely used anyway
2859
due to their availability in programming languages such as Perl and Tcl.
2860
<acronym>RE</acronym>s using these non-POSIX extensions are called
2861
<firstterm>advanced</> <acronym>RE</acronym>s or <acronym>ARE</>s
2862
in this documentation. AREs are almost an exact superset of EREs,
2863
but BREs have several notational incompatibilities (as well as being
2865
We first describe the ARE and ERE forms, noting features that apply
2866
only to AREs, and then describe how BREs differ.
2871
The form of regular expressions accepted by
2872
<productname>PostgreSQL</> can be chosen by setting the <xref
2873
linkend="guc-regex-flavor"> run-time parameter. The usual
2874
setting is <literal>advanced</>, but one might choose
2875
<literal>extended</> for maximum backwards compatibility with
2876
pre-7.4 releases of <productname>PostgreSQL</>.
2881
A regular expression is defined as one or more
2882
<firstterm>branches</firstterm>, separated by
2883
<literal>|</literal>. It matches anything that matches one of the
2888
A branch is zero or more <firstterm>quantified atoms</> or
2889
<firstterm>constraints</>, concatenated.
2890
It matches a match for the first, followed by a match for the second, etc;
2891
an empty branch matches the empty string.
2895
A quantified atom is an <firstterm>atom</> possibly followed
2896
by a single <firstterm>quantifier</>.
2897
Without a quantifier, it matches a match for the atom.
2898
With a quantifier, it can match some number of matches of the atom.
2899
An <firstterm>atom</firstterm> can be any of the possibilities
2900
shown in <xref linkend="posix-atoms-table">.
2901
The possible quantifiers and their meanings are shown in
2902
<xref linkend="posix-quantifiers-table">.
2906
A <firstterm>constraint</> matches an empty string, but matches only when
2907
specific conditions are met. A constraint can be used where an atom
2908
could be used, except it may not be followed by a quantifier.
2909
The simple constraints are shown in
2910
<xref linkend="posix-constraints-table">;
2911
some more constraints are described later.
2915
<table id="posix-atoms-table">
2916
<title>Regular Expression Atoms</title>
2922
<entry>Description</entry>
2928
<entry> <literal>(</><replaceable>re</><literal>)</> </entry>
2929
<entry> (where <replaceable>re</> is any regular expression)
2931
<replaceable>re</>, with the match noted for possible reporting </entry>
2935
<entry> <literal>(?:</><replaceable>re</><literal>)</> </entry>
2936
<entry> as above, but the match is not noted for reporting
2937
(a <quote>non-capturing</> set of parentheses)
2938
(AREs only) </entry>
2942
<entry> <literal>.</> </entry>
2943
<entry> matches any single character </entry>
2947
<entry> <literal>[</><replaceable>chars</><literal>]</> </entry>
2948
<entry> a <firstterm>bracket expression</>,
2949
matching any one of the <replaceable>chars</> (see
2950
<xref linkend="posix-bracket-expressions"> for more detail) </entry>
2954
<entry> <literal>\</><replaceable>k</> </entry>
2955
<entry> (where <replaceable>k</> is a non-alphanumeric character)
2956
matches that character taken as an ordinary character,
2957
e.g. <literal>\\</> matches a backslash character </entry>
2961
<entry> <literal>\</><replaceable>c</> </entry>
2962
<entry> where <replaceable>c</> is alphanumeric
2963
(possibly followed by other characters)
2964
is an <firstterm>escape</>, see <xref linkend="posix-escape-sequences">
2965
(AREs only; in EREs and BREs, this matches <replaceable>c</>) </entry>
2969
<entry> <literal>{</> </entry>
2970
<entry> when followed by a character other than a digit,
2971
matches the left-brace character <literal>{</>;
2972
when followed by a digit, it is the beginning of a
2973
<replaceable>bound</> (see below) </entry>
2977
<entry> <replaceable>x</> </entry>
2978
<entry> where <replaceable>x</> is a single character with no other
2979
significance, matches that character </entry>
2986
An RE may not end with <literal>\</>.
2991
Remember that the backslash (<literal>\</literal>) already has a special
2992
meaning in <productname>PostgreSQL</> string literals.
2993
To write a pattern constant that contains a backslash,
2994
you must write two backslashes in the statement.
2998
<table id="posix-quantifiers-table">
2999
<title>Regular Expression Quantifiers</title>
3004
<entry>Quantifier</entry>
3005
<entry>Matches</entry>
3011
<entry> <literal>*</> </entry>
3012
<entry> a sequence of 0 or more matches of the atom </entry>
3016
<entry> <literal>+</> </entry>
3017
<entry> a sequence of 1 or more matches of the atom </entry>
3021
<entry> <literal>?</> </entry>
3022
<entry> a sequence of 0 or 1 matches of the atom </entry>
3026
<entry> <literal>{</><replaceable>m</><literal>}</> </entry>
3027
<entry> a sequence of exactly <replaceable>m</> matches of the atom </entry>
3031
<entry> <literal>{</><replaceable>m</><literal>,}</> </entry>
3032
<entry> a sequence of <replaceable>m</> or more matches of the atom </entry>
3037
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</> </entry>
3038
<entry> a sequence of <replaceable>m</> through <replaceable>n</>
3039
(inclusive) matches of the atom; <replaceable>m</> may not exceed
3040
<replaceable>n</> </entry>
3044
<entry> <literal>*?</> </entry>
3045
<entry> non-greedy version of <literal>*</> </entry>
3049
<entry> <literal>+?</> </entry>
3050
<entry> non-greedy version of <literal>+</> </entry>
3054
<entry> <literal>??</> </entry>
3055
<entry> non-greedy version of <literal>?</> </entry>
3059
<entry> <literal>{</><replaceable>m</><literal>}?</> </entry>
3060
<entry> non-greedy version of <literal>{</><replaceable>m</><literal>}</> </entry>
3064
<entry> <literal>{</><replaceable>m</><literal>,}?</> </entry>
3065
<entry> non-greedy version of <literal>{</><replaceable>m</><literal>,}</> </entry>
3070
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</> </entry>
3071
<entry> non-greedy version of <literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</> </entry>
3078
The forms using <literal>{</><replaceable>...</><literal>}</>
3079
are known as <firstterm>bounds</>.
3080
The numbers <replaceable>m</> and <replaceable>n</> within a bound are
3081
unsigned decimal integers with permissible values from 0 to 255 inclusive.
3085
<firstterm>Non-greedy</> quantifiers (available in AREs only) match the
3086
same possibilities as their corresponding normal (<firstterm>greedy</>)
3087
counterparts, but prefer the smallest number rather than the largest
3089
See <xref linkend="posix-matching-rules"> for more detail.
3094
A quantifier cannot immediately follow another quantifier.
3096
begin an expression or subexpression or follow
3097
<literal>^</literal> or <literal>|</literal>.
3101
<table id="posix-constraints-table">
3102
<title>Regular Expression Constraints</title>
3107
<entry>Constraint</entry>
3108
<entry>Description</entry>
3114
<entry> <literal>^</> </entry>
3115
<entry> matches at the beginning of the string </entry>
3119
<entry> <literal>$</> </entry>
3120
<entry> matches at the end of the string </entry>
3124
<entry> <literal>(?=</><replaceable>re</><literal>)</> </entry>
3125
<entry> <firstterm>positive lookahead</> matches at any point
3126
where a substring matching <replaceable>re</> begins
3127
(AREs only) </entry>
3131
<entry> <literal>(?!</><replaceable>re</><literal>)</> </entry>
3132
<entry> <firstterm>negative lookahead</> matches at any point
3133
where no substring matching <replaceable>re</> begins
3134
(AREs only) </entry>
3141
Lookahead constraints may not contain <firstterm>back references</>
3142
(see <xref linkend="posix-escape-sequences">),
3143
and all parentheses within them are considered non-capturing.
3147
<sect3 id="posix-bracket-expressions">
3148
<title>Bracket Expressions</title>
3151
A <firstterm>bracket expression</firstterm> is a list of
3152
characters enclosed in <literal>[]</literal>. It normally matches
3153
any single character from the list (but see below). If the list
3154
begins with <literal>^</literal>, it matches any single character
3155
<emphasis>not</> from the rest of the list.
3157
in the list are separated by <literal>-</literal>, this is
3158
shorthand for the full range of characters between those two
3159
(inclusive) in the collating sequence,
3160
e.g. <literal>[0-9]</literal> in <acronym>ASCII</acronym> matches
3161
any decimal digit. It is illegal for two ranges to share an
3162
endpoint, e.g. <literal>a-c-e</literal>. Ranges are very
3163
collating-sequence-dependent, so portable programs should avoid
3168
To include a literal <literal>]</literal> in the list, make it the
3169
first character (following a possible <literal>^</literal>). To
3170
include a literal <literal>-</literal>, make it the first or last
3171
character, or the second endpoint of a range. To use a literal
3172
<literal>-</literal> as the first endpoint of a range, enclose it
3173
in <literal>[.</literal> and <literal>.]</literal> to make it a
3174
collating element (see below). With the exception of these characters,
3175
some combinations using <literal>[</literal>
3176
(see next paragraphs), and escapes (AREs only), all other special
3177
characters lose their special significance within a bracket expression.
3178
In particular, <literal>\</literal> is not special when following
3179
ERE or BRE rules, though it is special (as introducing an escape)
3184
Within a bracket expression, a collating element (a character, a
3185
multiple-character sequence that collates as if it were a single
3186
character, or a collating-sequence name for either) enclosed in
3187
<literal>[.</literal> and <literal>.]</literal> stands for the
3188
sequence of characters of that collating element. The sequence is
3189
a single element of the bracket expression's list. A bracket
3190
expression containing a multiple-character collating element can thus
3191
match more than one character, e.g. if the collating sequence
3192
includes a <literal>ch</literal> collating element, then the RE
3193
<literal>[[.ch.]]*c</literal> matches the first five characters of
3194
<literal>chchcc</literal>.
3199
<productname>PostgreSQL</> currently has no multi-character collating
3200
elements. This information describes possible future behavior.
3205
Within a bracket expression, a collating element enclosed in
3206
<literal>[=</literal> and <literal>=]</literal> is an equivalence
3207
class, standing for the sequences of characters of all collating
3208
elements equivalent to that one, including itself. (If there are
3209
no other equivalent collating elements, the treatment is as if the
3210
enclosing delimiters were <literal>[.</literal> and
3211
<literal>.]</literal>.) For example, if <literal>o</literal> and
3212
<literal>^</literal> are the members of an equivalence class, then
3213
<literal>[[=o=]]</literal>, <literal>[[=^=]]</literal>, and
3214
<literal>[o^]</literal> are all synonymous. An equivalence class
3215
may not be an endpoint of a range.
3219
Within a bracket expression, the name of a character class
3220
enclosed in <literal>[:</literal> and <literal>:]</literal> stands
3221
for the list of all characters belonging to that class. Standard
3222
character class names are: <literal>alnum</literal>,
3223
<literal>alpha</literal>, <literal>blank</literal>,
3224
<literal>cntrl</literal>, <literal>digit</literal>,
3225
<literal>graph</literal>, <literal>lower</literal>,
3226
<literal>print</literal>, <literal>punct</literal>,
3227
<literal>space</literal>, <literal>upper</literal>,
3228
<literal>xdigit</literal>. These stand for the character classes
3230
<citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>.
3231
A locale may provide others. A character class may not be used as
3232
an endpoint of a range.
3236
There are two special cases of bracket expressions: the bracket
3237
expressions <literal>[[:<:]]</literal> and
3238
<literal>[[:>:]]</literal> are constraints,
3239
matching empty strings at the beginning
3240
and end of a word respectively. A word is defined as a sequence
3241
of word characters that is neither preceded nor followed by word
3242
characters. A word character is an <literal>alnum</> character (as
3244
<citerefentry><refentrytitle>ctype</refentrytitle><manvolnum>3</manvolnum></citerefentry>)
3245
or an underscore. This is an extension, compatible with but not
3246
specified by <acronym>POSIX</acronym> 1003.2, and should be used with
3247
caution in software intended to be portable to other systems.
3248
The constraint escapes described below are usually preferable (they
3249
are no more standard, but are certainly easier to type).
3253
<sect3 id="posix-escape-sequences">
3254
<title>Regular Expression Escapes</title>
3257
<firstterm>Escapes</> are special sequences beginning with <literal>\</>
3258
followed by an alphanumeric character. Escapes come in several varieties:
3259
character entry, class shorthands, constraint escapes, and back references.
3260
A <literal>\</> followed by an alphanumeric character but not constituting
3261
a valid escape is illegal in AREs.
3262
In EREs, there are no escapes: outside a bracket expression,
3263
a <literal>\</> followed by an alphanumeric character merely stands for
3264
that character as an ordinary character, and inside a bracket expression,
3265
<literal>\</> is an ordinary character.
3266
(The latter is the one actual incompatibility between EREs and AREs.)
3270
<firstterm>Character-entry escapes</> exist to make it easier to specify
3271
non-printing and otherwise inconvenient characters in REs. They are
3272
shown in <xref linkend="posix-character-entry-escapes-table">.
3276
<firstterm>Class-shorthand escapes</> provide shorthands for certain
3277
commonly-used character classes. They are
3278
shown in <xref linkend="posix-class-shorthand-escapes-table">.
3282
A <firstterm>constraint escape</> is a constraint,
3283
matching the empty string if specific conditions are met,
3284
written as an escape. They are
3285
shown in <xref linkend="posix-constraint-escapes-table">.
3289
A <firstterm>back reference</> (<literal>\</><replaceable>n</>) matches the
3290
same string matched by the previous parenthesized subexpression specified
3291
by the number <replaceable>n</>
3292
(see <xref linkend="posix-constraint-backref-table">). For example,
3293
<literal>([bc])\1</> matches <literal>bb</> or <literal>cc</>
3294
but not <literal>bc</> or <literal>cb</>.
3295
The subexpression must entirely precede the back reference in the RE.
3296
Subexpressions are numbered in the order of their leading parentheses.
3297
Non-capturing parentheses do not define subexpressions.
3302
Keep in mind that an escape's leading <literal>\</> will need to be
3303
doubled when entering the pattern as an SQL string constant. For example:
3305
'123' ~ '^\\d{3}' <lineannotation>true</lineannotation>
3310
<table id="posix-character-entry-escapes-table">
3311
<title>Regular Expression Character-Entry Escapes</title>
3316
<entry>Escape</entry>
3317
<entry>Description</entry>
3323
<entry> <literal>\a</> </entry>
3324
<entry> alert (bell) character, as in C </entry>
3328
<entry> <literal>\b</> </entry>
3329
<entry> backspace, as in C </entry>
3333
<entry> <literal>\B</> </entry>
3334
<entry> synonym for <literal>\</> to help reduce the need for backslash
3339
<entry> <literal>\c</><replaceable>X</> </entry>
3340
<entry> (where <replaceable>X</> is any character) the character whose
3341
low-order 5 bits are the same as those of
3342
<replaceable>X</>, and whose other bits are all zero </entry>
3346
<entry> <literal>\e</> </entry>
3347
<entry> the character whose collating-sequence name
3349
or failing that, the character with octal value 033 </entry>
3353
<entry> <literal>\f</> </entry>
3354
<entry> form feed, as in C </entry>
3358
<entry> <literal>\n</> </entry>
3359
<entry> newline, as in C </entry>
3363
<entry> <literal>\r</> </entry>
3364
<entry> carriage return, as in C </entry>
3368
<entry> <literal>\t</> </entry>
3369
<entry> horizontal tab, as in C </entry>
3373
<entry> <literal>\u</><replaceable>wxyz</> </entry>
3374
<entry> (where <replaceable>wxyz</> is exactly four hexadecimal digits)
3375
the Unicode character <literal>U+</><replaceable>wxyz</>
3376
in the local byte ordering </entry>
3380
<entry> <literal>\U</><replaceable>stuvwxyz</> </entry>
3381
<entry> (where <replaceable>stuvwxyz</> is exactly eight hexadecimal
3383
reserved for a somewhat-hypothetical Unicode extension to 32 bits
3388
<entry> <literal>\v</> </entry>
3389
<entry> vertical tab, as in C </entry>
3393
<entry> <literal>\x</><replaceable>hhh</> </entry>
3394
<entry> (where <replaceable>hhh</> is any sequence of hexadecimal
3396
the character whose hexadecimal value is
3397
<literal>0x</><replaceable>hhh</>
3398
(a single character no matter how many hexadecimal digits are used)
3403
<entry> <literal>\0</> </entry>
3404
<entry> the character whose value is <literal>0</> </entry>
3408
<entry> <literal>\</><replaceable>xy</> </entry>
3409
<entry> (where <replaceable>xy</> is exactly two octal digits,
3410
and is not a <firstterm>back reference</>)
3411
the character whose octal value is
3412
<literal>0</><replaceable>xy</> </entry>
3416
<entry> <literal>\</><replaceable>xyz</> </entry>
3417
<entry> (where <replaceable>xyz</> is exactly three octal digits,
3418
and is not a <firstterm>back reference</>)
3419
the character whose octal value is
3420
<literal>0</><replaceable>xyz</> </entry>
3427
Hexadecimal digits are <literal>0</>-<literal>9</>,
3428
<literal>a</>-<literal>f</>, and <literal>A</>-<literal>F</>.
3429
Octal digits are <literal>0</>-<literal>7</>.
3433
The character-entry escapes are always taken as ordinary characters.
3434
For example, <literal>\135</> is <literal>]</> in ASCII, but
3435
<literal>\135</> does not terminate a bracket expression.
3438
<table id="posix-class-shorthand-escapes-table">
3439
<title>Regular Expression Class-Shorthand Escapes</title>
3444
<entry>Escape</entry>
3445
<entry>Description</entry>
3451
<entry> <literal>\d</> </entry>
3452
<entry> <literal>[[:digit:]]</> </entry>
3456
<entry> <literal>\s</> </entry>
3457
<entry> <literal>[[:space:]]</> </entry>
3461
<entry> <literal>\w</> </entry>
3462
<entry> <literal>[[:alnum:]_]</>
3463
(note underscore is included) </entry>
3467
<entry> <literal>\D</> </entry>
3468
<entry> <literal>[^[:digit:]]</> </entry>
3472
<entry> <literal>\S</> </entry>
3473
<entry> <literal>[^[:space:]]</> </entry>
3477
<entry> <literal>\W</> </entry>
3478
<entry> <literal>[^[:alnum:]_]</>
3479
(note underscore is included) </entry>
3486
Within bracket expressions, <literal>\d</>, <literal>\s</>,
3487
and <literal>\w</> lose their outer brackets,
3488
and <literal>\D</>, <literal>\S</>, and <literal>\W</> are illegal.
3489
(So, for example, <literal>[a-c\d]</> is equivalent to
3490
<literal>[a-c[:digit:]]</>.
3491
Also, <literal>[a-c\D]</>, which is equivalent to
3492
<literal>[a-c^[:digit:]]</>, is illegal.)
3495
<table id="posix-constraint-escapes-table">
3496
<title>Regular Expression Constraint Escapes</title>
3501
<entry>Escape</entry>
3502
<entry>Description</entry>
3508
<entry> <literal>\A</> </entry>
3509
<entry> matches only at the beginning of the string
3510
(see <xref linkend="posix-matching-rules"> for how this differs from
3511
<literal>^</>) </entry>
3515
<entry> <literal>\m</> </entry>
3516
<entry> matches only at the beginning of a word </entry>
3520
<entry> <literal>\M</> </entry>
3521
<entry> matches only at the end of a word </entry>
3525
<entry> <literal>\y</> </entry>
3526
<entry> matches only at the beginning or end of a word </entry>
3530
<entry> <literal>\Y</> </entry>
3531
<entry> matches only at a point that is not the beginning or end of a
3536
<entry> <literal>\Z</> </entry>
3537
<entry> matches only at the end of the string
3538
(see <xref linkend="posix-matching-rules"> for how this differs from
3539
<literal>$</>) </entry>
3546
A word is defined as in the specification of
3547
<literal>[[:<:]]</> and <literal>[[:>:]]</> above.
3548
Constraint escapes are illegal within bracket expressions.
3551
<table id="posix-constraint-backref-table">
3552
<title>Regular Expression Back References</title>
3557
<entry>Escape</entry>
3558
<entry>Description</entry>
3564
<entry> <literal>\</><replaceable>m</> </entry>
3565
<entry> (where <replaceable>m</> is a nonzero digit)
3566
a back reference to the <replaceable>m</>'th subexpression </entry>
3570
<entry> <literal>\</><replaceable>mnn</> </entry>
3571
<entry> (where <replaceable>m</> is a nonzero digit, and
3572
<replaceable>nn</> is some more digits, and the decimal value
3573
<replaceable>mnn</> is not greater than the number of closing capturing
3574
parentheses seen so far)
3575
a back reference to the <replaceable>mnn</>'th subexpression </entry>
3583
There is an inherent historical ambiguity between octal character-entry
3584
escapes and back references, which is resolved by heuristics,
3586
A leading zero always indicates an octal escape.
3587
A single non-zero digit, not followed by another digit,
3588
is always taken as a back reference.
3589
A multi-digit sequence not starting with a zero is taken as a back
3590
reference if it comes after a suitable subexpression
3591
(i.e. the number is in the legal range for a back reference),
3592
and otherwise is taken as octal.
3597
<sect3 id="posix-metasyntax">
3598
<title>Regular Expression Metasyntax</title>
3601
In addition to the main syntax described above, there are some special
3602
forms and miscellaneous syntactic facilities available.
3606
Normally the flavor of RE being used is determined by
3607
<varname>regex_flavor</>.
3608
However, this can be overridden by a <firstterm>director</> prefix.
3609
If an RE begins with <literal>***:</>,
3610
the rest of the RE is taken as an ARE regardless of
3611
<varname>regex_flavor</>.
3612
If an RE begins with <literal>***=</>,
3613
the rest of the RE is taken to be a literal string,
3614
with all characters considered ordinary characters.
3618
An ARE may begin with <firstterm>embedded options</>:
3619
a sequence <literal>(?</><replaceable>xyz</><literal>)</>
3620
(where <replaceable>xyz</> is one or more alphabetic characters)
3621
specifies options affecting the rest of the RE.
3622
These options override any previously determined options (including
3623
both the RE flavor and case sensitivity).
3624
The available option letters are
3625
shown in <xref linkend="posix-embedded-options-table">.
3628
<table id="posix-embedded-options-table">
3629
<title>ARE Embedded-Option Letters</title>
3634
<entry>Option</entry>
3635
<entry>Description</entry>
3641
<entry> <literal>b</> </entry>
3642
<entry> rest of RE is a BRE </entry>
3646
<entry> <literal>c</> </entry>
3647
<entry> case-sensitive matching (overrides operator type) </entry>
3651
<entry> <literal>e</> </entry>
3652
<entry> rest of RE is an ERE </entry>
3656
<entry> <literal>i</> </entry>
3657
<entry> case-insensitive matching (see
3658
<xref linkend="posix-matching-rules">) (overrides operator type) </entry>
3662
<entry> <literal>m</> </entry>
3663
<entry> historical synonym for <literal>n</> </entry>
3667
<entry> <literal>n</> </entry>
3668
<entry> newline-sensitive matching (see
3669
<xref linkend="posix-matching-rules">) </entry>
3673
<entry> <literal>p</> </entry>
3674
<entry> partial newline-sensitive matching (see
3675
<xref linkend="posix-matching-rules">) </entry>
3679
<entry> <literal>q</> </entry>
3680
<entry> rest of RE is a literal (<quote>quoted</>) string, all ordinary
3685
<entry> <literal>s</> </entry>
3686
<entry> non-newline-sensitive matching (default) </entry>
3690
<entry> <literal>t</> </entry>
3691
<entry> tight syntax (default; see below) </entry>
3695
<entry> <literal>w</> </entry>
3696
<entry> inverse partial newline-sensitive (<quote>weird</>) matching
3697
(see <xref linkend="posix-matching-rules">) </entry>
3701
<entry> <literal>x</> </entry>
3702
<entry> expanded syntax (see below) </entry>
3709
Embedded options take effect at the <literal>)</> terminating the sequence.
3710
They may appear only at the start of an ARE (after the
3711
<literal>***:</> director if any).
3715
In addition to the usual (<firstterm>tight</>) RE syntax, in which all
3716
characters are significant, there is an <firstterm>expanded</> syntax,
3717
available by specifying the embedded <literal>x</> option.
3718
In the expanded syntax,
3719
white-space characters in the RE are ignored, as are
3720
all characters between a <literal>#</>
3721
and the following newline (or the end of the RE). This
3722
permits paragraphing and commenting a complex RE.
3723
There are three exceptions to that basic rule:
3728
a white-space character or <literal>#</> preceded by <literal>\</> is
3734
white space or <literal>#</> within a bracket expression is retained
3739
white space and comments cannot appear within multi-character symbols,
3740
such as <literal>(?:</>
3745
For this purpose, white-space characters are blank, tab, newline, and
3746
any character that belongs to the <replaceable>space</> character class.
3750
Finally, in an ARE, outside bracket expressions, the sequence
3751
<literal>(?#</><replaceable>ttt</><literal>)</>
3752
(where <replaceable>ttt</> is any text not containing a <literal>)</>)
3753
is a comment, completely ignored.
3754
Again, this is not allowed between the characters of
3755
multi-character symbols, like <literal>(?:</>.
3756
Such comments are more a historical artifact than a useful facility,
3757
and their use is deprecated; use the expanded syntax instead.
3761
<emphasis>None</> of these metasyntax extensions is available if
3762
an initial <literal>***=</> director
3763
has specified that the user's input be treated as a literal string
3764
rather than as an RE.
3768
<sect3 id="posix-matching-rules">
3769
<title>Regular Expression Matching Rules</title>
3772
In the event that an RE could match more than one substring of a given
3773
string, the RE matches the one starting earliest in the string.
3774
If the RE could match more than one substring starting at that point,
3775
either the longest possible match or the shortest possible match will
3776
be taken, depending on whether the RE is <firstterm>greedy</> or
3777
<firstterm>non-greedy</>.
3781
Whether an RE is greedy or not is determined by the following rules:
3785
Most atoms, and all constraints, have no greediness attribute (because
3786
they cannot match variable amounts of text anyway).
3791
Adding parentheses around an RE does not change its greediness.
3796
A quantified atom with a fixed-repetition quantifier
3797
(<literal>{</><replaceable>m</><literal>}</>
3799
<literal>{</><replaceable>m</><literal>}?</>)
3800
has the same greediness (possibly none) as the atom itself.
3805
A quantified atom with other normal quantifiers (including
3806
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}</>
3807
with <replaceable>m</> equal to <replaceable>n</>)
3808
is greedy (prefers longest match).
3813
A quantified atom with a non-greedy quantifier (including
3814
<literal>{</><replaceable>m</><literal>,</><replaceable>n</><literal>}?</>
3815
with <replaceable>m</> equal to <replaceable>n</>)
3816
is non-greedy (prefers shortest match).
3821
A branch — that is, an RE that has no top-level
3822
<literal>|</> operator — has the same greediness as the first
3823
quantified atom in it that has a greediness attribute.
3828
An RE consisting of two or more branches connected by the
3829
<literal>|</> operator is always greedy.
3836
The above rules associate greediness attributes not only with individual
3837
quantified atoms, but with branches and entire REs that contain quantified
3838
atoms. What that means is that the matching is done in such a way that
3839
the branch, or whole RE, matches the longest or shortest possible
3840
substring <emphasis>as a whole</>. Once the length of the entire match
3841
is determined, the part of it that matches any particular subexpression
3842
is determined on the basis of the greediness attribute of that
3843
subexpression, with subexpressions starting earlier in the RE taking
3844
priority over ones starting later.
3848
An example of what this means:
3850
SELECT SUBSTRING('XY1234Z', 'Y*([0-9]{1,3})');
3851
<lineannotation>Result: </lineannotation><computeroutput>123</computeroutput>
3852
SELECT SUBSTRING('XY1234Z', 'Y*?([0-9]{1,3})');
3853
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
3855
In the first case, the RE as a whole is greedy because <literal>Y*</>
3856
is greedy. It can match beginning at the <literal>Y</>, and it matches
3857
the longest possible string starting there, i.e., <literal>Y123</>.
3858
The output is the parenthesized part of that, or <literal>123</>.
3859
In the second case, the RE as a whole is non-greedy because <literal>Y*?</>
3860
is non-greedy. It can match beginning at the <literal>Y</>, and it matches
3861
the shortest possible string starting there, i.e., <literal>Y1</>.
3862
The subexpression <literal>[0-9]{1,3}</> is greedy but it cannot change
3863
the decision as to the overall match length; so it is forced to match
3868
In short, when an RE contains both greedy and non-greedy subexpressions,
3869
the total match length is either as long as possible or as short as
3870
possible, according to the attribute assigned to the whole RE. The
3871
attributes assigned to the subexpressions only affect how much of that
3872
match they are allowed to <quote>eat</> relative to each other.
3876
The quantifiers <literal>{1,1}</> and <literal>{1,1}?</>
3877
can be used to force greediness or non-greediness, respectively,
3878
on a subexpression or a whole RE.
3882
Match lengths are measured in characters, not collating elements.
3883
An empty string is considered longer than no match at all.
3886
matches the three middle characters of <literal>abbbc</>;
3887
<literal>(week|wee)(night|knights)</>
3888
matches all ten characters of <literal>weeknights</>;
3889
when <literal>(.*).*</>
3890
is matched against <literal>abc</> the parenthesized subexpression
3891
matches all three characters; and when
3892
<literal>(a*)*</> is matched against <literal>bc</>
3893
both the whole RE and the parenthesized
3894
subexpression match an empty string.
3898
If case-independent matching is specified,
3899
the effect is much as if all case distinctions had vanished from the
3901
When an alphabetic that exists in multiple cases appears as an
3902
ordinary character outside a bracket expression, it is effectively
3903
transformed into a bracket expression containing both cases,
3904
e.g. <literal>x</> becomes <literal>[xX]</>.
3905
When it appears inside a bracket expression, all case counterparts
3906
of it are added to the bracket expression, e.g.
3907
<literal>[x]</> becomes <literal>[xX]</>
3908
and <literal>[^x]</> becomes <literal>[^xX]</>.
3912
If newline-sensitive matching is specified, <literal>.</>
3913
and bracket expressions using <literal>^</>
3914
will never match the newline character
3915
(so that matches will never cross newlines unless the RE
3916
explicitly arranges it)
3917
and <literal>^</>and <literal>$</>
3918
will match the empty string after and before a newline
3919
respectively, in addition to matching at beginning and end of string
3921
But the ARE escapes <literal>\A</> and <literal>\Z</>
3922
continue to match beginning or end of string <emphasis>only</>.
3926
If partial newline-sensitive matching is specified,
3927
this affects <literal>.</> and bracket expressions
3928
as with newline-sensitive matching, but not <literal>^</>
3933
If inverse partial newline-sensitive matching is specified,
3934
this affects <literal>^</> and <literal>$</>
3935
as with newline-sensitive matching, but not <literal>.</>
3936
and bracket expressions.
3937
This isn't very useful but is provided for symmetry.
3941
<sect3 id="posix-limits-compatibility">
3942
<title>Limits and Compatibility</title>
3945
No particular limit is imposed on the length of REs in this
3946
implementation. However,
3947
programs intended to be highly portable should not employ REs longer
3949
as a POSIX-compliant implementation can refuse to accept such REs.
3953
The only feature of AREs that is actually incompatible with
3954
POSIX EREs is that <literal>\</> does not lose its special
3955
significance inside bracket expressions.
3956
All other ARE features use syntax which is illegal or has
3957
undefined or unspecified effects in POSIX EREs;
3958
the <literal>***</> syntax of directors likewise is outside the POSIX
3959
syntax for both BREs and EREs.
3963
Many of the ARE extensions are borrowed from Perl, but some have
3964
been changed to clean them up, and a few Perl extensions are not present.
3965
Incompatibilities of note include <literal>\b</>, <literal>\B</>,
3966
the lack of special treatment for a trailing newline,
3967
the addition of complemented bracket expressions to the things
3968
affected by newline-sensitive matching,
3969
the restrictions on parentheses and back references in lookahead
3970
constraints, and the longest/shortest-match (rather than first-match)
3975
Two significant incompatibilities exist between AREs and the ERE syntax
3976
recognized by pre-7.4 releases of <productname>PostgreSQL</>:
3981
In AREs, <literal>\</> followed by an alphanumeric character is either
3982
an escape or an error, while in previous releases, it was just another
3983
way of writing the alphanumeric.
3984
This should not be much of a problem because there was no reason to
3985
write such a sequence in earlier releases.
3990
In AREs, <literal>\</> remains a special character within
3991
<literal>[]</>, so a literal <literal>\</> within a bracket
3992
expression must be written <literal>\\</>.
3997
While these differences are unlikely to create a problem for most
3998
applications, you can avoid them if necessary by
3999
setting <varname>regex_flavor</> to <literal>extended</>.
4003
<sect3 id="posix-basic-regexes">
4004
<title>Basic Regular Expressions</title>
4007
BREs differ from EREs in several respects.
4008
<literal>|</>, <literal>+</>, and <literal>?</>
4009
are ordinary characters and there is no equivalent
4010
for their functionality.
4011
The delimiters for bounds are
4012
<literal>\{</> and <literal>\}</>,
4013
with <literal>{</> and <literal>}</>
4014
by themselves ordinary characters.
4015
The parentheses for nested subexpressions are
4016
<literal>\(</> and <literal>\)</>,
4017
with <literal>(</> and <literal>)</> by themselves ordinary characters.
4018
<literal>^</> is an ordinary character except at the beginning of the
4019
RE or the beginning of a parenthesized subexpression,
4020
<literal>$</> is an ordinary character except at the end of the
4021
RE or the end of a parenthesized subexpression,
4022
and <literal>*</> is an ordinary character if it appears at the beginning
4023
of the RE or the beginning of a parenthesized subexpression
4024
(after a possible leading <literal>^</>).
4025
Finally, single-digit back references are available, and
4026
<literal>\<</> and <literal>\></>
4028
<literal>[[:<:]]</> and <literal>[[:>:]]</>
4029
respectively; no other escapes are available.
4033
<!-- end re_syntax.n man page -->
4039
<sect1 id="functions-formatting">
4040
<title>Data Type Formatting Functions</title>
4042
<indexterm zone="functions-formatting">
4043
<primary>formatting</primary>
4046
<indexterm zone="functions-formatting">
4047
<primary>to_char</primary>
4051
The <productname>PostgreSQL</productname> formatting functions
4052
provide a powerful set of tools for converting various data types
4053
(date/time, integer, floating point, numeric) to formatted strings
4054
and for converting from formatted strings to specific data types.
4055
<xref linkend="functions-formatting-table"> lists them.
4056
These functions all follow a common calling convention: the first
4057
argument is the value to be formatted and the second argument is a
4058
template that defines the output or input format.
4061
<table id="functions-formatting-table">
4062
<title>Formatting Functions</title>
4066
<entry>Function</entry>
4067
<entry>Return Type</entry>
4068
<entry>Description</entry>
4069
<entry>Example</entry>
4074
<entry><literal><function>to_char</function>(<type>timestamp</type>, <type>text</type>)</literal></entry>
4075
<entry><type>text</type></entry>
4076
<entry>convert time stamp to string</entry>
4077
<entry><literal>to_char(current_timestamp, 'HH12:MI:SS')</literal></entry>
4080
<entry><literal><function>to_char</function>(<type>interval</type>, <type>text</type>)</literal></entry>
4081
<entry><type>text</type></entry>
4082
<entry>convert interval to string</entry>
4083
<entry><literal>to_char(interval '15h 2m 12s', 'HH24:MI:SS')</literal></entry>
4086
<entry><literal><function>to_char</function>(<type>int</type>, <type>text</type>)</literal></entry>
4087
<entry><type>text</type></entry>
4088
<entry>convert integer to string</entry>
4089
<entry><literal>to_char(125, '999')</literal></entry>
4092
<entry><literal><function>to_char</function>(<type>double precision</type>,
4093
<type>text</type>)</literal></entry>
4094
<entry><type>text</type></entry>
4095
<entry>convert real/double precision to string</entry>
4096
<entry><literal>to_char(125.8::real, '999D9')</literal></entry>
4099
<entry><literal><function>to_char</function>(<type>numeric</type>, <type>text</type>)</literal></entry>
4100
<entry><type>text</type></entry>
4101
<entry>convert numeric to string</entry>
4102
<entry><literal>to_char(-125.8, '999D99S')</literal></entry>
4105
<entry><literal><function>to_date</function>(<type>text</type>, <type>text</type>)</literal></entry>
4106
<entry><type>date</type></entry>
4107
<entry>convert string to date</entry>
4108
<entry><literal>to_date('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
4111
<entry><literal><function>to_timestamp</function>(<type>text</type>, <type>text</type>)</literal></entry>
4112
<entry><type>timestamp with time zone</type></entry>
4113
<entry>convert string to time stamp</entry>
4114
<entry><literal>to_timestamp('05 Dec 2000', 'DD Mon YYYY')</literal></entry>
4117
<entry><literal><function>to_number</function>(<type>text</type>, <type>text</type>)</literal></entry>
4118
<entry><type>numeric</type></entry>
4119
<entry>convert string to numeric</entry>
4120
<entry><literal>to_number('12,454.8-', '99G999D9S')</literal></entry>
4127
Warning: <literal><function>to_char</function>(<type>interval</type>, <type>text</type>)</literal>
4128
is deprecated and should not be used in newly-written code. It will be removed in the next version.
4132
In an output template string (for <function>to_char</>), there are certain patterns that are
4133
recognized and replaced with appropriately-formatted data from the value
4134
to be formatted. Any text that is not a template pattern is simply
4135
copied verbatim. Similarly, in an input template string (for anything but <function>to_char</>), template patterns
4136
identify the parts of the input data string to be looked at and the
4137
values to be found there.
4141
<xref linkend="functions-formatting-datetime-table"> shows the
4142
template patterns available for formatting date and time values.
4145
<table id="functions-formatting-datetime-table">
4146
<title>Template Patterns for Date/Time Formatting</title>
4150
<entry>Pattern</entry>
4151
<entry>Description</entry>
4156
<entry><literal>HH</literal></entry>
4157
<entry>hour of day (01-12)</entry>
4160
<entry><literal>HH12</literal></entry>
4161
<entry>hour of day (01-12)</entry>
4164
<entry><literal>HH24</literal></entry>
4165
<entry>hour of day (00-23)</entry>
4168
<entry><literal>MI</literal></entry>
4169
<entry>minute (00-59)</entry>
4172
<entry><literal>SS</literal></entry>
4173
<entry>second (00-59)</entry>
4176
<entry><literal>MS</literal></entry>
4177
<entry>millisecond (000-999)</entry>
4180
<entry><literal>US</literal></entry>
4181
<entry>microsecond (000000-999999)</entry>
4184
<entry><literal>SSSS</literal></entry>
4185
<entry>seconds past midnight (0-86399)</entry>
4188
<entry><literal>AM</literal> or <literal>A.M.</literal> or
4189
<literal>PM</literal> or <literal>P.M.</literal></entry>
4190
<entry>meridian indicator (uppercase)</entry>
4193
<entry><literal>am</literal> or <literal>a.m.</literal> or
4194
<literal>pm</literal> or <literal>p.m.</literal></entry>
4195
<entry>meridian indicator (lowercase)</entry>
4198
<entry><literal>Y,YYY</literal></entry>
4199
<entry>year (4 and more digits) with comma</entry>
4202
<entry><literal>YYYY</literal></entry>
4203
<entry>year (4 and more digits)</entry>
4206
<entry><literal>YYY</literal></entry>
4207
<entry>last 3 digits of year</entry>
4210
<entry><literal>YY</literal></entry>
4211
<entry>last 2 digits of year</entry>
4214
<entry><literal>Y</literal></entry>
4215
<entry>last digit of year</entry>
4218
<entry><literal>IYYY</literal></entry>
4219
<entry>ISO year (4 and more digits)</entry>
4222
<entry><literal>IYY</literal></entry>
4223
<entry>last 3 digits of ISO year</entry>
4226
<entry><literal>IY</literal></entry>
4227
<entry>last 2 digits of ISO year</entry>
4230
<entry><literal>I</literal></entry>
4231
<entry>last digits of ISO year</entry>
4234
<entry><literal>BC</literal> or <literal>B.C.</literal> or
4235
<literal>AD</literal> or <literal>A.D.</literal></entry>
4236
<entry>era indicator (uppercase)</entry>
4239
<entry><literal>bc</literal> or <literal>b.c.</literal> or
4240
<literal>ad</literal> or <literal>a.d.</literal></entry>
4241
<entry>era indicator (lowercase)</entry>
4244
<entry><literal>MONTH</literal></entry>
4245
<entry>full uppercase month name (blank-padded to 9 chars)</entry>
4248
<entry><literal>Month</literal></entry>
4249
<entry>full mixed-case month name (blank-padded to 9 chars)</entry>
4252
<entry><literal>month</literal></entry>
4253
<entry>full lowercase month name (blank-padded to 9 chars)</entry>
4256
<entry><literal>MON</literal></entry>
4257
<entry>abbreviated uppercase month name (3 chars)</entry>
4260
<entry><literal>Mon</literal></entry>
4261
<entry>abbreviated mixed-case month name (3 chars)</entry>
4264
<entry><literal>mon</literal></entry>
4265
<entry>abbreviated lowercase month name (3 chars)</entry>
4268
<entry><literal>MM</literal></entry>
4269
<entry>month number (01-12)</entry>
4272
<entry><literal>DAY</literal></entry>
4273
<entry>full uppercase day name (blank-padded to 9 chars)</entry>
4276
<entry><literal>Day</literal></entry>
4277
<entry>full mixed-case day name (blank-padded to 9 chars)</entry>
4280
<entry><literal>day</literal></entry>
4281
<entry>full lowercase day name (blank-padded to 9 chars)</entry>
4284
<entry><literal>DY</literal></entry>
4285
<entry>abbreviated uppercase day name (3 chars)</entry>
4288
<entry><literal>Dy</literal></entry>
4289
<entry>abbreviated mixed-case day name (3 chars)</entry>
4292
<entry><literal>dy</literal></entry>
4293
<entry>abbreviated lowercase day name (3 chars)</entry>
4296
<entry><literal>DDD</literal></entry>
4297
<entry>day of year (001-366)</entry>
4300
<entry><literal>DD</literal></entry>
4301
<entry>day of month (01-31)</entry>
4304
<entry><literal>D</literal></entry>
4305
<entry>day of week (1-7; Sunday is 1)</entry>
4308
<entry><literal>W</literal></entry>
4309
<entry>week of month (1-5) (The first week starts on the first day of the month.)</entry>
4312
<entry><literal>WW</literal></entry>
4313
<entry>week number of year (1-53) (The first week starts on the first day of the year.)</entry>
4316
<entry><literal>IW</literal></entry>
4317
<entry>ISO week number of year (The first Thursday of the new year is in week 1.)</entry>
4320
<entry><literal>CC</literal></entry>
4321
<entry>century (2 digits)</entry>
4324
<entry><literal>J</literal></entry>
4325
<entry>Julian Day (days since January 1, 4712 BC)</entry>
4328
<entry><literal>Q</literal></entry>
4329
<entry>quarter</entry>
4332
<entry><literal>RM</literal></entry>
4333
<entry>month in Roman numerals (I-XII; I=January) (uppercase)</entry>
4336
<entry><literal>rm</literal></entry>
4337
<entry>month in Roman numerals (i-xii; i=January) (lowercase)</entry>
4340
<entry><literal>TZ</literal></entry>
4341
<entry>time-zone name (uppercase)</entry>
4344
<entry><literal>tz</literal></entry>
4345
<entry>time-zone name (lowercase)</entry>
4352
Certain modifiers may be applied to any template pattern to alter its
4353
behavior. For example, <literal>FMMonth</literal>
4354
is the <literal>Month</literal> pattern with the
4355
<literal>FM</literal> modifier.
4356
<xref linkend="functions-formatting-datetimemod-table"> shows the
4357
modifier patterns for date/time formatting.
4360
<table id="functions-formatting-datetimemod-table">
4361
<title>Template Pattern Modifiers for Date/Time Formatting</title>
4365
<entry>Modifier</entry>
4366
<entry>Description</entry>
4367
<entry>Example</entry>
4372
<entry><literal>FM</literal> prefix</entry>
4373
<entry>fill mode (suppress padding blanks and zeroes)</entry>
4374
<entry><literal>FMMonth</literal></entry>
4377
<entry><literal>TH</literal> suffix</entry>
4378
<entry>uppercase ordinal number suffix</entry>
4379
<entry><literal>DDTH</literal></entry>
4382
<entry><literal>th</literal> suffix</entry>
4383
<entry>lowercase ordinal number suffix</entry>
4384
<entry><literal>DDth</literal></entry>
4387
<entry><literal>FX</literal> prefix</entry>
4388
<entry>fixed format global option (see usage notes)</entry>
4389
<entry><literal>FX Month DD Day</literal></entry>
4392
<entry><literal>SP</literal> suffix</entry>
4393
<entry>spell mode (not yet implemented)</entry>
4394
<entry><literal>DDSP</literal></entry>
4401
Usage notes for date/time formatting:
4406
<literal>FM</literal> suppresses leading zeroes and trailing blanks
4407
that would otherwise be added to make the output of a pattern be
4414
<function>to_timestamp</function> and <function>to_date</function>
4415
skip multiple blank spaces in the input string if the <literal>FX</literal> option
4416
is not used. <literal>FX</literal> must be specified as the first item
4417
in the template. For example
4418
<literal>to_timestamp('2000 JUN', 'YYYY MON')</literal> is correct, but
4419
<literal>to_timestamp('2000 JUN', 'FXYYYY MON')</literal> returns an error,
4420
because <function>to_timestamp</function> expects one space only.
4426
Ordinary text is allowed in <function>to_char</function>
4427
templates and will be output literally. You can put a substring
4428
in double quotes to force it to be interpreted as literal text
4429
even if it contains pattern key words. For example, in
4430
<literal>'"Hello Year "YYYY'</literal>, the <literal>YYYY</literal>
4431
will be replaced by the year data, but the single <literal>Y</literal> in <literal>Year</literal>
4438
If you want to have a double quote in the output you must
4439
precede it with a backslash, for example <literal>'\\"YYYY
4440
Month\\"'</literal>. <!-- "" font-lock sanity :-) -->
4441
(Two backslashes are necessary because the backslash already
4442
has a special meaning in a string constant.)
4448
The <literal>YYYY</literal> conversion from string to <type>timestamp</type> or
4449
<type>date</type> has a restriction if you use a year with more than 4 digits. You must
4450
use some non-digit character or template after <literal>YYYY</literal>,
4451
otherwise the year is always interpreted as 4 digits. For example
4452
(with the year 20000):
4453
<literal>to_date('200001131', 'YYYYMMDD')</literal> will be
4454
interpreted as a 4-digit year; instead use a non-digit
4455
separator after the year, like
4456
<literal>to_date('20000-1131', 'YYYY-MMDD')</literal> or
4457
<literal>to_date('20000Nov31', 'YYYYMonDD')</literal>.
4463
In conversions from string to <type>timestamp</type> or
4464
<type>date</type>, the <literal>CC</literal> field is ignored if there
4465
is a <literal>YYY</literal>, <literal>YYYY</literal> or
4466
<literal>Y,YYY</literal> field. If <literal>CC</literal> is used with
4467
<literal>YY</literal> or <literal>Y</literal> then the year is computed
4468
as <literal>(CC-1)*100+YY</literal>.
4474
Millisecond (<literal>MS</literal>) and microsecond (<literal>US</literal>)
4475
values in a conversion from string to <type>timestamp</type> are used as part of the
4476
seconds after the decimal point. For example
4477
<literal>to_timestamp('12:3', 'SS:MS')</literal> is not 3 milliseconds,
4478
but 300, because the conversion counts it as 12 + 0.3 seconds.
4479
This means for the format <literal>SS:MS</literal>, the input values
4480
<literal>12:3</literal>, <literal>12:30</literal>, and <literal>12:300</literal> specify the
4481
same number of milliseconds. To get three milliseconds, one must use
4482
<literal>12:003</literal>, which the conversion counts as
4483
12 + 0.003 = 12.003 seconds.
4489
<literal>to_timestamp('15:12:02.020.001230', 'HH:MI:SS.MS.US')</literal>
4490
is 15 hours, 12 minutes, and 2 seconds + 20 milliseconds +
4491
1230 microseconds = 2.021230 seconds.
4496
<para><function>to_char</function>'s day of the week numbering
4497
(see the 'D' formatting pattern) is different from that of the
4498
<function>extract</function> function.
4505
<xref linkend="functions-formatting-numeric-table"> shows the
4506
template patterns available for formatting numeric values.
4509
<table id="functions-formatting-numeric-table">
4510
<title>Template Patterns for Numeric Formatting</title>
4514
<entry>Pattern</entry>
4515
<entry>Description</entry>
4520
<entry><literal>9</literal></entry>
4521
<entry>value with the specified number of digits</entry>
4524
<entry><literal>0</literal></entry>
4525
<entry>value with leading zeros</entry>
4528
<entry><literal>.</literal> (period)</entry>
4529
<entry>decimal point</entry>
4532
<entry><literal>,</literal> (comma)</entry>
4533
<entry>group (thousand) separator</entry>
4536
<entry><literal>PR</literal></entry>
4537
<entry>negative value in angle brackets</entry>
4540
<entry><literal>S</literal></entry>
4541
<entry>sign anchored to number (uses locale)</entry>
4544
<entry><literal>L</literal></entry>
4545
<entry>currency symbol (uses locale)</entry>
4548
<entry><literal>D</literal></entry>
4549
<entry>decimal point (uses locale)</entry>
4552
<entry><literal>G</literal></entry>
4553
<entry>group separator (uses locale)</entry>
4556
<entry><literal>MI</literal></entry>
4557
<entry>minus sign in specified position (if number < 0)</entry>
4560
<entry><literal>PL</literal></entry>
4561
<entry>plus sign in specified position (if number > 0)</entry>
4564
<entry><literal>SG</literal></entry>
4565
<entry>plus/minus sign in specified position</entry>
4568
<entry><literal>RN</literal></entry>
4569
<entry>roman numeral (input between 1 and 3999)</entry>
4572
<entry><literal>TH</literal> or <literal>th</literal></entry>
4573
<entry>ordinal number suffix</entry>
4576
<entry><literal>V</literal></entry>
4577
<entry>shift specified number of digits (see notes)</entry>
4580
<entry><literal>EEEE</literal></entry>
4581
<entry>scientific notation (not implemented yet)</entry>
4588
Usage notes for numeric formatting:
4593
A sign formatted using <literal>SG</literal>, <literal>PL</literal>, or
4594
<literal>MI</literal> is not anchored to
4595
the number; for example,
4596
<literal>to_char(-12, 'S9999')</literal> produces <literal>' -12'</literal>,
4597
but <literal>to_char(-12, 'MI9999')</literal> produces <literal>'- 12'</literal>.
4598
The Oracle implementation does not allow the use of
4599
<literal>MI</literal> ahead of <literal>9</literal>, but rather
4600
requires that <literal>9</literal> precede
4601
<literal>MI</literal>.
4607
<literal>9</literal> results in a value with the same number of
4608
digits as there are <literal>9</literal>s. If a digit is
4609
not available it outputs a space.
4615
<literal>TH</literal> does not convert values less than zero
4616
and does not convert fractional numbers.
4622
<literal>PL</literal>, <literal>SG</literal>, and
4623
<literal>TH</literal> are <productname>PostgreSQL</productname>
4630
<literal>V</literal> effectively
4631
multiplies the input values by
4632
<literal>10^<replaceable>n</replaceable></literal>, where
4633
<replaceable>n</replaceable> is the number of digits following
4634
<literal>V</literal>.
4635
<function>to_char</function> does not support the use of
4636
<literal>V</literal> combined with a decimal point.
4637
(E.g., <literal>99.9V99</literal> is not allowed.)
4644
<xref linkend="functions-formatting-examples-table"> shows some
4645
examples of the use of the <function>to_char</function> function.
4648
<table id="functions-formatting-examples-table">
4649
<title><function>to_char</function> Examples</title>
4653
<entry>Expression</entry>
4654
<entry>Result</entry>
4659
<entry><literal>to_char(current_timestamp, 'Day, DD HH12:MI:SS')</literal></entry>
4660
<entry><literal>'Tuesday , 06 05:39:18'</literal></entry>
4663
<entry><literal>to_char(current_timestamp, 'FMDay, FMDD HH12:MI:SS')</literal></entry>
4664
<entry><literal>'Tuesday, 6 05:39:18'</literal></entry>
4667
<entry><literal>to_char(-0.1, '99.99')</literal></entry>
4668
<entry><literal>' -.10'</literal></entry>
4671
<entry><literal>to_char(-0.1, 'FM9.99')</literal></entry>
4672
<entry><literal>'-.1'</literal></entry>
4675
<entry><literal>to_char(0.1, '0.9')</literal></entry>
4676
<entry><literal>' 0.1'</literal></entry>
4679
<entry><literal>to_char(12, '9990999.9')</literal></entry>
4680
<entry><literal>' 0012.0'</literal></entry>
4683
<entry><literal>to_char(12, 'FM9990999.9')</literal></entry>
4684
<entry><literal>'0012.'</literal></entry>
4687
<entry><literal>to_char(485, '999')</literal></entry>
4688
<entry><literal>' 485'</literal></entry>
4691
<entry><literal>to_char(-485, '999')</literal></entry>
4692
<entry><literal>'-485'</literal></entry>
4695
<entry><literal>to_char(485, '9 9 9')</literal></entry>
4696
<entry><literal>' 4 8 5'</literal></entry>
4699
<entry><literal>to_char(1485, '9,999')</literal></entry>
4700
<entry><literal>' 1,485'</literal></entry>
4703
<entry><literal>to_char(1485, '9G999')</literal></entry>
4704
<entry><literal>' 1 485'</literal></entry>
4707
<entry><literal>to_char(148.5, '999.999')</literal></entry>
4708
<entry><literal>' 148.500'</literal></entry>
4711
<entry><literal>to_char(148.5, 'FM999.999')</literal></entry>
4712
<entry><literal>'148.5'</literal></entry>
4715
<entry><literal>to_char(148.5, 'FM999.990')</literal></entry>
4716
<entry><literal>'148.500'</literal></entry>
4719
<entry><literal>to_char(148.5, '999D999')</literal></entry>
4720
<entry><literal>' 148,500'</literal></entry>
4723
<entry><literal>to_char(3148.5, '9G999D999')</literal></entry>
4724
<entry><literal>' 3 148,500'</literal></entry>
4727
<entry><literal>to_char(-485, '999S')</literal></entry>
4728
<entry><literal>'485-'</literal></entry>
4731
<entry><literal>to_char(-485, '999MI')</literal></entry>
4732
<entry><literal>'485-'</literal></entry>
4735
<entry><literal>to_char(485, '999MI')</literal></entry>
4736
<entry><literal>'485 '</literal></entry>
4739
<entry><literal>to_char(485, 'FM999MI')</literal></entry>
4740
<entry><literal>'485'</literal></entry>
4743
<entry><literal>to_char(485, 'PL999')</literal></entry>
4744
<entry><literal>'+485'</literal></entry>
4747
<entry><literal>to_char(485, 'SG999')</literal></entry>
4748
<entry><literal>'+485'</literal></entry>
4751
<entry><literal>to_char(-485, 'SG999')</literal></entry>
4752
<entry><literal>'-485'</literal></entry>
4755
<entry><literal>to_char(-485, '9SG99')</literal></entry>
4756
<entry><literal>'4-85'</literal></entry>
4759
<entry><literal>to_char(-485, '999PR')</literal></entry>
4760
<entry><literal>'<485>'</literal></entry>
4763
<entry><literal>to_char(485, 'L999')</literal></entry>
4764
<entry><literal>'DM 485</literal></entry>
4767
<entry><literal>to_char(485, 'RN')</literal></entry>
4768
<entry><literal>' CDLXXXV'</literal></entry>
4771
<entry><literal>to_char(485, 'FMRN')</literal></entry>
4772
<entry><literal>'CDLXXXV'</literal></entry>
4775
<entry><literal>to_char(5.2, 'FMRN')</literal></entry>
4776
<entry><literal>'V'</literal></entry>
4779
<entry><literal>to_char(482, '999th')</literal></entry>
4780
<entry><literal>' 482nd'</literal></entry>
4783
<entry><literal>to_char(485, '"Good number:"999')</literal></entry>
4784
<entry><literal>'Good number: 485'</literal></entry>
4787
<entry><literal>to_char(485.8, '"Pre:"999" Post:" .999')</literal></entry>
4788
<entry><literal>'Pre: 485 Post: .800'</literal></entry>
4791
<entry><literal>to_char(12, '99V999')</literal></entry>
4792
<entry><literal>' 12000'</literal></entry>
4795
<entry><literal>to_char(12.4, '99V999')</literal></entry>
4796
<entry><literal>' 12400'</literal></entry>
4799
<entry><literal>to_char(12.45, '99V9')</literal></entry>
4800
<entry><literal>' 125'</literal></entry>
4809
<sect1 id="functions-datetime">
4810
<title>Date/Time Functions and Operators</title>
4813
<xref linkend="functions-datetime-table"> shows the available
4814
functions for date/time value processing, with details appearing in
4815
the following subsections. <xref
4816
linkend="operators-datetime-table"> illustrates the behaviors of
4817
the basic arithmetic operators (<literal>+</literal>,
4818
<literal>*</literal>, etc.). For formatting functions, refer to
4819
<xref linkend="functions-formatting">. You should be familiar with
4820
the background information on date/time data types from <xref
4821
linkend="datatype-datetime">.
4825
All the functions and operators described below that take <type>time</type> or <type>timestamp</type>
4826
inputs actually come in two variants: one that takes <type>time with time zone</type> or <type>timestamp
4827
with time zone</type>, and one that takes <type>time without time zone</type> or <type>timestamp without time zone</type>.
4828
For brevity, these variants are not shown separately. Also, the
4829
<literal>+</> and <literal>*</> operators come in commutative pairs (for
4830
example both date + integer and integer + date); we show only one of each
4834
<table id="operators-datetime-table">
4835
<title>Date/Time Operators</title>
4840
<entry>Operator</entry>
4841
<entry>Example</entry>
4842
<entry>Result</entry>
4848
<entry> <literal>+</literal> </entry>
4849
<entry><literal>date '2001-09-28' + integer '7'</literal></entry>
4850
<entry><literal>date '2001-10-05'</literal></entry>
4854
<entry> <literal>+</literal> </entry>
4855
<entry><literal>date '2001-09-28' + interval '1 hour'</literal></entry>
4856
<entry><literal>timestamp '2001-09-28 01:00'</literal></entry>
4860
<entry> <literal>+</literal> </entry>
4861
<entry><literal>date '2001-09-28' + time '03:00'</literal></entry>
4862
<entry><literal>timestamp '2001-09-28 03:00'</literal></entry>
4866
<entry> <literal>+</literal> </entry>
4867
<entry><literal>interval '1 day' + interval '1 hour'</literal></entry>
4868
<entry><literal>interval '1 day 01:00'</literal></entry>
4872
<entry> <literal>+</literal> </entry>
4873
<entry><literal>timestamp '2001-09-28 01:00' + interval '23 hours'</literal></entry>
4874
<entry><literal>timestamp '2001-09-29 00:00'</literal></entry>
4878
<entry> <literal>+</literal> </entry>
4879
<entry><literal>time '01:00' + interval '3 hours'</literal></entry>
4880
<entry><literal>time '04:00'</literal></entry>
4884
<entry> <literal>-</literal> </entry>
4885
<entry><literal>- interval '23 hours'</literal></entry>
4886
<entry><literal>interval '-23:00'</literal></entry>
4890
<entry> <literal>-</literal> </entry>
4891
<entry><literal>date '2001-10-01' - date '2001-09-28'</literal></entry>
4892
<entry><literal>integer '3'</literal></entry>
4896
<entry> <literal>-</literal> </entry>
4897
<entry><literal>date '2001-10-01' - integer '7'</literal></entry>
4898
<entry><literal>date '2001-09-24'</literal></entry>
4902
<entry> <literal>-</literal> </entry>
4903
<entry><literal>date '2001-09-28' - interval '1 hour'</literal></entry>
4904
<entry><literal>timestamp '2001-09-27 23:00'</literal></entry>
4908
<entry> <literal>-</literal> </entry>
4909
<entry><literal>time '05:00' - time '03:00'</literal></entry>
4910
<entry><literal>interval '02:00'</literal></entry>
4914
<entry> <literal>-</literal> </entry>
4915
<entry><literal>time '05:00' - interval '2 hours'</literal></entry>
4916
<entry><literal>time '03:00'</literal></entry>
4920
<entry> <literal>-</literal> </entry>
4921
<entry><literal>timestamp '2001-09-28 23:00' - interval '23 hours'</literal></entry>
4922
<entry><literal>timestamp '2001-09-28 00:00'</literal></entry>
4926
<entry> <literal>-</literal> </entry>
4927
<entry><literal>interval '1 day' - interval '1 hour'</literal></entry>
4928
<entry><literal>interval '23:00'</literal></entry>
4932
<entry> <literal>-</literal> </entry>
4933
<entry><literal>timestamp '2001-09-29 03:00' - timestamp '2001-09-27 12:00'</literal></entry>
4934
<entry><literal>interval '1 day 15:00'</literal></entry>
4938
<entry> <literal>*</literal> </entry>
4939
<entry><literal>interval '1 hour' * double precision '3.5'</literal></entry>
4940
<entry><literal>interval '03:30'</literal></entry>
4944
<entry> <literal>/</literal> </entry>
4945
<entry><literal>interval '1 hour' / double precision '1.5'</literal></entry>
4946
<entry><literal>interval '00:40'</literal></entry>
4952
<table id="functions-datetime-table">
4953
<title>Date/Time Functions</title>
4957
<entry>Function</entry>
4958
<entry>Return Type</entry>
4959
<entry>Description</entry>
4960
<entry>Example</entry>
4961
<entry>Result</entry>
4967
<entry><literal><function>age</function>(<type>timestamp</type>, <type>timestamp</type>)</literal></entry>
4968
<entry><type>interval</type></entry>
4969
<entry>Subtract arguments, producing a <quote>symbolic</> result that
4970
uses years and months</entry>
4971
<entry><literal>age(timestamp '2001-04-10', timestamp '1957-06-13')</literal></entry>
4972
<entry><literal>43 years 9 mons 27 days</literal></entry>
4976
<entry><literal><function>age</function>(<type>timestamp</type>)</literal></entry>
4977
<entry><type>interval</type></entry>
4978
<entry>Subtract from <function>current_date</function></entry>
4979
<entry><literal>age(timestamp '1957-06-13')</literal></entry>
4980
<entry><literal>43 years 8 mons 3 days</literal></entry>
4984
<entry><literal><function>current_date</function></literal></entry>
4985
<entry><type>date</type></entry>
4986
<entry>Today's date; see <xref linkend="functions-datetime-current">
4993
<entry><literal><function>current_time</function></literal></entry>
4994
<entry><type>time with time zone</type></entry>
4995
<entry>Time of day; see <xref linkend="functions-datetime-current">
5002
<entry><literal><function>current_timestamp</function></literal></entry>
5003
<entry><type>timestamp with time zone</type></entry>
5004
<entry>Date and time; see <xref linkend="functions-datetime-current">
5011
<entry><literal><function>date_part</function>(<type>text</type>, <type>timestamp</type>)</literal></entry>
5012
<entry><type>double precision</type></entry>
5013
<entry>Get subfield (equivalent to
5014
<function>extract</function>); see <xref linkend="functions-datetime-extract">
5016
<entry><literal>date_part('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
5017
<entry><literal>20</literal></entry>
5021
<entry><literal><function>date_part</function>(<type>text</type>, <type>interval</type>)</literal></entry>
5022
<entry><type>double precision</type></entry>
5023
<entry>Get subfield (equivalent to
5024
<function>extract</function>); see <xref linkend="functions-datetime-extract">
5026
<entry><literal>date_part('month', interval '2 years 3 months')</literal></entry>
5027
<entry><literal>3</literal></entry>
5031
<entry><literal><function>date_trunc</function>(<type>text</type>, <type>timestamp</type>)</literal></entry>
5032
<entry><type>timestamp</type></entry>
5033
<entry>Truncate to specified precision; see also <xref
5034
linkend="functions-datetime-trunc">
5036
<entry><literal>date_trunc('hour', timestamp '2001-02-16 20:38:40')</literal></entry>
5037
<entry><literal>2001-02-16 20:00:00</literal></entry>
5041
<entry><literal><function>extract</function>(<parameter>field</parameter> from
5042
<type>timestamp</type>)</literal></entry>
5043
<entry><type>double precision</type></entry>
5044
<entry>Get subfield; see <xref linkend="functions-datetime-extract">
5046
<entry><literal>extract(hour from timestamp '2001-02-16 20:38:40')</literal></entry>
5047
<entry><literal>20</literal></entry>
5051
<entry><literal><function>extract</function>(<parameter>field</parameter> from
5052
<type>interval</type>)</literal></entry>
5053
<entry><type>double precision</type></entry>
5054
<entry>Get subfield; see <xref linkend="functions-datetime-extract">
5056
<entry><literal>extract(month from interval '2 years 3 months')</literal></entry>
5057
<entry><literal>3</literal></entry>
5061
<entry><literal><function>isfinite</function>(<type>timestamp</type>)</literal></entry>
5062
<entry><type>boolean</type></entry>
5063
<entry>Test for finite time stamp (not equal to infinity)</entry>
5064
<entry><literal>isfinite(timestamp '2001-02-16 21:28:30')</literal></entry>
5065
<entry><literal>true</literal></entry>
5069
<entry><literal><function>isfinite</function>(<type>interval</type>)</literal></entry>
5070
<entry><type>boolean</type></entry>
5071
<entry>Test for finite interval</entry>
5072
<entry><literal>isfinite(interval '4 hours')</literal></entry>
5073
<entry><literal>true</literal></entry>
5077
<entry><literal><function>localtime</function></literal></entry>
5078
<entry><type>time</type></entry>
5079
<entry>Time of day; see <xref linkend="functions-datetime-current">
5086
<entry><literal><function>localtimestamp</function></literal></entry>
5087
<entry><type>timestamp</type></entry>
5088
<entry>Date and time; see <xref linkend="functions-datetime-current">
5095
<entry><literal><function>now</function>()</literal></entry>
5096
<entry><type>timestamp with time zone</type></entry>
5097
<entry>Current date and time (equivalent to
5098
<function>current_timestamp</function>); see <xref
5099
linkend="functions-datetime-current">
5106
<entry><literal><function>timeofday()</function></literal></entry>
5107
<entry><type>text</type></entry>
5108
<entry>Current date and time; see <xref
5109
linkend="functions-datetime-current">
5120
In addition to these functions, the SQL <literal>OVERLAPS</> operator is
5123
( <replaceable>start1</replaceable>, <replaceable>end1</replaceable> ) OVERLAPS ( <replaceable>start2</replaceable>, <replaceable>end2</replaceable> )
5124
( <replaceable>start1</replaceable>, <replaceable>length1</replaceable> ) OVERLAPS ( <replaceable>start2</replaceable>, <replaceable>length2</replaceable> )
5126
This expression yields true when two time periods (defined by their
5127
endpoints) overlap, false when they do not overlap. The endpoints
5128
can be specified as pairs of dates, times, or time stamps; or as
5129
a date, time, or time stamp followed by an interval.
5133
SELECT (DATE '2001-02-16', DATE '2001-12-21') OVERLAPS
5134
(DATE '2001-10-30', DATE '2002-10-30');
5135
<lineannotation>Result: </lineannotation><computeroutput>true</computeroutput>
5136
SELECT (DATE '2001-02-16', INTERVAL '100 days') OVERLAPS
5137
(DATE '2001-10-30', DATE '2002-10-30');
5138
<lineannotation>Result: </lineannotation><computeroutput>false</computeroutput>
5141
<sect2 id="functions-datetime-extract">
5142
<title><function>EXTRACT</function>, <function>date_part</function></title>
5145
EXTRACT (<replaceable>field</replaceable> FROM <replaceable>source</replaceable>)
5149
The <function>extract</function> function retrieves subfields
5150
such as year or hour from date/time values.
5151
<replaceable>source</replaceable> must be a value expression of
5152
type <type>timestamp</type>, <type>time</type>, or <type>interval</type>.
5153
(Expressions of type <type>date</type> will
5154
be cast to <type>timestamp</type> and can therefore be used as
5155
well.) <replaceable>field</replaceable> is an identifier or
5156
string that selects what field to extract from the source value.
5157
The <function>extract</function> function returns values of type
5158
<type>double precision</type>.
5159
The following are valid field names:
5161
<!-- alphabetical -->
5164
<term><literal>century</literal></term>
5171
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2000-12-16 12:21:13');
5172
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
5173
SELECT EXTRACT(CENTURY FROM TIMESTAMP '2001-02-16 20:38:40');
5174
<lineannotation>Result: </lineannotation><computeroutput>21</computeroutput>
5178
The first century starts at 0001-01-01 00:00:00 AD, although
5179
they did not know it at the time. This definition applies to all
5180
Gregorian calendar countries. There is no century number 0,
5181
you go from -1 to 1.
5183
If you disagree with this, please write your complaint to:
5184
Pope, Cathedral Saint-Peter of Roma, Vatican.
5188
<productname>PostgreSQL</productname> releases before 8.0 did not
5189
follow the conventional numbering of centuries, but just returned
5190
the year field divided by 100.
5196
<term><literal>day</literal></term>
5199
The day (of the month) field (1 - 31)
5203
SELECT EXTRACT(DAY FROM TIMESTAMP '2001-02-16 20:38:40');
5204
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
5210
<term><literal>decade</literal></term>
5213
The year field divided by 10
5217
SELECT EXTRACT(DECADE FROM TIMESTAMP '2001-02-16 20:38:40');
5218
<lineannotation>Result: </lineannotation><computeroutput>200</computeroutput>
5224
<term><literal>dow</literal></term>
5227
The day of the week (0 - 6; Sunday is 0) (for
5228
<type>timestamp</type> values only)
5232
SELECT EXTRACT(DOW FROM TIMESTAMP '2001-02-16 20:38:40');
5233
<lineannotation>Result: </lineannotation><computeroutput>5</computeroutput>
5236
Note that <function>extract</function>'s day of the week numbering is
5237
different from that of the <function>to_char</function> function.
5244
<term><literal>doy</literal></term>
5247
The day of the year (1 - 365/366) (for <type>timestamp</type> values only)
5251
SELECT EXTRACT(DOY FROM TIMESTAMP '2001-02-16 20:38:40');
5252
<lineannotation>Result: </lineannotation><computeroutput>47</computeroutput>
5258
<term><literal>epoch</literal></term>
5261
For <type>date</type> and <type>timestamp</type> values, the
5262
number of seconds since 1970-01-01 00:00:00-00 (can be negative);
5263
for <type>interval</type> values, the total number
5264
of seconds in the interval
5268
SELECT EXTRACT(EPOCH FROM TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-08');
5269
<lineannotation>Result: </lineannotation><computeroutput>982384720</computeroutput>
5271
SELECT EXTRACT(EPOCH FROM INTERVAL '5 days 3 hours');
5272
<lineannotation>Result: </lineannotation><computeroutput>442800</computeroutput>
5276
Here is how you can convert an epoch value back to a time
5281
SELECT TIMESTAMP WITH TIME ZONE 'epoch' + 982384720 * INTERVAL '1 second';
5287
<term><literal>hour</literal></term>
5290
The hour field (0 - 23)
5294
SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40');
5295
<lineannotation>Result: </lineannotation><computeroutput>20</computeroutput>
5301
<term><literal>microseconds</literal></term>
5304
The seconds field, including fractional parts, multiplied by 1
5305
000 000. Note that this includes full seconds.
5309
SELECT EXTRACT(MICROSECONDS FROM TIME '17:12:28.5');
5310
<lineannotation>Result: </lineannotation><computeroutput>28500000</computeroutput>
5316
<term><literal>millennium</literal></term>
5323
SELECT EXTRACT(MILLENNIUM FROM TIMESTAMP '2001-02-16 20:38:40');
5324
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
5328
Years in the 1900s are in the second millennium.
5329
The third millennium starts January 1, 2001.
5333
<productname>PostgreSQL</productname> releases before 8.0 did not
5334
follow the conventional numbering of millennia, but just returned
5335
the year field divided by 1000.
5341
<term><literal>milliseconds</literal></term>
5344
The seconds field, including fractional parts, multiplied by
5345
1000. Note that this includes full seconds.
5349
SELECT EXTRACT(MILLISECONDS FROM TIME '17:12:28.5');
5350
<lineannotation>Result: </lineannotation><computeroutput>28500</computeroutput>
5356
<term><literal>minute</literal></term>
5359
The minutes field (0 - 59)
5363
SELECT EXTRACT(MINUTE FROM TIMESTAMP '2001-02-16 20:38:40');
5364
<lineannotation>Result: </lineannotation><computeroutput>38</computeroutput>
5370
<term><literal>month</literal></term>
5373
For <type>timestamp</type> values, the number of the month
5374
within the year (1 - 12) ; for <type>interval</type> values
5375
the number of months, modulo 12 (0 - 11)
5379
SELECT EXTRACT(MONTH FROM TIMESTAMP '2001-02-16 20:38:40');
5380
<lineannotation>Result: </lineannotation><computeroutput>2</computeroutput>
5382
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 3 months');
5383
<lineannotation>Result: </lineannotation><computeroutput>3</computeroutput>
5385
SELECT EXTRACT(MONTH FROM INTERVAL '2 years 13 months');
5386
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
5392
<term><literal>quarter</literal></term>
5395
The quarter of the year (1 - 4) that the day is in (for
5396
<type>timestamp</type> values only)
5400
SELECT EXTRACT(QUARTER FROM TIMESTAMP '2001-02-16 20:38:40');
5401
<lineannotation>Result: </lineannotation><computeroutput>1</computeroutput>
5407
<term><literal>second</literal></term>
5410
The seconds field, including fractional parts (0 -
5411
59<footnote><simpara>60 if leap seconds are
5412
implemented by the operating system</simpara></footnote>)
5416
SELECT EXTRACT(SECOND FROM TIMESTAMP '2001-02-16 20:38:40');
5417
<lineannotation>Result: </lineannotation><computeroutput>40</computeroutput>
5419
SELECT EXTRACT(SECOND FROM TIME '17:12:28.5');
5420
<lineannotation>Result: </lineannotation><computeroutput>28.5</computeroutput>
5425
<term><literal>timezone</literal></term>
5428
The time zone offset from UTC, measured in seconds. Positive values
5429
correspond to time zones east of UTC, negative values to
5436
<term><literal>timezone_hour</literal></term>
5439
The hour component of the time zone offset
5445
<term><literal>timezone_minute</literal></term>
5448
The minute component of the time zone offset
5454
<term><literal>week</literal></term>
5457
The number of the week of the year that the day is in. By definition
5458
(<acronym>ISO</acronym> 8601), the first week of a year
5459
contains January 4 of that year. (The <acronym>ISO</acronym>-8601
5460
week starts on Monday.) In other words, the first Thursday of
5461
a year is in week 1 of that year. (for <type>timestamp</type> values only)
5464
Because of this, it is possible for early January dates to be part of the
5465
52nd or 53rd week of the previous year. For example, <literal>2005-01-01</>
5466
is part of the 53rd week of year 2004, and <literal>2006-01-01</> is part of
5467
the 52nd week of year 2005.
5471
SELECT EXTRACT(WEEK FROM TIMESTAMP '2001-02-16 20:38:40');
5472
<lineannotation>Result: </lineannotation><computeroutput>7</computeroutput>
5478
<term><literal>year</literal></term>
5481
The year field. Keep in mind there is no <literal>0 AD</>, so subtracting
5482
<literal>BC</> years from <literal>AD</> years should be done with care.
5486
SELECT EXTRACT(YEAR FROM TIMESTAMP '2001-02-16 20:38:40');
5487
<lineannotation>Result: </lineannotation><computeroutput>2001</computeroutput>
5497
The <function>extract</function> function is primarily intended
5498
for computational processing. For formatting date/time values for
5499
display, see <xref linkend="functions-formatting">.
5503
The <function>date_part</function> function is modeled on the traditional
5504
<productname>Ingres</productname> equivalent to the
5505
<acronym>SQL</acronym>-standard function <function>extract</function>:
5507
date_part('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
5509
Note that here the <replaceable>field</replaceable> parameter needs to
5510
be a string value, not a name. The valid field names for
5511
<function>date_part</function> are the same as for
5512
<function>extract</function>.
5516
SELECT date_part('day', TIMESTAMP '2001-02-16 20:38:40');
5517
<lineannotation>Result: </lineannotation><computeroutput>16</computeroutput>
5519
SELECT date_part('hour', INTERVAL '4 hours 3 minutes');
5520
<lineannotation>Result: </lineannotation><computeroutput>4</computeroutput>
5525
<sect2 id="functions-datetime-trunc">
5526
<title><function>date_trunc</function></title>
5529
The function <function>date_trunc</function> is conceptually
5530
similar to the <function>trunc</function> function for numbers.
5535
date_trunc('<replaceable>field</replaceable>', <replaceable>source</replaceable>)
5537
<replaceable>source</replaceable> is a value expression of type
5538
<type>timestamp</type> or <type>interval</>.
5539
(Values of type <type>date</type> and
5540
<type>time</type> are cast automatically, to <type>timestamp</type> or
5541
<type>interval</> respectively.)
5542
<replaceable>field</replaceable> selects to which precision to
5543
truncate the input value. The return value is of type
5544
<type>timestamp</type> or <type>interval</>
5545
with all fields that are less significant than the
5546
selected one set to zero (or one, for day and month).
5550
Valid values for <replaceable>field</replaceable> are:
5552
<member><literal>microseconds</literal></member>
5553
<member><literal>milliseconds</literal></member>
5554
<member><literal>second</literal></member>
5555
<member><literal>minute</literal></member>
5556
<member><literal>hour</literal></member>
5557
<member><literal>day</literal></member>
5558
<member><literal>week</literal></member>
5559
<member><literal>month</literal></member>
5560
<member><literal>year</literal></member>
5561
<member><literal>decade</literal></member>
5562
<member><literal>century</literal></member>
5563
<member><literal>millennium</literal></member>
5570
SELECT date_trunc('hour', TIMESTAMP '2001-02-16 20:38:40');
5571
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 20:00:00</computeroutput>
5573
SELECT date_trunc('year', TIMESTAMP '2001-02-16 20:38:40');
5574
<lineannotation>Result: </lineannotation><computeroutput>2001-01-01 00:00:00</computeroutput>
5579
<sect2 id="functions-datetime-zoneconvert">
5580
<title><literal>AT TIME ZONE</literal></title>
5583
<primary>time zone</primary>
5584
<secondary>conversion</secondary>
5588
The <literal>AT TIME ZONE</literal> construct allows conversions
5589
of time stamps to different time zones. <xref
5590
linkend="functions-datetime-zoneconvert-table"> shows its
5594
<table id="functions-datetime-zoneconvert-table">
5595
<title><literal>AT TIME ZONE</literal> Variants</title>
5599
<entry>Expression</entry>
5600
<entry>Return Type</entry>
5601
<entry>Description</entry>
5608
<literal><type>timestamp without time zone</type> AT TIME ZONE <replaceable>zone</></literal>
5610
<entry><type>timestamp with time zone</type></entry>
5611
<entry>Convert local time in given time zone to UTC</entry>
5616
<literal><type>timestamp with time zone</type> AT TIME ZONE <replaceable>zone</></literal>
5618
<entry><type>timestamp without time zone</type></entry>
5619
<entry>Convert UTC to local time in given time zone</entry>
5624
<literal><type>time with time zone</type> AT TIME ZONE <replaceable>zone</></literal>
5626
<entry><type>time with time zone</type></entry>
5627
<entry>Convert local time across time zones</entry>
5634
In these expressions, the desired time zone <replaceable>zone</> can be
5635
specified either as a text string (e.g., <literal>'PST'</literal>)
5636
or as an interval (e.g., <literal>INTERVAL '-08:00'</literal>).
5637
In the text case, the available zone names are those shown in
5638
<xref linkend="datetime-timezone-input-table">. (It would be useful
5639
to support the more general names shown in
5640
<xref linkend="datetime-timezone-set-table">, but this is not yet
5645
Examples (supposing that the local time zone is <literal>PST8PDT</>):
5647
SELECT TIMESTAMP '2001-02-16 20:38:40' AT TIME ZONE 'MST';
5648
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 19:38:40-08</computeroutput>
5650
SELECT TIMESTAMP WITH TIME ZONE '2001-02-16 20:38:40-05' AT TIME ZONE 'MST';
5651
<lineannotation>Result: </lineannotation><computeroutput>2001-02-16 18:38:40</computeroutput>
5653
The first example takes a zone-less time stamp and interprets it as MST time
5654
(UTC-7) to produce a UTC time stamp, which is then rotated to PST (UTC-8)
5655
for display. The second example takes a time stamp specified in EST
5656
(UTC-5) and converts it to local time in MST (UTC-7).
5660
The function <literal><function>timezone</function>(<replaceable>zone</>,
5661
<replaceable>timestamp</>)</literal> is equivalent to the SQL-conforming construct
5662
<literal><replaceable>timestamp</> AT TIME ZONE
5663
<replaceable>zone</></literal>.
5667
<sect2 id="functions-datetime-current">
5668
<title>Current Date/Time</title>
5671
<primary>date</primary>
5672
<secondary>current</secondary>
5676
<primary>time</primary>
5677
<secondary>current</secondary>
5681
The following functions are available to obtain the current date and/or
5687
CURRENT_TIME ( <replaceable>precision</replaceable> )
5688
CURRENT_TIMESTAMP ( <replaceable>precision</replaceable> )
5691
LOCALTIME ( <replaceable>precision</replaceable> )
5692
LOCALTIMESTAMP ( <replaceable>precision</replaceable> )
5697
<function>CURRENT_TIME</function> and
5698
<function>CURRENT_TIMESTAMP</function> deliver values with time zone;
5699
<function>LOCALTIME</function> and
5700
<function>LOCALTIMESTAMP</function> deliver values without time zone.
5704
<function>CURRENT_TIME</function>,
5705
<function>CURRENT_TIMESTAMP</function>,
5706
<function>LOCALTIME</function>, and
5707
<function>LOCALTIMESTAMP</function>
5708
can optionally be given
5709
a precision parameter, which causes the result to be rounded
5710
to that many fractional digits in the seconds field. Without a precision parameter,
5711
the result is given to the full available precision.
5716
Prior to <productname>PostgreSQL</productname> 7.2, the precision
5717
parameters were unimplemented, and the result was always given
5725
SELECT CURRENT_TIME;
5726
<lineannotation>Result: </lineannotation><computeroutput>14:39:53.662522-05</computeroutput>
5728
SELECT CURRENT_DATE;
5729
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23</computeroutput>
5731
SELECT CURRENT_TIMESTAMP;
5732
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.662522-05</computeroutput>
5734
SELECT CURRENT_TIMESTAMP(2);
5735
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.66-05</computeroutput>
5737
SELECT LOCALTIMESTAMP;
5738
<lineannotation>Result: </lineannotation><computeroutput>2001-12-23 14:39:53.662522</computeroutput>
5743
The function <function>now()</function> is the traditional
5744
<productname>PostgreSQL</productname> equivalent to
5745
<function>CURRENT_TIMESTAMP</function>.
5749
There is also the function <function>timeofday()</function>, which for historical
5750
reasons returns a <type>text</type> string rather than a <type>timestamp</type> value:
5753
<lineannotation>Result: </lineannotation><computeroutput>Sat Feb 17 19:07:32.000126 2001 EST</computeroutput>
5758
It is important to know that
5759
<function>CURRENT_TIMESTAMP</function> and related functions return
5760
the start time of the current transaction; their values do not
5761
change during the transaction. This is considered a feature:
5762
the intent is to allow a single transaction to have a consistent
5763
notion of the <quote>current</quote> time, so that multiple
5764
modifications within the same transaction bear the same
5765
time stamp. <function>timeofday()</function>
5766
returns the wall-clock time and does advance during transactions.
5771
Other database systems may advance these values more
5777
All the date/time data types also accept the special literal value
5778
<literal>now</literal> to specify the current date and time. Thus,
5779
the following three all return the same result:
5781
SELECT CURRENT_TIMESTAMP;
5783
SELECT TIMESTAMP 'now';
5789
You do not want to use the third form when specifying a <literal>DEFAULT</>
5790
clause while creating a table. The system will convert <literal>now</literal>
5791
to a <type>timestamp</type> as soon as the constant is parsed, so that when
5792
the default value is needed,
5793
the time of the table creation would be used! The first two
5794
forms will not be evaluated until the default value is used,
5795
because they are function calls. Thus they will give the desired
5796
behavior of defaulting to the time of row insertion.
5803
<sect1 id="functions-geometry">
5804
<title>Geometric Functions and Operators</title>
5807
The geometric types <type>point</type>, <type>box</type>,
5808
<type>lseg</type>, <type>line</type>, <type>path</type>,
5809
<type>polygon</type>, and <type>circle</type> have a large set of
5810
native support functions and operators, shown in <xref
5811
linkend="functions-geometry-op-table">, <xref
5812
linkend="functions-geometry-func-table">, and <xref
5813
linkend="functions-geometry-conv-table">.
5816
<table id="functions-geometry-op-table">
5817
<title>Geometric Operators</title>
5821
<entry>Operator</entry>
5822
<entry>Description</entry>
5823
<entry>Example</entry>
5828
<entry> <literal>+</literal> </entry>
5829
<entry>Translation</entry>
5830
<entry><literal>box '((0,0),(1,1))' + point '(2.0,0)'</literal></entry>
5833
<entry> <literal>-</literal> </entry>
5834
<entry>Translation</entry>
5835
<entry><literal>box '((0,0),(1,1))' - point '(2.0,0)'</literal></entry>
5838
<entry> <literal>*</literal> </entry>
5839
<entry>Scaling/rotation</entry>
5840
<entry><literal>box '((0,0),(1,1))' * point '(2.0,0)'</literal></entry>
5843
<entry> <literal>/</literal> </entry>
5844
<entry>Scaling/rotation</entry>
5845
<entry><literal>box '((0,0),(2,2))' / point '(2.0,0)'</literal></entry>
5848
<entry> <literal>#</literal> </entry>
5849
<entry>Point or box of intersection</entry>
5850
<entry><literal>'((1,-1),(-1,1))' # '((1,1),(-1,-1))'</literal></entry>
5853
<entry> <literal>#</literal> </entry>
5854
<entry>Number of points in path or polygon</entry>
5855
<entry><literal># '((1,0),(0,1),(-1,0))'</literal></entry>
5858
<entry> <literal>@-@</literal> </entry>
5859
<entry>Length or circumference</entry>
5860
<entry><literal>@-@ path '((0,0),(1,0))'</literal></entry>
5863
<entry> <literal>@@</literal> </entry>
5864
<entry>Center</entry>
5865
<entry><literal>@@ circle '((0,0),10)'</literal></entry>
5868
<entry> <literal>##</literal> </entry>
5869
<entry>Closest point to first operand on second operand</entry>
5870
<entry><literal>point '(0,0)' ## lseg '((2,0),(0,2))'</literal></entry>
5873
<entry> <literal><-></literal> </entry>
5874
<entry>Distance between</entry>
5875
<entry><literal>circle '((0,0),1)' <-> circle '((5,0),1)'</literal></entry>
5878
<entry> <literal>&&</literal> </entry>
5879
<entry>Overlaps?</entry>
5880
<entry><literal>box '((0,0),(1,1))' && box '((0,0),(2,2))'</literal></entry>
5883
<entry> <literal>&<</literal> </entry>
5884
<entry>Does not extend to the right of?</entry>
5885
<entry><literal>box '((0,0),(1,1))' &< box '((0,0),(2,2))'</literal></entry>
5888
<entry> <literal>&></literal> </entry>
5889
<entry>Does not extend to the left of?</entry>
5890
<entry><literal>box '((0,0),(3,3))' &> box '((0,0),(2,2))'</literal></entry>
5893
<entry> <literal><<</literal> </entry>
5894
<entry>Is left of?</entry>
5895
<entry><literal>circle '((0,0),1)' << circle '((5,0),1)'</literal></entry>
5898
<entry> <literal>>></literal> </entry>
5899
<entry>Is right of?</entry>
5900
<entry><literal>circle '((5,0),1)' >> circle '((0,0),1)'</literal></entry>
5903
<entry> <literal><^</literal> </entry>
5904
<entry>Is below?</entry>
5905
<entry><literal>circle '((0,0),1)' <^ circle '((0,5),1)'</literal></entry>
5908
<entry> <literal>>^</literal> </entry>
5909
<entry>Is above?</entry>
5910
<entry><literal>circle '((0,5),1)' >^ circle '((0,0),1)'</literal></entry>
5913
<entry> <literal>?#</literal> </entry>
5914
<entry>Intersects?</entry>
5915
<entry><literal>lseg '((-1,0),(1,0))' ?# box '((-2,-2),(2,2))'</literal></entry>
5918
<entry> <literal>?-</literal> </entry>
5919
<entry>Is horizontal?</entry>
5920
<entry><literal>?- lseg '((-1,0),(1,0))'</literal></entry>
5923
<entry> <literal>?-</literal> </entry>
5924
<entry>Are horizontally aligned?</entry>
5925
<entry><literal>point '(1,0)' ?- point '(0,0)'</literal></entry>
5928
<entry> <literal>?|</literal> </entry>
5929
<entry>Is vertical?</entry>
5930
<entry><literal>?| lseg '((-1,0),(1,0))'</literal></entry>
5933
<entry> <literal>?|</literal> </entry>
5934
<entry>Are vertically aligned?</entry>
5935
<entry><literal>point '(0,1)' ?| point '(0,0)'</literal></entry>
5938
<entry> <literal>?-|</literal> </entry>
5939
<entry>Is perpendicular?</entry>
5940
<entry><literal>lseg '((0,0),(0,1))' ?-| lseg '((0,0),(1,0))'</literal></entry>
5943
<entry> <literal>?||</literal> </entry>
5944
<entry>Are parallel?</entry>
5945
<entry><literal>lseg '((-1,0),(1,0))' ?|| lseg '((-1,2),(1,2))'</literal></entry>
5948
<entry> <literal>~</literal> </entry>
5949
<entry>Contains?</entry>
5950
<entry><literal>circle '((0,0),2)' ~ point '(1,1)'</literal></entry>
5953
<entry> <literal>@</literal> </entry>
5954
<entry>Contained in or on?</entry>
5955
<entry><literal>point '(1,1)' @ circle '((0,0),2)'</literal></entry>
5958
<entry> <literal>~=</literal> </entry>
5959
<entry>Same as?</entry>
5960
<entry><literal>polygon '((0,0),(1,1))' ~= polygon '((1,1),(0,0))'</literal></entry>
5967
<table id="functions-geometry-func-table">
5968
<title>Geometric Functions</title>
5972
<entry>Function</entry>
5973
<entry>Return Type</entry>
5974
<entry>Description</entry>
5975
<entry>Example</entry>
5980
<entry><literal><function>area</function>(<replaceable>object</>)</literal></entry>
5981
<entry><type>double precision</type></entry>
5983
<entry><literal>area(box '((0,0),(1,1))')</literal></entry>
5986
<entry><literal><function>box_intersect</function>(<type>box</>, <type>box</>)</literal></entry>
5987
<entry><type>box</type></entry>
5988
<entry>intersection box</entry>
5989
<entry><literal>box_intersect(box '((0,0),(1,1))',box '((0.5,0.5),(2,2))')</literal></entry>
5992
<entry><literal><function>center</function>(<replaceable>object</>)</literal></entry>
5993
<entry><type>point</type></entry>
5994
<entry>center</entry>
5995
<entry><literal>center(box '((0,0),(1,2))')</literal></entry>
5998
<entry><literal><function>diameter</function>(<type>circle</>)</literal></entry>
5999
<entry><type>double precision</type></entry>
6000
<entry>diameter of circle</entry>
6001
<entry><literal>diameter(circle '((0,0),2.0)')</literal></entry>
6004
<entry><literal><function>height</function>(<type>box</>)</literal></entry>
6005
<entry><type>double precision</type></entry>
6006
<entry>vertical size of box</entry>
6007
<entry><literal>height(box '((0,0),(1,1))')</literal></entry>
6010
<entry><literal><function>isclosed</function>(<type>path</>)</literal></entry>
6011
<entry><type>boolean</type></entry>
6012
<entry>a closed path?</entry>
6013
<entry><literal>isclosed(path '((0,0),(1,1),(2,0))')</literal></entry>
6016
<entry><literal><function>isopen</function>(<type>path</>)</literal></entry>
6017
<entry><type>boolean</type></entry>
6018
<entry>an open path?</entry>
6019
<entry><literal>isopen(path '[(0,0),(1,1),(2,0)]')</literal></entry>
6022
<entry><literal><function>length</function>(<replaceable>object</>)</literal></entry>
6023
<entry><type>double precision</type></entry>
6024
<entry>length</entry>
6025
<entry><literal>length(path '((-1,0),(1,0))')</literal></entry>
6028
<entry><literal><function>npoints</function>(<type>path</>)</literal></entry>
6029
<entry><type>integer</type></entry>
6030
<entry>number of points</entry>
6031
<entry><literal>npoints(path '[(0,0),(1,1),(2,0)]')</literal></entry>
6034
<entry><literal><function>npoints</function>(<type>polygon</>)</literal></entry>
6035
<entry><type>integer</type></entry>
6036
<entry>number of points</entry>
6037
<entry><literal>npoints(polygon '((1,1),(0,0))')</literal></entry>
6040
<entry><literal><function>pclose</function>(<type>path</>)</literal></entry>
6041
<entry><type>path</type></entry>
6042
<entry>convert path to closed</entry>
6043
<entry><literal>pclose(path '[(0,0),(1,1),(2,0)]')</literal></entry>
6046
<!-- Not defined by this name. Implements the intersection operator '#' -->
6048
<entry><literal><function>point</function>(<type>lseg</>, <type>lseg</>)</literal></entry>
6049
<entry><type>point</type></entry>
6050
<entry>intersection</entry>
6051
<entry><literal>point(lseg '((-1,0),(1,0))',lseg '((-2,-2),(2,2))')</literal></entry>
6055
<entry><literal><function>popen</function>(<type>path</>)</literal></entry>
6056
<entry><type>path</type></entry>
6057
<entry>convert path to open</entry>
6058
<entry><literal>popen(path '((0,0),(1,1),(2,0))')</literal></entry>
6061
<entry><literal><function>radius</function>(<type>circle</type>)</literal></entry>
6062
<entry><type>double precision</type></entry>
6063
<entry>radius of circle</entry>
6064
<entry><literal>radius(circle '((0,0),2.0)')</literal></entry>
6067
<entry><literal><function>width</function>(<type>box</>)</literal></entry>
6068
<entry><type>double precision</type></entry>
6069
<entry>horizontal size of box</entry>
6070
<entry><literal>width(box '((0,0),(1,1))')</literal></entry>
6077
<table id="functions-geometry-conv-table">
6078
<title>Geometric Type Conversion Functions</title>
6082
<entry>Function</entry>
6083
<entry>Return Type</entry>
6084
<entry>Description</entry>
6085
<entry>Example</entry>
6090
<entry><literal><function>box</function>(<type>circle</type>)</literal></entry>
6091
<entry><type>box</type></entry>
6092
<entry>circle to box</entry>
6093
<entry><literal>box(circle '((0,0),2.0)')</literal></entry>
6096
<entry><literal><function>box</function>(<type>point</type>, <type>point</type>)</literal></entry>
6097
<entry><type>box</type></entry>
6098
<entry>points to box</entry>
6099
<entry><literal>box(point '(0,0)', point '(1,1)')</literal></entry>
6102
<entry><literal><function>box</function>(<type>polygon</type>)</literal></entry>
6103
<entry><type>box</type></entry>
6104
<entry>polygon to box</entry>
6105
<entry><literal>box(polygon '((0,0),(1,1),(2,0))')</literal></entry>
6108
<entry><literal><function>circle</function>(<type>box</type>)</literal></entry>
6109
<entry><type>circle</type></entry>
6110
<entry>box to circle</entry>
6111
<entry><literal>circle(box '((0,0),(1,1))')</literal></entry>
6114
<entry><literal><function>circle</function>(<type>point</type>, <type>double precision</type>)</literal></entry>
6115
<entry><type>circle</type></entry>
6116
<entry>center and radius to circle</entry>
6117
<entry><literal>circle(point '(0,0)', 2.0)</literal></entry>
6120
<entry><literal><function>lseg</function>(<type>box</type>)</literal></entry>
6121
<entry><type>lseg</type></entry>
6122
<entry>box diagonal to line segment</entry>
6123
<entry><literal>lseg(box '((-1,0),(1,0))')</literal></entry>
6126
<entry><literal><function>lseg</function>(<type>point</type>, <type>point</type>)</literal></entry>
6127
<entry><type>lseg</type></entry>
6128
<entry>points to line segment</entry>
6129
<entry><literal>lseg(point '(-1,0)', point '(1,0)')</literal></entry>
6132
<entry><literal><function>path</function>(<type>polygon</type>)</literal></entry>
6133
<entry><type>point</type></entry>
6134
<entry>polygon to path</entry>
6135
<entry><literal>path(polygon '((0,0),(1,1),(2,0))')</literal></entry>
6138
<entry><literal><function>point</function>(<type>double
6139
precision</type>, <type>double precision</type>)</literal></entry>
6140
<entry><type>point</type></entry>
6141
<entry>construct point</entry>
6142
<entry><literal>point(23.4, -44.5)</literal></entry>
6145
<entry><literal><function>point</function>(<type>box</type>)</literal></entry>
6146
<entry><type>point</type></entry>
6147
<entry>center of box</entry>
6148
<entry><literal>point(box '((-1,0),(1,0))')</literal></entry>
6151
<entry><literal><function>point</function>(<type>circle</type>)</literal></entry>
6152
<entry><type>point</type></entry>
6153
<entry>center of circle</entry>
6154
<entry><literal>point(circle '((0,0),2.0)')</literal></entry>
6157
<entry><literal><function>point</function>(<type>lseg</type>)</literal></entry>
6158
<entry><type>point</type></entry>
6159
<entry>center of lseg</entry>
6160
<entry><literal>point(lseg '((-1,0),(1,0))')</literal></entry>
6163
<entry><literal><function>point</function>(<type>lseg</type>, <type>lseg</type>)</literal></entry>
6164
<entry><type>point</type></entry>
6165
<entry>intersection</entry>
6166
<entry><literal>point(lseg '((-1,0),(1,0))', lseg '((-2,-2),(2,2))')</literal></entry>
6169
<entry><literal><function>point</function>(<type>polygon</type>)</literal></entry>
6170
<entry><type>point</type></entry>
6171
<entry>center of polygon</entry>
6172
<entry><literal>point(polygon '((0,0),(1,1),(2,0))')</literal></entry>
6175
<entry><literal><function>polygon</function>(<type>box</type>)</literal></entry>
6176
<entry><type>polygon</type></entry>
6177
<entry>box to 4-point polygon</entry>
6178
<entry><literal>polygon(box '((0,0),(1,1))')</literal></entry>
6181
<entry><literal><function>polygon</function>(<type>circle</type>)</literal></entry>
6182
<entry><type>polygon</type></entry>
6183
<entry>circle to 12-point polygon</entry>
6184
<entry><literal>polygon(circle '((0,0),2.0)')</literal></entry>
6187
<entry><literal><function>polygon</function>(<replaceable class="parameter">npts</replaceable>, <type>circle</type>)</literal></entry>
6188
<entry><type>polygon</type></entry>
6189
<entry>circle to <replaceable class="parameter">npts</replaceable>-point polygon</entry>
6190
<entry><literal>polygon(12, circle '((0,0),2.0)')</literal></entry>
6193
<entry><literal><function>polygon</function>(<type>path</type>)</literal></entry>
6194
<entry><type>polygon</type></entry>
6195
<entry>path to polygon</entry>
6196
<entry><literal>polygon(path '((0,0),(1,1),(2,0))')</literal></entry>
6203
It is possible to access the two component numbers of a <type>point</>
6204
as though it were an array with indices 0 and 1. For example, if
6205
<literal>t.p</> is a <type>point</> column then
6206
<literal>SELECT p[0] FROM t</> retrieves the X coordinate and
6207
<literal>UPDATE t SET p[1] = ...</> changes the Y coordinate.
6208
In the same way, a value of type <type>box</> or <type>lseg</> may be treated
6209
as an array of two <type>point</> values.
6213
The <function>area</function> function works for the types
6214
<type>box</type>, <type>circle</type>, and <type>path</type>.
6215
The <function>area</function> function only works on the
6216
<type>path</type> data type if the points in the
6217
<type>path</type> are non-intersecting. For example, the
6219
<literal>'((0,0),(0,1),(2,1),(2,2),(1,2),(1,0),(0,0))'::PATH</literal>
6220
won't work, however, the following visually identical
6222
<literal>'((0,0),(0,1),(1,1),(1,2),(2,2),(2,1),(1,1),(1,0),(0,0))'::PATH</literal>
6223
will work. If the concept of an intersecting versus
6224
non-intersecting <type>path</type> is confusing, draw both of the
6225
above <type>path</type>s side by side on a piece of graph paper.
6231
<sect1 id="functions-net">
6232
<title>Network Address Functions and Operators</title>
6235
<xref linkend="cidr-inet-operators-table"> shows the operators
6236
available for the <type>cidr</type> and <type>inet</type> types.
6237
The operators <literal><<</literal>,
6238
<literal><<=</literal>, <literal>>></literal>, and
6239
<literal>>>=</literal> test for subnet inclusion. They
6240
consider only the network parts of the two addresses, ignoring any
6241
host part, and determine whether one network part is identical to
6242
or a subnet of the other.
6245
<table id="cidr-inet-operators-table">
6246
<title><type>cidr</type> and <type>inet</type> Operators</title>
6250
<entry>Operator</entry>
6251
<entry>Description</entry>
6252
<entry>Example</entry>
6257
<entry> <literal><</literal> </entry>
6258
<entry>is less than</entry>
6259
<entry><literal>inet '192.168.1.5' < inet '192.168.1.6'</literal></entry>
6262
<entry> <literal><=</literal> </entry>
6263
<entry>is less than or equal</entry>
6264
<entry><literal>inet '192.168.1.5' <= inet '192.168.1.5'</literal></entry>
6267
<entry> <literal>=</literal> </entry>
6268
<entry>equals</entry>
6269
<entry><literal>inet '192.168.1.5' = inet '192.168.1.5'</literal></entry>
6272
<entry> <literal>>=</literal> </entry>
6273
<entry>is greater or equal</entry>
6274
<entry><literal>inet '192.168.1.5' >= inet '192.168.1.5'</literal></entry>
6277
<entry> <literal>></literal> </entry>
6278
<entry>is greater than</entry>
6279
<entry><literal>inet '192.168.1.5' > inet '192.168.1.4'</literal></entry>
6282
<entry> <literal><></literal> </entry>
6283
<entry>is not equal</entry>
6284
<entry><literal>inet '192.168.1.5' <> inet '192.168.1.4'</literal></entry>
6287
<entry> <literal><<</literal> </entry>
6288
<entry>is contained within</entry>
6289
<entry><literal>inet '192.168.1.5' << inet '192.168.1/24'</literal></entry>
6292
<entry> <literal><<=</literal> </entry>
6293
<entry>is contained within or equals</entry>
6294
<entry><literal>inet '192.168.1/24' <<= inet '192.168.1/24'</literal></entry>
6297
<entry> <literal>>></literal> </entry>
6298
<entry>contains</entry>
6299
<entry><literal>inet '192.168.1/24' >> inet '192.168.1.5'</literal></entry>
6302
<entry> <literal>>>=</literal> </entry>
6303
<entry>contains or equals</entry>
6304
<entry><literal>inet '192.168.1/24' >>= inet '192.168.1/24'</literal></entry>
6311
<xref linkend="cidr-inet-functions-table"> shows the functions
6312
available for use with the <type>cidr</type> and <type>inet</type>
6313
types. The <function>host</function>,
6314
<function>text</function>, and <function>abbrev</function>
6315
functions are primarily intended to offer alternative display
6316
formats. You can cast a text value to <type>inet</> using normal casting
6317
syntax: <literal>inet(<replaceable>expression</>)</literal> or
6318
<literal><replaceable>colname</>::inet</literal>.
6321
<table id="cidr-inet-functions-table">
6322
<title><type>cidr</type> and <type>inet</type> Functions</title>
6326
<entry>Function</entry>
6327
<entry>Return Type</entry>
6328
<entry>Description</entry>
6329
<entry>Example</entry>
6330
<entry>Result</entry>
6335
<entry><literal><function>broadcast</function>(<type>inet</type>)</literal></entry>
6336
<entry><type>inet</type></entry>
6337
<entry>broadcast address for network</entry>
6338
<entry><literal>broadcast('192.168.1.5/24')</literal></entry>
6339
<entry><literal>192.168.1.255/24</literal></entry>
6342
<entry><literal><function>host</function>(<type>inet</type>)</literal></entry>
6343
<entry><type>text</type></entry>
6344
<entry>extract IP address as text</entry>
6345
<entry><literal>host('192.168.1.5/24')</literal></entry>
6346
<entry><literal>192.168.1.5</literal></entry>
6349
<entry><literal><function>masklen</function>(<type>inet</type>)</literal></entry>
6350
<entry><type>integer</type></entry>
6351
<entry>extract netmask length</entry>
6352
<entry><literal>masklen('192.168.1.5/24')</literal></entry>
6353
<entry><literal>24</literal></entry>
6356
<entry><literal><function>set_masklen</function>(<type>inet</type>, <type>integer</type>)</literal></entry>
6357
<entry><type>inet</type></entry>
6358
<entry>set netmask length for <type>inet</type> value</entry>
6359
<entry><literal>set_masklen('192.168.1.5/24', 16)</literal></entry>
6360
<entry><literal>192.168.1.5/16</literal></entry>
6363
<entry><literal><function>netmask</function>(<type>inet</type>)</literal></entry>
6364
<entry><type>inet</type></entry>
6365
<entry>construct netmask for network</entry>
6366
<entry><literal>netmask('192.168.1.5/24')</literal></entry>
6367
<entry><literal>255.255.255.0</literal></entry>
6370
<entry><literal><function>hostmask</function>(<type>inet</type>)</literal></entry>
6371
<entry><type>inet</type></entry>
6372
<entry>construct host mask for network</entry>
6373
<entry><literal>hostmask('192.168.23.20/30')</literal></entry>
6374
<entry><literal>0.0.0.3</literal></entry>
6377
<entry><literal><function>network</function>(<type>inet</type>)</literal></entry>
6378
<entry><type>cidr</type></entry>
6379
<entry>extract network part of address</entry>
6380
<entry><literal>network('192.168.1.5/24')</literal></entry>
6381
<entry><literal>192.168.1.0/24</literal></entry>
6384
<entry><literal><function>text</function>(<type>inet</type>)</literal></entry>
6385
<entry><type>text</type></entry>
6386
<entry>extract IP address and netmask length as text</entry>
6387
<entry><literal>text(inet '192.168.1.5')</literal></entry>
6388
<entry><literal>192.168.1.5/32</literal></entry>
6391
<entry><literal><function>abbrev</function>(<type>inet</type>)</literal></entry>
6392
<entry><type>text</type></entry>
6393
<entry>abbreviated display format as text</entry>
6394
<entry><literal>abbrev(cidr '10.1.0.0/16')</literal></entry>
6395
<entry><literal>10.1/16</literal></entry>
6398
<entry><literal><function>family</function>(<type>inet</type>)</literal></entry>
6399
<entry><type>integer</type></entry>
6400
<entry>extract family of address; <literal>4</literal> for IPv4,
6401
<literal>6</literal> for IPv6</entry>
6402
<entry><literal>family('::1')</literal></entry>
6403
<entry><literal>6</literal></entry>
6410
<xref linkend="macaddr-functions-table"> shows the functions
6411
available for use with the <type>macaddr</type> type. The function
6412
<literal><function>trunc</function>(<type>macaddr</type>)</literal> returns a MAC
6413
address with the last 3 bytes set to zero. This can be used to
6414
associate the remaining prefix with a manufacturer. The directory
6415
<filename>contrib/mac</filename> in the source distribution
6416
contains some utilities to create and maintain such an association
6420
<table id="macaddr-functions-table">
6421
<title><type>macaddr</type> Functions</title>
6425
<entry>Function</entry>
6426
<entry>Return Type</entry>
6427
<entry>Description</entry>
6428
<entry>Example</entry>
6429
<entry>Result</entry>
6434
<entry><literal><function>trunc</function>(<type>macaddr</type>)</literal></entry>
6435
<entry><type>macaddr</type></entry>
6436
<entry>set last 3 bytes to zero</entry>
6437
<entry><literal>trunc(macaddr '12:34:56:78:90:ab')</literal></entry>
6438
<entry><literal>12:34:56:00:00:00</literal></entry>
6445
The <type>macaddr</type> type also supports the standard relational
6446
operators (<literal>></literal>, <literal><=</literal>, etc.) for
6447
lexicographical ordering.
6453
<sect1 id="functions-sequence">
6454
<title>Sequence Manipulation Functions</title>
6457
<primary>sequence</primary>
6460
<primary>nextval</primary>
6463
<primary>currval</primary>
6466
<primary>setval</primary>
6470
This section describes <productname>PostgreSQL</productname>'s functions
6471
for operating on <firstterm>sequence objects</firstterm>.
6472
Sequence objects (also called sequence generators or
6473
just sequences) are special single-row tables created with
6474
<command>CREATE SEQUENCE</command>. A sequence object is usually used to
6475
generate unique identifiers for rows of a table. The sequence functions,
6476
listed in <xref linkend="functions-sequence-table">,
6477
provide simple, multiuser-safe methods for obtaining successive
6478
sequence values from sequence objects.
6481
<table id="functions-sequence-table">
6482
<title>Sequence Functions</title>
6485
<row><entry>Function</entry> <entry>Return Type</entry> <entry>Description</entry></row>
6490
<entry><literal><function>nextval</function>(<type>text</type>)</literal></entry>
6491
<entry><type>bigint</type></entry>
6492
<entry>Advance sequence and return new value</entry>
6495
<entry><literal><function>currval</function>(<type>text</type>)</literal></entry>
6496
<entry><type>bigint</type></entry>
6497
<entry>Return value most recently obtained with <function>nextval</function></entry>
6500
<entry><literal><function>setval</function>(<type>text</type>, <type>bigint</type>)</literal></entry>
6501
<entry><type>bigint</type></entry>
6502
<entry>Set sequence's current value</entry>
6505
<entry><literal><function>setval</function>(<type>text</type>, <type>bigint</type>, <type>boolean</type>)</literal></entry>
6506
<entry><type>bigint</type></entry>
6507
<entry>Set sequence's current value and <literal>is_called</literal> flag</entry>
6514
For largely historical reasons, the sequence to be operated on by a
6515
sequence-function call is specified by a text-string argument. To
6516
achieve some compatibility with the handling of ordinary
6517
<acronym>SQL</acronym> names, the sequence functions convert their
6518
argument to lowercase unless the string is double-quoted. Thus
6520
nextval('foo') <lineannotation>operates on sequence <literal>foo</literal></>
6521
nextval('FOO') <lineannotation>operates on sequence <literal>foo</literal></>
6522
nextval('"Foo"') <lineannotation>operates on sequence <literal>Foo</literal></>
6524
The sequence name can be schema-qualified if necessary:
6526
nextval('myschema.foo') <lineannotation>operates on <literal>myschema.foo</literal></>
6527
nextval('"myschema".foo') <lineannotation>same as above</lineannotation>
6528
nextval('foo') <lineannotation>searches search path for <literal>foo</literal></>
6530
Of course, the text argument can be the result of an expression,
6531
not only a simple literal, which is occasionally useful.
6535
The available sequence functions are:
6539
<term><function>nextval</function></term>
6542
Advance the sequence object to its next value and return that
6543
value. This is done atomically: even if multiple sessions
6544
execute <function>nextval</function> concurrently, each will safely receive
6545
a distinct sequence value.
6551
<term><function>currval</function></term>
6554
Return the value most recently obtained by <function>nextval</function>
6555
for this sequence in the current session. (An error is
6556
reported if <function>nextval</function> has never been called for this
6557
sequence in this session.) Notice that because this is returning
6558
a session-local value, it gives a predictable answer whether or not
6559
other sessions have executed <function>nextval</function> since the
6560
current session did.
6566
<term><function>setval</function></term>
6569
Reset the sequence object's counter value. The two-parameter
6570
form sets the sequence's <literal>last_value</literal> field to the specified
6571
value and sets its <literal>is_called</literal> field to <literal>true</literal>,
6572
meaning that the next <function>nextval</function> will advance the sequence
6573
before returning a value. In the three-parameter form,
6574
<literal>is_called</literal> may be set either <literal>true</literal> or
6575
<literal>false</literal>. If it's set to <literal>false</literal>,
6576
the next <function>nextval</function> will return exactly the specified
6577
value, and sequence advancement commences with the following
6578
<function>nextval</function>. For example,
6581
SELECT setval('foo', 42); <lineannotation>Next <function>nextval</> will return 43</lineannotation>
6582
SELECT setval('foo', 42, true); <lineannotation>Same as above</lineannotation>
6583
SELECT setval('foo', 42, false); <lineannotation>Next <function>nextval</> will return 42</lineannotation>
6586
The result returned by <function>setval</function> is just the value of its
6596
To avoid blocking of concurrent transactions that obtain numbers from the
6597
same sequence, a <function>nextval</function> operation is never rolled back;
6598
that is, once a value has been fetched it is considered used, even if the
6599
transaction that did the <function>nextval</function> later aborts. This means
6600
that aborted transactions may leave unused <quote>holes</quote> in the
6601
sequence of assigned values. <function>setval</function> operations are never
6602
rolled back, either.
6607
If a sequence object has been created with default parameters,
6608
<function>nextval</function> calls on it will return successive values
6609
beginning with 1. Other behaviors can be obtained by using
6610
special parameters in the <xref linkend="SQL-CREATESEQUENCE"> command;
6611
see its command reference page for more information.
6617
<sect1 id="functions-conditional">
6618
<title>Conditional Expressions</title>
6621
<primary>CASE</primary>
6625
<primary>conditional expression</primary>
6629
This section describes the <acronym>SQL</acronym>-compliant conditional expressions
6630
available in <productname>PostgreSQL</productname>.
6635
If your needs go beyond the capabilities of these conditional
6636
expressions you might want to consider writing a stored procedure
6637
in a more expressive programming language.
6642
<title><literal>CASE</></title>
6645
The <acronym>SQL</acronym> <token>CASE</token> expression is a
6646
generic conditional expression, similar to if/else statements in
6650
CASE WHEN <replaceable>condition</replaceable> THEN <replaceable>result</replaceable>
6651
<optional>WHEN ...</optional>
6652
<optional>ELSE <replaceable>result</replaceable></optional>
6656
<token>CASE</token> clauses can be used wherever
6657
an expression is valid. <replaceable>condition</replaceable> is an
6658
expression that returns a <type>boolean</type> result. If the result is true
6659
then the value of the <token>CASE</token> expression is the
6660
<replaceable>result</replaceable> that follows the condition. If the result is false any
6661
subsequent <token>WHEN</token> clauses are searched in the same
6662
manner. If no <token>WHEN</token>
6663
<replaceable>condition</replaceable> is true then the value of the
6664
case expression is the <replaceable>result</replaceable> in the
6665
<token>ELSE</token> clause. If the <token>ELSE</token> clause is
6666
omitted and no condition matches, the result is null.
6682
CASE WHEN a=1 THEN 'one'
6697
The data types of all the <replaceable>result</replaceable>
6698
expressions must be convertible to a single output type.
6699
See <xref linkend="typeconv-union-case"> for more detail.
6703
The following <quote>simple</quote> <token>CASE</token> expression is a
6704
specialized variant of the general form above:
6707
CASE <replaceable>expression</replaceable>
6708
WHEN <replaceable>value</replaceable> THEN <replaceable>result</replaceable>
6709
<optional>WHEN ...</optional>
6710
<optional>ELSE <replaceable>result</replaceable></optional>
6715
<replaceable>expression</replaceable> is computed and compared to
6716
all the <replaceable>value</replaceable> specifications in the
6717
<token>WHEN</token> clauses until one is found that is equal. If
6718
no match is found, the <replaceable>result</replaceable> in the
6719
<token>ELSE</token> clause (or a null value) is returned. This is similar
6720
to the <function>switch</function> statement in C.
6724
The example above can be written using the simple
6725
<token>CASE</token> syntax:
6728
CASE a WHEN 1 THEN 'one'
6743
A <token>CASE</token> expression does not evaluate any subexpressions
6744
that are not needed to determine the result. For example, this is a
6745
possible way of avoiding a division-by-zero failure:
6747
SELECT ... WHERE CASE WHEN x <> 0 THEN y/x > 1.5 ELSE false END;
6753
<title><literal>COALESCE</></title>
6756
<primary>COALESCE</primary>
6760
<function>COALESCE</function>(<replaceable>value</replaceable> <optional>, ...</optional>)
6764
The <function>COALESCE</function> function returns the first of its
6765
arguments that is not null. Null is returned only if all arguments
6766
are null. This is often useful to substitute a
6767
default value for null values when data is retrieved for display,
6770
SELECT COALESCE(description, short_description, '(none)') ...
6775
Like a <token>CASE</token> expression, <function>COALESCE</function> will
6776
not evaluate arguments that are not needed to determine the result;
6777
that is, arguments to the right of the first non-null argument are
6783
<title><literal>NULLIF</></title>
6786
<primary>NULLIF</primary>
6790
<function>NULLIF</function>(<replaceable>value1</replaceable>, <replaceable>value2</replaceable>)
6794
The <function>NULLIF</function> function returns a null value if and only
6795
if <replaceable>value1</replaceable> and
6796
<replaceable>value2</replaceable> are equal. Otherwise it returns
6797
<replaceable>value1</replaceable>. This can be used to perform the
6798
inverse operation of the <function>COALESCE</function> example
6801
SELECT NULLIF(value, '(none)') ...
6810
<sect1 id="functions-array">
6811
<title>Array Functions and Operators</title>
6814
<xref linkend="array-operators-table"> shows the operators
6815
available for <type>array</type> types.
6818
<table id="array-operators-table">
6819
<title><type>array</type> Operators</title>
6823
<entry>Operator</entry>
6824
<entry>Description</entry>
6825
<entry>Example</entry>
6826
<entry>Result</entry>
6831
<entry> <literal>=</literal> </entry>
6832
<entry>equal</entry>
6833
<entry><literal>ARRAY[1.1,2.1,3.1]::int[] = ARRAY[1,2,3]</literal></entry>
6834
<entry><literal>t</literal></entry>
6838
<entry> <literal><></literal> </entry>
6839
<entry>not equal</entry>
6840
<entry><literal>ARRAY[1,2,3] <> ARRAY[1,2,4]</literal></entry>
6841
<entry><literal>t</literal></entry>
6845
<entry> <literal><</literal> </entry>
6846
<entry>less than</entry>
6847
<entry><literal>ARRAY[1,2,3] < ARRAY[1,2,4]</literal></entry>
6848
<entry><literal>t</literal></entry>
6852
<entry> <literal>></literal> </entry>
6853
<entry>greater than</entry>
6854
<entry><literal>ARRAY[1,4,3] > ARRAY[1,2,4]</literal></entry>
6855
<entry><literal>t</literal></entry>
6859
<entry> <literal><=</literal> </entry>
6860
<entry>less than or equal</entry>
6861
<entry><literal>ARRAY[1,2,3] <= ARRAY[1,2,3]</literal></entry>
6862
<entry><literal>t</literal></entry>
6866
<entry> <literal>>=</literal> </entry>
6867
<entry>greater than or equal</entry>
6868
<entry><literal>ARRAY[1,4,3] >= ARRAY[1,4,3]</literal></entry>
6869
<entry><literal>t</literal></entry>
6873
<entry> <literal>||</literal> </entry>
6874
<entry>array-to-array concatenation</entry>
6875
<entry><literal>ARRAY[1,2,3] || ARRAY[4,5,6]</literal></entry>
6876
<entry><literal>{1,2,3,4,5,6}</literal></entry>
6880
<entry> <literal>||</literal> </entry>
6881
<entry>array-to-array concatenation</entry>
6882
<entry><literal>ARRAY[1,2,3] || ARRAY[[4,5,6],[7,8,9]]</literal></entry>
6883
<entry><literal>{{1,2,3},{4,5,6},{7,8,9}}</literal></entry>
6887
<entry> <literal>||</literal> </entry>
6888
<entry>element-to-array concatenation</entry>
6889
<entry><literal>3 || ARRAY[4,5,6]</literal></entry>
6890
<entry><literal>{3,4,5,6}</literal></entry>
6894
<entry> <literal>||</literal> </entry>
6895
<entry>array-to-element concatenation</entry>
6896
<entry><literal>ARRAY[4,5,6] || 7</literal></entry>
6897
<entry><literal>{4,5,6,7}</literal></entry>
6904
See <xref linkend="arrays"> for more details about array operator
6909
<xref linkend="array-functions-table"> shows the functions
6910
available for use with array types. See <xref linkend="arrays">
6911
for more discussion and examples of the use of these functions.
6914
<table id="array-functions-table">
6915
<title><type>array</type> Functions</title>
6919
<entry>Function</entry>
6920
<entry>Return Type</entry>
6921
<entry>Description</entry>
6922
<entry>Example</entry>
6923
<entry>Result</entry>
6930
<function>array_cat</function>
6931
(<type>anyarray</type>, <type>anyarray</type>)
6934
<entry><type>anyarray</type></entry>
6935
<entry>concatenate two arrays</entry>
6936
<entry><literal>array_cat(ARRAY[1,2,3], ARRAY[4,5])</literal></entry>
6937
<entry><literal>{1,2,3,4,5}</literal></entry>
6942
<function>array_append</function>
6943
(<type>anyarray</type>, <type>anyelement</type>)
6946
<entry><type>anyarray</type></entry>
6947
<entry>append an element to the end of an array</entry>
6948
<entry><literal>array_append(ARRAY[1,2], 3)</literal></entry>
6949
<entry><literal>{1,2,3}</literal></entry>
6954
<function>array_prepend</function>
6955
(<type>anyelement</type>, <type>anyarray</type>)
6958
<entry><type>anyarray</type></entry>
6959
<entry>append an element to the beginning of an array</entry>
6960
<entry><literal>array_prepend(1, ARRAY[2,3])</literal></entry>
6961
<entry><literal>{1,2,3}</literal></entry>
6966
<function>array_dims</function>
6967
(<type>anyarray</type>)
6970
<entry><type>text</type></entry>
6971
<entry>returns a text representation of array's dimensions</entry>
6972
<entry><literal>array_dims(array[[1,2,3], [4,5,6]])</literal></entry>
6973
<entry><literal>[1:2][1:3]</literal></entry>
6978
<function>array_lower</function>
6979
(<type>anyarray</type>, <type>integer</type>)
6982
<entry><type>integer</type></entry>
6983
<entry>returns lower bound of the requested array dimension</entry>
6984
<entry><literal>array_lower(array_prepend(0, ARRAY[1,2,3]), 1)</literal></entry>
6985
<entry><literal>0</literal></entry>
6990
<function>array_upper</function>
6991
(<type>anyarray</type>, <type>integer</type>)
6994
<entry><type>integer</type></entry>
6995
<entry>returns upper bound of the requested array dimension</entry>
6996
<entry><literal>array_upper(ARRAY[1,2,3,4], 1)</literal></entry>
6997
<entry><literal>4</literal></entry>
7002
<function>array_to_string</function>
7003
(<type>anyarray</type>, <type>text</type>)
7006
<entry><type>text</type></entry>
7007
<entry>concatenates array elements using provided delimiter</entry>
7008
<entry><literal>array_to_string(array[1, 2, 3], '~^~')</literal></entry>
7009
<entry><literal>1~^~2~^~3</literal></entry>
7014
<function>string_to_array</function>
7015
(<type>text</type>, <type>text</type>)
7018
<entry><type>text[]</type></entry>
7019
<entry>splits string into array elements using provided delimiter</entry>
7020
<entry><literal>string_to_array( 'xx~^~yy~^~zz', '~^~')</literal></entry>
7021
<entry><literal>{xx,yy,zz}</literal></entry>
7028
<sect1 id="functions-aggregate">
7029
<title>Aggregate Functions</title>
7031
<indexterm zone="functions-aggregate">
7032
<primary>aggregate function</primary>
7033
<secondary>built-in</secondary>
7037
<firstterm>Aggregate functions</firstterm> compute a single result
7038
value from a set of input values. <xref
7039
linkend="functions-aggregate-table"> shows the built-in aggregate
7040
functions. The special syntax considerations for aggregate
7041
functions are explained in <xref linkend="syntax-aggregates">.
7042
Consult <xref linkend="tutorial-agg"> for additional introductory
7046
<table id="functions-aggregate-table">
7047
<title>Aggregate Functions</title>
7052
<entry>Function</entry>
7053
<entry>Argument Type</entry>
7054
<entry>Return Type</entry>
7055
<entry>Description</entry>
7063
<primary>average</primary>
7065
<function>avg(<replaceable class="parameter">expression</replaceable>)</function>
7068
<type>smallint</type>, <type>integer</type>,
7069
<type>bigint</type>, <type>real</type>, <type>double
7070
precision</type>, <type>numeric</type>, or <type>interval</type>
7073
<type>numeric</type> for any integer type argument,
7074
<type>double precision</type> for a floating-point argument,
7075
otherwise the same as the argument data type
7077
<entry>the average (arithmetic mean) of all input values</entry>
7083
<primary>bit_and</primary>
7085
<function>bit_and(<replaceable class="parameter">expression</replaceable>)</function>
7088
<type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
7092
same as argument data type
7094
<entry>the bitwise AND of all non-null input values, or null if none</entry>
7100
<primary>bit_or</primary>
7102
<function>bit_or(<replaceable class="parameter">expression</replaceable>)</function>
7105
<type>smallint</type>, <type>integer</type>, <type>bigint</type>, or
7109
same as argument data type
7111
<entry>the bitwise OR of all non-null input values, or null if none</entry>
7117
<primary>bool_and</primary>
7119
<function>bool_and(<replaceable class="parameter">expression</replaceable>)</function>
7127
<entry>true if all input values are true, otherwise false</entry>
7133
<primary>bool_or</primary>
7135
<function>bool_or(<replaceable class="parameter">expression</replaceable>)</function>
7143
<entry>true if at least one input value is true, otherwise false</entry>
7147
<entry><function>count(*)</function></entry>
7149
<entry><type>bigint</type></entry>
7150
<entry>number of input values</entry>
7154
<entry><function>count(<replaceable class="parameter">expression</replaceable>)</function></entry>
7156
<entry><type>bigint</type></entry>
7158
number of input values for which the value of <replaceable
7159
class="parameter">expression</replaceable> is not null
7166
<primary>every</primary>
7168
<function>every(<replaceable class="parameter">expression</replaceable>)</function>
7176
<entry>equivalent to <function>bool_and</function></entry>
7180
<entry><function>max(<replaceable class="parameter">expression</replaceable>)</function></entry>
7181
<entry>any numeric, string, or date/time type</entry>
7182
<entry>same as argument type</entry>
7184
maximum value of <replaceable
7185
class="parameter">expression</replaceable> across all input
7191
<entry><function>min(<replaceable class="parameter">expression</replaceable>)</function></entry>
7192
<entry>any numeric, string, or date/time type</entry>
7193
<entry>same as argument type</entry>
7195
minimum value of <replaceable
7196
class="parameter">expression</replaceable> across all input
7204
<primary>standard deviation</primary>
7206
<function>stddev(<replaceable class="parameter">expression</replaceable>)</function>
7209
<type>smallint</type>, <type>integer</type>,
7210
<type>bigint</type>, <type>real</type>, <type>double
7211
precision</type>, or <type>numeric</type>
7214
<type>double precision</type> for floating-point arguments,
7215
otherwise <type>numeric</type>
7217
<entry>sample standard deviation of the input values</entry>
7221
<entry><function>sum(<replaceable class="parameter">expression</replaceable>)</function></entry>
7223
<type>smallint</type>, <type>integer</type>,
7224
<type>bigint</type>, <type>real</type>, <type>double
7225
precision</type>, <type>numeric</type>, or
7226
<type>interval</type>
7229
<type>bigint</type> for <type>smallint</type> or
7230
<type>integer</type> arguments, <type>numeric</type> for
7231
<type>bigint</type> arguments, <type>double precision</type>
7232
for floating-point arguments, otherwise the same as the
7235
<entry>sum of <replaceable class="parameter">expression</replaceable> across all input values</entry>
7241
<primary>variance</primary>
7243
<function>variance</function>(<replaceable class="parameter">expression</replaceable>)
7246
<type>smallint</type>, <type>integer</type>,
7247
<type>bigint</type>, <type>real</type>, <type>double
7248
precision</type>, or <type>numeric</type>
7251
<type>double precision</type> for floating-point arguments,
7252
otherwise <type>numeric</type>
7254
<entry>sample variance of the input values (square of the sample standard deviation)</entry>
7262
It should be noted that except for <function>count</function>,
7263
these functions return a null value when no rows are selected. In
7264
particular, <function>sum</function> of no rows returns null, not
7265
zero as one might expect. The <function>coalesce</function> function may be
7266
used to substitute zero for null when necessary.
7271
<primary>ANY</primary>
7274
<primary>SOME</primary>
7277
Boolean aggregates <function>bool_and</function> and
7278
<function>bool_or</function> correspond to standard SQL aggregates
7279
<function>every</function> and <function>any</function> or
7280
<function>some</function>.
7281
As for <function>any</function> and <function>some</function>,
7282
it seems that there is an ambiguity built into the standard syntax:
7284
SELECT b1 = ANY((SELECT b2 FROM t2 ...)) FROM t1 ...;
7286
Here <function>ANY</function> can be considered both as leading
7287
to a subquery or as an aggregate if the select expression returns 1 row.
7288
Thus the standard name cannot be given to these aggregates.
7294
Users accustomed to working with other SQL database management
7295
systems may be surprised by the performance characteristics of
7296
certain aggregate functions in
7297
<productname>PostgreSQL</productname> when the aggregate is
7298
applied to the entire table (in other words, no
7299
<literal>WHERE</literal> clause is specified). In particular, a
7302
SELECT min(col) FROM sometable;
7304
will be executed by <productname>PostgreSQL</productname> using a
7305
sequential scan of the entire table. Other database systems may
7306
optimize queries of this form to use an index on the column, if
7307
one is available. Similarly, the aggregate functions
7308
<function>max()</function> and <function>count()</function> always
7309
require a sequential scan if applied to the entire table in
7310
<productname>PostgreSQL</productname>.
7314
<productname>PostgreSQL</productname> cannot easily implement this
7315
optimization because it also allows for user-defined aggregate
7316
queries. Since <function>min()</function>,
7317
<function>max()</function>, and <function>count()</function> are
7318
defined using a generic API for aggregate functions, there is no
7319
provision for special-casing the execution of these functions
7320
under certain circumstances.
7324
Fortunately, there is a simple workaround for
7325
<function>min()</function> and <function>max()</function>. The
7326
query shown below is equivalent to the query above, except that it
7327
can take advantage of a B-tree index if there is one present on
7328
the column in question.
7330
SELECT col FROM sometable ORDER BY col ASC LIMIT 1;
7332
A similar query (obtained by substituting <literal>DESC</literal>
7333
for <literal>ASC</literal> in the query above) can be used in the
7334
place of <function>max()</function>.
7338
Unfortunately, there is no similarly trivial query that can be
7339
used to improve the performance of <function>count()</function>
7340
when applied to the entire table.
7347
<sect1 id="functions-subquery">
7348
<title>Subquery Expressions</title>
7351
<primary>EXISTS</primary>
7355
<primary>IN</primary>
7359
<primary>NOT IN</primary>
7363
<primary>ANY</primary>
7367
<primary>ALL</primary>
7371
<primary>SOME</primary>
7375
<primary>subquery</primary>
7379
This section describes the <acronym>SQL</acronym>-compliant subquery
7380
expressions available in <productname>PostgreSQL</productname>.
7381
All of the expression forms documented in this section return
7382
Boolean (true/false) results.
7386
<title><literal>EXISTS</literal></title>
7389
EXISTS ( <replaceable>subquery</replaceable> )
7393
The argument of <token>EXISTS</token> is an arbitrary <command>SELECT</> statement,
7394
or <firstterm>subquery</firstterm>. The
7395
subquery is evaluated to determine whether it returns any rows.
7396
If it returns at least one row, the result of <token>EXISTS</token> is
7397
<quote>true</>; if the subquery returns no rows, the result of <token>EXISTS</token>
7402
The subquery can refer to variables from the surrounding query,
7403
which will act as constants during any one evaluation of the subquery.
7407
The subquery will generally only be executed far enough to determine
7408
whether at least one row is returned, not all the way to completion.
7409
It is unwise to write a subquery that has any side effects (such as
7410
calling sequence functions); whether the side effects occur or not
7411
may be difficult to predict.
7415
Since the result depends only on whether any rows are returned,
7416
and not on the contents of those rows, the output list of the
7417
subquery is normally uninteresting. A common coding convention is
7418
to write all <literal>EXISTS</> tests in the form
7419
<literal>EXISTS(SELECT 1 WHERE ...)</literal>. There are exceptions to
7420
this rule however, such as subqueries that use <token>INTERSECT</token>.
7424
This simple example is like an inner join on <literal>col2</>, but
7425
it produces at most one output row for each <literal>tab1</> row,
7426
even if there are multiple matching <literal>tab2</> rows:
7428
SELECT col1 FROM tab1
7429
WHERE EXISTS(SELECT 1 FROM tab2 WHERE col2 = tab1.col2);
7435
<title><literal>IN</literal></title>
7438
<replaceable>expression</replaceable> IN (<replaceable>subquery</replaceable>)
7442
The right-hand side is a parenthesized
7443
subquery, which must return exactly one column. The left-hand expression
7444
is evaluated and compared to each row of the subquery result.
7445
The result of <token>IN</token> is <quote>true</> if any equal subquery row is found.
7446
The result is <quote>false</> if no equal row is found (including the special
7447
case where the subquery returns no rows).
7451
Note that if the left-hand expression yields null, or if there are
7452
no equal right-hand values and at least one right-hand row yields
7453
null, the result of the <token>IN</token> construct will be null, not false.
7454
This is in accordance with SQL's normal rules for Boolean combinations
7459
As with <token>EXISTS</token>, it's unwise to assume that the subquery will
7460
be evaluated completely.
7464
<replaceable>row_constructor</replaceable> IN (<replaceable>subquery</replaceable>)
7468
The left-hand side of this form of <token>IN</token> is a row constructor,
7469
as described in <xref linkend="sql-syntax-row-constructors">.
7470
The right-hand side is a parenthesized
7471
subquery, which must return exactly as many columns as there are
7472
expressions in the left-hand row. The left-hand expressions are
7473
evaluated and compared row-wise to each row of the subquery result.
7474
The result of <token>IN</token> is <quote>true</> if any equal subquery row is found.
7475
The result is <quote>false</> if no equal row is found (including the special
7476
case where the subquery returns no rows).
7480
As usual, null values in the rows are combined per
7481
the normal rules of SQL Boolean expressions. Two rows are considered
7482
equal if all their corresponding members are non-null and equal; the rows
7483
are unequal if any corresponding members are non-null and unequal;
7484
otherwise the result of that row comparison is unknown (null).
7485
If all the row results are either unequal or null, with at least one null,
7486
then the result of <token>IN</token> is null.
7491
<title><literal>NOT IN</literal></title>
7494
<replaceable>expression</replaceable> NOT IN (<replaceable>subquery</replaceable>)
7498
The right-hand side is a parenthesized
7499
subquery, which must return exactly one column. The left-hand expression
7500
is evaluated and compared to each row of the subquery result.
7501
The result of <token>NOT IN</token> is <quote>true</> if only unequal subquery rows
7502
are found (including the special case where the subquery returns no rows).
7503
The result is <quote>false</> if any equal row is found.
7507
Note that if the left-hand expression yields null, or if there are
7508
no equal right-hand values and at least one right-hand row yields
7509
null, the result of the <token>NOT IN</token> construct will be null, not true.
7510
This is in accordance with SQL's normal rules for Boolean combinations
7515
As with <token>EXISTS</token>, it's unwise to assume that the subquery will
7516
be evaluated completely.
7520
<replaceable>row_constructor</replaceable> NOT IN (<replaceable>subquery</replaceable>)
7524
The left-hand side of this form of <token>NOT IN</token> is a row constructor,
7525
as described in <xref linkend="sql-syntax-row-constructors">.
7526
The right-hand side is a parenthesized
7527
subquery, which must return exactly as many columns as there are
7528
expressions in the left-hand row. The left-hand expressions are
7529
evaluated and compared row-wise to each row of the subquery result.
7530
The result of <token>NOT IN</token> is <quote>true</> if only unequal subquery rows
7531
are found (including the special case where the subquery returns no rows).
7532
The result is <quote>false</> if any equal row is found.
7536
As usual, null values in the rows are combined per
7537
the normal rules of SQL Boolean expressions. Two rows are considered
7538
equal if all their corresponding members are non-null and equal; the rows
7539
are unequal if any corresponding members are non-null and unequal;
7540
otherwise the result of that row comparison is unknown (null).
7541
If all the row results are either unequal or null, with at least one null,
7542
then the result of <token>NOT IN</token> is null.
7547
<title><literal>ANY</literal>/<literal>SOME</literal></title>
7550
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ANY (<replaceable>subquery</replaceable>)
7551
<replaceable>expression</replaceable> <replaceable>operator</replaceable> SOME (<replaceable>subquery</replaceable>)
7555
The right-hand side is a parenthesized
7556
subquery, which must return exactly one column. The left-hand expression
7557
is evaluated and compared to each row of the subquery result using the
7558
given <replaceable>operator</replaceable>, which must yield a Boolean
7560
The result of <token>ANY</token> is <quote>true</> if any true result is obtained.
7561
The result is <quote>false</> if no true result is found (including the special
7562
case where the subquery returns no rows).
7566
<token>SOME</token> is a synonym for <token>ANY</token>.
7567
<token>IN</token> is equivalent to <literal>= ANY</literal>.
7571
Note that if there are no successes and at least one right-hand row yields
7572
null for the operator's result, the result of the <token>ANY</token> construct
7573
will be null, not false.
7574
This is in accordance with SQL's normal rules for Boolean combinations
7579
As with <token>EXISTS</token>, it's unwise to assume that the subquery will
7580
be evaluated completely.
7584
<replaceable>row_constructor</replaceable> <replaceable>operator</> ANY (<replaceable>subquery</replaceable>)
7585
<replaceable>row_constructor</replaceable> <replaceable>operator</> SOME (<replaceable>subquery</replaceable>)
7589
The left-hand side of this form of <token>ANY</token> is a row constructor,
7590
as described in <xref linkend="sql-syntax-row-constructors">.
7591
The right-hand side is a parenthesized
7592
subquery, which must return exactly as many columns as there are
7593
expressions in the left-hand row. The left-hand expressions are
7594
evaluated and compared row-wise to each row of the subquery result,
7595
using the given <replaceable>operator</replaceable>. Presently,
7596
only <literal>=</literal> and <literal><></literal> operators are allowed
7597
in row-wise <token>ANY</token> constructs.
7598
The result of <token>ANY</token> is <quote>true</> if any equal or unequal row is
7599
found, respectively.
7600
The result is <quote>false</> if no such row is found (including the special
7601
case where the subquery returns no rows).
7605
As usual, null values in the rows are combined per
7606
the normal rules of SQL Boolean expressions. Two rows are considered
7607
equal if all their corresponding members are non-null and equal; the rows
7608
are unequal if any corresponding members are non-null and unequal;
7609
otherwise the result of that row comparison is unknown (null).
7610
If there is at least one null row result, then the result of <token>ANY</token>
7611
cannot be false; it will be true or null.
7616
<title><literal>ALL</literal></title>
7619
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
7623
The right-hand side is a parenthesized
7624
subquery, which must return exactly one column. The left-hand expression
7625
is evaluated and compared to each row of the subquery result using the
7626
given <replaceable>operator</replaceable>, which must yield a Boolean
7628
The result of <token>ALL</token> is <quote>true</> if all rows yield true
7629
(including the special case where the subquery returns no rows).
7630
The result is <quote>false</> if any false result is found.
7634
<token>NOT IN</token> is equivalent to <literal><> ALL</literal>.
7638
Note that if there are no failures but at least one right-hand row yields
7639
null for the operator's result, the result of the <token>ALL</token> construct
7640
will be null, not true.
7641
This is in accordance with SQL's normal rules for Boolean combinations
7646
As with <token>EXISTS</token>, it's unwise to assume that the subquery will
7647
be evaluated completely.
7651
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>subquery</replaceable>)
7655
The left-hand side of this form of <token>ALL</token> is a row constructor,
7656
as described in <xref linkend="sql-syntax-row-constructors">.
7657
The right-hand side is a parenthesized
7658
subquery, which must return exactly as many columns as there are
7659
expressions in the left-hand row. The left-hand expressions are
7660
evaluated and compared row-wise to each row of the subquery result,
7661
using the given <replaceable>operator</replaceable>. Presently,
7662
only <literal>=</literal> and <literal><></literal> operators are allowed
7663
in row-wise <token>ALL</token> queries.
7664
The result of <token>ALL</token> is <quote>true</> if all subquery rows are equal
7665
or unequal, respectively (including the special
7666
case where the subquery returns no rows).
7667
The result is <quote>false</> if any row is found to be unequal or equal,
7672
As usual, null values in the rows are combined per
7673
the normal rules of SQL Boolean expressions. Two rows are considered
7674
equal if all their corresponding members are non-null and equal; the rows
7675
are unequal if any corresponding members are non-null and unequal;
7676
otherwise the result of that row comparison is unknown (null).
7677
If there is at least one null row result, then the result of <token>ALL</token>
7678
cannot be true; it will be false or null.
7683
<title>Row-wise Comparison</title>
7685
<indexterm zone="functions-subquery">
7686
<primary>comparison</primary>
7687
<secondary>subquery result row</secondary>
7691
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> (<replaceable>subquery</replaceable>)
7695
The left-hand side is a row constructor,
7696
as described in <xref linkend="sql-syntax-row-constructors">.
7697
The right-hand side is a parenthesized subquery, which must return exactly
7698
as many columns as there are expressions in the left-hand row. Furthermore,
7699
the subquery cannot return more than one row. (If it returns zero rows,
7700
the result is taken to be null.) The left-hand side is evaluated and
7701
compared row-wise to the single subquery result row.
7702
Presently, only <literal>=</literal> and <literal><></literal> operators are allowed
7703
in row-wise comparisons.
7704
The result is <quote>true</> if the two rows are equal or unequal, respectively.
7708
As usual, null values in the rows are combined per
7709
the normal rules of SQL Boolean expressions. Two rows are considered
7710
equal if all their corresponding members are non-null and equal; the rows
7711
are unequal if any corresponding members are non-null and unequal;
7712
otherwise the result of the row comparison is unknown (null).
7718
<sect1 id="functions-comparisons">
7719
<title>Row and Array Comparisons</title>
7722
<primary>IN</primary>
7726
<primary>NOT IN</primary>
7730
<primary>ANY</primary>
7734
<primary>ALL</primary>
7738
<primary>SOME</primary>
7742
<primary>comparison</primary>
7743
<secondary>row-wise</secondary>
7747
<primary>IS DISTINCT FROM</primary>
7751
<primary>IS NULL</primary>
7755
<primary>IS NOT NULL</primary>
7759
This section describes several specialized constructs for making
7760
multiple comparisons between groups of values. These forms are
7761
syntactically related to the subquery forms of the previous section,
7762
but do not involve subqueries.
7763
The forms involving array subexpressions are
7764
<productname>PostgreSQL</productname> extensions; the rest are
7765
<acronym>SQL</acronym>-compliant.
7766
All of the expression forms documented in this section return
7767
Boolean (true/false) results.
7771
<title><literal>IN</literal></title>
7774
<replaceable>expression</replaceable> IN (<replaceable>value</replaceable><optional>, ...</optional>)
7778
The right-hand side is a parenthesized list
7779
of scalar expressions. The result is <quote>true</> if the left-hand expression's
7780
result is equal to any of the right-hand expressions. This is a shorthand
7784
<replaceable>expression</replaceable> = <replaceable>value1</replaceable>
7786
<replaceable>expression</replaceable> = <replaceable>value2</replaceable>
7793
Note that if the left-hand expression yields null, or if there are
7794
no equal right-hand values and at least one right-hand expression yields
7795
null, the result of the <token>IN</token> construct will be null, not false.
7796
This is in accordance with SQL's normal rules for Boolean combinations
7802
<title><literal>NOT IN</literal></title>
7805
<replaceable>expression</replaceable> NOT IN (<replaceable>value</replaceable><optional>, ...</optional>)
7809
The right-hand side is a parenthesized list
7810
of scalar expressions. The result is <quote>true</quote> if the left-hand expression's
7811
result is unequal to all of the right-hand expressions. This is a shorthand
7815
<replaceable>expression</replaceable> <> <replaceable>value1</replaceable>
7817
<replaceable>expression</replaceable> <> <replaceable>value2</replaceable>
7824
Note that if the left-hand expression yields null, or if there are
7825
no equal right-hand values and at least one right-hand expression yields
7826
null, the result of the <token>NOT IN</token> construct will be null, not true
7827
as one might naively expect.
7828
This is in accordance with SQL's normal rules for Boolean combinations
7834
<literal>x NOT IN y</literal> is equivalent to <literal>NOT (x IN y)</literal> in all
7835
cases. However, null values are much more likely to trip up the novice when
7836
working with <token>NOT IN</token> than when working with <token>IN</token>.
7837
It's best to express your condition positively if possible.
7843
<title><literal>ANY</literal>/<literal>SOME</literal> (array)</title>
7846
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ANY (<replaceable>array expression</replaceable>)
7847
<replaceable>expression</replaceable> <replaceable>operator</replaceable> SOME (<replaceable>array expression</replaceable>)
7851
The right-hand side is a parenthesized expression, which must yield an
7853
The left-hand expression
7854
is evaluated and compared to each element of the array using the
7855
given <replaceable>operator</replaceable>, which must yield a Boolean
7857
The result of <token>ANY</token> is <quote>true</> if any true result is obtained.
7858
The result is <quote>false</> if no true result is found (including the special
7859
case where the array has zero elements).
7863
<token>SOME</token> is a synonym for <token>ANY</token>.
7868
<title><literal>ALL</literal> (array)</title>
7871
<replaceable>expression</replaceable> <replaceable>operator</replaceable> ALL (<replaceable>array expression</replaceable>)
7875
The right-hand side is a parenthesized expression, which must yield an
7877
The left-hand expression
7878
is evaluated and compared to each element of the array using the
7879
given <replaceable>operator</replaceable>, which must yield a Boolean
7881
The result of <token>ALL</token> is <quote>true</> if all comparisons yield true
7882
(including the special case where the array has zero elements).
7883
The result is <quote>false</> if any false result is found.
7888
<title>Row-wise Comparison</title>
7891
<replaceable>row_constructor</replaceable> <replaceable>operator</replaceable> <replaceable>row_constructor</replaceable>
7895
Each side is a row constructor,
7896
as described in <xref linkend="sql-syntax-row-constructors">.
7897
The two row values must have the same number of fields.
7898
Each side is evaluated and they are compared row-wise.
7899
Presently, only <literal>=</literal> and <literal><></literal> operators are allowed
7900
in row-wise comparisons.
7901
The result is <quote>true</> if the two rows are equal or unequal, respectively.
7905
As usual, null values in the rows are combined per
7906
the normal rules of SQL Boolean expressions. Two rows are considered
7907
equal if all their corresponding members are non-null and equal; the rows
7908
are unequal if any corresponding members are non-null and unequal;
7909
otherwise the result of the row comparison is unknown (null).
7913
<replaceable>row_constructor</replaceable> IS DISTINCT FROM <replaceable>row_constructor</replaceable>
7917
This construct is similar to a <literal><></literal> row comparison,
7918
but it does not yield null for null inputs. Instead, any null value is
7919
considered unequal to (distinct from) any non-null value, and any two
7920
nulls are considered equal (not distinct). Thus the result will always
7921
be either true or false, never null.
7925
<replaceable>row_constructor</replaceable> IS NULL
7926
<replaceable>row_constructor</replaceable> IS NOT NULL
7930
These constructs test a row value for null or not null. A row value
7931
is considered not null if it has at least one field that is not null.
7937
<sect1 id="functions-srf">
7938
<title>Set Returning Functions</title>
7940
<indexterm zone="functions-srf">
7941
<primary>set returning functions</primary>
7942
<secondary>functions</secondary>
7946
This section describes functions that possibly return more than one row.
7947
Currently the only functions in this class are series generating functions,
7948
as detailed in <xref linkend="functions-srf-series">.
7951
<table id="functions-srf-series">
7952
<title>Series Generating Functions</title>
7956
<entry>Function</entry>
7957
<entry>Argument Type</entry>
7958
<entry>Return Type</entry>
7959
<entry>Description</entry>
7965
<entry><literal><function>generate_series</function>(<parameter>start</parameter>, <parameter>stop</parameter>)</literal></entry>
7966
<entry><type>int</type> or <type>bigint</type></entry>
7967
<entry><type>setof int</type> or <type>setof bigint</type> (same as argument type)</entry>
7969
Generate a series of values, from <parameter>start</parameter> to <parameter>stop</parameter>
7970
with a step size of one.
7975
<entry><literal><function>generate_series</function>(<parameter>start</parameter>, <parameter>stop</parameter>, <parameter>step</parameter>)</literal></entry>
7976
<entry><type>int</type> or <type>bigint</type></entry>
7977
<entry><type>setof int</type> or <type>setof bigint</type> (same as argument type)</entry>
7979
Generate a series of values, from <parameter>start</parameter> to <parameter>stop</parameter>
7980
with a step size of <parameter>step</parameter>.
7989
When <parameter>step</parameter> is positive, zero rows are returned if
7990
<parameter>start</parameter> is greater than <parameter>stop</parameter>.
7991
Conversely, when <parameter>step</parameter> is negative, zero rows are
7992
returned if <parameter>start</parameter> is less than <parameter>stop</parameter>.
7993
Zero rows are also returned for <literal>NULL</literal> inputs. It is an error
7994
for <parameter>step</parameter> to be zero. Some examples follow:
7996
select * from generate_series(2,4);
8004
select * from generate_series(5,1,-2);
8012
select * from generate_series(4,3);
8017
select current_date + s.a as dates from generate_series(0,14,7) as s(a);
8028
<sect1 id="functions-info">
8029
<title>System Information Functions</title>
8032
<xref linkend="functions-info-session-table"> shows several
8033
functions that extract session and system information.
8036
<table id="functions-info-session-table">
8037
<title>Session Information Functions</title>
8040
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8045
<entry><function>current_database()</function></entry>
8046
<entry><type>name</type></entry>
8047
<entry>name of current database</entry>
8051
<entry><function>current_schema()</function></entry>
8052
<entry><type>name</type></entry>
8053
<entry>name of current schema</entry>
8057
<entry><function>current_schemas(boolean)</function></entry>
8058
<entry><type>name[]</type></entry>
8059
<entry>names of schemas in search path optionally including implicit schemas</entry>
8063
<entry><function>current_user</function></entry>
8064
<entry><type>name</type></entry>
8065
<entry>user name of current execution context</entry>
8069
<entry><function>inet_client_addr()</function></entry>
8070
<entry><type>inet</type></entry>
8071
<entry>address of the remote connection</entry>
8075
<entry><function>inet_client_port()</function></entry>
8076
<entry><type>int4</type></entry>
8077
<entry>port of the remote connection</entry>
8081
<entry><function>inet_server_addr()</function></entry>
8082
<entry><type>inet</type></entry>
8083
<entry>address of the local connection</entry>
8087
<entry><function>inet_server_port()</function></entry>
8088
<entry><type>int4</type></entry>
8089
<entry>port of the local connection</entry>
8093
<entry><function>session_user</function></entry>
8094
<entry><type>name</type></entry>
8095
<entry>session user name</entry>
8099
<entry><function>user</function></entry>
8100
<entry><type>name</type></entry>
8101
<entry>equivalent to <function>current_user</function></entry>
8105
<entry><function>version()</function></entry>
8106
<entry><type>text</type></entry>
8107
<entry>PostgreSQL version information</entry>
8113
<indexterm zone="functions-info">
8114
<primary>user</primary>
8115
<secondary>current</secondary>
8118
<indexterm zone="functions-info">
8119
<primary>schema</primary>
8120
<secondary>current</secondary>
8123
<indexterm zone="functions-info">
8124
<primary>search path</primary>
8125
<secondary>current</secondary>
8129
The <function>session_user</function> is normally the user who initiated
8130
the current database connection; but superusers can change this setting
8131
with <xref linkend="sql-set-session-authorization">.
8132
The <function>current_user</function> is the user identifier
8133
that is applicable for permission checking. Normally, it is equal
8134
to the session user, but it changes during the execution of
8135
functions with the attribute <literal>SECURITY DEFINER</literal>.
8136
In Unix parlance, the session user is the <quote>real user</quote> and
8137
the current user is the <quote>effective user</quote>.
8142
<function>current_user</function>, <function>session_user</function>, and
8143
<function>user</function> have special syntactic status in <acronym>SQL</acronym>:
8144
they must be called without trailing parentheses.
8149
<function>current_schema</function> returns the name of the schema that is
8150
at the front of the search path (or a null value if the search path is
8151
empty). This is the schema that will be used for any tables or
8152
other named objects that are created without specifying a target schema.
8153
<function>current_schemas(boolean)</function> returns an array of the names of all
8154
schemas presently in the search path. The Boolean option determines whether or not
8155
implicitly included system schemas such as <literal>pg_catalog</> are included in the search
8161
The search path may be altered at run time. The command is:
8163
SET search_path TO <replaceable>schema</> <optional>, <replaceable>schema</>, ...</optional>
8168
<indexterm zone="functions-info">
8169
<primary>inet_client_addr</primary>
8172
<indexterm zone="functions-info">
8173
<primary>inet_client_port</primary>
8176
<indexterm zone="functions-info">
8177
<primary>inet_server_addr</primary>
8180
<indexterm zone="functions-info">
8181
<primary>inet_server_port</primary>
8185
<function>inet_client_addr</function> returns the IP address of the
8186
current client, and <function>inet_client_port</function> returns the
8188
<function>inet_server_addr</function> returns the IP address on which
8189
the server accepted the current connection, and
8190
<function>inet_server_port</function> returns the port number.
8191
All these functions return NULL if the current connection is via a
8195
<indexterm zone="functions-info">
8196
<primary>version</primary>
8200
<function>version()</function> returns a string describing the
8201
<productname>PostgreSQL</productname> server's version.
8205
<primary>privilege</primary>
8206
<secondary>querying</secondary>
8210
<xref linkend="functions-info-access-table"> lists functions that
8211
allow the user to query object access privileges programmatically.
8212
See <xref linkend="ddl-priv"> for more information about
8216
<table id="functions-info-access-table">
8217
<title>Access Privilege Inquiry Functions</title>
8220
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8225
<entry><literal><function>has_table_privilege</function>(<parameter>user</parameter>,
8226
<parameter>table</parameter>,
8227
<parameter>privilege</parameter>)</literal>
8229
<entry><type>boolean</type></entry>
8230
<entry>does user have privilege for table</entry>
8233
<entry><literal><function>has_table_privilege</function>(<parameter>table</parameter>,
8234
<parameter>privilege</parameter>)</literal>
8236
<entry><type>boolean</type></entry>
8237
<entry>does current user have privilege for table</entry>
8240
<entry><literal><function>has_database_privilege</function>(<parameter>user</parameter>,
8241
<parameter>database</parameter>,
8242
<parameter>privilege</parameter>)</literal>
8244
<entry><type>boolean</type></entry>
8245
<entry>does user have privilege for database</entry>
8248
<entry><literal><function>has_database_privilege</function>(<parameter>database</parameter>,
8249
<parameter>privilege</parameter>)</literal>
8251
<entry><type>boolean</type></entry>
8252
<entry>does current user have privilege for database</entry>
8255
<entry><literal><function>has_function_privilege</function>(<parameter>user</parameter>,
8256
<parameter>function</parameter>,
8257
<parameter>privilege</parameter>)</literal>
8259
<entry><type>boolean</type></entry>
8260
<entry>does user have privilege for function</entry>
8263
<entry><literal><function>has_function_privilege</function>(<parameter>function</parameter>,
8264
<parameter>privilege</parameter>)</literal>
8266
<entry><type>boolean</type></entry>
8267
<entry>does current user have privilege for function</entry>
8270
<entry><literal><function>has_language_privilege</function>(<parameter>user</parameter>,
8271
<parameter>language</parameter>,
8272
<parameter>privilege</parameter>)</literal>
8274
<entry><type>boolean</type></entry>
8275
<entry>does user have privilege for language</entry>
8278
<entry><literal><function>has_language_privilege</function>(<parameter>language</parameter>,
8279
<parameter>privilege</parameter>)</literal>
8281
<entry><type>boolean</type></entry>
8282
<entry>does current user have privilege for language</entry>
8285
<entry><literal><function>has_schema_privilege</function>(<parameter>user</parameter>,
8286
<parameter>schema</parameter>,
8287
<parameter>privilege</parameter>)</literal>
8289
<entry><type>boolean</type></entry>
8290
<entry>does user have privilege for schema</entry>
8293
<entry><literal><function>has_schema_privilege</function>(<parameter>schema</parameter>,
8294
<parameter>privilege</parameter>)</literal>
8296
<entry><type>boolean</type></entry>
8297
<entry>does current user have privilege for schema</entry>
8300
<entry><literal><function>has_tablespace_privilege</function>(<parameter>user</parameter>,
8301
<parameter>tablespace</parameter>,
8302
<parameter>privilege</parameter>)</literal>
8304
<entry><type>boolean</type></entry>
8305
<entry>does user have privilege for tablespace</entry>
8308
<entry><literal><function>has_tablespace_privilege</function>(<parameter>tablespace</parameter>,
8309
<parameter>privilege</parameter>)</literal>
8311
<entry><type>boolean</type></entry>
8312
<entry>does current user have privilege for tablespace</entry>
8318
<indexterm zone="functions-info">
8319
<primary>has_table_privilege</primary>
8321
<indexterm zone="functions-info">
8322
<primary>has_database_privilege</primary>
8324
<indexterm zone="functions-info">
8325
<primary>has_function_privilege</primary>
8327
<indexterm zone="functions-info">
8328
<primary>has_language_privilege</primary>
8330
<indexterm zone="functions-info">
8331
<primary>has_schema_privilege</primary>
8333
<indexterm zone="functions-info">
8334
<primary>has_tablespace_privilege</primary>
8338
<function>has_table_privilege</function> checks whether a user
8339
can access a table in a particular way. The user can be
8340
specified by name or by ID
8341
(<literal>pg_user.usesysid</literal>), or if the argument is
8343
<function>current_user</function> is assumed. The table can be specified
8344
by name or by OID. (Thus, there are actually six variants of
8345
<function>has_table_privilege</function>, which can be distinguished by
8346
the number and types of their arguments.) When specifying by name,
8347
the name can be schema-qualified if necessary.
8348
The desired access privilege type
8349
is specified by a text string, which must evaluate to one of the
8350
values <literal>SELECT</literal>, <literal>INSERT</literal>, <literal>UPDATE</literal>,
8351
<literal>DELETE</literal>, <literal>RULE</literal>, <literal>REFERENCES</literal>, or
8352
<literal>TRIGGER</literal>. (Case of the string is not significant, however.)
8355
SELECT has_table_privilege('myschema.mytable', 'select');
8360
<function>has_database_privilege</function> checks whether a user
8361
can access a database in a particular way. The possibilities for its
8362
arguments are analogous to <function>has_table_privilege</function>.
8363
The desired access privilege type must evaluate to
8364
<literal>CREATE</literal>,
8365
<literal>TEMPORARY</literal>, or
8366
<literal>TEMP</literal> (which is equivalent to
8367
<literal>TEMPORARY</literal>).
8371
<function>has_function_privilege</function> checks whether a user
8372
can access a function in a particular way. The possibilities for its
8373
arguments are analogous to <function>has_table_privilege</function>.
8374
When specifying a function by a text string rather than by OID,
8375
the allowed input is the same as for the <type>regprocedure</> data type
8376
(see <xref linkend="datatype-oid">).
8377
The desired access privilege type must evaluate to
8378
<literal>EXECUTE</literal>.
8381
SELECT has_function_privilege('joeuser', 'myfunc(int, text)', 'execute');
8386
<function>has_language_privilege</function> checks whether a user
8387
can access a procedural language in a particular way. The possibilities
8388
for its arguments are analogous to <function>has_table_privilege</function>.
8389
The desired access privilege type must evaluate to
8390
<literal>USAGE</literal>.
8394
<function>has_schema_privilege</function> checks whether a user
8395
can access a schema in a particular way. The possibilities for its
8396
arguments are analogous to <function>has_table_privilege</function>.
8397
The desired access privilege type must evaluate to
8398
<literal>CREATE</literal> or
8399
<literal>USAGE</literal>.
8403
<function>has_tablespace_privilege</function> checks whether a user
8404
can access a tablespace in a particular way. The possibilities for its
8405
arguments are analogous to <function>has_table_privilege</function>.
8406
The desired access privilege type must evaluate to
8407
<literal>CREATE</literal>.
8411
To test whether a user holds a grant option on the privilege,
8412
append <literal> WITH GRANT OPTION</literal> to the privilege key
8413
word; for example <literal>'UPDATE WITH GRANT OPTION'</literal>.
8417
<xref linkend="functions-info-schema-table"> shows functions that
8418
determine whether a certain object is <firstterm>visible</> in the
8419
current schema search path. A table is said to be visible if its
8420
containing schema is in the search path and no table of the same
8421
name appears earlier in the search path. This is equivalent to the
8422
statement that the table can be referenced by name without explicit
8423
schema qualification. For example, to list the names of all
8426
SELECT relname FROM pg_class WHERE pg_table_is_visible(oid);
8430
<table id="functions-info-schema-table">
8431
<title>Schema Visibility Inquiry Functions</title>
8434
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8439
<entry><literal><function>pg_table_is_visible</function>(<parameter>table_oid</parameter>)</literal>
8441
<entry><type>boolean</type></entry>
8442
<entry>is table visible in search path</entry>
8445
<entry><literal><function>pg_type_is_visible</function>(<parameter>type_oid</parameter>)</literal>
8447
<entry><type>boolean</type></entry>
8448
<entry>is type (or domain) visible in search path</entry>
8451
<entry><literal><function>pg_function_is_visible</function>(<parameter>function_oid</parameter>)</literal>
8453
<entry><type>boolean</type></entry>
8454
<entry>is function visible in search path</entry>
8457
<entry><literal><function>pg_operator_is_visible</function>(<parameter>operator_oid</parameter>)</literal>
8459
<entry><type>boolean</type></entry>
8460
<entry>is operator visible in search path</entry>
8463
<entry><literal><function>pg_opclass_is_visible</function>(<parameter>opclass_oid</parameter>)</literal>
8465
<entry><type>boolean</type></entry>
8466
<entry>is operator class visible in search path</entry>
8469
<entry><literal><function>pg_conversion_is_visible</function>(<parameter>conversion_oid</parameter>)</literal>
8471
<entry><type>boolean</type></entry>
8472
<entry>is conversion visible in search path</entry>
8478
<indexterm zone="functions-info">
8479
<primary>pg_table_is_visible</primary>
8481
<indexterm zone="functions-info">
8482
<primary>pg_type_is_visible</primary>
8484
<indexterm zone="functions-info">
8485
<primary>pg_function_is_visible</primary>
8487
<indexterm zone="functions-info">
8488
<primary>pg_operator_is_visible</primary>
8490
<indexterm zone="functions-info">
8491
<primary>pg_opclass_is_visible</primary>
8493
<indexterm zone="functions-info">
8494
<primary>pg_conversion_is_visible</primary>
8498
<function>pg_table_is_visible</function> performs the check for
8499
tables (or views, or any other kind of <literal>pg_class</> entry).
8500
<function>pg_type_is_visible</function>,
8501
<function>pg_function_is_visible</function>,
8502
<function>pg_operator_is_visible</function>,
8503
<function>pg_opclass_is_visible</function>, and
8504
<function>pg_conversion_is_visible</function> perform the same sort of
8505
visibility check for types (and domains), functions, operators, operator classes
8506
and conversions, respectively. For functions and operators, an object in
8507
the search path is visible if there is no object of the same name
8508
<emphasis>and argument data type(s)</> earlier in the path. For
8509
operator classes, both name and associated index access method are
8514
All these functions require object OIDs to identify the object to be
8515
checked. If you want to test an object by name, it is convenient to use
8516
the OID alias types (<type>regclass</>, <type>regtype</>,
8517
<type>regprocedure</>, or <type>regoperator</>), for example
8519
SELECT pg_type_is_visible('myschema.widget'::regtype);
8521
Note that it would not make much sense to test an unqualified name in
8522
this way — if the name can be recognized at all, it must be visible.
8525
<indexterm zone="functions-info">
8526
<primary>format_type</primary>
8529
<indexterm zone="functions-info">
8530
<primary>pg_get_viewdef</primary>
8533
<indexterm zone="functions-info">
8534
<primary>pg_get_ruledef</primary>
8537
<indexterm zone="functions-info">
8538
<primary>pg_get_indexdef</primary>
8541
<indexterm zone="functions-info">
8542
<primary>pg_get_triggerdef</primary>
8545
<indexterm zone="functions-info">
8546
<primary>pg_get_constraintdef</primary>
8549
<indexterm zone="functions-info">
8550
<primary>pg_get_expr</primary>
8553
<indexterm zone="functions-info">
8554
<primary>pg_get_userbyid</primary>
8557
<indexterm zone="functions-info">
8558
<primary>pg_get_serial_sequence</primary>
8561
<indexterm zone="functions-info">
8562
<primary>pg_tablespace_databases</primary>
8566
<xref linkend="functions-info-catalog-table"> lists functions that
8567
extract information from the system catalogs.
8570
<table id="functions-info-catalog-table">
8571
<title>System Catalog Information Functions</title>
8574
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8579
<entry><literal><function>format_type</function>(<parameter>type_oid</parameter>, <parameter>typemod</>)</literal></entry>
8580
<entry><type>text</type></entry>
8581
<entry>get SQL name of a data type</entry>
8584
<entry><literal><function>pg_get_viewdef</function>(<parameter>view_name</parameter>)</literal></entry>
8585
<entry><type>text</type></entry>
8586
<entry>get <command>CREATE VIEW</> command for view (<emphasis>deprecated</emphasis>)</entry>
8589
<entry><literal><function>pg_get_viewdef</function>(<parameter>view_name</parameter>, <parameter>pretty_bool</>)</literal></entry>
8590
<entry><type>text</type></entry>
8591
<entry>get <command>CREATE VIEW</> command for view (<emphasis>deprecated</emphasis>)</entry>
8594
<entry><literal><function>pg_get_viewdef</function>(<parameter>view_oid</parameter>)</literal></entry>
8595
<entry><type>text</type></entry>
8596
<entry>get <command>CREATE VIEW</> command for view</entry>
8599
<entry><literal><function>pg_get_viewdef</function>(<parameter>view_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
8600
<entry><type>text</type></entry>
8601
<entry>get <command>CREATE VIEW</> command for view</entry>
8604
<entry><literal><function>pg_get_ruledef</function>(<parameter>rule_oid</parameter>)</literal></entry>
8605
<entry><type>text</type></entry>
8606
<entry>get <command>CREATE RULE</> command for rule</entry>
8609
<entry><literal><function>pg_get_ruledef</function>(<parameter>rule_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
8610
<entry><type>text</type></entry>
8611
<entry>get <command>CREATE RULE</> command for rule</entry>
8614
<entry><literal><function>pg_get_indexdef</function>(<parameter>index_oid</parameter>)</literal></entry>
8615
<entry><type>text</type></entry>
8616
<entry>get <command>CREATE INDEX</> command for index</entry>
8619
<entry><literal><function>pg_get_indexdef</function>(<parameter>index_oid</parameter>, <parameter>column_no</>, <parameter>pretty_bool</>)</literal></entry>
8620
<entry><type>text</type></entry>
8621
<entry>get <command>CREATE INDEX</> command for index,
8622
or definition of just one index column when
8623
<parameter>column_no</> is not zero</entry>
8626
<entry><function>pg_get_triggerdef</function>(<parameter>trigger_oid</parameter>)</entry>
8627
<entry><type>text</type></entry>
8628
<entry>get <command>CREATE [ CONSTRAINT ] TRIGGER</> command for trigger</entry>
8631
<entry><literal><function>pg_get_constraintdef</function>(<parameter>constraint_oid</parameter>)</literal></entry>
8632
<entry><type>text</type></entry>
8633
<entry>get definition of a constraint</entry>
8636
<entry><literal><function>pg_get_constraintdef</function>(<parameter>constraint_oid</parameter>, <parameter>pretty_bool</>)</literal></entry>
8637
<entry><type>text</type></entry>
8638
<entry>get definition of a constraint</entry>
8641
<entry><literal><function>pg_get_expr</function>(<parameter>expr_text</parameter>, <parameter>relation_oid</>)</literal></entry>
8642
<entry><type>text</type></entry>
8643
<entry>decompile internal form of an expression, assuming that any Vars
8644
in it refer to the relation indicated by the second parameter</entry>
8647
<entry><literal><function>pg_get_expr</function>(<parameter>expr_text</parameter>, <parameter>relation_oid</>, <parameter>pretty_bool</>)</literal></entry>
8648
<entry><type>text</type></entry>
8649
<entry>decompile internal form of an expression, assuming that any Vars
8650
in it refer to the relation indicated by the second parameter</entry>
8653
<entry><literal><function>pg_get_userbyid</function>(<parameter>userid</parameter>)</literal></entry>
8654
<entry><type>name</type></entry>
8655
<entry>get user name with given ID</entry>
8658
<entry><literal><function>pg_get_serial_sequence</function>(<parameter>table_name</parameter>, <parameter>column_name</parameter>)</literal></entry>
8659
<entry><type>text</type></entry>
8660
<entry>get name of the sequence that a <type>serial</type> or <type>bigserial</type> column
8664
<entry><literal><function>pg_tablespace_databases</function>(<parameter>tablespace_oid</parameter>)</literal></entry>
8665
<entry><type>setof oid</type></entry>
8666
<entry>get set of database OIDs that have objects in the tablespace</entry>
8673
<function>format_type</function> returns the SQL name of a data type that
8674
is identified by its type OID and possibly a type modifier. Pass NULL
8675
for the type modifier if no specific modifier is known.
8679
<function>pg_get_viewdef</function>,
8680
<function>pg_get_ruledef</function>,
8681
<function>pg_get_indexdef</function>,
8682
<function>pg_get_triggerdef</function>, and
8683
<function>pg_get_constraintdef</function> respectively
8684
reconstruct the creating command for a view, rule, index, trigger, or
8685
constraint. (Note that this is a decompiled reconstruction, not
8686
the original text of the command.)
8687
<function>pg_get_expr</function> decompiles the internal form of an
8688
individual expression, such as the default value for a column. It
8689
may be useful when examining the contents of system catalogs.
8690
Most of these functions come in two
8691
variants, one of which can optionally <quote>pretty-print</> the result.
8692
The pretty-printed format is more readable, but the default format is more
8694
interpreted the same way by future versions of <productname>PostgreSQL</>;
8695
avoid using pretty-printed output for dump purposes.
8696
Passing <literal>false</> for the pretty-print parameter yields the
8697
same result as the variant that does not have the parameter at all.
8701
<function>pg_get_userbyid</function>
8702
extracts a user's name given a user ID number.
8703
<function>pg_get_serial_sequence</function>
8704
fetches the name of the sequence associated with a serial or
8705
bigserial column. The name is suitably formatted
8706
for passing to the sequence functions (see <xref
8707
linkend="functions-sequence">).
8708
NULL is returned if the column does not have a sequence attached.
8712
<function>pg_tablespace_databases</function> allows usage examination of a
8713
tablespace. It will return a set of OIDs of databases that have objects
8714
stored in the tablespace. If this function returns any row, the
8715
tablespace is not empty and cannot be dropped. To
8716
display the specific objects populating the tablespace, you will need
8717
to connect to the databases identified by
8718
<function>pg_tablespace_databases</function> and query their
8719
<structname>pg_class</> catalogs.
8722
<indexterm zone="functions-info">
8723
<primary>obj_description</primary>
8726
<indexterm zone="functions-info">
8727
<primary>col_description</primary>
8730
<indexterm zone="functions-info">
8731
<primary>comment</primary>
8732
<secondary sortas="database objects">about database objects</secondary>
8736
The functions shown in <xref
8737
linkend="functions-info-comment-table"> extract comments
8738
previously stored with the <command>COMMENT</command> command. A
8739
null value is returned if no comment could be found matching the
8740
specified parameters.
8743
<table id="functions-info-comment-table">
8744
<title>Comment Information Functions</title>
8747
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8752
<entry><literal><function>obj_description</function>(<parameter>object_oid</parameter>, <parameter>catalog_name</parameter>)</literal></entry>
8753
<entry><type>text</type></entry>
8754
<entry>get comment for a database object</entry>
8757
<entry><literal><function>obj_description</function>(<parameter>object_oid</parameter>)</literal></entry>
8758
<entry><type>text</type></entry>
8759
<entry>get comment for a database object (<emphasis>deprecated</emphasis>)</entry>
8762
<entry><literal><function>col_description</function>(<parameter>table_oid</parameter>, <parameter>column_number</parameter>)</literal></entry>
8763
<entry><type>text</type></entry>
8764
<entry>get comment for a table column</entry>
8771
The two-parameter form of <function>obj_description</function> returns the
8772
comment for a database object specified by its OID and the name of the
8773
containing system catalog. For example,
8774
<literal>obj_description(123456,'pg_class')</literal>
8775
would retrieve the comment for a table with OID 123456.
8776
The one-parameter form of <function>obj_description</function> requires only
8777
the object OID. It is now deprecated since there is no guarantee that
8778
OIDs are unique across different system catalogs; therefore, the wrong
8779
comment could be returned.
8783
<function>col_description</function> returns the comment for a table column,
8784
which is specified by the OID of its table and its column number.
8785
<function>obj_description</function> cannot be used for table columns since
8786
columns do not have OIDs of their own.
8790
<sect1 id="functions-admin">
8791
<title>System Administration Functions</title>
8794
<xref linkend="functions-admin-set-table"> shows the functions
8795
available to query and alter run-time configuration parameters.
8798
<table id="functions-admin-set-table">
8799
<title>Configuration Settings Functions</title>
8802
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry></row>
8808
<literal><function>current_setting</function>(<parameter>setting_name</parameter>)</literal>
8810
<entry><type>text</type></entry>
8811
<entry>current value of setting</entry>
8815
<literal><function>set_config(<parameter>setting_name</parameter>,
8816
<parameter>new_value</parameter>,
8817
<parameter>is_local</parameter>)</function></literal>
8819
<entry><type>text</type></entry>
8820
<entry>set parameter and return new value</entry>
8826
<indexterm zone="functions-admin">
8827
<primary>SET</primary>
8830
<indexterm zone="functions-admin">
8831
<primary>SHOW</primary>
8834
<indexterm zone="functions-admin">
8835
<primary>configuration</primary>
8836
<secondary sortas="server">of the server</secondary>
8837
<tertiary>functions</tertiary>
8841
The function <function>current_setting</function> yields the
8842
current value of the setting <parameter>setting_name</parameter>.
8843
It corresponds to the <acronym>SQL</acronym> command
8844
<command>SHOW</command>. An example:
8846
SELECT current_setting('datestyle');
8856
<function>set_config</function> sets the parameter
8857
<parameter>setting_name</parameter> to
8858
<parameter>new_value</parameter>. If
8859
<parameter>is_local</parameter> is <literal>true</literal>, the
8860
new value will only apply to the current transaction. If you want
8861
the new value to apply for the current session, use
8862
<literal>false</literal> instead. The function corresponds to the
8863
SQL command <command>SET</command>. An example:
8865
SELECT set_config('log_statement_stats', 'off', false);
8874
<indexterm zone="functions-admin">
8875
<primary>pg_cancel_backend</primary>
8878
<indexterm zone="functions-admin">
8879
<primary>signal</primary>
8880
<secondary sortas="backend">backend processes</secondary>
8884
The function shown in <xref
8885
linkend="functions-admin-signal-table"> sends control signals to
8886
other server processes. Use of this function is restricted
8890
<table id="functions-admin-signal-table">
8891
<title>Backend Signalling Functions</title>
8894
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry>
8901
<literal><function>pg_cancel_backend</function>(<parameter>pid</parameter>)</literal>
8903
<entry><type>int</type></entry>
8904
<entry>Cancel a backend's current query</entry>
8911
This function returns 1 if successful, 0 if not successful.
8912
The process ID (<literal>pid</literal>) of an active backend can be found
8913
from the <structfield>procpid</structfield> column in the
8914
<structname>pg_stat_activity</structname> view, or by listing the <command>postgres</command>
8915
processes on the server with <application>ps</>.
8918
<indexterm zone="functions-admin">
8919
<primary>pg_start_backup</primary>
8922
<indexterm zone="functions-admin">
8923
<primary>pg_stop_backup</primary>
8926
<indexterm zone="functions-admin">
8927
<primary>backup</primary>
8931
The functions shown in <xref
8932
linkend="functions-admin-backup-table"> assist in making on-line backups.
8933
Use of these functions is restricted to superusers.
8936
<table id="functions-admin-backup-table">
8937
<title>Backup Control Functions</title>
8940
<row><entry>Name</entry> <entry>Return Type</entry> <entry>Description</entry>
8947
<literal><function>pg_start_backup</function>(<parameter>label_text</parameter>)</literal>
8949
<entry><type>text</type></entry>
8950
<entry>Set up for performing on-line backup</entry>
8954
<literal><function>pg_stop_backup</function>()</literal>
8956
<entry><type>text</type></entry>
8957
<entry>Finish performing on-line backup</entry>
8964
<function>pg_start_backup</> accepts a single parameter which is an
8965
arbitrary user-defined label for the backup. (Typically this would be
8966
the name under which the backup dump file will be stored.) The function
8967
writes a backup label file into the database cluster's data directory,
8968
and then returns the backup's starting WAL offset as text. (The user
8969
need not pay any attention to this result value, but it is provided in
8974
<function>pg_stop_backup</> removes the label file created by
8975
<function>pg_start_backup</>, and instead creates a backup history file in
8976
the WAL archive area. The history file includes the label given to
8977
<function>pg_start_backup</>, the starting and ending WAL offsets for
8978
the backup, and the starting and ending times of the backup. The return
8979
value is the backup's ending WAL offset (which again may be of little
8984
For details about proper usage of these functions, see
8985
<xref linkend="backup-online">.
8990
<!-- Keep this comment at the end of the file
8995
sgml-minimize-attributes:nil
8996
sgml-always-quote-attributes:t
8999
sgml-parent-document:nil
9000
sgml-default-dtd-file:"./reference.ced"
9001
sgml-exposed-tags:nil
9002
sgml-local-catalogs:("/usr/lib/sgml/catalog")
9003
sgml-local-ecat-files:nil