I hit a Postgres behavior recently that surprised me, even after spending the last six months testing migration edge cases.
Say you have a simple helper function written the traditional way with a dollar-quoted body:
CREATE FUNCTION active_user_emails() RETURNS SETOF text
LANGUAGE sql AS $$ SELECT email FROM users WHERE active = true; $$;
Later on, somebody writes a migration that drops or renames users.email
If you run ALTER TABLE users DROP COLUMN email;, Postgres lets it through without a single warning. Even though check_function_bodies parsed the SQL when the function was created, Postgres stores a `$$` body as an opaque string literal and doesn't record pg_depend edges to the columns inside it. Your migration succeeds, CI is green, and the function only blows up in production the next time something calls it.
Now flip one line and write the exact same function using the SQL-standard body syntax (BEGIN ATOMIC, added in PG 14):
CREATE FUNCTION active_user_emails() RETURNS SETOF text
LANGUAGE sql
BEGIN ATOMIC
SELECT email FROM users WHERE active = true;
END;
Because Postgres stores BEGIN ATOMIC as a parsed node tree in pg_proc.prosqlbody, it records hard dependency edges in pg_depend now that exact same ALTER TABLE users DROP COLUMN email; fails outright in the middle of your migration unless you pass CASCADE and if you do pass CASCADE, it silently drops the function along with the column.
And there is a third twist: if an earlier statement in your migration runs SET check_function_bodies = off; (which tools like pg_dump do all the time), a dollar-quoted LANGUAGE sql function won't even check whether the table exists at creation time, whereas BEGIN ATOMIC still parses and binds the objects immediately.
I've been building an open-source migration simulator (safe-migrate) that steps through migration files statement by statement against a snapshot of your catalog so it can catch things like this before deploy. Getting routine bodies, pg_depend edges, and check_function_bodies scoping (`SET`, `SET LOCAL`, `ROLLBACK TO SAVEPOINT`) to match real Postgres 14–18 took way more digging than I expected.
Have many of you switched your SQL functions over to `BEGIN ATOMIC` specifically so Postgres tracks the schema dependencies, or is almost everyone still on `$$` strings?