![]() |
VOOZH | about |
When working with PostgreSQL, managing string literals can sometimes be complex, especially when dealing with special characters like single quotes ('), backslashes (), or double quotes ("). Dollar-quoting, introduced in PostgreSQL 8.0, simplifies this process, offering an elegant way to handle strings without the need for constant escaping. In this article, we will explain the PostgreSQL dollar-quoted string constants ($$), their syntax, and their practical use cases.
In PostgreSQL, dollar-quoted strings ($$) are used to handle complex string literals in a cleaner and more readable way. Instead of escaping special characters repeatedly, developers can use dollar quoting to encapsulate strings with minimal effort. This is particularly useful in user-defined functions, stored procedures, and dynamic SQL queries. By employing tags within dollar-quoted strings, users can define multiline and complex text efficiently.
Syntax
$tag$<string_constant>$tag$
Key Terms
In this syntax, the tag is optional. It may contain zero or many characters. Between the $tag$, we can place any string with single quotes (') and backslashes (\). For example:
SELECT $$This is a 'string' with "quotes" and a \backslash.$$;
Output
This is a 'string' with "quotes" and a \backslash.Tags can be added between the dollar signs to enhance clarity. In this example, we did not specify the tag between the two dollar signs($). The following example uses the dollar-quoted string constant syntax with a tag:
SELECT $message$I'm a string constant that contains a backslash \$message$;
Output
I'm a string constant with a backslash \For the purpose of demonstrating the use of dollar quoted constants, let's create a function that returns the total no of films in the sample database, ie, dvdrental, through a 'film_count()' function as shown below.
Letβs create a function to count the total number of films in a sample database.
Query
DO $$
DECLARE
film_count INTEGER;
BEGIN
SELECT COUNT(*) INTO film_count FROM film;
RAISE NOTICE 'The total number of films: %', film_count;
END;
$$;
Output
π PostgreSQL Dollar-Quoted String Constants ExampleExplanation:
film table, and outputs the result.RAISE NOTICE message.The following function finds a film by its 'id'.
Query:
CREATE FUNCTION find_film_by_id(id INT) RETURNS film LANGUAGE SQL AS $$
SELECT * FROM film WHERE film_id = id;
$$;
Now call the function using the below statement:
SELECT find_film_by_id(3);
Output
π PostgreSQL Dollar-Quoted String Constants ExampleExplanation:
The function uses dollar-quoting to encapsulate the SQL query, avoiding the need to escape single quotes or special characters.
The dollar-quoted string constants ($$) in PostgreSQL provide a versatile and powerful way to handle complex string literals and multiline text without the hassle of escaping special characters. This feature is particularly advantageous when writing PL/pgSQL functions, dynamic SQL, or even simple queries requiring strings with quotes or backslashes.
By mastering PostgreSQL dollar-quoted strings, developers can write cleaner, more efficient code and simplify their database operations. This flexibility enhances readability and reduces the likelihood of errors, making it an essential tool in PostgreSQL development.