0% found this document useful (0 votes)
69 views

Anatomy of A Stored Procedure: Function Arguments

- Dollar quoting was introduced in PostgreSQL version 8.0 as a way to write strings in stored procedures without having to escape characters like single quotes. It allows the use of a custom string to mark the start and end of a quoted string. - Stored procedures in PostgreSQL are defined using the PL/pgSQL programming language. PL/pgSQL is a block-structured language similar to Pascal or C with variable declarations, blocks, and control structures. - Functions in PL/pgSQL can accept parameters and return a value. Within the function body, parameters are referred to by position (e.g. $1, $2). The document provides an example of a function to calculate a geometric average.

Uploaded by

naresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
69 views

Anatomy of A Stored Procedure: Function Arguments

- Dollar quoting was introduced in PostgreSQL version 8.0 as a way to write strings in stored procedures without having to escape characters like single quotes. It allows the use of a custom string to mark the start and end of a quoted string. - Stored procedures in PostgreSQL are defined using the PL/pgSQL programming language. PL/pgSQL is a block-structured language similar to Pascal or C with variable declarations, blocks, and control structures. - Functions in PL/pgSQL can accept parameters and return a value. Within the function body, parameters are referred to by position (e.g. $1, $2). The document provides an example of a function to calculate a geometric average.

Uploaded by

naresh
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 8

Thankfully, in PostgreSQL version 8.0 and later, the feature called dollar quoting is available.

Similar to the way that Perl and the UNIX/Linux shells work, dollar quoting allows us to
choose a string to use in place of an opening and closing quote. By choosing a suitable string
that does not occur in our procedure, we do not need to use any escapes. A dollar quote is a
string of zero or more characters between $ characters. So, the previous example looks like this:
create function ... as $$ ... return 'string with a single '' in it '; ... $$ ...
If we have the string $$ in our procedure, we can choose a different dollar quote:
create function ... as $WHAT$ ... return 'string with a $$ in it '; ... $WHAT$ ...
The examples in this chapter from now on will use dollar quoting. If you are using a version of
PostgreSQL before 8.0, we suggest that you upgrade to the latest version. Otherwise, you will
need to use traditional quoting.

Anatomy of a Stored Procedure


