|PL/SQL User's Guide and Reference
10g Release 1 (10.1)
Part Number B10807-01
To be callable from SQL statements, a stored function must obey certain "purity" rules, which control side-effects. (See "Controlling Side Effects of PL/SQL Subprograms".) The fewer side-effects a function has, the better it can be optimized within a query, particular when the
DETERMINISTIC hints are used. The same rules that apply to the function itself also apply to any functions or procedures that it calls.
If any SQL statement inside the function body violates a rule, you get an error at run time (when the statement is parsed). To check for violations of the rules at compile time, you can use the compiler directive
PRAGMA RESTRICT_REFERENCES. This pragma asserts that a function does not read and/or write database tables and/or package variables. Functions that do any of these read or write operations are difficult to optimize, because any call might produce different results or encounter errors.
For more information, see Oracle Database Application Developer's Guide - Fundamentals.
Keyword and Parameter Description
Specifies that the pragma applies to all subprograms in the package spec or object type spec. You can still declare the pragma for individual subprograms. Such pragmas override the default pragma.
A user-defined function or procedure.
Signifies that the statement is a compiler directive. Pragmas are processed at compile time, not at run time. They do not affect the meaning of a program; they convey information to the compiler.
Asserts that the subprogram reads no database state (does not query database tables).
Asserts that the subprogram reads no package state (does not reference the values of packaged variables)
Asserts that the subprogram can be trusted not to violate one or more rules. This value is needed for functions written in C or Java that are called from PL/SQL, since PL/SQL cannot verify them at run time.
Asserts that the subprogram writes no database state (does not modify database tables).
Asserts that the subprogram writes no package state (does not change the values of packaged variables).
You can declare the pragma
RESTRICT_REFERENCES only in a package spec or object type spec. You can specify up to four constraints (
WNPS) in any order. To call a function from parallel queries, you must specify all four constraints. No constraint implies another.
When you specify
TRUST, the function body is not checked for violations of the constraints listed in the pragma. The function is trusted not to violate them. Skipping these checks can improve performance.
If you specify
DEFAULT instead of a subprogram name, the pragma applies to all subprograms in the package spec or object type spec (including the system-defined constructor for object types). You can still declare the pragma for individual subprograms, overriding the default pragma.
RESTRICT_REFERENCES pragma can apply to only one subprogram declaration. A pragma that references the name of overloaded subprograms always applies to the most recent subprogram declaration.
Typically, you only specify this pragma for functions. If a function calls procedures, then you need to specify the pragma for those procedures as well.
This example asserts that the function
BALANCE writes no database state (
WNDS) and reads no package state (
RNPS). That is, it does not issue any DDL or DML statements, and does not refer to any package variables, and neither do any procedures or functions that it calls. It might issue queries or assign values to package variables.
CREATE PACKAGE loans AS FUNCTION balance(account NUMBER) RETURN NUMBER; PRAGMA RESTRICT_REFERENCES (balance, WNDS, RNPS); END loans; / DROP PACKAGE loans;