postgresql_16:6.1.5.1 Arguments for SQL Functions
PostgreSQL 16 > Server Programming > Extending SQL > Query Language (SQL) Functions > Arguments for SQL Functions
Arguments of an SQL function can be referenced in the function body using either names or numbers. Examples of both methods appear below.
To use a name, declare the function argument as having a name, and then just write that name in the function body. If the argument name is the same as any column name in the current SQL command within the function, the column name will take precedence. To override this, qualify the argument name with the name of the function itself, that is *function_name*.*argument_name*. (If this would conflict with a qualified column name, again the column name wins. You can avoid the ambiguity by choosing a different alias for the table within the SQL command.)
In the older numeric approach, arguments are referenced using the syntax $*n*: $1 refers to the first input argument, $2 to the second, and so on. This will work whether or not the particular argument was declared with a name.
If an argument is of a composite type, then the dot notation, e.g., *argname*.*fieldname* or $1.*fieldname*, can be used to access attributes of the argument. Again, you might need to qualify the argument's name with the function name to make the form with an argument name unambiguous.
SQL function arguments can only be used as data values, not as identifiers. Thus for example this is reasonable: INSERT INTO mytable VALUES ($1); but this will not work: INSERT INTO $1 VALUES (42);
Note
The ability to use names to reference SQL function arguments was added in PostgreSQL 9.2. Functions to be used in older servers must use the $*n* notation.