Sub topic 2
Writing the Body: Dollar Quoting and `BEGIN ATOMIC`
Assume you know basic CREATE FUNCTION syntax, SQL expressions, PostgreSQL data types, and how to run statements in psql or another PostgreSQL client. This chapter focuses on two ways to write the body of a SQL-language function: dollar-quoted text and the SQL-standard BEGIN ATOMIC form.
Dollar-quoted function bodies
A function body is itself a string passed to CREATE FUNCTION. A normal string would require you to escape every single quote inside the function. Dollar quoting avoids that problem.
A dollar-quoted string starts and ends with $$. PostgreSQL sends everything between those markers to the function language as the body:
CREATE FUNCTION add_tax(price numeric, rate numeric DEFAULT 0.20)
RETURNS numeric
LANGUAGE sql
IMMUTABLE
AS $$
SELECT price * (1 + rate);
$$;
SELECT add_tax(100);
SELECT add_tax(100, 0.05);The outer CREATE FUNCTION statement ends after the closing $$ and a semicolon. The semicolon inside the body ends the SQL statement executed by the function. The semicolon after $$ ends the CREATE FUNCTION command.
You can use a tag instead of an empty dollar quote. The opening and closing tags must match:
CREATE FUNCTION format_label(label text)
RETURNS text
LANGUAGE sql
IMMUTABLE
AS $function$
SELECT '[' || label || ']';
$function$;Tagged quotes help when the function body contains $$, such as generated SQL or another quoted value.
`BEGIN ATOMIC` bodies
PostgreSQL 14 and later also support a body written with BEGIN ATOMIC:
CREATE FUNCTION full_name(first_name text, last_name text)
RETURNS text
LANGUAGE sql
IMMUTABLE
STRICT
BEGIN ATOMIC
SELECT first_name || ' ' || last_name;
END;This form contains SQL statements directly in the function definition. PostgreSQL parses those statements when it creates the function. That lets PostgreSQL resolve object references and report many syntax errors immediately.
With a dollar-quoted body, PostgreSQL stores the body text and the SQL-language function parses it when the function runs. This difference affects dependency tracking and error timing.
BEGIN ATOMIC is not a transaction block. It groups the statements that make up the function body. A SQL function can contain multiple statements in either form:
CREATE FUNCTION normalize_name(raw_name text)
RETURNS text
LANGUAGE sql
IMMUTABLE
AS $$
SELECT lower(trim(raw_name));
$$;For a simple SQL function, use the form that fits your deployment environment. Dollar quoting is widely compatible and flexible. BEGIN ATOMIC provides earlier validation and clearer parsing behavior, but requires PostgreSQL 14 or later.
Common errors
- Omitting the semicolon after the closing dollar quote.
- Using different tags, such as
$fn$to open and$$to close. - Treating
BEGIN ATOMICas permission to use procedural control flow such asIFor loops. Those features generally require PL/pgSQL. - Deploying
BEGIN ATOMICdefinitions to PostgreSQL versions older than 14.
Exercise
What is the main parsing difference between a dollar-quoted SQL function body and a BEGIN ATOMIC body?
Show answer
PostgreSQL generally parses a dollar-quoted body when the function runs, but parses a BEGIN ATOMIC body when the function is created.