postgresql_16:6.6.13.2 Other Things to Watch For
PostgreSQL 16 > Server Programming > PL/pgSQL — SQL Procedural Language > Porting from Oracle PL/SQL > Other Things to Watch For
This section explains a few other things to watch for when porting Oracle PL/SQL functions to PostgreSQL.
Implicit Rollback after Exceptions
In PL/pgSQL, when an exception is caught by an EXCEPTION clause, all database changes since the block's BEGIN are automatically rolled back. That is, the behavior is equivalent to what you'd get in Oracle with: BEGIN SAVEPOINT s1; ... code here ... EXCEPTION WHEN ... THEN ROLLBACK TO s1; ... code here ... WHEN ... THEN ROLLBACK TO s1; ... code here ... END; If you are translating an Oracle procedure that uses SAVEPOINT and ROLLBACK TO in this style, your task is easy: just omit the SAVEPOINT and ROLLBACK TO. If you have a procedure that uses SAVEPOINT and ROLLBACK TO in a different way then some actual thought will be required.
EXECUTE
The PL/pgSQL version of EXECUTE works similarly to the PL/SQL version, but you have to remember to use quote_literal and quote_ident as described in (see postgresql_16:6.6.5.4). Constructs of the type EXECUTE 'SELECT * FROM $1'; will not work reliably unless you use these functions.
Optimizing PL/pgSQL Functions
PostgreSQL gives you two function creation modifiers to optimize execution: “volatility” (whether the function always returns the same result when given the same arguments) and “strictness” (whether the function returns null if any argument is null). Consult the (see postgresql_16:7.1.65) reference page for details.
When making use of these optimization attributes, your CREATE FUNCTION statement might look something like this: CREATE FUNCTION foo(...) RETURNS integer AS $$ ... $$ LANGUAGE plpgsql STRICT IMMUTABLE;