Home Explore Blog CI



postgresql

8th chunk of `doc/src/sgml/syntax.sgml`
c858a20cb818b040eec37b115bd8b2350c894ef720aecd7c0000000100000fa6
 an
     error message.
    </para>
   </sect3>

   <sect3 id="sql-syntax-dollar-quoting">
    <title>Dollar-Quoted String Constants</title>

     <indexterm>
      <primary>dollar quoting</primary>
     </indexterm>

    <para>
     While the standard syntax for specifying string constants is usually
     convenient, it can be difficult to understand when the desired string
     contains many single quotes, since each of those must
     be doubled. To allow more readable queries in such situations,
     <productname>PostgreSQL</productname> provides another way, called
     <quote>dollar quoting</quote>, to write string constants.
     A dollar-quoted string constant
     consists of a dollar sign (<literal>$</literal>), an optional
     <quote>tag</quote> of zero or more characters, another dollar
     sign, an arbitrary sequence of characters that makes up the
     string content, a dollar sign, the same tag that began this
     dollar quote, and a dollar sign. For example, here are two
     different ways to specify the string <quote>Dianne's horse</quote>
     using dollar quoting:
<programlisting>
$$Dianne's horse$$
$SomeTag$Dianne's horse$SomeTag$
</programlisting>
     Notice that inside the dollar-quoted string, single quotes can be
     used without needing to be escaped.  Indeed, no characters inside
     a dollar-quoted string are ever escaped: the string content is always
     written literally.  Backslashes are not special, and neither are
     dollar signs, unless they are part of a sequence matching the opening
     tag.
    </para>

    <para>
     It is possible to nest dollar-quoted string constants by choosing
     different tags at each nesting level.  This is most commonly used in
     writing function definitions.  For example:
<programlisting>
$function$
BEGIN
    RETURN ($1 ~ $q$[\t\r\n\v\\]$q$);
END;
$function$
</programlisting>
     Here, the sequence <literal>$q$[\t\r\n\v\\]$q$</literal> represents a
     dollar-quoted literal string <literal>[\t\r\n\v\\]</literal>, which will
     be recognized when the function body is executed by
     <productname>PostgreSQL</productname>.  But since the sequence does not match
     the outer dollar quoting delimiter <literal>$function$</literal>, it is
     just some more characters within the constant so far as the outer
     string is concerned.
    </para>

    <para>
     The tag, if any, of a dollar-quoted string follows the same rules
     as an unquoted identifier, except that it cannot contain a dollar sign.
     Tags are case sensitive, so <literal>$tag$String content$tag$</literal>
     is correct, but <literal>$TAG$String content$tag$</literal> is not.
    </para>

    <para>
     A dollar-quoted string that follows a keyword or identifier must
     be separated from it by whitespace; otherwise the dollar quoting
     delimiter would be taken as part of the preceding identifier.
    </para>

    <para>
     Dollar quoting is not part of the SQL standard, but it is often a more
     convenient way to write complicated string literals than the
     standard-compliant single quote syntax.  It is particularly useful when
     representing string constants inside other constants, as is often needed
     in procedural function definitions.  With single-quote syntax, each
     backslash in the above example would have to be written as four
     backslashes, which would be reduced to two backslashes in parsing the
     original string constant, and then to one when the inner string constant
     is re-parsed during function execution.
    </para>
   </sect3>

   <sect3 id="sql-syntax-bit-strings">
    <title>Bit-String Constants</title>

    <indexterm zone="sql-syntax-bit-strings">
     <primary>bit string</primary>
     <secondary>constant</secondary>
    </indexterm>

    <para>
     Bit-string constants look like regular string constants with a
     <literal>B</literal> (upper or lower case) immediately before the
     opening quote (no intervening whitespace),

Title: Dollar-Quoted String Constants in PostgreSQL
Summary
This section details dollar-quoted string constants in PostgreSQL, an alternative to the standard single-quote syntax, particularly useful for strings containing many single quotes. It explains the structure of dollar quoting, involving a dollar sign, an optional tag, the string content, and the same tag enclosed in dollar signs. Dollar quoting allows single quotes and backslashes to be used literally without escaping. It also covers nesting dollar-quoted strings using different tags and emphasizes that the tag follows the same rules as unquoted identifiers. It highlights that dollar quoting is not part of the SQL standard but is a more convenient way to write complicated string literals, especially within procedural function definitions.