PRAGMA DIRECTIVES:
Pragma compiler directives instructs compiler with certain information. It is Processed at compile time not at run time. The directive restricts member subprograms to query or modify database tables and packaged variables.
PRAGMA directive should be in declarative section of the PL-SQL block.
As of release 11.2 we have 5 PRAGMA directives:
1) PRAGMA AUTONOMOUS_TRANSACTION
2) PRAGMA SERIALLY_REUSABLE
3) PRAGMA RESTRICT_REFRENCES
4) PRAGMA EXCEPTION_INIT
5) PRAGMA INLINE
We will see fundamental use of each Pragma:
PRAGMA AUTONOMOUS_TRANSACTION :
This directive instructs the compiler to treat current PL-SQL block as independent from the mail transaction from which it is called. This means that any changes made to the database in the autonomous transaction are independent of the main transaction and are either committed or rolled back without affecting the main transaction.
Once started, an autonomous transaction is fully independent. It shares no locks, resources, or commit-dependencies with the main transaction. You can log events, increment retry counters, and so on, even if the main transaction rolls back.
Autonomous triggers (Trigger with Autonomous transaction directive) can contain commit or rollback as well as DDL statements.
If you try to exit an active autonomous transaction without committing or rolling back, Oracle raises an exception. If the exception goes unhandled, or if the transaction ends because of some other unhandled exception, the transaction is rolled back.
Changes made by an autonomous transaction become visible to other transactions when the autonomous transaction commits. The changes also become visible to the main transaction when it resumes, but only if its isolation level is set to READ COMMITTED (the default). If you set the isolation level of the main transaction to SERIALIZABLE, changes made by its autonomous transactions are not visible to the main transaction when it resumes.
In the main transaction, rolling back to a savepoint located before the call to the autonomous subprogram does not roll back the autonomous transaction. Remember, autonomous transactions are fully independent of the main transaction.
If an autonomous transaction attempts to access a resource held by the main transaction (which cannot resume until the autonomous routine exits), a deadlock can occur. Oracle raises an exception in the autonomous transaction, which is rolled back if the exception goes unhandled.
PRAGMA SERIALLY_REUSABLE:
sing this Pragma, State of any package will be lost after its call to server. So It will be helpful to reduce memory consumption in long running process.The state information for this package is not kept in Memory per user but It is kept in SGA for reuses.
The pool is kept in SGA memory so that the work area of a package can be reused across users who have requests for the same package. In this scheme, the maximum number of work areas needed for a package is only as many as there are concurrent users of the package, which is typically much fewer than the total number of logged on users. The user of "serially reusable" packages does increase the shared-pool requirements slightly, but this is more than offset by the decrease in the per-user memory requirements.
Further, Oracle ages out work areas not in use when it needs to reclaim shared pool memory.
PRAGMA RESTRICT_REFERENCES:
This directive instructs compiler that Pl-SQL block should not read or write table or package variables.
If user defined functions are used to any extent it is advisable to encapsulate them within a package and designate this package as "pure", that is, the functions in the package do not modify the database. The "purity" of the package is denoted with the PL/SQL pragma: RESTRICT_REFERENCES. This pragma has one mandatory argument and three optional arguments. The optional arguments define the level of the "purity" of the function.
You can declare the pragma RESTRICT_REFERENCES only in a package spec or object type spec. You can specify up to four constraints (RNDS, RNPS, WNDS, WNPS) To call the function from parallel queries, you must specify all four constraints. No constraint implies another. For example, WNPS does not imply RNPS.
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.
If you specify DEFAULT instead of a function name, the pragma applies to all functions in the package spec or object type spec (including, in the latter case, the system-defined constructor). You can still declare the pragma for individual functions. Such pragmas override the default pragma.
A RESTRICT_REFERENCES pragma can apply to only one function declaration. So, a pragma that references the name of overloaded functions always applies to the nearest foregoing function declaration.
RNDS
Asserts that the subprogram reads no database state (does not query database tables).
WNDS
Asserts that the subprogram writes no database state (does not modify tables).
RNPS
Asserts that the subprogram reads no package state (does not reference the values of packaged variables)
You cannot specify RNPS if the subprogram invokes the SQLCODE or SQLERRM function.
WNPS
Asserts that the subprogram writes no package state (does not change the values of packaged variables).
You cannot specify WNPS if the subprogram invokes the SQLCODE or SQLERRM function.
TRUST
Asserts that the subprogram can be trusted not to violate one or more rules.
When you specify TRUST, the subprogram body is not checked for violations of the constraints listed in the pragma. The subprogram is trusted not to violate them. Skipping these checks can improve performance. TRUST is needed for functions written in C or Java that are invoked from PL/SQL, since PL/SQL cannot verify them at run time.
Usage Notes:
A RESTRICT_REFERENCES pragma can appear only in a package specification or object type specification. Typically, this pragma is specified for functions. If a function calls procedures, specify the pragma for those procedures also.
To invoke a subprogram from parallel queries, you must specify all four constraints—RNDS, WNDS, RNPS, and WNPS. No constraint implies another.
Example:
CREATE PACKAGE test_sr AS
FUNCTION test_function RETURN BOOLEAN;
PRAGMA RESTRICT_REFERENCES (test_function, WNDS, RNPS);
END loans;
/
PRAGMA EXCEPTION_INIT:
This Pragma will associate used defined exception with Oracle Error number. You can catch user defined exception in exception handler.
PRAGMA EXCEPTION_INIT(exception_name, ORACLE_error_number);
where exception_name is the name of a previously declared exception.
DECLARE
insufficient_privileges EXCEPTION;
PRAGMA EXCEPTION_INIT(insufficient_privileges, -1031);
---
----
BEGIN
…
EXCEPTION
WHEN insufficient_privileges THEN
— handle the error
…
END;
PRAGMA INLINE :
The INLINE pragma specifys that a subprogram call is, or is not, to be inlined. Inlining replaces a subprogram call (to a subprogram in the same program unit) with a copy of the called subprogram.
To reiterate, with subprogram inlining, Oracle will re-write our code to replace a call or calls to a subprogram with the executable code of the subprogram itself.
when the INLINE pragma immediately precedes one of these statements, the pragma affects every call to the specified subprogram in that statement
Pragma compiler directives instructs compiler with certain information. It is Processed at compile time not at run time. The directive restricts member subprograms to query or modify database tables and packaged variables.
PRAGMA directive should be in declarative section of the PL-SQL block.
As of release 11.2 we have 5 PRAGMA directives:
1) PRAGMA AUTONOMOUS_TRANSACTION
2) PRAGMA SERIALLY_REUSABLE
3) PRAGMA RESTRICT_REFRENCES
4) PRAGMA EXCEPTION_INIT
5) PRAGMA INLINE
We will see fundamental use of each Pragma:
PRAGMA AUTONOMOUS_TRANSACTION :
This directive instructs the compiler to treat current PL-SQL block as independent from the mail transaction from which it is called. This means that any changes made to the database in the autonomous transaction are independent of the main transaction and are either committed or rolled back without affecting the main transaction.
Once started, an autonomous transaction is fully independent. It shares no locks, resources, or commit-dependencies with the main transaction. You can log events, increment retry counters, and so on, even if the main transaction rolls back.
Autonomous triggers (Trigger with Autonomous transaction directive) can contain commit or rollback as well as DDL statements.
If you try to exit an active autonomous transaction without committing or rolling back, Oracle raises an exception. If the exception goes unhandled, or if the transaction ends because of some other unhandled exception, the transaction is rolled back.
Changes made by an autonomous transaction become visible to other transactions when the autonomous transaction commits. The changes also become visible to the main transaction when it resumes, but only if its isolation level is set to READ COMMITTED (the default). If you set the isolation level of the main transaction to SERIALIZABLE, changes made by its autonomous transactions are not visible to the main transaction when it resumes.
In the main transaction, rolling back to a savepoint located before the call to the autonomous subprogram does not roll back the autonomous transaction. Remember, autonomous transactions are fully independent of the main transaction.
If an autonomous transaction attempts to access a resource held by the main transaction (which cannot resume until the autonomous routine exits), a deadlock can occur. Oracle raises an exception in the autonomous transaction, which is rolled back if the exception goes unhandled.
PRAGMA SERIALLY_REUSABLE:
sing this Pragma, State of any package will be lost after its call to server. So It will be helpful to reduce memory consumption in long running process.The state information for this package is not kept in Memory per user but It is kept in SGA for reuses.
The pool is kept in SGA memory so that the work area of a package can be reused across users who have requests for the same package. In this scheme, the maximum number of work areas needed for a package is only as many as there are concurrent users of the package, which is typically much fewer than the total number of logged on users. The user of "serially reusable" packages does increase the shared-pool requirements slightly, but this is more than offset by the decrease in the per-user memory requirements.
Further, Oracle ages out work areas not in use when it needs to reclaim shared pool memory.
PRAGMA RESTRICT_REFERENCES:
This directive instructs compiler that Pl-SQL block should not read or write table or package variables.
If user defined functions are used to any extent it is advisable to encapsulate them within a package and designate this package as "pure", that is, the functions in the package do not modify the database. The "purity" of the package is denoted with the PL/SQL pragma: RESTRICT_REFERENCES. This pragma has one mandatory argument and three optional arguments. The optional arguments define the level of the "purity" of the function.
You can declare the pragma RESTRICT_REFERENCES only in a package spec or object type spec. You can specify up to four constraints (RNDS, RNPS, WNDS, WNPS) To call the function from parallel queries, you must specify all four constraints. No constraint implies another. For example, WNPS does not imply RNPS.
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.
If you specify DEFAULT instead of a function name, the pragma applies to all functions in the package spec or object type spec (including, in the latter case, the system-defined constructor). You can still declare the pragma for individual functions. Such pragmas override the default pragma.
A RESTRICT_REFERENCES pragma can apply to only one function declaration. So, a pragma that references the name of overloaded functions always applies to the nearest foregoing function declaration.
RNDS
Asserts that the subprogram reads no database state (does not query database tables).
WNDS
Asserts that the subprogram writes no database state (does not modify tables).
RNPS
Asserts that the subprogram reads no package state (does not reference the values of packaged variables)
You cannot specify RNPS if the subprogram invokes the SQLCODE or SQLERRM function.
WNPS
Asserts that the subprogram writes no package state (does not change the values of packaged variables).
You cannot specify WNPS if the subprogram invokes the SQLCODE or SQLERRM function.
TRUST
Asserts that the subprogram can be trusted not to violate one or more rules.
When you specify TRUST, the subprogram body is not checked for violations of the constraints listed in the pragma. The subprogram is trusted not to violate them. Skipping these checks can improve performance. TRUST is needed for functions written in C or Java that are invoked from PL/SQL, since PL/SQL cannot verify them at run time.
Usage Notes:
A RESTRICT_REFERENCES pragma can appear only in a package specification or object type specification. Typically, this pragma is specified for functions. If a function calls procedures, specify the pragma for those procedures also.
To invoke a subprogram from parallel queries, you must specify all four constraints—RNDS, WNDS, RNPS, and WNPS. No constraint implies another.
Example:
CREATE PACKAGE test_sr AS
FUNCTION test_function RETURN BOOLEAN;
PRAGMA RESTRICT_REFERENCES (test_function, WNDS, RNPS);
END loans;
/
PRAGMA EXCEPTION_INIT:
This Pragma will associate used defined exception with Oracle Error number. You can catch user defined exception in exception handler.
PRAGMA EXCEPTION_INIT(exception_name, ORACLE_error_number);
where exception_name is the name of a previously declared exception.
DECLARE
insufficient_privileges EXCEPTION;
PRAGMA EXCEPTION_INIT(insufficient_privileges, -1031);
---
----
BEGIN
…
EXCEPTION
WHEN insufficient_privileges THEN
— handle the error
…
END;
PRAGMA INLINE :
The INLINE pragma specifys that a subprogram call is, or is not, to be inlined. Inlining replaces a subprogram call (to a subprogram in the same program unit) with a copy of the called subprogram.
To reiterate, with subprogram inlining, Oracle will re-write our code to replace a call or calls to a subprogram with the executable code of the subprogram itself.
when the INLINE pragma immediately precedes one of these statements, the pragma affects every call to the specified subprogram in that statement
- Assignment
- Call
- Conditional
- CASE
- CONTINUE WHEN
- EXECUTE IMMEDIATE
- EXIT WHEN
- LOOP
- RETURN