commit da8f0114fc9a661c001e49df540ccaa35dbbdb29 Author: David G. Johnston Date: Mon Nov 30 03:17:26 2020 +0000 Minor cleanup and rewording of plpgsql docs diff --git a/doc/src/sgml/plpgsql.sgml b/doc/src/sgml/plpgsql.sgml index 9ec168b0c4..ffe124ab54 100644 --- a/doc/src/sgml/plpgsql.sgml +++ b/doc/src/sgml/plpgsql.sgml @@ -894,7 +894,7 @@ SELECT expression to the main SQL engine. While forming the SELECT command, any occurrences of PL/pgSQL variable names - are replaced by parameters, as discussed in detail in + are replaced by query parameters, as discussed in detail in . This allows the query plan for the SELECT to be prepared just once and then reused for subsequent @@ -976,15 +976,23 @@ my_record.user_id := 20; Executing a Command with No Result - For any SQL command that does not return rows, for example - INSERT without a RETURNING clause, you can - execute the command within a PL/pgSQL function - just by writing the command. + The utility SQL commands, which are incapable of returning results, + (e.g., DDL commands like CREATE TABLE and DROP TYPE, + and the environment changing command SET), are executed just by + writing the command. - Any PL/pgSQL variable name appearing - in the command text is treated as a parameter, and then the + You can also execute a DELETE, INSERT, + or UPDATE SQL command, without a RETURNING + clause, just by writing the command. Adding the RETURNING + makes the command behave like SELECT, which is described + in the next section. + + + + For the non-utility commands, any PL/pgSQL variables + appearing in the command text are replaced by query parameters, and then the current value of the variable is provided as the parameter value at run time. This is exactly like the processing described earlier for expressions; for details see . @@ -2545,7 +2553,7 @@ $$ LANGUAGE plpgsql; - PL/pgSQL variables are substituted into the query text, + PL/pgSQL variables are converted into query parameters, and the query plan is cached for possible re-use, as discussed in detail in and . @@ -4621,9 +4629,10 @@ CREATE EVENT TRIGGER snitch ON ddl_command_start EXECUTE FUNCTION snitch(); SQL statements and expressions within a PL/pgSQL function can refer to variables and parameters of the function. Behind the scenes, PL/pgSQL substitutes query parameters for such references. - Parameters will only be substituted in places where a parameter or - column reference is syntactically allowed. As an extreme case, consider - this example of poor programming style: + Query parameters will only be substituted in places where syntactically allowed + (in particular, SQL identifiers - such as schema, table, and column names - can never + be replaced with a query parameter.) + As an extreme case, consider this example of poor programming style: INSERT INTO foo (foo) VALUES (foo); @@ -4635,13 +4644,6 @@ INSERT INTO foo (foo) VALUES (foo); variable. - - - PostgreSQL versions before 9.0 would try - to substitute the variable in all three cases, leading to syntax errors. - - - Since the names of variables are syntactically no different from the names of table columns, there can be ambiguity in statements that also refer to @@ -5291,13 +5293,14 @@ HINT: Make sure the query returns the exact list of columns. - If a name used in a SQL command could be either a column name of a - table or a reference to a variable of the function, - PL/SQL treats it as a column name. This corresponds - to PL/pgSQL's + If a name used in a SQL command matches a known column name and also + a named variable within the function, + PL/SQL treats it as a column name. + By default, PL/pgSQL will treat it as a variable, + but one can specify plpgsql.variable_conflict = use_column - behavior, which is not the default, - as explained in . + (see for specifics) + to change the behavior to match PL/SQL. It's often best to avoid such ambiguities in the first place, but if you have to port a large amount of code that depends on this behavior, setting variable_conflict may be the