1 <?xml version="1.0" encoding="UTF-8" standalone="no"?>
2 <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml"><head><meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /><title>4.1. Lexical Structure</title><link rel="stylesheet" type="text/css" href="stylesheet.css" /><link rev="made" href="pgsql-docs@lists.postgresql.org" /><meta name="generator" content="DocBook XSL Stylesheets Vsnapshot" /><link rel="prev" href="sql-syntax.html" title="Chapter 4. SQL Syntax" /><link rel="next" href="sql-expressions.html" title="4.2. Value Expressions" /></head><body id="docContent" class="container-fluid col-10"><div class="navheader"><table width="100%" summary="Navigation header"><tr><th colspan="5" align="center">4.1. Lexical Structure</th></tr><tr><td width="10%" align="left"><a accesskey="p" href="sql-syntax.html" title="Chapter 4. SQL Syntax">Prev</a> </td><td width="10%" align="left"><a accesskey="u" href="sql-syntax.html" title="Chapter 4. SQL Syntax">Up</a></td><th width="60%" align="center">Chapter 4. SQL Syntax</th><td width="10%" align="right"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="10%" align="right"> <a accesskey="n" href="sql-expressions.html" title="4.2. Value Expressions">Next</a></td></tr></table><hr /></div><div class="sect1" id="SQL-SYNTAX-LEXICAL"><div class="titlepage"><div><div><h2 class="title" style="clear: both">4.1. Lexical Structure <a href="#SQL-SYNTAX-LEXICAL" class="id_link">#</a></h2></div></div></div><div class="toc"><dl class="toc"><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-SYNTAX-IDENTIFIERS">4.1.1. Identifiers and Key Words</a></span></dt><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS">4.1.2. Constants</a></span></dt><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-SYNTAX-OPERATORS">4.1.3. Operators</a></span></dt><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-SYNTAX-SPECIAL-CHARS">4.1.4. Special Characters</a></span></dt><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-SYNTAX-COMMENTS">4.1.5. Comments</a></span></dt><dt><span class="sect2"><a href="sql-syntax-lexical.html#SQL-PRECEDENCE">4.1.6. Operator Precedence</a></span></dt></dl></div><a id="id-1.5.3.5.2" class="indexterm"></a><p>
3 SQL input consists of a sequence of
4 <em class="firstterm">commands</em>. A command is composed of a
5 sequence of <em class="firstterm">tokens</em>, terminated by a
6 semicolon (<span class="quote">“<span class="quote">;</span>”</span>). The end of the input stream also
7 terminates a command. Which tokens are valid depends on the syntax
8 of the particular command.
10 A token can be a <em class="firstterm">key word</em>, an
11 <em class="firstterm">identifier</em>, a <em class="firstterm">quoted
12 identifier</em>, a <em class="firstterm">literal</em> (or
13 constant), or a special character symbol. Tokens are normally
14 separated by whitespace (space, tab, newline), but need not be if
15 there is no ambiguity (which is generally only the case if a
16 special character is adjacent to some other token type).
18 For example, the following is (syntactically) valid SQL input:
19 </p><pre class="programlisting">
20 SELECT * FROM MY_TABLE;
21 UPDATE MY_TABLE SET A = 5;
22 INSERT INTO MY_TABLE VALUES (3, 'hi there');
24 This is a sequence of three commands, one per line (although this
25 is not required; more than one command can be on a line, and
26 commands can usefully be split across lines).
28 Additionally, <em class="firstterm">comments</em> can occur in SQL
29 input. They are not tokens, they are effectively equivalent to
32 The SQL syntax is not very consistent regarding what tokens
33 identify commands and which are operands or parameters. The first
34 few tokens are generally the command name, so in the above example
35 we would usually speak of a <span class="quote">“<span class="quote">SELECT</span>”</span>, an
36 <span class="quote">“<span class="quote">UPDATE</span>”</span>, and an <span class="quote">“<span class="quote">INSERT</span>”</span> command. But
37 for instance the <code class="command">UPDATE</code> command always requires
38 a <code class="token">SET</code> token to appear in a certain position, and
39 this particular variation of <code class="command">INSERT</code> also
40 requires a <code class="token">VALUES</code> in order to be complete. The
41 precise syntax rules for each command are described in <a class="xref" href="reference.html" title="Part VI. Reference">Part VI</a>.
42 </p><div class="sect2" id="SQL-SYNTAX-IDENTIFIERS"><div class="titlepage"><div><div><h3 class="title">4.1.1. Identifiers and Key Words <a href="#SQL-SYNTAX-IDENTIFIERS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.3.5.8.2" class="indexterm"></a><a id="id-1.5.3.5.8.3" class="indexterm"></a><a id="id-1.5.3.5.8.4" class="indexterm"></a><p>
43 Tokens such as <code class="token">SELECT</code>, <code class="token">UPDATE</code>, or
44 <code class="token">VALUES</code> in the example above are examples of
45 <em class="firstterm">key words</em>, that is, words that have a fixed
46 meaning in the SQL language. The tokens <code class="token">MY_TABLE</code>
47 and <code class="token">A</code> are examples of
48 <em class="firstterm">identifiers</em>. They identify names of
49 tables, columns, or other database objects, depending on the
50 command they are used in. Therefore they are sometimes simply
51 called <span class="quote">“<span class="quote">names</span>”</span>. Key words and identifiers have the
52 same lexical structure, meaning that one cannot know whether a
53 token is an identifier or a key word without knowing the language.
54 A complete list of key words can be found in <a class="xref" href="sql-keywords-appendix.html" title="Appendix C. SQL Key Words">Appendix C</a>.
56 SQL identifiers and key words must begin with a letter
57 (<code class="literal">a</code>-<code class="literal">z</code>, but also letters with
58 diacritical marks and non-Latin letters) or an underscore
59 (<code class="literal">_</code>). Subsequent characters in an identifier or
60 key word can be letters, underscores, digits
61 (<code class="literal">0</code>-<code class="literal">9</code>), or dollar signs
62 (<code class="literal">$</code>). Note that dollar signs are not allowed in identifiers
63 according to the letter of the SQL standard, so their use might render
64 applications less portable.
65 The SQL standard will not define a key word that contains
66 digits or starts or ends with an underscore, so identifiers of this
67 form are safe against possible conflict with future extensions of the
70 <a id="id-1.5.3.5.8.7.1" class="indexterm"></a>
71 The system uses no more than <code class="symbol">NAMEDATALEN</code>-1
72 bytes of an identifier; longer names can be written in
73 commands, but they will be truncated. By default,
74 <code class="symbol">NAMEDATALEN</code> is 64 so the maximum identifier
75 length is 63 bytes. If this limit is problematic, it can be raised by
76 changing the <code class="symbol">NAMEDATALEN</code> constant in
77 <code class="filename">src/include/pg_config_manual.h</code>.
79 <a id="id-1.5.3.5.8.8.1" class="indexterm"></a>
80 Key words and unquoted identifiers are case-insensitive. Therefore:
81 </p><pre class="programlisting">
82 UPDATE MY_TABLE SET A = 5;
84 can equivalently be written as:
85 </p><pre class="programlisting">
86 uPDaTE my_TabLE SeT a = 5;
88 A convention often used is to write key words in upper
89 case and names in lower case, e.g.:
90 </p><pre class="programlisting">
91 UPDATE my_table SET a = 5;
94 <a id="id-1.5.3.5.8.9.1" class="indexterm"></a>
95 There is a second kind of identifier: the <em class="firstterm">delimited
96 identifier</em> or <em class="firstterm">quoted
97 identifier</em>. It is formed by enclosing an arbitrary
98 sequence of characters in double-quotes
99 (<code class="literal">"</code>). A delimited
100 identifier is always an identifier, never a key word. So
101 <code class="literal">"select"</code> could be used to refer to a column or
102 table named <span class="quote">“<span class="quote">select</span>”</span>, whereas an unquoted
103 <code class="literal">select</code> would be taken as a key word and
104 would therefore provoke a parse error when used where a table or
105 column name is expected. The example can be written with quoted
106 identifiers like this:
107 </p><pre class="programlisting">
108 UPDATE "my_table" SET "a" = 5;
111 Quoted identifiers can contain any character, except the character
112 with code zero. (To include a double quote, write two double quotes.)
113 This allows constructing table or column names that would
114 otherwise not be possible, such as ones containing spaces or
115 ampersands. The length limitation still applies.
117 Quoting an identifier also makes it case-sensitive, whereas
118 unquoted names are always folded to lower case. For example, the
119 identifiers <code class="literal">FOO</code>, <code class="literal">foo</code>, and
120 <code class="literal">"foo"</code> are considered the same by
121 <span class="productname">PostgreSQL</span>, but
122 <code class="literal">"Foo"</code> and <code class="literal">"FOO"</code> are
123 different from these three and each other. (The folding of
124 unquoted names to lower case in <span class="productname">PostgreSQL</span> is
125 incompatible with the SQL standard, which says that unquoted names
126 should be folded to upper case. Thus, <code class="literal">foo</code>
127 should be equivalent to <code class="literal">"FOO"</code> not
128 <code class="literal">"foo"</code> according to the standard. If you want
129 to write portable applications you are advised to always quote a
130 particular name or never quote it.)
131 </p><a id="id-1.5.3.5.8.12" class="indexterm"></a><p>
133 identifiers allows including escaped Unicode characters identified
134 by their code points. This variant starts
135 with <code class="literal">U&</code> (upper or lower case U followed by
136 ampersand) immediately before the opening double quote, without
137 any spaces in between, for example <code class="literal">U&"foo"</code>.
138 (Note that this creates an ambiguity with the
139 operator <code class="literal">&</code>. Use spaces around the operator to
140 avoid this problem.) Inside the quotes, Unicode characters can be
141 specified in escaped form by writing a backslash followed by the
142 four-digit hexadecimal code point number or alternatively a
143 backslash followed by a plus sign followed by a six-digit
144 hexadecimal code point number. For example, the
145 identifier <code class="literal">"data"</code> could be written as
146 </p><pre class="programlisting">
147 U&"d\0061t\+000061"
149 The following less trivial example writes the Russian
150 word <span class="quote">“<span class="quote">slon</span>”</span> (elephant) in Cyrillic letters:
151 </p><pre class="programlisting">
152 U&"\0441\043B\043E\043D"
155 If a different escape character than backslash is desired, it can
157 the <code class="literal">UESCAPE</code><a id="id-1.5.3.5.8.14.2" class="indexterm"></a>
158 clause after the string, for example:
159 </p><pre class="programlisting">
160 U&"d!0061t!+000061" UESCAPE '!'
162 The escape character can be any single character other than a
163 hexadecimal digit, the plus sign, a single quote, a double quote,
164 or a whitespace character. Note that the escape character is
165 written in single quotes, not double quotes,
166 after <code class="literal">UESCAPE</code>.
168 To include the escape character in the identifier literally, write
171 Either the 4-digit or the 6-digit escape form can be used to
172 specify UTF-16 surrogate pairs to compose characters with code
173 points larger than U+FFFF, although the availability of the
174 6-digit form technically makes this unnecessary. (Surrogate
175 pairs are not stored directly, but are combined into a single
178 If the server encoding is not UTF-8, the Unicode code point identified
179 by one of these escape sequences is converted to the actual server
180 encoding; an error is reported if that's not possible.
181 </p></div><div class="sect2" id="SQL-SYNTAX-CONSTANTS"><div class="titlepage"><div><div><h3 class="title">4.1.2. Constants <a href="#SQL-SYNTAX-CONSTANTS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.3.5.9.2" class="indexterm"></a><p>
182 There are three kinds of <em class="firstterm">implicitly-typed
183 constants</em> in <span class="productname">PostgreSQL</span>:
184 strings, bit strings, and numbers.
185 Constants can also be specified with explicit types, which can
186 enable more accurate representation and more efficient handling by
187 the system. These alternatives are discussed in the following
189 </p><div class="sect3" id="SQL-SYNTAX-STRINGS"><div class="titlepage"><div><div><h4 class="title">4.1.2.1. String Constants <a href="#SQL-SYNTAX-STRINGS" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.4.2" class="indexterm"></a><p>
190 <a id="id-1.5.3.5.9.4.3.1" class="indexterm"></a>
191 A string constant in SQL is an arbitrary sequence of characters
192 bounded by single quotes (<code class="literal">'</code>), for example
193 <code class="literal">'This is a string'</code>. To include
194 a single-quote character within a string constant,
195 write two adjacent single quotes, e.g.,
196 <code class="literal">'Dianne''s horse'</code>.
197 Note that this is <span class="emphasis"><em>not</em></span> the same as a double-quote
198 character (<code class="literal">"</code>).
200 Two string constants that are only separated by whitespace
201 <span class="emphasis"><em>with at least one newline</em></span> are concatenated
202 and effectively treated as if the string had been written as one
203 constant. For example:
204 </p><pre class="programlisting">
209 </p><pre class="programlisting">
213 </p><pre class="programlisting">
216 is not valid syntax. (This slightly bizarre behavior is specified
217 by <acronym class="acronym">SQL</acronym>; <span class="productname">PostgreSQL</span> is
218 following the standard.)
219 </p></div><div class="sect3" id="SQL-SYNTAX-STRINGS-ESCAPE"><div class="titlepage"><div><div><h4 class="title">4.1.2.2. String Constants with C-Style Escapes <a href="#SQL-SYNTAX-STRINGS-ESCAPE" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.5.2" class="indexterm"></a><a id="id-1.5.3.5.9.5.3" class="indexterm"></a><p>
220 <span class="productname">PostgreSQL</span> also accepts <span class="quote">“<span class="quote">escape</span>”</span>
221 string constants, which are an extension to the SQL standard.
222 An escape string constant is specified by writing the letter
223 <code class="literal">E</code> (upper or lower case) just before the opening single
224 quote, e.g., <code class="literal">E'foo'</code>. (When continuing an escape string
225 constant across lines, write <code class="literal">E</code> only before the first opening
227 Within an escape string, a backslash character (<code class="literal">\</code>) begins a
228 C-like <em class="firstterm">backslash escape</em> sequence, in which the combination
229 of backslash and following character(s) represent a special byte
230 value, as shown in <a class="xref" href="sql-syntax-lexical.html#SQL-BACKSLASH-TABLE" title="Table 4.1. Backslash Escape Sequences">Table 4.1</a>.
231 </p><div class="table" id="SQL-BACKSLASH-TABLE"><p class="title"><strong>Table 4.1. Backslash Escape Sequences</strong></p><div class="table-contents"><table class="table" summary="Backslash Escape Sequences" border="1"><colgroup><col /><col /></colgroup><thead><tr><th>Backslash Escape Sequence</th><th>Interpretation</th></tr></thead><tbody><tr><td><code class="literal">\b</code></td><td>backspace</td></tr><tr><td><code class="literal">\f</code></td><td>form feed</td></tr><tr><td><code class="literal">\n</code></td><td>newline</td></tr><tr><td><code class="literal">\r</code></td><td>carriage return</td></tr><tr><td><code class="literal">\t</code></td><td>tab</td></tr><tr><td>
232 <code class="literal">\<em class="replaceable"><code>o</code></em></code>,
233 <code class="literal">\<em class="replaceable"><code>oo</code></em></code>,
234 <code class="literal">\<em class="replaceable"><code>ooo</code></em></code>
235 (<em class="replaceable"><code>o</code></em> = 0–7)
236 </td><td>octal byte value</td></tr><tr><td>
237 <code class="literal">\x<em class="replaceable"><code>h</code></em></code>,
238 <code class="literal">\x<em class="replaceable"><code>hh</code></em></code>
239 (<em class="replaceable"><code>h</code></em> = 0–9, A–F)
240 </td><td>hexadecimal byte value</td></tr><tr><td>
241 <code class="literal">\u<em class="replaceable"><code>xxxx</code></em></code>,
242 <code class="literal">\U<em class="replaceable"><code>xxxxxxxx</code></em></code>
243 (<em class="replaceable"><code>x</code></em> = 0–9, A–F)
244 </td><td>16 or 32-bit hexadecimal Unicode character value</td></tr></tbody></table></div></div><br class="table-break" /><p>
246 character following a backslash is taken literally. Thus, to
247 include a backslash character, write two backslashes (<code class="literal">\\</code>).
248 Also, a single quote can be included in an escape string by writing
249 <code class="literal">\'</code>, in addition to the normal way of <code class="literal">''</code>.
251 It is your responsibility that the byte sequences you create,
252 especially when using the octal or hexadecimal escapes, compose
253 valid characters in the server character set encoding.
254 A useful alternative is to use Unicode escapes or the
255 alternative Unicode escape syntax, explained
256 in <a class="xref" href="sql-syntax-lexical.html#SQL-SYNTAX-STRINGS-UESCAPE" title="4.1.2.3. String Constants with Unicode Escapes">Section 4.1.2.3</a>; then the server
257 will check that the character conversion is possible.
258 </p><div class="caution"><h3 class="title">Caution</h3><p>
259 If the configuration parameter
260 <a class="xref" href="runtime-config-compatible.html#GUC-STANDARD-CONFORMING-STRINGS">standard_conforming_strings</a> is <code class="literal">off</code>,
261 then <span class="productname">PostgreSQL</span> recognizes backslash escapes
262 in both regular and escape string constants. However, as of
263 <span class="productname">PostgreSQL</span> 9.1, the default is <code class="literal">on</code>, meaning
264 that backslash escapes are recognized only in escape string constants.
265 This behavior is more standards-compliant, but might break applications
266 which rely on the historical behavior, where backslash escapes
267 were always recognized. As a workaround, you can set this parameter
268 to <code class="literal">off</code>, but it is better to migrate away from using backslash
269 escapes. If you need to use a backslash escape to represent a special
270 character, write the string constant with an <code class="literal">E</code>.
272 In addition to <code class="varname">standard_conforming_strings</code>, the configuration
273 parameters <a class="xref" href="runtime-config-compatible.html#GUC-ESCAPE-STRING-WARNING">escape_string_warning</a> and
274 <a class="xref" href="runtime-config-compatible.html#GUC-BACKSLASH-QUOTE">backslash_quote</a> govern treatment of backslashes
277 The character with the code zero cannot be in a string constant.
278 </p></div><div class="sect3" id="SQL-SYNTAX-STRINGS-UESCAPE"><div class="titlepage"><div><div><h4 class="title">4.1.2.3. String Constants with Unicode Escapes <a href="#SQL-SYNTAX-STRINGS-UESCAPE" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.6.2" class="indexterm"></a><p>
279 <span class="productname">PostgreSQL</span> also supports another type
280 of escape syntax for strings that allows specifying arbitrary
281 Unicode characters by code point. A Unicode escape string
282 constant starts with <code class="literal">U&</code> (upper or lower case
283 letter U followed by ampersand) immediately before the opening
284 quote, without any spaces in between, for
285 example <code class="literal">U&'foo'</code>. (Note that this creates an
286 ambiguity with the operator <code class="literal">&</code>. Use spaces
287 around the operator to avoid this problem.) Inside the quotes,
288 Unicode characters can be specified in escaped form by writing a
289 backslash followed by the four-digit hexadecimal code point
290 number or alternatively a backslash followed by a plus sign
291 followed by a six-digit hexadecimal code point number. For
292 example, the string <code class="literal">'data'</code> could be written as
293 </p><pre class="programlisting">
294 U&'d\0061t\+000061'
296 The following less trivial example writes the Russian
297 word <span class="quote">“<span class="quote">slon</span>”</span> (elephant) in Cyrillic letters:
298 </p><pre class="programlisting">
299 U&'\0441\043B\043E\043D'
302 If a different escape character than backslash is desired, it can
304 the <code class="literal">UESCAPE</code><a id="id-1.5.3.5.9.6.4.2" class="indexterm"></a>
305 clause after the string, for example:
306 </p><pre class="programlisting">
307 U&'d!0061t!+000061' UESCAPE '!'
309 The escape character can be any single character other than a
310 hexadecimal digit, the plus sign, a single quote, a double quote,
311 or a whitespace character.
313 To include the escape character in the string literally, write
316 Either the 4-digit or the 6-digit escape form can be used to
317 specify UTF-16 surrogate pairs to compose characters with code
318 points larger than U+FFFF, although the availability of the
319 6-digit form technically makes this unnecessary. (Surrogate
320 pairs are not stored directly, but are combined into a single
323 If the server encoding is not UTF-8, the Unicode code point identified
324 by one of these escape sequences is converted to the actual server
325 encoding; an error is reported if that's not possible.
327 Also, the Unicode escape syntax for string constants only works
328 when the configuration
329 parameter <a class="xref" href="runtime-config-compatible.html#GUC-STANDARD-CONFORMING-STRINGS">standard_conforming_strings</a> is
330 turned on. This is because otherwise this syntax could confuse
331 clients that parse the SQL statements to the point that it could
332 lead to SQL injections and similar security issues. If the
333 parameter is set to off, this syntax will be rejected with an
335 </p></div><div class="sect3" id="SQL-SYNTAX-DOLLAR-QUOTING"><div class="titlepage"><div><div><h4 class="title">4.1.2.4. Dollar-Quoted String Constants <a href="#SQL-SYNTAX-DOLLAR-QUOTING" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.7.2" class="indexterm"></a><p>
336 While the standard syntax for specifying string constants is usually
337 convenient, it can be difficult to understand when the desired string
338 contains many single quotes, since each of those must
339 be doubled. To allow more readable queries in such situations,
340 <span class="productname">PostgreSQL</span> provides another way, called
341 <span class="quote">“<span class="quote">dollar quoting</span>”</span>, to write string constants.
342 A dollar-quoted string constant
343 consists of a dollar sign (<code class="literal">$</code>), an optional
344 <span class="quote">“<span class="quote">tag</span>”</span> of zero or more characters, another dollar
345 sign, an arbitrary sequence of characters that makes up the
346 string content, a dollar sign, the same tag that began this
347 dollar quote, and a dollar sign. For example, here are two
348 different ways to specify the string <span class="quote">“<span class="quote">Dianne's horse</span>”</span>
349 using dollar quoting:
350 </p><pre class="programlisting">
352 $SomeTag$Dianne's horse$SomeTag$
354 Notice that inside the dollar-quoted string, single quotes can be
355 used without needing to be escaped. Indeed, no characters inside
356 a dollar-quoted string are ever escaped: the string content is always
357 written literally. Backslashes are not special, and neither are
358 dollar signs, unless they are part of a sequence matching the opening
361 It is possible to nest dollar-quoted string constants by choosing
362 different tags at each nesting level. This is most commonly used in
363 writing function definitions. For example:
364 </p><pre class="programlisting">
367 RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
371 Here, the sequence <code class="literal">$q$[\t\r\n\v\\]$q$</code> represents a
372 dollar-quoted literal string <code class="literal">[\t\r\n\v\\]</code>, which will
373 be recognized when the function body is executed by
374 <span class="productname">PostgreSQL</span>. But since the sequence does not match
375 the outer dollar quoting delimiter <code class="literal">$function$</code>, it is
376 just some more characters within the constant so far as the outer
379 The tag, if any, of a dollar-quoted string follows the same rules
380 as an unquoted identifier, except that it cannot contain a dollar sign.
381 Tags are case sensitive, so <code class="literal">$tag$String content$tag$</code>
382 is correct, but <code class="literal">$TAG$String content$tag$</code> is not.
384 A dollar-quoted string that follows a keyword or identifier must
385 be separated from it by whitespace; otherwise the dollar quoting
386 delimiter would be taken as part of the preceding identifier.
388 Dollar quoting is not part of the SQL standard, but it is often a more
389 convenient way to write complicated string literals than the
390 standard-compliant single quote syntax. It is particularly useful when
391 representing string constants inside other constants, as is often needed
392 in procedural function definitions. With single-quote syntax, each
393 backslash in the above example would have to be written as four
394 backslashes, which would be reduced to two backslashes in parsing the
395 original string constant, and then to one when the inner string constant
396 is re-parsed during function execution.
397 </p></div><div class="sect3" id="SQL-SYNTAX-BIT-STRINGS"><div class="titlepage"><div><div><h4 class="title">4.1.2.5. Bit-String Constants <a href="#SQL-SYNTAX-BIT-STRINGS" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.8.2" class="indexterm"></a><p>
398 Bit-string constants look like regular string constants with a
399 <code class="literal">B</code> (upper or lower case) immediately before the
400 opening quote (no intervening whitespace), e.g.,
401 <code class="literal">B'1001'</code>. The only characters allowed within
402 bit-string constants are <code class="literal">0</code> and
403 <code class="literal">1</code>.
405 Alternatively, bit-string constants can be specified in hexadecimal
406 notation, using a leading <code class="literal">X</code> (upper or lower case),
407 e.g., <code class="literal">X'1FF'</code>. This notation is equivalent to
408 a bit-string constant with four binary digits for each hexadecimal digit.
410 Both forms of bit-string constant can be continued
411 across lines in the same way as regular string constants.
412 Dollar quoting cannot be used in a bit-string constant.
413 </p></div><div class="sect3" id="SQL-SYNTAX-CONSTANTS-NUMERIC"><div class="titlepage"><div><div><h4 class="title">4.1.2.6. Numeric Constants <a href="#SQL-SYNTAX-CONSTANTS-NUMERIC" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.9.2" class="indexterm"></a><p>
414 Numeric constants are accepted in these general forms:
415 </p><pre class="synopsis">
416 <em class="replaceable"><code>digits</code></em>
417 <em class="replaceable"><code>digits</code></em>.[<span class="optional"><em class="replaceable"><code>digits</code></em></span>][<span class="optional">e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em></span>]
418 [<span class="optional"><em class="replaceable"><code>digits</code></em></span>].<em class="replaceable"><code>digits</code></em>[<span class="optional">e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em></span>]
419 <em class="replaceable"><code>digits</code></em>e[<span class="optional">+-</span>]<em class="replaceable"><code>digits</code></em>
421 where <em class="replaceable"><code>digits</code></em> is one or more decimal
422 digits (0 through 9). At least one digit must be before or after the
423 decimal point, if one is used. At least one digit must follow the
424 exponent marker (<code class="literal">e</code>), if one is present.
425 There cannot be any spaces or other characters embedded in the
426 constant, except for underscores, which can be used for visual grouping as
427 described below. Note that any leading plus or minus sign is not actually
428 considered part of the constant; it is an operator applied to the
431 These are some examples of valid numeric constants:
432 </p><div class="literallayout"><p><br />
441 Additionally, non-decimal integer constants are accepted in these forms:
442 </p><pre class="synopsis">
443 0x<em class="replaceable"><code>hexdigits</code></em>
444 0o<em class="replaceable"><code>octdigits</code></em>
445 0b<em class="replaceable"><code>bindigits</code></em>
447 where <em class="replaceable"><code>hexdigits</code></em> is one or more hexadecimal digits
448 (0-9, A-F), <em class="replaceable"><code>octdigits</code></em> is one or more octal
449 digits (0-7), and <em class="replaceable"><code>bindigits</code></em> is one or more binary
450 digits (0 or 1). Hexadecimal digits and the radix prefixes can be in
451 upper or lower case. Note that only integers can have non-decimal forms,
452 not numbers with fractional parts.
454 These are some examples of valid non-decimal integer constants:
455 </p><div class="literallayout"><p><br />
464 For visual grouping, underscores can be inserted between digits. These
465 have no further effect on the value of the constant. For example:
466 </p><div class="literallayout"><p><br />
468 0b10001000_00000000<br />
473 Underscores are not allowed at the start or end of a numeric constant or
474 a group of digits (that is, immediately before or after the decimal point
475 or the exponent marker), and more than one underscore in a row is not
478 <a id="id-1.5.3.5.9.9.8.1" class="indexterm"></a>
479 <a id="id-1.5.3.5.9.9.8.2" class="indexterm"></a>
480 <a id="id-1.5.3.5.9.9.8.3" class="indexterm"></a>
481 A numeric constant that contains neither a decimal point nor an
482 exponent is initially presumed to be type <code class="type">integer</code> if its
483 value fits in type <code class="type">integer</code> (32 bits); otherwise it is
484 presumed to be type <code class="type">bigint</code> if its
485 value fits in type <code class="type">bigint</code> (64 bits); otherwise it is
486 taken to be type <code class="type">numeric</code>. Constants that contain decimal
487 points and/or exponents are always initially presumed to be type
488 <code class="type">numeric</code>.
490 The initially assigned data type of a numeric constant is just a
491 starting point for the type resolution algorithms. In most cases
492 the constant will be automatically coerced to the most
493 appropriate type depending on context. When necessary, you can
494 force a numeric value to be interpreted as a specific data type
495 by casting it.<a id="id-1.5.3.5.9.9.9.1" class="indexterm"></a>
496 For example, you can force a numeric value to be treated as type
497 <code class="type">real</code> (<code class="type">float4</code>) by writing:
499 </p><pre class="programlisting">
500 REAL '1.23' -- string style
501 1.23::REAL -- PostgreSQL (historical) style
504 These are actually just special cases of the general casting
505 notations discussed next.
506 </p></div><div class="sect3" id="SQL-SYNTAX-CONSTANTS-GENERIC"><div class="titlepage"><div><div><h4 class="title">4.1.2.7. Constants of Other Types <a href="#SQL-SYNTAX-CONSTANTS-GENERIC" class="id_link">#</a></h4></div></div></div><a id="id-1.5.3.5.9.10.2" class="indexterm"></a><p>
507 A constant of an <span class="emphasis"><em>arbitrary</em></span> type can be
508 entered using any one of the following notations:
509 </p><pre class="synopsis">
510 <em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'
511 '<em class="replaceable"><code>string</code></em>'::<em class="replaceable"><code>type</code></em>
512 CAST ( '<em class="replaceable"><code>string</code></em>' AS <em class="replaceable"><code>type</code></em> )
514 The string constant's text is passed to the input conversion
515 routine for the type called <em class="replaceable"><code>type</code></em>. The
516 result is a constant of the indicated type. The explicit type
517 cast can be omitted if there is no ambiguity as to the type the
518 constant must be (for example, when it is assigned directly to a
519 table column), in which case it is automatically coerced.
521 The string constant can be written using either regular SQL
522 notation or dollar-quoting.
524 It is also possible to specify a type coercion using a function-like
526 </p><pre class="synopsis">
527 <em class="replaceable"><code>typename</code></em> ( '<em class="replaceable"><code>string</code></em>' )
529 but not all type names can be used in this way; see <a class="xref" href="sql-expressions.html#SQL-SYNTAX-TYPE-CASTS" title="4.2.9. Type Casts">Section 4.2.9</a> for details.
531 The <code class="literal">::</code>, <code class="literal">CAST()</code>, and
532 function-call syntaxes can also be used to specify run-time type
533 conversions of arbitrary expressions, as discussed in <a class="xref" href="sql-expressions.html#SQL-SYNTAX-TYPE-CASTS" title="4.2.9. Type Casts">Section 4.2.9</a>. To avoid syntactic ambiguity, the
534 <code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
535 syntax can only be used to specify the type of a simple literal constant.
536 Another restriction on the
537 <code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
538 syntax is that it does not work for array types; use <code class="literal">::</code>
539 or <code class="literal">CAST()</code> to specify the type of an array constant.
541 The <code class="literal">CAST()</code> syntax conforms to SQL. The
542 <code class="literal"><em class="replaceable"><code>type</code></em> '<em class="replaceable"><code>string</code></em>'</code>
543 syntax is a generalization of the standard: SQL specifies this syntax only
544 for a few data types, but <span class="productname">PostgreSQL</span> allows it
545 for all types. The syntax with
546 <code class="literal">::</code> is historical <span class="productname">PostgreSQL</span>
547 usage, as is the function-call syntax.
548 </p></div></div><div class="sect2" id="SQL-SYNTAX-OPERATORS"><div class="titlepage"><div><div><h3 class="title">4.1.3. Operators <a href="#SQL-SYNTAX-OPERATORS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.3.5.10.2" class="indexterm"></a><p>
549 An operator name is a sequence of up to <code class="symbol">NAMEDATALEN</code>-1
550 (63 by default) characters from the following list:
551 </p><div class="literallayout"><p><br />
552 + - * / < > = ~ ! @ # % ^ & | ` ?<br />
555 There are a few restrictions on operator names, however:
556 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
557 <code class="literal">--</code> and <code class="literal">/*</code> cannot appear
558 anywhere in an operator name, since they will be taken as the
560 </p></li><li class="listitem"><p>
561 A multiple-character operator name cannot end in <code class="literal">+</code> or <code class="literal">-</code>,
562 unless the name also contains at least one of these characters:
563 </p><div class="literallayout"><p><br />
564 ~ ! @ # % ^ & | ` ?<br />
566 For example, <code class="literal">@-</code> is an allowed operator name,
567 but <code class="literal">*-</code> is not. This restriction allows
568 <span class="productname">PostgreSQL</span> to parse SQL-compliant
569 queries without requiring spaces between tokens.
570 </p></li></ul></div><p>
572 When working with non-SQL-standard operator names, you will usually
573 need to separate adjacent operators with spaces to avoid ambiguity.
574 For example, if you have defined a prefix operator named <code class="literal">@</code>,
575 you cannot write <code class="literal">X*@Y</code>; you must write
576 <code class="literal">X* @Y</code> to ensure that
577 <span class="productname">PostgreSQL</span> reads it as two operator names
579 </p></div><div class="sect2" id="SQL-SYNTAX-SPECIAL-CHARS"><div class="titlepage"><div><div><h3 class="title">4.1.4. Special Characters <a href="#SQL-SYNTAX-SPECIAL-CHARS" class="id_link">#</a></h3></div></div></div><p>
580 Some characters that are not alphanumeric have a special meaning
581 that is different from being an operator. Details on the usage can
582 be found at the location where the respective syntax element is
583 described. This section only exists to advise the existence and
584 summarize the purposes of these characters.
586 </p><div class="itemizedlist"><ul class="itemizedlist" style="list-style-type: disc; "><li class="listitem"><p>
587 A dollar sign (<code class="literal">$</code>) followed by digits is used
588 to represent a positional parameter in the body of a function
589 definition or a prepared statement. In other contexts the
590 dollar sign can be part of an identifier or a dollar-quoted string
592 </p></li><li class="listitem"><p>
593 Parentheses (<code class="literal">()</code>) have their usual meaning to
594 group expressions and enforce precedence. In some cases
595 parentheses are required as part of the fixed syntax of a
596 particular SQL command.
597 </p></li><li class="listitem"><p>
598 Brackets (<code class="literal">[]</code>) are used to select the elements
599 of an array. See <a class="xref" href="arrays.html" title="8.15. Arrays">Section 8.15</a> for more information
601 </p></li><li class="listitem"><p>
602 Commas (<code class="literal">,</code>) are used in some syntactical
603 constructs to separate the elements of a list.
604 </p></li><li class="listitem"><p>
605 The semicolon (<code class="literal">;</code>) terminates an SQL command.
606 It cannot appear anywhere within a command, except within a
607 string constant or quoted identifier.
608 </p></li><li class="listitem"><p>
609 The colon (<code class="literal">:</code>) is used to select
610 <span class="quote">“<span class="quote">slices</span>”</span> from arrays. (See <a class="xref" href="arrays.html" title="8.15. Arrays">Section 8.15</a>.) In certain SQL dialects (such as Embedded
611 SQL), the colon is used to prefix variable names.
612 </p></li><li class="listitem"><p>
613 The asterisk (<code class="literal">*</code>) is used in some contexts to denote
614 all the fields of a table row or composite value. It also
615 has a special meaning when used as the argument of an
616 aggregate function, namely that the aggregate does not require
617 any explicit parameter.
618 </p></li><li class="listitem"><p>
619 The period (<code class="literal">.</code>) is used in numeric
620 constants, and to separate schema, table, and column names.
621 </p></li></ul></div><p>
623 </p></div><div class="sect2" id="SQL-SYNTAX-COMMENTS"><div class="titlepage"><div><div><h3 class="title">4.1.5. Comments <a href="#SQL-SYNTAX-COMMENTS" class="id_link">#</a></h3></div></div></div><a id="id-1.5.3.5.12.2" class="indexterm"></a><p>
624 A comment is a sequence of characters beginning with
625 double dashes and extending to the end of the line, e.g.:
626 </p><pre class="programlisting">
627 -- This is a standard SQL comment
630 Alternatively, C-style block comments can be used:
631 </p><pre class="programlisting">
633 * with nesting: /* nested block comment */
636 where the comment begins with <code class="literal">/*</code> and extends to
637 the matching occurrence of <code class="literal">*/</code>. These block
638 comments nest, as specified in the SQL standard but unlike C, so that one can
639 comment out larger blocks of code that might contain existing block
642 A comment is removed from the input stream before further syntax
643 analysis and is effectively replaced by whitespace.
644 </p></div><div class="sect2" id="SQL-PRECEDENCE"><div class="titlepage"><div><div><h3 class="title">4.1.6. Operator Precedence <a href="#SQL-PRECEDENCE" class="id_link">#</a></h3></div></div></div><a id="id-1.5.3.5.13.2" class="indexterm"></a><p>
645 <a class="xref" href="sql-syntax-lexical.html#SQL-PRECEDENCE-TABLE" title="Table 4.2. Operator Precedence (highest to lowest)">Table 4.2</a> shows the precedence and
646 associativity of the operators in <span class="productname">PostgreSQL</span>.
647 Most operators have the same precedence and are left-associative.
648 The precedence and associativity of the operators is hard-wired
650 Add parentheses if you want an expression with multiple operators
651 to be parsed in some other way than what the precedence rules imply.
652 </p><div class="table" id="SQL-PRECEDENCE-TABLE"><p class="title"><strong>Table 4.2. Operator Precedence (highest to lowest)</strong></p><div class="table-contents"><table class="table" summary="Operator Precedence (highest to lowest)" border="1"><colgroup><col class="col1" /><col class="col2" /><col class="col3" /></colgroup><thead><tr><th>Operator/Element</th><th>Associativity</th><th>Description</th></tr></thead><tbody><tr><td><code class="token">.</code></td><td>left</td><td>table/column name separator</td></tr><tr><td><code class="token">::</code></td><td>left</td><td><span class="productname">PostgreSQL</span>-style typecast</td></tr><tr><td><code class="token">[</code> <code class="token">]</code></td><td>left</td><td>array element selection</td></tr><tr><td><code class="token">+</code> <code class="token">-</code></td><td>right</td><td>unary plus, unary minus</td></tr><tr><td><code class="token">COLLATE</code></td><td>left</td><td>collation selection</td></tr><tr><td><code class="token">AT</code></td><td>left</td><td><code class="literal">AT TIME ZONE</code>, <code class="literal">AT LOCAL</code></td></tr><tr><td><code class="token">^</code></td><td>left</td><td>exponentiation</td></tr><tr><td><code class="token">*</code> <code class="token">/</code> <code class="token">%</code></td><td>left</td><td>multiplication, division, modulo</td></tr><tr><td><code class="token">+</code> <code class="token">-</code></td><td>left</td><td>addition, subtraction</td></tr><tr><td>(any other operator)</td><td>left</td><td>all other native and user-defined operators</td></tr><tr><td><code class="token">BETWEEN</code> <code class="token">IN</code> <code class="token">LIKE</code> <code class="token">ILIKE</code> <code class="token">SIMILAR</code></td><td> </td><td>range containment, set membership, string matching</td></tr><tr><td><code class="token"><</code> <code class="token">></code> <code class="token">=</code> <code class="token"><=</code> <code class="token">>=</code> <code class="token"><></code>
653 </td><td> </td><td>comparison operators</td></tr><tr><td><code class="token">IS</code> <code class="token">ISNULL</code> <code class="token">NOTNULL</code></td><td> </td><td><code class="literal">IS TRUE</code>, <code class="literal">IS FALSE</code>, <code class="literal">IS
654 NULL</code>, <code class="literal">IS DISTINCT FROM</code>, etc.</td></tr><tr><td><code class="token">NOT</code></td><td>right</td><td>logical negation</td></tr><tr><td><code class="token">AND</code></td><td>left</td><td>logical conjunction</td></tr><tr><td><code class="token">OR</code></td><td>left</td><td>logical disjunction</td></tr></tbody></table></div></div><br class="table-break" /><p>
655 Note that the operator precedence rules also apply to user-defined
656 operators that have the same names as the built-in operators
657 mentioned above. For example, if you define a
658 <span class="quote">“<span class="quote">+</span>”</span> operator for some custom data type it will have
659 the same precedence as the built-in <span class="quote">“<span class="quote">+</span>”</span> operator, no
660 matter what yours does.
662 When a schema-qualified operator name is used in the
663 <code class="literal">OPERATOR</code> syntax, as for example in:
664 </p><pre class="programlisting">
665 SELECT 3 OPERATOR(pg_catalog.+) 4;
667 the <code class="literal">OPERATOR</code> construct is taken to have the default precedence
668 shown in <a class="xref" href="sql-syntax-lexical.html#SQL-PRECEDENCE-TABLE" title="Table 4.2. Operator Precedence (highest to lowest)">Table 4.2</a> for
669 <span class="quote">“<span class="quote">any other operator</span>”</span>. This is true no matter
670 which specific operator appears inside <code class="literal">OPERATOR()</code>.
671 </p><div class="note"><h3 class="title">Note</h3><p>
672 <span class="productname">PostgreSQL</span> versions before 9.5 used slightly different
673 operator precedence rules. In particular, <code class="token"><=</code>
674 <code class="token">>=</code> and <code class="token"><></code> used to be treated as
675 generic operators; <code class="literal">IS</code> tests used to have higher priority;
676 and <code class="literal">NOT BETWEEN</code> and related constructs acted inconsistently,
677 being taken in some cases as having the precedence of <code class="literal">NOT</code>
678 rather than <code class="literal">BETWEEN</code>. These rules were changed for better
679 compliance with the SQL standard and to reduce confusion from
680 inconsistent treatment of logically equivalent constructs. In most
681 cases, these changes will result in no behavioral change, or perhaps
682 in <span class="quote">“<span class="quote">no such operator</span>”</span> failures which can be resolved by adding
683 parentheses. However there are corner cases in which a query might
684 change behavior without any parsing error being reported.
685 </p></div></div></div><div class="navfooter"><hr /><table width="100%" summary="Navigation footer"><tr><td width="40%" align="left"><a accesskey="p" href="sql-syntax.html" title="Chapter 4. SQL Syntax">Prev</a> </td><td width="20%" align="center"><a accesskey="u" href="sql-syntax.html" title="Chapter 4. SQL Syntax">Up</a></td><td width="40%" align="right"> <a accesskey="n" href="sql-expressions.html" title="4.2. Value Expressions">Next</a></td></tr><tr><td width="40%" align="left" valign="top">Chapter 4. SQL Syntax </td><td width="20%" align="center"><a accesskey="h" href="index.html" title="PostgreSQL 18.0 Documentation">Home</a></td><td width="40%" align="right" valign="top"> 4.2. Value Expressions</td></tr></table></div></body></html>