PostgreSQL document says:
The entire body of a SQL function is parsed before any of it is executed. While a SQL function can contain commands that alter the
system catalogs (e.g., CREATE TABLE
), the effects of such commands
will not be visible during parse analysis of later commands in the
function. Thus, for example, CREATE TABLE foo (...); INSERT INTO foo VALUES(...);
will not work as desired if packaged up into a single
SQL function, since foo won't exist yet when the INSERT
command is parsed.
It's recommended to use PL/pgSQL instead of a SQL function
in this type of situation.
Why "It's recommended to use PL/pgSQL instead of a SQL function in this type of situation", where the PL/pgSQL or SQL function contains commands that alter the system catalogs, such as CREATE TABLE foo (...); INSERT INTO foo VALUES(...);
?
"The entire body of a SQL function is parsed before any of it is executed". Is it not true for a PL/pgSQL function? What differences are between SQL functions and PL/pgSQL functions, in terms of parsing and executing the commands in their bodies?
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…