SQL procedures vs functions with postgresql (i.e. plpgsql) examples

Q01 What is the difference between SQL stored Procedures & Functions?
A01 Stored procedure does not have a RETURNS clause, but you can use the OUT or INOUT parameter. Stored procedures are transactional whereas functions are not. Apart from these differences, the stored procedures are similar to functions.

You can use functions to update and retrieve data, or to just perform a procedure by returning a type void.

Example 1: plpgsql procedure

Schema SQL in DB-FIDDLE. Select PostgreSQL v17 as the database.

Query SQL

Note that null needs to be passed for the OUT parameter as well as it is in the procedure definition.

Outputs:

Example 2: plpgsql function

A function can use a RETURNS clause or use an OUT parameter as above. It can also return a void as shown below.

Query SQL

Q02 What are $$ in PostgreSQL?
A02 It is used for dollar-quoting, which allows defining string literals or code blocks without escaping special characters like single quotes. It is commonly used in PL/pgSQL functions to simplify the handling of complex text or SQL code.

Instead of $$, you can also use $some string$ as in $body$ or $func$ as shown below:

Example 3: plpgsql function with $body$

in MySQL:

Example 4: plpgsql function with int return type

Schema SQL:

Query SQL:

Outputs:

Example 5: plpgsql function with setof as a return type

Schema SQL:

Query SQL:

Outputs:

How can you return as a table?

Outputs:

to select only state?

Outputs:

Example 6: plpgsql function with table as a return type

Schema SQL:

Query SQL:

Outputs:

Q03 What is the difference between a function using SQL as a language vs plpgsql as a language?
A03 plpgsql and plain SQL functions are both part of the toolsets, and try to use the simplest toolset possible to get the task at hand done.

1. Use views where possible.

2. Where a view is not suitable, use an SQL function.

3. Where an SQL function isn’t suitable, use plpgsql function.

4. Where plpgsql is too limited or not expressive enough, use PL/Perl, PL/Python, PL/Java, or whatever your preference is.

Example 7: function using IN and OUT parameters with SQL language

Schema SQL:

Query SQL:

Outputs:

Example 8: function using IN and OUT parameters as above with plpgsql language

Schema SQL:

Query SQL:

Outputs:

Example 9: function using setof with SQL language

Schema SQL:

Use tbl_employee to return a single row, and setof tbl_employee to return multiple rows.

Query SQL:

Outputs:

Example 10: function using plpgsql language

Schema SQL:

Query SQL:

Outputs:

Example 11: function using plpgsql language using INTO

Schema SQL:

Query SQL:

Outputs:

Example 12: function using plpgsql language using INTO

Schema SQL:

Query SQL:

Outputs:

Example 13: function using plpgsql language with LOOP

Schema SQL:

Query SQL:

Outputs:

Q04 What is a DO block in plpgsql?
A04 DO executes an anonymous code block, or in other words a transient anonymous function in a procedural language.

Example 14: plpgsql language with DO block & RAISE NOTICE

Schema SQL:

Example 15: plpgsql language with DO block & FOREACH LOOP

Schema SQL:

Example 16: plpgsql language with DO block & WHILE LOOP

Schema SQL:

Refer documentation to use CONTINUE and EXIT in loops.


300+ Java Interview FAQs

Tutorials on Java & Big Data