Now that we have seen how to create, execute, and drop a sample stored procedure, it is time
to move on to consider the construction of PL/pgSQL stored procedures in more detail.
PL/pgSQL is a block-structured language, like Pascal or C, with variables having declarations
and block scope. Each block has an optional label; it may have some variable declarations, and
encloses statements that make up the block between BEGIN and END keywords. The syntax for a
block is as follows:
[<<label>>]
[DECLARE declarations]
BEGIN
statements
END;
■Note PL/pgSQL is case-insensitive. All keywords and variable names may be written in either case.
A PL/pgSQL function is defined with a CREATE FUNCTION statement with a block as the definition
part, enclosed in quotes (either single quotes or dollar quotes).
-- For all PostgreSQL versions
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
AS 'block definition'
LANGUAGE 'plpgsql';
MatthewStones_4789C10.fm Page 282 Wednesday, February 23, 2005 6:47 AM
CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS 283
-- For PostgreSQL 8.0 and later
CREATE FUNCTION name ( [ [arg] ftype [, ...] ] )
RETURNS rtype
AS $$
block definition
$$ LANGUAGE plpgsql;
Within the definition, as shown in our examples, a double dash (--) introduces a comment
that extends to the end of the line and will be ignored. We’ll look at using comments shortly.
Function Arguments
A PL/pgSQL function can take zero or more parameters, and the types of the parameters are
given in parentheses after the function name. The types are built-in PostgreSQL types, such as
int4 or float8. All stored procedures must return a value, and the return type is specified in the
RETURNS clause of the function definition.
Within a function body, the parameters of the function are referred to as $1, $2, and so on,
in the order they are defined. We will see later that it is possible to give the parameters names
using an ALIAS declaration. In PostgreSQL 8.0, it became possible to give names to parameters
where they are declared.
Consider this simple stored procedure that provides a floating-point geometric average of
two integers:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(int4, int4) returns float8 as $$
begin
return sqrt($1 * $2::float8);
end;
$$ language plpgsql;
Using parameter naming in PostgreSQL 8.0, we can declare the same function like this:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(a int4, b int4) returns float8 as $$
begin
return sqrt(a * b::float8);
end;
$$ language plpgsql;
Notice that we have cast one of the integer values to a floating-point value so that we are
certain to pass a floating-point result of the multiplication to the sqrt function. If we do not do
this, we will run the risk of getting an error saying that the function sqrt(int4) does not exist,
as was the case before version 8.0 of PostgreSQL. Where functions are overloaded, the use of a
cast will ensure that we call the desired version of the function.
MatthewStones_4789C10.fm Page 283 Wednesday, February 23, 2005 6:47 AM
284 CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS
Comments
As you can see from the examples so far, PL/pgSQL allows comments to be included in function
definitions. In fact there are two types of comments: single-line comments and block comments.
A standard SQL single-line comment is introduced by two dashes (--). Everything following
the two dashes up to the end of the line is ignored:
-- This is a single line comment
create -- comments can
function -- come anywhere, and extend to the end of the line
Block comments are used to introduce larger blocks of text as comments or for temporarily
removing sections of code not required. The syntax is the same as C and C++, with blocks of
comments being surrounded by /* and */:
/*
This is a block comment used to describe
the use and behavior of the following function
*/
create function blah() returns integer as $$
begin
/* comment out call

Thankfully, in PostgreSQL version 8.0 and later, the feature called dollar quoting is available.
Similar to the way that Perl and the UNIX/Linux shells work, dollar quoting allows us to
choose a string to use in place of an opening and closing quote. By choosing a suitable string
that does not occur in our procedure, we do not need to use any escapes. A dollar quote is a
string of zero or more characters between $ characters. So, the previous example looks like this:
create function ... as $$ ... return 'string with a single '' in it '; ... $$ ...
If we have the string $$ in our procedure, we can choose a different dollar quote:
create function ... as $WHAT$ ... return 'string with a $$ in it '; ... $WHAT$ ...
The examples in this chapter from now on will use dollar quoting. If you are using a version of
PostgreSQL before 8.0, we suggest that you upgrade to the latest version. Otherwise, you will
need to use traditional quoting.

Anatomy of a Stored Procedure


Now that we have seen how to create, execute, and drop a sample stored procedure, it is time
to move on to consider the construction of PL/pgSQL stored procedures in more detail.
PL/pgSQL is a block-structured language, like Pascal or C, with variables having declarations
and block scope. Each block has an optional label; it may have some variable declarations, and
encloses statements that make up the block between BEGIN and END keywords. The syntax for a
block is as follows:
[<<label>>]
[DECLARE declarations]
BEGIN
statements
END;
■Note PL/pgSQL is case-insensitive. All keywords and variable names may be written in either case.
A PL/pgSQL function is defined with a CREATE FUNCTION statement with a block as the definition
part, enclosed in quotes (either single quotes or dollar quotes).
-- For all PostgreSQL versions
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
AS 'block definition'
LANGUAGE 'plpgsql';
MatthewStones_4789C10.fm Page 282 Wednesday, February 23, 2005 6:47 AM
CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS 283
-- For PostgreSQL 8.0 and later
CREATE FUNCTION name ( [ [arg] ftype [, ...] ] )
RETURNS rtype
AS $$
block definition
$$ LANGUAGE plpgsql;
Within the definition, as shown in our examples, a double dash (--) introduces a comment
that extends to the end of the line and will be ignored. We’ll look at using comments shortly.
Function Arguments
A PL/pgSQL function can take zero or more parameters, and the types of the parameters are
given in parentheses after the function name. The types are built-in PostgreSQL types, such as
int4 or float8. All stored procedures must return a value, and the return type is specified in the
RETURNS clause of the function definition.
Within a function body, the parameters of the function are referred to as $1, $2, and so on,
in the order they are defined. We will see later that it is possible to give the parameters names
using an ALIAS declaration. In PostgreSQL 8.0, it became possible to give names to parameters
where they are declared.
Consider this simple stored procedure that provides a floating-point geometric average of
two integers:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(int4, int4) returns float8 as $$
begin
return sqrt($1 * $2::float8);
end;
$$ language plpgsql;
Using parameter naming in PostgreSQL 8.0, we can declare the same function like this:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(a int4, b int4) returns float8 as $$
begin
return sqrt(a * b::float8);
end;
$$ language plpgsql;
Notice that we have cast one of the integer values to a floating-point value so that we are
certain to pass a floating-point result of the multiplication to the sqrt function. If we do not do
this, we will run the risk of getting an error saying that the function sqrt(int4) does not exist,
as was the case before version 8.0 of PostgreSQL. Where functions are overloaded, the use of a
cast will ensure that we call the desired version of the function.
MatthewStones_4789C10.fm Page 283 Wednesday, February 23, 2005 6:47 AM
284 CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS
Comments
As you can see from the examples so far, PL/pgSQL allows comments to be included in function
definitions. In fact there are two types of comments: single-line comments and block comments.
A standard SQL single-line comment is introduced by two dashes (--). Everything following
the two dashes up to the end of the line is ignored:
-- This is a single line comment
create -- comments can
function -- come anywhere, and extend to the end of the line
Block comments are used to introduce larger blocks of text as comments or for temporarily
removing sections of code not required. The syntax is the same as C and C++, with blocks of
comments being surrounded by /* and */:
/*
This is a block comment used to describe
the use and behavior of the following function
*/
create function blah() returns integer as $$
begin
/* comment out call
Thankfully, in PostgreSQL version 8.0 and later, the feature called dollar quoting is available.
Similar to the way that Perl and the UNIX/Linux shells work, dollar quoting allows us to
choose a string to use in place of an opening and closing quote. By choosing a suitable string
that does not occur in our procedure, we do not need to use any escapes. A dollar quote is a
string of zero or more characters between $ characters. So, the previous example looks like this:
create function ... as $$ ... return 'string with a single '' in it '; ... $$ ...
If we have the string $$ in our procedure, we can choose a different dollar quote:
create function ... as $WHAT$ ... return 'string with a $$ in it '; ... $WHAT$ ...
The examples in this chapter from now on will use dollar quoting. If you are using a version of
PostgreSQL before 8.0, we suggest that you upgrade to the latest version. Otherwise, you will
need to use traditional quoting.

Anatomy of a Stored Procedure


Now that we have seen how to create, execute, and drop a sample stored procedure, it is time
to move on to consider the construction of PL/pgSQL stored procedures in more detail.
PL/pgSQL is a block-structured language, like Pascal or C, with variables having declarations
and block scope. Each block has an optional label; it may have some variable declarations, and
encloses statements that make up the block between BEGIN and END keywords. The syntax for a
block is as follows:
[<<label>>]
[DECLARE declarations]
BEGIN
statements
END;
■Note PL/pgSQL is case-insensitive. All keywords and variable names may be written in either case.
A PL/pgSQL function is defined with a CREATE FUNCTION statement with a block as the definition
part, enclosed in quotes (either single quotes or dollar quotes).
-- For all PostgreSQL versions
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
AS 'block definition'
LANGUAGE 'plpgsql';
MatthewStones_4789C10.fm Page 282 Wednesday, February 23, 2005 6:47 AM
CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS 283
-- For PostgreSQL 8.0 and later
CREATE FUNCTION name ( [ [arg] ftype [, ...] ] )
RETURNS rtype
AS $$
block definition
$$ LANGUAGE plpgsql;
Within the definition, as shown in our examples, a double dash (--) introduces a comment
that extends to the end of the line and will be ignored. We’ll look at using comments shortly.
Function Arguments
A PL/pgSQL function can take zero or more parameters, and the types of the parameters are
given in parentheses after the function name. The types are built-in PostgreSQL types, such as
int4 or float8. All stored procedures must return a value, and the return type is specified in the
RETURNS clause of the function definition.
Within a function body, the parameters of the function are referred to as $1, $2, and so on,
in the order they are defined. We will see later that it is possible to give the parameters names
using an ALIAS declaration. In PostgreSQL 8.0, it became possible to give names to parameters
where they are declared.
Consider this simple stored procedure that provides a floating-point geometric average of
two integers:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(int4, int4) returns float8 as $$
begin
return sqrt($1 * $2::float8);
end;
$$ language plpgsql;
Using parameter naming in PostgreSQL 8.0, we can declare the same function like this:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(a int4, b int4) returns float8 as $$
begin
return sqrt(a * b::float8);
end;
$$ language plpgsql;
Notice that we have cast one of the integer values to a floating-point value so that we are
certain to pass a floating-point result of the multiplication to the sqrt function. If we do not do
this, we will run the risk of getting an error saying that the function sqrt(int4) does not exist,
as was the case before version 8.0 of PostgreSQL. Where functions are overloaded, the use of a
cast will ensure that we call the desired version of the function.
MatthewStones_4789C10.fm Page 283 Wednesday, February 23, 2005 6:47 AM
284 CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS
Comments
As you can see from the examples so far, PL/pgSQL allows comments to be included in function
definitions. In fact there are two types of comments: single-line comments and block comments.
A standard SQL single-line comment is introduced by two dashes (--). Everything following
the two dashes up to the end of the line is ignored:
-- This is a single line comment
create -- comments can
function -- come anywhere, and extend to the end of the line
Block comments are used to introduce larger blocks of text as comments or for temporarily
removing sections of code not required. The syntax is the same as C and C++, with blocks of
comments being surrounded by /* and */:
/*
This is a block comment used to describe
the use and behavior of the following function
*/
create function blah() returns integer as $$
begin
/* comment out call
Thankfully, in PostgreSQL version 8.0 and later, the feature called dollar quoting is available.
Similar to the way that Perl and the UNIX/Linux shells work, dollar quoting allows us to
choose a string to use in place of an opening and closing quote. By choosing a suitable string
that does not occur in our procedure, we do not need to use any escapes. A dollar quote is a
string of zero or more characters between $ characters. So, the previous example looks like this:
create function ... as $$ ... return 'string with a single '' in it '; ... $$ ...
If we have the string $$ in our procedure, we can choose a different dollar quote:
create function ... as $WHAT$ ... return 'string with a $$ in it '; ... $WHAT$ ...
The examples in this chapter from now on will use dollar quoting. If you are using a version of
PostgreSQL before 8.0, we suggest that you upgrade to the latest version. Otherwise, you will
need to use traditional quoting.

Anatomy of a Stored Procedure


Now that we have seen how to create, execute, and drop a sample stored procedure, it is time
to move on to consider the construction of PL/pgSQL stored procedures in more detail.
PL/pgSQL is a block-structured language, like Pascal or C, with variables having declarations
and block scope. Each block has an optional label; it may have some variable declarations, and
encloses statements that make up the block between BEGIN and END keywords. The syntax for a
block is as follows:
[<<label>>]
[DECLARE declarations]
BEGIN
statements
END;
■Note PL/pgSQL is case-insensitive. All keywords and variable names may be written in either case.
A PL/pgSQL function is defined with a CREATE FUNCTION statement with a block as the definition
part, enclosed in quotes (either single quotes or dollar quotes).
-- For all PostgreSQL versions
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
AS 'block definition'
LANGUAGE 'plpgsql';
MatthewStones_4789C10.fm Page 282 Wednesday, February 23, 2005 6:47 AM
CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS 283
-- For PostgreSQL 8.0 and later
CREATE FUNCTION name ( [ [arg] ftype [, ...] ] )
RETURNS rtype
AS $$
block definition
$$ LANGUAGE plpgsql;
Within the definition, as shown in our examples, a double dash (--) introduces a comment
that extends to the end of the line and will be ignored. We’ll look at using comments shortly.
Function Arguments
A PL/pgSQL function can take zero or more parameters, and the types of the parameters are
given in parentheses after the function name. The types are built-in PostgreSQL types, such as
int4 or float8. All stored procedures must return a value, and the return type is specified in the
RETURNS clause of the function definition.
Within a function body, the parameters of the function are referred to as $1, $2, and so on,
in the order they are defined. We will see later that it is possible to give the parameters names
using an ALIAS declaration. In PostgreSQL 8.0, it became possible to give names to parameters
where they are declared.
Consider this simple stored procedure that provides a floating-point geometric average of
two integers:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(int4, int4) returns float8 as $$
begin
return sqrt($1 * $2::float8);
end;
$$ language plpgsql;
Using parameter naming in PostgreSQL 8.0, we can declare the same function like this:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(a int4, b int4) returns float8 as $$
begin
return sqrt(a * b::float8);
end;
$$ language plpgsql;
Notice that we have cast one of the integer values to a floating-point value so that we are
certain to pass a floating-point result of the multiplication to the sqrt function. If we do not do
this, we will run the risk of getting an error saying that the function sqrt(int4) does not exist,
as was the case before version 8.0 of PostgreSQL. Where functions are overloaded, the use of a
cast will ensure that we call the desired version of the function.
MatthewStones_4789C10.fm Page 283 Wednesday, February 23, 2005 6:47 AM
284 CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS
Comments
As you can see from the examples so far, PL/pgSQL allows comments to be included in function
definitions. In fact there are two types of comments: single-line comments and block comments.
A standard SQL single-line comment is introduced by two dashes (--). Everything following
the two dashes up to the end of the line is ignored:
-- This is a single line comment
create -- comments can
function -- come anywhere, and extend to the end of the line
Block comments are used to introduce larger blocks of text as comments or for temporarily
removing sections of code not required. The syntax is the same as C and C++, with blocks of
comments being surrounded by /* and */:
/*
This is a block comment used to describe
the use and behavior of the following function
*/
create function blah() returns integer as $$
begin
/* comment out call

Thankfully, in PostgreSQL version 8.0 and later, the feature called dollar quoting is available.
Similar to the way that Perl and the UNIX/Linux shells work, dollar quoting allows us to
choose a string to use in place of an opening and closing quote. By choosing a suitable string
that does not occur in our procedure, we do not need to use any escapes. A dollar quote is a
string of zero or more characters between $ characters. So, the previous example looks like this:
create function ... as $$ ... return 'string with a single '' in it '; ... $$ ...
If we have the string $$ in our procedure, we can choose a different dollar quote:
create function ... as $WHAT$ ... return 'string with a $$ in it '; ... $WHAT$ ...
The examples in this chapter from now on will use dollar quoting. If you are using a version of
PostgreSQL before 8.0, we suggest that you upgrade to the latest version. Otherwise, you will
need to use traditional quoting.

Anatomy of a Stored Procedure


Now that we have seen how to create, execute, and drop a sample stored procedure, it is time
to move on to consider the construction of PL/pgSQL stored procedures in more detail.
PL/pgSQL is a block-structured language, like Pascal or C, with variables having declarations
and block scope. Each block has an optional label; it may have some variable declarations, and
encloses statements that make up the block between BEGIN and END keywords. The syntax for a
block is as follows:
[<<label>>]
[DECLARE declarations]
BEGIN
statements
END;
■Note PL/pgSQL is case-insensitive. All keywords and variable names may be written in either case.
A PL/pgSQL function is defined with a CREATE FUNCTION statement with a block as the definition
part, enclosed in quotes (either single quotes or dollar quotes).
-- For all PostgreSQL versions
CREATE FUNCTION name ( [ ftype [, ...] ] )
RETURNS rtype
AS 'block definition'
LANGUAGE 'plpgsql';
MatthewStones_4789C10.fm Page 282 Wednesday, February 23, 2005 6:47 AM
CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS 283
-- For PostgreSQL 8.0 and later
CREATE FUNCTION name ( [ [arg] ftype [, ...] ] )
RETURNS rtype
AS $$
block definition
$$ LANGUAGE plpgsql;
Within the definition, as shown in our examples, a double dash (--) introduces a comment
that extends to the end of the line and will be ignored. We’ll look at using comments shortly.
Function Arguments
A PL/pgSQL function can take zero or more parameters, and the types of the parameters are
given in parentheses after the function name. The types are built-in PostgreSQL types, such as
int4 or float8. All stored procedures must return a value, and the return type is specified in the
RETURNS clause of the function definition.
Within a function body, the parameters of the function are referred to as $1, $2, and so on,
in the order they are defined. We will see later that it is possible to give the parameters names
using an ALIAS declaration. In PostgreSQL 8.0, it became possible to give names to parameters
where they are declared.
Consider this simple stored procedure that provides a floating-point geometric average of
two integers:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(int4, int4) returns float8 as $$
begin
return sqrt($1 * $2::float8);
end;
$$ language plpgsql;
Using parameter naming in PostgreSQL 8.0, we can declare the same function like this:
-- geom_avg
-- get a geometric average of two integers
create function geom_avg(a int4, b int4) returns float8 as $$
begin
return sqrt(a * b::float8);
end;
$$ language plpgsql;
Notice that we have cast one of the integer values to a floating-point value so that we are
certain to pass a floating-point result of the multiplication to the sqrt function. If we do not do
this, we will run the risk of getting an error saying that the function sqrt(int4) does not exist,
as was the case before version 8.0 of PostgreSQL. Where functions are overloaded, the use of a
cast will ensure that we call the desired version of the function.
MatthewStones_4789C10.fm Page 283 Wednesday, February 23, 2005 6:47 AM
284 CHAPTER 10 ■ FUNCTIONS, STORED PROCEDURES, AND TRIGGERS
Comments
As you can see from the examples so far, PL/pgSQL allows comments to be included in function
definitions. In fact there are two types of comments: single-line comments and block comments.
A standard SQL single-line comment is introduced by two dashes (--). Everything following
the two dashes up to the end of the line is ignored:
-- This is a single line comment
create -- comments can
function -- come anywhere, and extend to the end of the line
Block comments are used to introduce larger blocks of text as comments or for temporarily
removing sections of code not required. The syntax is the same as C and C++, with blocks of
comments being surrounded by /* and */:
/*
This is a block comment used to describe
the use and behavior of the following function
*/
create function blah() returns integer as $$
begin
/* comment out call

You might also like