Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declare "LANGUAGE plpgsql" before or after the function body?

Is there any difference between:

CREATE FUNCTION func() RETURNS integer
    LANGUAGE plpgsql AS $$
    declare
    begin
      -- do something
    end
$$;

and

CREATE FUNCTION func() RETURNS INTEGER AS $$
    declare
    begin
      -- do something
    end
$$ LANGUAGE plpgsql;

Does LANGUAGE plpgsql basically just have to be outside the scope of the $$ usage?

like image 848
Ben Avatar asked Aug 03 '26 06:08

Ben


1 Answers

No difference whatsoever.
The function body is a string literal. The $$ are just dollar-quotes and could be single quotes, too (but better use dollar quotes!):

  • What are '$$' used for in PL/pgSQL
  • Insert text with single quotes in PostgreSQL

CREATE FUNCTION is a declarative SQL-DDL command, and the order of keywords is pretty free, as per definition in the manual. (Key words within the curly braces in the command definition can be arranged freely, but not the rest).

I eventually settled on always putting the language declaration on top with the rest of the function header, before the function body. Makes more sense.

like image 69
Erwin Brandstetter Avatar answered Aug 06 '26 05:08

Erwin Brandstetter