PostgreSQL Tutorial
Data Types
Querying & Filtering Data
Managing Tables
Modifying Data
Conditionals
Control Flow
Transactions & Constraints
Working with JOINS & Schemas
Roles & Permissions
Working with Sets
Subquery & CTEs
User-defined Functions
Important In-Built Functions
PostgreSQL PL/pgSQL
Variables & Constants
Stored Procedures
Working with Triggers
Working with Views & Indexes
Errors & Exception Handling
In PostgreSQL, when creating functions, you can specify parameter modes for the function parameters. The parameter modes define how the arguments can be used inside the function body. There are three primary parameter modes:
IN:
OUT:
OUT
parameters, effectively allowing it to return multiple columns.OUT
parameter can be assigned a value inside the function, which will be returned to the caller.INOUT:
Using IN (implicit) and OUT parameters:
CREATE OR REPLACE FUNCTION divide_numbers(numerator double precision, denominator double precision, result OUT double precision, remainder OUT double precision) AS $$ BEGIN result := numerator / denominator; remainder := mod(numerator, denominator); END; $$ LANGUAGE plpgsql; SELECT * FROM divide_numbers(10, 3);
This function takes two numbers, divides them, and returns both the result and the remainder.
Using INOUT parameter:
CREATE OR REPLACE FUNCTION increment_value(INOUT number integer) AS $$ BEGIN number := number + 1; END; $$ LANGUAGE plpgsql; SELECT increment_value(5);
This function takes a number, increments it, and returns the incremented value.
It's essential to understand how to use these parameter modes effectively to create flexible and modular functions in PostgreSQL. While IN
and OUT
parameters are more commonly used, INOUT
can be beneficial when you want to transform a given input and send it back as part of the output.
IN parameter mode in PostgreSQL functions:
CREATE OR REPLACE FUNCTION calculate_square(IN x INTEGER) RETURNS INTEGER AS $$ BEGIN RETURN x * x; END; $$ LANGUAGE plpgsql;
OUT parameter mode in PostgreSQL functions:
CREATE OR REPLACE FUNCTION generate_greeting(OUT greeting TEXT) AS $$ BEGIN greeting := 'Hello, World!'; END; $$ LANGUAGE plpgsql;
INOUT parameter mode in PostgreSQL functions:
CREATE OR REPLACE FUNCTION increment_value(INOUT x INTEGER, IN increment_by INTEGER) AS $$ BEGIN x := x + increment_by; END; $$ LANGUAGE plpgsql;
VARIADIC parameter mode in PostgreSQL functions:
CREATE OR REPLACE FUNCTION concatenate_strings(VARIADIC strings TEXT[]) RETURNS TEXT AS $$ BEGIN RETURN array_to_string(strings, ' '); END; $$ LANGUAGE plpgsql;
How to use different parameter modes in PostgreSQL functions:
CREATE OR REPLACE FUNCTION process_data(IN input_data INTEGER, OUT result_data INTEGER, INOUT status TEXT) AS $$ BEGIN -- Processing logic result_data := input_data * 2; status := 'Processed'; END; $$ LANGUAGE plpgsql;
Mixing parameter modes in PostgreSQL function parameters:
CREATE OR REPLACE FUNCTION mix_parameter_modes(IN x INTEGER, OUT doubled INTEGER, INOUT status TEXT) AS $$ BEGIN doubled := x * 2; status := 'Processed'; END; $$ LANGUAGE plpgsql;
Passing values by reference in PostgreSQL functions:
INOUT
parameter mode to pass values by reference.CREATE OR REPLACE FUNCTION increment_by_reference(INOUT x INTEGER, IN increment_by INTEGER) AS $$ BEGIN x := x + increment_by; END; $$ LANGUAGE plpgsql;
Default values with parameter modes in PostgreSQL functions:
CREATE OR REPLACE FUNCTION greet_person(INOUT name TEXT DEFAULT 'Guest', OUT greeting TEXT) AS $$ BEGIN greeting := 'Hello, ' || name || '!'; END; $$ LANGUAGE plpgsql;
Returning multiple values with OUT and INOUT modes in PostgreSQL:
OUT
and INOUT
parameters to return multiple values.CREATE OR REPLACE FUNCTION calculate_stats(IN x INTEGER, OUT sum INTEGER, OUT average FLOAT) AS $$ BEGIN -- Calculation logic sum := x + 10; average := (x + 10) / 2.0; END; $$ LANGUAGE plpgsql;
Using VARIADIC mode for variable-length argument lists in PostgreSQL:
VARIADIC
mode.CREATE OR REPLACE FUNCTION sum_numbers(VARIADIC numbers INTEGER[]) RETURNS INTEGER AS $$ BEGIN RETURN COALESCE(SUM(numbers), 0); END; $$ LANGUAGE plpgsql;
Input and output parameters in PostgreSQL functions:
IN
, OUT
, and INOUT
parameters for comprehensive functionality.CREATE OR REPLACE FUNCTION process_data(IN input_data INTEGER, OUT result_data INTEGER, INOUT status TEXT) AS $$ BEGIN -- Processing logic result_data := input_data * 2; status := 'Processed'; END; $$ LANGUAGE plpgsql;