- publishing free software manuals
The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming Guide
by The PostgreSQL Global Development Group
Paperback (6"x9"), 478 pages
ISBN 9781906966065
RRP £14.95 ($19.95)

Sales of this book support the PostgreSQL project! Get a printed copy>>>

5.4.6 SQL Functions with Default Values for Arguments

Functions can be declared with default values for some or all input arguments. The default values are inserted whenever the function is called with insufficiently many actual arguments. Since arguments can only be omitted from the end of the actual argument list, all parameters after a parameter with a default value have to have default values as well. (Although the use of named argument notation could allow this restriction to be relaxed, it's still enforced so that positional argument notation works sensibly.)

For example:

CREATE FUNCTION foo(a int, b int DEFAULT 2, c int DEFAULT 3)
AS $$
    SELECT $1 + $2 + $3;

SELECT foo(10, 20, 30);
(1 row)

SELECT foo(10, 20);
(1 row)

SELECT foo(10);
(1 row)

SELECT foo();  -- fails since there is no default for the 
  first argument
ERROR:  function foo() does not exist

The = sign can also be used in place of the key word DEFAULT.

ISBN 9781906966065The PostgreSQL 9.0 Reference Manual - Volume 2 - Programming GuideSee the print edition