The documentation says that only BEGIN ATOMIC parses the function body at definition time. Whereas AS parses the function body at execution time:
(and look at the explanation of sql_body)
For PLPGSQL this works the way I would expect:
postgres=# create or replace function t (f int) returns void language plpgsql as $$ begin select abs (f); end; $$;
CREATE FUNCTION
postgres=# create or replace function t (f int) returns void language plpgsql as $$ begin select absasdf (f); end; $$;
CREATE FUNCTION
But for SQL it does not - it's pretty clear some pretty detailed checking of the function body is happening:
postgres=# create or replace function t (f int) returns void language sql as $$ select abs (f); $$;
CREATE FUNCTION
postgres=# create or replace function t (f int) returns void language sql as $$ select absasdf (f); $$;
ERROR: function absasdf(integer) does not exist
LINE 1: ... t (f int) returns void language sql as $$ select absasdf (f...
^
HINT: No function matches the given name and argument types. You might need to add explicit type casts.
The only difference between the last two statements is whether or not the specified function actually exists.
The other thing I don't understand is how this interacts with the behaviour of search_path. My understanding was that the function behaviour could change depending on the search_path at call time; but if that's true it's nonsensical to parse (or at least to look up objects used by) the function at definition time, because the meaning of the body depends on the search_path and could be valid at execution time even if not at definition time.
If we talk about overall syntax, not just things like function name lookup, then my confusion extends to PLPGSQL as well:
postgres=# create or replace function t (f int) returns void language plpgsql as $$ begin select; end; $$;
CREATE FUNCTION
postgres=# create or replace function t (f int) returns void language plpgsql as $$ begin selec; end; $$;
ERROR: syntax error at or near "selec"
LINE 1: ...(f int) returns void language plpgsql as $$ begin selec; end...
^
In other words, while PLPGSQL doesn't look up function names to check that they exist, it won't accept a syntactically invalid function body.
I feel that I must somehow be confusing myself, because the documentation describes how I thought the system worked but I don't see how that can be reconciled with the observed behaviour. Do we need a documentation update?
This is all on a reasonably recent version:
postgres=# select version ();
version
─────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
PostgreSQL 18.1 (Homebrew) on x86_64-apple-darwin23.6.0, compiled by Apple clang version 16.0.0 (clang-1600.0.26.6), 64-bit
(1 row)
Thanks for any insight anybody can provide.