A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://www.geeksforgeeks.org/postgresql-create-function-statement/ below:

PostgreSQL - CREATE FUNCTION Statement

PostgreSQL - CREATE FUNCTION Statement

Last Updated : 15 Jul, 2025

PostgreSQL allows developers to create user-defined functions to encapsulate reusable logic, making database operations more efficient and modular. The CREATE FUNCTION statement is used to define a new function, supporting various procedural languages, with plpgsql being the most commonly used in PostgreSQL.

In this article, we will explain PostgreSQL CREATE FUNCTION Statement with its syntax, structure, and practical usage of the CREATE FUNCTION statement, using examples from the dvdrental sample database.

Introduction to PostgreSQL CREATE FUNCTION

The CREATE FUNCTION statement in PostgreSQL is a powerful tool for defining custom functions that can be reused throughout our database operations. These functions can accept parameters, perform operations, and return values. Functions are especially useful for simplifying complex queries and centralizing logic that can be executed multiple times without rewriting the code.

Syntax

CREATE [OR REPLACE] FUNCTION function_name(param_list)
RETURNS return_type
LANGUAGE plpgsql
AS
$$
DECLARE
-- variable declaration
BEGIN
-- logic
END;
$$;
Key terms Example of PostgreSQL CREATE FUNCTION

Let us explain an example of CREATE FUNCTION Statement in PostgreSQL to better understand the concept. For the purpose of example, we will use the sample database ie, dvdrental. The following statement creates a function that counts the films whose length between the 'len_from' and 'len_to' parameters:

Query:

CREATE FUNCTION get_film_count(len_from INT, len_to INT)
RETURNS INT
LANGUAGE plpgsql
AS
$$
DECLARE
film_count INTEGER;
BEGIN
SELECT COUNT(*)
INTO film_count
FROM film
WHERE length BETWEEN len_from AND len_to;

RETURN film_count;
END;
$$;

The function 'get_film_count' has two main sections: header and body

Header Section Body Section

Output

Running the Function

To create the function, follow these steps:

1. Using pgAdmin: Open pgAdmin, navigate to the dvdrental database, and enter the function code into the query editor. Click Execute to run the query

CREATE FUNCTION

2. Using psql: Open psql, connect to the dvdrental database, and run the function creation script. Once executed, we should see:

CREATE FUNCTION

3. Listing Functions: Finally, use the below command to list all user-defined in the current database:

\df

This will show you all the user-defined functions in the database as shown below:

Calling the Function

Once the function is created, we can call it using different notations:

1. Positional Notation

In this example, the function counts films with lengths between 40 and 90.

Query:

SELECT get_film_count(40, 90);

Output

 get_film_count 
----------------
325
(1 row)
2. Named Notation

Named notation makes it clearer which parameters are being passed to the function.

Query:

SELECT get_film_count(len_from => 40, len_to => 90);

Output

 get_film_count 
----------------
325
(1 row)
Important Points About PostgreSQL CREATE FUNCTION Statement Conclusion

Mastering the PostgreSQL CREATE FUNCTION statement opens the door to writing reusable and efficient database logic. Functions help simplify complex queries, improve performance, and keep our code organized. Whether we are writing a function to calculate values dynamically or retrieve specific data.

Using functions in PostgreSQL adds an extra layer of flexibility to our database management. Functions in PostgreSQL are not just limited to simple queries; they can be used in various advanced scenarios, making PostgreSQL a robust and versatile database system for developers.



RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4