Introducing dollar quoting

Passing code to PostgreSQL as a string is very flexible. However, using single quotes can be an issue. In many programming languages, single quotes show up frequently. To be able to use quotes, people have to escape them when passing the string to PostgreSQL. For many years, this has been the standard procedure. Fortunately, those old times have passed by and new means to pass the code to PostgreSQL are available:

test=# CREATE OR REPLACE FUNCTION mysum(int, int)  
RETURNS int AS 
$$ 
    SELECT $1 + $2; 
$$ LANGUAGE 'sql';  
CREATE FUNCTION 

The solution to the problem of quoting strings is called dollar quoting. Instead of using quotes to start and end strings, you can simply use $$. Currently, I am only aware of two languages ...

Get Mastering PostgreSQL 10 now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.