Dollar-Quoted String Constants
Summary: in this tutorial, you will learn how to use the dollar-quoted string constants ($$
) in user-defined functions and stored procedures.
Introduction the dollar-quoted string constant syntax
In PostgreSQL, dollar-quoted string constants allow you to construct strings that contain single quotes without a need to escape them.
For example, you can surround a string constant using single quotes like this:
But when a string constant contains a single quote ('
), you need to escape it by doubling up the single quote:
To make the code more readable, PostgreSQL offers a better syntax called dollar-quoted string constant or dollar quoting:
In this example, we don’t have to double up the single quote.
Here’s the basic syntax of the dollar-quoted string constants:
In this syntax, the tag
is optional. It follows the same rules as unquoted identifiers:
- Must begin with a letter (a-z, A-Z) or underscore.
- Can include letters (case-insensitive), digits, and underscores.
- Limited to 63 characters (longer ones are truncated).
- Cannot contain whitespaces, or reserved keywords without quotes.
Between the $tag$
, you can place any string including single quotes ('
). For example:
Output:
In this example, we do not specify the tag
between the two dollar signs($
).
The following example uses the dollar-quoted string constant syntax with a tag:
Output:
In this example, we use the string message
as a tag between the two dollar signs ($
).
Using dollar-quoted string constants in anonymous blocks
The following shows the anonymous block in PL/pgSQL:
Note that you will learn about the anonymous block in the PL/pgSQL block structure tutorial. In this tutorial, you can copy and paste the code in any PostgreSQL client tool like pgAdmin or psql to execute it.
Output:
The code in a block must be surrounded by single quotes. If it has any single quote, you need to escape it by doubling it like this:
To avoid escaping every single quotes and backslashes, you can use the dollar-quoted string as follows:
Using dollar-quoted string constants in functions
The following shows the syntax of the CREATE FUNCTION
statement that allows you to create a user-defined function:
Note that you will learn about the syntax of CREATE FUNCTION
statement in the creating function tutorial.
In this syntax, the function_body
is a string constant. For example, the following function finds a film by its id:
In this example, the body of the find_film_by_id()
function is surrounded by single quotes.
If the function has many statements, it becomes more difficult to read. In this case, you can use dollar-quoted string constant syntax:
Now, you can place any piece of code between the $$
and $$
without using the need to escape single quotes.
Using dollar-quoted string constants in stored procedures
Similarly, you can use the dollar-quoted string constant syntax in stored procedures like this:
Summary
- Use quoted-dollar string constant syntax to construct string constants without the need to escape single quotes.
- Do use quoted-dollar string constants in anonymous blocks, user-defined functions, and stored procedures.