The function is the set of PostgreSQL statements that stored on the database server and can be invoked using the SQL interface. PostgreSQL function is also known as Stored Procedure (If you are familiar with SQL Server you will be aware with Stored Procedure). In PostgreSQL, procedural languages such as PL/pgSQL, C, Perl, Python, and Tcl are referred to as stored procedures.
Syntax of PostgreSQL function
CREATE [OR REPLACE] FUNCTION function_name (arguments) RETURNS return_datatype AS $variable_name$ DECLARE declaration; [...] BEGIN < function_body > [...] RETURN { variable_name | value } END; LANGUAGE plpgsql; |
function_name: Name of the function
[OR REPLACE]: It is optional, uses it when you want to modify the function
RETURN: It specifies a data type that is same as function body is returning
function_body: The function_body contains the executable parts(PostgreSQL statements).
plpgsql: It specifies the name of the language in which the function is implemented.
For the illustration, see the following example.
1- Create a Product table
CREATETABLEpublic.product ( idserialNOTNULL, brandtextCOLLATEpg_catalog."default", categorytextCOLLATEpg_catalog."default", qtyinteger ) |
2- Insert some records
INSERTINTOproduct(brand,category,qty) VALUES('Arrow','Cloths',3000); INSERTINTOproduct(brand,category,qty) VALUES('Samsung','Mobile',4500); INSERTINTOproduct(brand,category,qty) VALUES('iPad','Tablet',2000); INSERTINTOproduct(brand,category,qty) VALUES('Prestige','Kitchen',200); |
3- Create a function that will return the total product quantity
CREATEORREPLACEFUNCTIONtotalProductQuantity() RETURNSintegerAS$totalQty$ declare sumQtyinteger; BEGIN SELECTSUM(Qty)intosumQtyFROMProduct; RETURNsumQty; END; $totalQty$LANGUAGEplpgsql; |
4- If you got the following message then your function is ready to invoke using the SQL interface.
Success Message: CREATE FUNCTION Query returned successfully in 117 msec. |
5- Invoke the function as below and see the result:
select*fromtotalProductQuantity(); |
Result:
totalProductQuantity
970
5- If you are using pgAdmin then you can see under the functions "totalProductQuantity"
Example of a function as a return table type
1- I want to result set of all records using function, then you can see the following example.
CREATEORREPLACEFUNCTIONGetAllProducts() RETURNSTABLE(prod_idinteger,brandtext,categorytext,qtyinteger) AS$BODY$ BEGIN RETURNQUERYSELECTp.id,p.brand,p.category,p.qtyFROMProductp; END; $BODY$LANGUAGEplpgsql; |
2- Execute the function
select*fromGetAllProducts(); |
Result:
Example of a parameterized function
1- I want to pass the parameter in function and result set accordingly, suppose I want to search product on the Brand basis.
CREATEORREPLACEFUNCTIONSearchByBrand(brandnametext) RETURNSTABLE(prod_idinteger,brandtext,categorytext,qtyinteger) AS$BODY$ BEGIN RETURNQUERYSELECTp.id,p.brand,p.category,p.qtyFROMProductp WHEREp.brandlike'%'||brandname||'%'; END; $BODY$LANGUAGEplpgsql; |
2- Now, run the function as below
select*fromSearchByBrand('Arrow'); |
Result: