EDUCBA

EDUCBA

MENUMENU
  • Free Tutorials
  • Free Courses
  • Certification Courses
  • 360+ Courses All in One Bundle
  • Login

PostgreSQL Functions

Home » Data Science » Data Science Tutorials » PostgreSQL Tutorial » PostgreSQL Functions

PostgreSQL Functions

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:

Start Your Free Data Science Course

Hadoop, Data Science, Statistics & others

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:

PostgreSQL Function 1

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:

PostgreSQL Function 2JPG

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:

modify

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:

drop

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 –

  1. PostgreSQL UNIQUE Constraint
  2. PostgreSQL LAG()
  3. Top 9 Parameters of PostgreSQL log
  4. PostgreSQL cluster | Examples
  5. Guide to PostgreSQL While Loop
  6. PostgreSQL Auto Increment | Types and Methods
  7. PostgreSQL Trunc() | How to Work?

All in One Data Science Bundle (360+ Courses, 50+ projects)

360+ Online Courses

50+ projects

1500+ Hours

Verifiable Certificates

Lifetime Access

Learn More

0 Shares
Share
Tweet
Share
Primary Sidebar
PostgreSQL Tutorial
  • Advanced
    • PostgreSQL Schema
    • Postgres List Schemas
    • PostgreSQL VARCHAR
    • Array in PostgreSQL
    • PostgreSQL DDL
    • PostgreSQL List Users
    • Postgres Default User
    • Postgres add user
    • PostgreSQL log_statement
    • PostgreSQL String Functions
    • PostgreSQL Compare Strings
    • PostgreSQL Text Search
    • PostgreSQL TEXT
    • PostgreSQL String Array
    • PostgreSQL Constraints
    • PostgreSQL UNIQUE Constraint
    • PostgreSQL INTERSECT
    • PostgreSQL Like
    • Cursors in PostgreSQL
    • PostgreSQL UNION ALL
    • Indexes in PostgreSQL
    • PostgreSQL Index Types
    • PostgreSQL REINDEX
    • PostgreSQL UNIQUE Index
    • PostgreSQL Clustered Index
    • PostgreSQL DROP INDEX
    • PostgreSQL DISTINCT
    • PostgreSQL FETCH
    • PostgreSQL RAISE EXCEPTION
    • PostgreSQL Auto Increment
    • Sequence in PostgreSQL
    • Wildcards in PostgreSQL
    • PostgreSQL Subquery
    • PostgreSQL Alias
    • PostgreSQL LIMIT
    • PostgreSQL Limit Offset
    • PostgreSQL LAG()
    • PostgreSQL Table
    • Postgres Show Tables
    • PostgreSQL Describe Table
    • PostgreSQL Lock Table
    • PostgreSQL ALTER TABLE
    • Postgres Rename Table
    • Postgres DROP Table
    • PostgreSQL Functions
    • PostgreSQL Math Functions
    • PostgreSQL Window Functions
    • Aggregate Functions in PostgreSQL
    • PostgreSQL Primary Key
    • Foreign Key in PostgreSQL
    • PostgreSQL Procedures
    • PostgreSQL Stored Procedures
    • PostgreSQL Views
    • PostgreSQL Materialized Views
    • Postgres Create View
    • PostgreSQL Triggers
    • PostgreSQL DROP TRIGGER
    • PostgreSQL Date Functions
    • PostgreSQL TO_DATE()
    • PostgreSQL Timestamp
    • PostgreSQL CURRENT_TIMESTAMP()
    • PostgreSQL Notify
    • PostgreSQL RANK()
    • PostgreSQL Select
    • PostgreSQL Average
    • PostgreSQL DATE_PART()
    • PostgreSQL EXECUTE
    • PostgreSQL COALESCE
    • PostgreSQL EXTRACT()
    • PostgreSQL Sort
    • PostgreSQL TO_CHAR
    • PostgreSQL Interval
    • PostgreSQL Number Types
    • PostgreSQL ROW_NUMBER
    • Alter Column in PostgreSQL
    • PostgreSQL Identity Column
    • PostgreSQL SPLIT_PART()
    • PostgreSQL CONCAT()
    • PostgreSQL replace
    • PostgreSQL TRIM()
    • PostgreSQL MAX
    • PostgreSQL DELETE
    • PostgreSQL Float
    • PostgreSQL OID
    • PostgreSQL log
    • PostgreSQL REGEXP_MATCHES()
    • PostgreSQL MD5 
    • PostgreSQL NOW()
    • PostgreSQL RANDOM
    • PostgreSQL round
    • PostgreSQL Trunc()
    • PostgreSQL TIME
    • PostgreSQL IS NULL
    • PostgreSQL CURRENT_TIME
    • PostgreSQL MOD()
    • Postgresql Count
    • PostgreSQL Datetime
    • PostgreSQL MIN()
    • PostgreSQL age()
    • PostgreSQL enum
    • PostgreSQL OR
    • PostgreSQL Wal
    • PostgreSQL NOT IN
    • PostgreSQL SET
    • PostgreSQL Current Date
    • PostgreSQL Compare Date
    • PostgreSQL SERIAL
    • PostgreSQL Database
    • PostgreSQL Clone Database
    • PostgreSQL Copy Database
    • PostgreSQL Restore Database
    • PostgreSQL DROP DATABASE
    • PostgreSQL ALTER DATABASE
    • Postgres DROP Database
    • Postgres Dump Database
    • PostgreSQL OFFSET
    • PostgreSQL GRANT
    • PostgreSQL COMMIT
    • PostgreSQL ROLLUP
    • PostgreSQL JSON
    • EXPLAIN ANALYZE in PostgreSQL
    • PostgreSQL Temporary Table
    • PostgreSQL cluster
    • PostgreSQL Replication
    • PostgreSQL Logical Replication
    • PostgreSQL flush privileges
    • PostgreSQL Tablespaces
    • CAST in PostgreSQL
    • PostgreSQL CTE
    • hstore in PostgreSQL
    • PostgreSQL DECODE()
    • PostgreSQL Vacuum
    • PostgreSQL EXCLUDE
    • Postgres Change Password
    • Postgres Delete Cascade
    • PostgreSQL EXCEPT
    • PostgreSQL Roles
    • PostgreSQL Link
    • PostgreSQL Partition
    • PostgreSQL column does not exist
    • PostgreSQL Log Queries
    • PostgreSQL escape single quote
    • PostgreSQL Query Optimization
    • PostgreSQL Character Varying
    • PostgreSQL Transaction
    • PostgreSQL Extensions
    • PostgreSQL Import CSV
    • PostgreSQL Client
    • PostgreSQL caching
    • PostgreSQL JDBC Driver
    • PostgreSQL Interview Questions
  • Basic
    • What is PostgreSQL
    • PostgreSQL Features
    • How to Install PostgreSQL
    • PostgreSQL Versions
    • PostgreSQL Architecture
    • PostgreSQL GUI
    • PostgreSQL Variables
    • PostgreSQL Data Types
    • PostgreSQL NOT NULL
    • PostgreSQL Integer
    • PostgreSQL Boolean
    • PostgreSQL NULLIF
    • PostgreSQL Administration
    • PostgreSQL Commands
    • PostgreSQL Operators
    • PostgreSQL IN Operator
  • Control Statement
    • PostgreSQL IF Statement
    • PostgreSQL if else
    • PostgreSQL CASE Statement
    • PostgreSQL LOOP
    • PostgreSQL For Loop
    • PostgreSQL While Loop
  • Joins
    • Joins in PostgreSQL
    • PostgreSQL Inner Join
    • PostgreSQL Outer Join
    • LEFT OUTER JOIN in PostgreSQL
    • PostgreSQL FULL OUTER JOIN
    • PostgreSQL LEFT JOIN
    • PostgreSQL Full Join
    • PostgreSQL Cross Join
    • PostgreSQL NATURAL JOIN
    • PostgreSQL UPDATE JOIN
  • Queries
    • PostgreSQL Queries
    • PostgreSQL WHERE Clause
    • PostgreSQL WITH Clause
    • PostgreSQL ORDER BY
    • PostgreSQL ORDER BY Random
    • PostgreSQL GROUP BY
    • PostgreSQL group_concat
    • PostgreSQL HAVING
    • PostgreSQL Recursive Query
