Skip to main content

Routine bodies

A PostgreSQL function, procedure or DO block keeps its body in a string, and the server reads that string in the routine's own language. routineBodies decides which of those bodies dfmt reads and lays out:

  • all (the default): SQL bodies (language sql) and PL/pgSQL bodies (language plpgsql, and a DO block that names no language).
  • sql: SQL bodies only. PL/pgSQL bodies are kept exactly as written.
  • as_written: none. Every body is kept exactly as written. Only the statement around it is laid out.

Only the routine's declared language decides what a body is. A body in any other language (c, plpython3u, …), a single-quoted body, and a body that does not read in its language are always kept as written, whatever this option says.

Choose sql or as_written for a project that adopts dfmt before it wants its routines re-indented. The statements that create them still get formatted.

A PL/pgSQL body​

create function touch_updated_at() returns trigger language plpgsql as $$ begin new.updated_at := now(); return new; end; $$;
create function touch_updated_at() returns trigger language plpgsql as $$
    begin
        new.updated_at := now();
        return new;
    end;
$$;
create function touch_updated_at() returns trigger language plpgsql as $$ begin new.updated_at := now(); return new; end; $$;
create function touch_updated_at()
returns trigger
language plpgsql
as $$ begin new.updated_at := now(); return new; end; $$;

A SQL body​

create function active_customers(p_since date) returns setof customers language sql stable as $$ select c.* from customers c join orders o on o.customer_id = c.id where o.created_at >= p_since $$;
create function active_customers(p_since date)
returns setof customers
language sql
stable
as $$
    select c.*
    from customers c
    join orders o on o.customer_id = c.id
    where o.created_at >= p_since
$$;
create function active_customers(p_since date) returns setof customers language sql stable as $$ select c.* from customers c join orders o on o.customer_id = c.id where o.created_at >= p_since $$;
create function active_customers(p_since date)
returns setof customers
language sql
stable
as $$ select c.* from customers c join orders o on o.customer_id = c.id where o.created_at >= p_since $$;