Introduction to PostgreSQL Functions
PostgreSQL functions are also called as a stored procedure in PostgreSQL; PostgreSQL stored procedure or function is set in SQL statements. Create a statement used to create a new function; we can create a PostgreSQL function in many languages like C, python, SQL and PL/pgsql. The function is essential in PostgreSQL before the PostgreSQL version 11 function will act as a procedure in the PostgreSQL database after PostgreSQL version 11, function and procedure are differentiated. Its function name will be unique, which means it will not be the same as other names of a function that was already created in the database.
Syntax:
Below given is the syntax:
CREATE [OR REPLACE (Replace the function if already exist)] FUNCTION
name (Name of function) ( [ [ argmode (Mode of argument)] [ argname(Name of argument) ] argtype (Type of argument)[ { DEFAULT | = } default_expr (Default expression of function) ] [, ...] ] )
[RETURNS rettype (Return type of functions)
| RETURNS TABLE (Table name which we have used to return from function)(column_name (Name of column) column_type (Data type of column)[, ...] ) ]
{LANGUAGE lang_name (Language name which was used to create function)
| WINDOW (Window function)
| IMMUTABLE | STABLE | VOLATILE (Attribute of function)
| COST execution_cost (Execution costs)
| ROWS result_rows (No of rows returned from function)
| SET configuration_parameter (Specified configuration parameter) {TO value | = value | FROM CURRENT}
| AS 'definition' (Definition of function)
} ...
[WITH (attribute [, ...] ) ] (Display optional function information)
Below is the parameter description of the above syntax:
- Create: Creates a new function.
- Function name: Name of function which we are creating. The function name should be unique.
- Replace: Replace the function name which is already present in the database.
- Argmode: Mode of argument which we have used in function, default argument mode of function is IN.
- Argname: Name of argument which we have used to create a function.
- Argtype: Type of argument which we have used to create a function.
- Default: Default states that if we not given any argument, it will take the default.
- Rettype: This defines as a return type of functions. The return type can be anything composite, base or table column.
- Table: Table name which column we have returned from a function.
- Column name: This defines that the name of columns that we have a return in return table syntax.
- Column type: This defines as a data type of column that we have returned in the return table syntax.
- Language name: This is defined as the name of the language we have used to create a function. The language can be python, pl/pgsql or C.
- Window: This defines as we are creating a window function instead of a plain function.
- Execution cost: This parameter defines as the total time required to execute the function; it shows the positive number.
- Cost: The cost parameter define the execution cost of function.
- Rows: Rows parameter defines as total rows returned from the function.
- Result rows: Result of rows that have returned from the function.
- Configuration parameter: This defines as a configuration parameter set as a different value.
- Definition: This parameter defines a definition of a function.
- Attribute: This will display extra information of a function.
How do PostgreSQL Functions work?
- PostgreSQL functions are also called as a stored procedure or stored function in PostgreSQL.
- PostgreSQL stored procedure or function is set in SQL statements or in PL/SQL statements.
- Its name will be unique means it will not be the same as other names of a function that have already created in the database.
- Create a statement is used in a function to create a new function; also, we can replace the existing function using replace keyword at the time of new function creation.
- We create a new function in many languages like C, python, SQL and PL/pgsql. This language is used to create new function in it.
- Before the version 11 function, it is essential to act as a PostgreSQL procedure after PostgreSQL version 11 function and procedure are differentiated.
- It is nothing but a set of SQL statements that were stored on a database server and invoked after using a SQL interface.
- It is used to execute several queries in one function and return the result as the same that was written in the function.
- We can check all function from the database by using a “\df”. This command is used to display all function from a database.
- We can implement it in different languages. If we do not define the language at the time of function creation, it will choose the default language.
- It uses an interface that defines an argument and return type of function, as we have stated in the function’s syntax.
Examples of PostgreSQL Functions
Given below is the example to create, modify and drop a function:
Example #1: Create PostgreSQL function
Below is an example of create new function.
Code:
CREATE OR REPLACE FUNCTION Func_Test ()
RETURNS integer AS $test$
declare
test integer;
BEGIN
SELECT count (1) into test FROM EMP;
RETURN test;
END;
$test$ LANGUAGE plpgsql;
\df
Output:
In the above example, we have created a function name as Func_Test. We have used pl/pgsql language to create a new function.
We have a count record from EMP table. We can call or execute Func_Test by using the following command are as follows.
Code:
select Func_Test();
\df+
Output:
Example #2: Modify PostgreSQL function
We are changing owner of the Func_Test function from postgres to test user.
Code:
ALTER FUNCTION Func_Test OWNER to test;
\df+
Output:
Example #3: Drop PostgreSQL function
We can drop by using a drop command. We are dropping Func_Test function.
Code:
drop function Func_Test;
\df+
Output:
Conclusion
PostgreSQL function or stored procedure is nothing but a set of SQL statements stored on the database server and invoked after using a SQL interface. Create a statement is used to create a new function in it. We can implement this function in any languages like C, Python and pl/pgsql.
Recommended Articles
This is a guide to PostgreSQL Functions. Here we discuss the introduction to PostgreSQL functions along with how does it works and respective examples. You may also have a look at the following articles to learn more –