Footer
About Us
  • Blog
  • Who is EDUCBA?
  • Sign Up
  • Corporate Training
  • Certificate from Top Institutions
  • Contact Us
  • Verifiable Certificate
  • Reviews
  • Terms and Conditions
  • Privacy Policy
  •  
Apps
  • iPhone & iPad
  • Android
Resources
  • Free Courses
  • Database Management
  • Machine Learning
  • All Tutorials
Certification Courses
  • All Courses
  • Data Science Course - All in One Bundle
  • Machine Learning Course
  • Hadoop Certification Training
  • Cloud Computing Training Course
  • R Programming Course
  • AWS Training Course
  • SAS Training Course

© 2020 - EDUCBA. ALL RIGHTS RESERVED. THE CERTIFICATION NAMES ARE THE TRADEMARKS OF THEIR RESPECTIVE OWNERS.

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you
Book Your One Instructor : One Learner Free Class

Let’s Get Started

This website or its third-party tools use cookies, which are necessary to its functioning and required to achieve the purposes illustrated in the cookie policy. By closing this banner, scrolling this page, clicking a link or continuing to browse otherwise, you agree to our Privacy Policy

EDUCBA

*Please provide your correct email id. Login details for this Free course will be emailed to you
EDUCBA Login

Forgot Password?

EDUCBA
Free Data Science Course

Hadoop, Data Science, Statistics & others

*Please provide your correct email id. Login details for this Free course will be emailed to you

Special Offer - All in One Data Science Bundle (360+ Courses, 50+ projects) Learn More