PL/SQL Compiler
- Several initialization parameters (compiler parameters) affect the compilation of PL/SQL units.
- The values at the time of compilation of the
PLSQL_CCFLAGS, PLSQL_CODE_TYPE, PLSQL_DEBUG, PLSQL_OPTIMIZE_LEVEL, PLSQL_WARNINGS, and NLS_LENGTH_SEMANTICS
initialization parameters are stored with the unit's metadata. ALL_PLSQL_OBJECT_SETTINGS
view: Keeps information about the settings of these parameters.
PLSCOPE_SETTINGS | Controls compile-time collection, cross-reference, and storage of PL/SQL source text identifier data. PLSCOPE_SETTINGS = IDENTIFIERS:{ NONE | ALL } | PLSQL_CCFLAGS | Enables you to control conditional compilation of each PL/SQL unit independently. | PLQL_CODE_TYPE | Defines compilation mode for PL/SQL libary units.PLSQL_CODE_TYPE = { INTERPRETED | NATIVE } |
PLSQL_OPTIMIZE_LEVEL | The higher the setting of this parameter, the more effort the compiler makes to optimize PL/SQL library units.Range: {0,3} |
PLSQL_DEBUG | Specifies whether to compile PL/SQL units for debugging. Deprecated on 11g. To compile PL/SQL units for debugging, specify PLSQL_OPTIMIZE_LEVEL=1 . |
PLSQL_NATIVE_LIBRARY_DIR PLSQL_NATIVE_LIBRARY_SUBDIR_COUNT | Related to Native Compilation.Deprecated on 11g. | PLSQL_WARNINGS | Enables/disables the reporting of warning messages by the PL/SQL compiler. Compile-time warning categories: [ SEVERE | PERFORMANCE | INFORMATIONAL ] |
NLS_LENGTH_SEMANTICS |
PL/SQL Optimizer
Prior to Oracle Database 10g Release 1 (10.1), the PL/SQL compiler translated your source text to system code without applying many changes to improve performance. Now, PL/SQL uses an optimizer that can rearrange code for better performance.
The optimizer is enabled by default. In rare cases, if the overhead of the optimizer makes compilation of very large applications too slow, you can lower the optimization by setting the compilation parameter PLSQL_OPTIMIZE_LEVEL=1 instead of its default value 2. In even rarer cases, PL/SQL might raise an exception earlier than expected or not at all. Setting PLSQL_OPTIMIZE_LEVEL=1 prevents the code from being rearranged.
On 10g R1 |
PLSQL_OPTIMIZE_LEVEL
init parameter: Controls Global optimization of Pl/SQL Code. Default value is often good enough.Native compilation: fewer initialization parameters to set and less compiler configuration. Object code is stored in the database.
PLSQL_NATIVE_LIBRARY_DIR:
Only parameter required.(Deprecated on 11g)PLSQL_CODE_TYPE:
turn native compilation ON|OFF.PLSQL_COMPILER_FLAGS
: Deprecated.$ORACLE_HOME/plsql/spnc_commands
file contains the commands and options for compiling and linkingdbmsupgnv.sql
: recompiles all the PL/SQL modules in a database as NATIVE.dbmsupgin.sql
: recompiles all the PL/SQL modules in a database as INTERPRETED.
Compile-Time Warnings
- May be issued when you compile subprograms that produce ambiguous results or use inefficient constructs.
- Enable | Disable warnigns: Use
PLSQL_WARNINGS
init parameter orDBMS_WARNING
package.
On 10g R2 |
- Conditional Compilation
- Enables selective inclusion of code depending on the values of the conditions evaluated during compilation. For example:
- You can determine which PL/SQL features in a PL/SQL application are used for specific database releases.
- Also useful when you want to execute debugging procedures in a development environment, but want to turn off the debugging routines in a production environment.
- Dynamic Wrap
DBMS_DDL
wrap subprograms obfuscate (hide) dynamically generated PL/SQL code units.
--------------------------------------
Pragmas |
Pragma Autonomous_transaction
- Marks a routine [(not-nested) anonymous block, subprogram, method (of ADT), Trigger] as autonomous; that is, independent of the main transaction
- Autonomous transactions do SQL operations and commit or rollback, without committing or rolling back the main transaction.
- When you enter the executable section of an autonomous routine, the main transaction suspends. When you exit the routine, the main transaction resumes.
- If you try to exit an active autonomous transaction without committing or rolling back, the database raises an exception.
- To exit normally, you must explicitly commit or roll back all autonomous transactions.
(1) Autonomous PL/SQL Block
-- create table emp as bellow: SQL> create table emp (name varchar2(20), jobid varchar2(10)); -- insert four initial rows and commit inserts. SQL> insert into emp values('John', 'Accounting'); SQL> insert into emp values('Paul', 'Accounting'); SQL> insert into emp values('George', 'Ads'); SQL> insert into emp values('Ringo', 'Ads'); SQL> commit; -- check the inserted rows. SQL> select * from emp; NAME JOBID -------------------- ---------- John Accounting Paul Accounting George Ads Ringo Ads -- insert a 5th row. and do not commit. -- Check the inserted row. SQL> insert into emp values('Ghost', 'Ads'); 1 row created. SQL> select * from emp; NAME JOBID -------------------- ---------- John Accounting Paul Accounting George Ads Ringo Ads Ghost Ads -- Now add the autonomous block. SQL> set serveroutput on SQL> declare 2 pragma autonomous_transaction; 3 numemp number; 4 begin 5 select count(*) into numemp from emp; 6 dbms_output.put_line('num emps before autonomous insert: ' || numemp); 7 insert into emp values('Autonomous', 'New'); 8 dbms_output.put_line('Inserted: Autonomous'); 9 select count(*) into numemp from emp; 10 dbms_output.put_line('num emps after autonomous insert: ' || numemp); 11 commit; 12 end; 13 / num emps before autonomous insert: 4 Inserted: Autonomous num emps after autonomous insert: 5 PL/SQL procedure successfully completed. -- Now outside the autonomous block, check the existing -- rows and rollback the insertion of "Ghost" SQL> select * from emp; NAME JOBID -------------------- ---------- John Accounting Paul Accounting George Ads Ringo Ads Ghost Ads Autonomous New SQL> rollback; Rollback complete. -- Check that the row for "Ghost" was rolled back, but the -- value inserted in the autonomous transaction remained. SQL> select * from emp; NAME JOBID -------------------- ---------- John Accounting Paul Accounting George Ads Ringo Ads Autonomous New SQL>
(2) Autonomous standalone procedure
In the example above, the anonymous block could be rewritten as an autonomous procedure
as below:
CREATE OR REPLACE PROCEDURE ins_emp AS pragma autonomous_transaction; numemp number; BEGIN select count(*) into numemp from emp; dbms_output.put_line('num emps before autonomous insert: ' || numemp); insert into emp values('Autonomous', 'New'); dbms_output.put_line('Inserted: Autonomous'); select count(*) into numemp from emp; dbms_output.put_line('num emps after autonomous insert: ' || numemp); commit; END ins_emp;
(3) Autonomous Triggers
A trigger must be autonomous to run TCL or DDL statements.
To run DDL statements, the trigger must use native dynamic SQL.
Associates a user-defined exception name with an error code.
Can appear only in the same declarative part as its associated exception, anywhere after the exception declaration.
DECLARE past_due EXCEPTION; -- declare exception PRAGMA EXCEPTION_INIT (past_due, -20000); -- assign error code to exception BEGIN ...Check herefor an example.
Pragma Inline (check here)
- Specifies whether a subprogram invocation (or statement) is to be inlined.
- Inlining replaces a subprogram invocation with a copy of the invoked subprogram (if the invoked and invoking subprograms are in the same program unit).
- To allow subprogram inlining:
PLSQL_OPTIMIZE_LEVEL
compilation parameter := [2 (default) | 3 ] - If a particular subprogram is inlined, performance almost always improves. However, because the compiler inlines subprograms early in the optimization process, it is possible for subprogram inlining to preclude later, more powerful optimizations.
Pragma Restrict_References
- Asserts that a user-defined subprogram does not read or write database tables or package variables.
- Can appear only in a package specification or ADT specification. Typically, this pragma is specified for functions.
- Subprograms that read or write database tables or package variables are difficult to optimize, because any invocation of the subprogram might produce different results or encounter errors. .
Package... | memory usage |
---|---|
...IS NOT SERIALY_REUSABLE | Package state stored in the user global area (UGA) for each user. The amount of UGA memory needed increases linearly with the number of users. Package state can persist for the life of a session, locking UGA memory. |
...IS SERIALY_REUSABLE | Package state stored in a work area in a small pool in the system global area (SGA). Package state persists only for the life of a server call, after which the work area returns to the pool. If a subsequent server call references the package, then Oracle Database reuses an instantiation from the pool. Reusing an instantiation re-initializes it; therefore, changes made to the package state in previous server calls are invisible. |
CREATE OR REPLACE PACKAGE pkg IS PRAGMA SERIALLY_REUSABLE; n number:= 5; END; / CREATE OR REPLACE PACKAGE BODY pkg IS PRAGMA SERIALLY_REUSABLE; BEGIN n := 5; END; /
- Allow you to better manage memory for scalability.
- Specifies that the package state is needed for only one call to the server, after which the storage for the package variables can be reused, reducing the memory overhead for long-running sessions.
- Appropriate for packages that declare large temporary work areas that are used once in the same session.
--------------------------
Conditional compilation |
- The Conditional compilation (CC) feature and related PL/SQL packages are available for the 10g Release 1 (10.1.0.4) and later.
- CC allows constructs — with formally defined syntax and semantics — to be used to mark up text so that a preprocessor can deterministically derive the text that will be submitted to the compiler proper.
- CC uses (a) selection directives($) (similar to IF stmt), (b) inquiry directives($$), and (c) error directives.
- CC uses preprocessor control tokens($) to mark code that is processed before the PL/SQL unit is compiled:
$IF, $THEN, $ELSE, $ELSIF, $ERROR
- Oracle’s Applications Division provided the use case that motivated the introduction of PL/SQL conditional compilation. They wanted their code to be able to span different releases of Oracle Database, using the latest features in the latest release and using a fallback in earlier releases.(see more here)
Uses of Conditional Compilation (CC) {see detailed discussion in this white paper}
- Allowing self-tracing code to be turned on during development and to be turned off when the code goes live.
- Allowing alternative code fragments, each appropriate for the peculiarities of a particular operating system and inappropriate or illegal for other operating systems, to coexist in the same source text so the correct fragment can be selected for compilation according to the circumstances.
- Newer Oracle releases introduce new features with new syntax and programs that take advantage of these are illegal in earlier releases. PL/SQL conditional compilation supports this use in an elegant and powerful way.
- A developer often realizes that more than one approach to the design of a subprogram will result in its correct behavior; sometimes the alternative approaches result in source code versions which are textually largely the same but which differ critically in small areas distributed fairly evenly thought the source. PL/SQL conditional compilation allows all the approaches to be coded in a single source text — while they are being evaluated — and thereby eliminates the risk of carelessly introduced unintended differences.
- Modular delivery of extra functionality can be implemented by optional PL/SQL compilation units which are installed according to what the customer has licensed. PL/SQL’s dependency model prevents the core part of the application referring statically to optional components that are not installed. However, the core part of the application should not need reinstallation in order to accommodate the installation of a new optional component. This has forced the use of dynamic invocation — which has some drawbacks. Conditional compilation allows a new approach.
Using selection directives
($IF, $ELSE)
and error directives ($ERROR)
set serveroutput on begin $IF dbms_db_version.ver_le_10_1 $THEN $ERROR 'unsupported database release' $END $ELSE dbms_output.put_line ( 'release ' || dbms_db_version.version || '.' || dbms_db_version.release || ' is supported.' ); -- since its a newer release, use a new commit syntax -- supported in 10.2 commit write immediate nowait; $END end; / anonymous block completed release 11.2 is supported.
The package
DBMS_DB_VERSION
provide static constants:- dbms_db_version.version
- dbms_db_version.release
- dbms_db_version.ver_le_v
- dbms_db_version.ver_le_v_r
Inquiry directives($$)
$$PLSQL_LINE:
the number of the source line on which the directive appears in the current PL/SQL unit.$$PLSQL_UNIT:
the name of the current PL/SQL unit.
- You can assign values to inquiry directives with the
PLSQL_CCFLAGS
compilation parameter ALTER SESSION set PLSQL_CCFLAGS = 'flag:True, val:5'
$$plsql_compilation_parameter
: a PL/SQL compilation parameter
Displaying Values of PL/SQL Compilation Parameters
BEGIN DBMS_OUTPUT.PUT_LINE('$$PLSCOPE_SETTINGS = ' || $$PLSCOPE_SETTINGS); DBMS_OUTPUT.PUT_LINE('$$PLSQL_CCFLAGS = ' || $$PLSQL_CCFLAGS); DBMS_OUTPUT.PUT_LINE('$$PLSQL_CODE_TYPE = ' || $$PLSQL_CODE_TYPE); DBMS_OUTPUT.PUT_LINE('$$PLSQL_OPTIMIZE_LEVEL = ' || $$PLSQL_OPTIMIZE_LEVEL); DBMS_OUTPUT.PUT_LINE('$$PLSQL_WARNINGS = ' || $$PLSQL_WARNINGS); DBMS_OUTPUT.PUT_LINE('$$NLS_LENGTH_SEMANTICS = ' || $$NLS_LENGTH_SEMANTICS); END; / anonymous block completed $$PLSCOPE_SETTINGS = $$PLSQL_CCFLAGS = $$PLSQL_CODE_TYPE = INTERPRETED $$PLSQL_OPTIMIZE_LEVEL = 0 $$PLSQL_WARNINGS = DISABLE:ALL $$NLS_LENGTH_SEMANTICS = BYTE
Using inquiry directives (example extracted from here) |
-- USE PACKAGE SPEC and BODY to define and declare a USER-DEFINED subtype. -- Package specification: Use selection directive ($IF) to test for the -- version of the database. -- If version < 10g, define subtype as NUMBER. -- If version >= 10g, define subtype as BINARY_DOUBLE create or replace package my_pkg AS subtype my_real IS $IF dbms_db_version.version < 10 $THEN number; $ELSE binary_double; $END my_pi my_real; my_e my_real; end my_pkg; / -- Package body: test for db version. Assign values -- for the subtype variable accordingly. create or replace package body my_pkg AS begin $IF dbms_db_version.version < 10 $THEN my_pi := 3.14159265358979323846264338327950288420; my_e := 2.71828182845904523536028747135266249775; $ELSE my_pi := 3.14159265358979323846264338327950288420d; my_e := 2.71828182845904523536028747135266249775d; $END END my_pkg; / -- Use the subtype defined in the package on a standalone procedure. -- Uses inquiry directive($$my_debug) to decide whether or not -- to run some code. If $$my_debug = TRUE, run the debug code that -- check for the datatype of the subtype my_real. create or replace procedure circle_area (radius my_pkg.my_real) IS my_area my_pkg.my_real; my_data_type varchar2(30); begin my_area := my_pkg.my_pi * (radius**2); dbms_output.put_line ('Radius: '|| to_char(radius) || ' Area: '|| to_char(my_area)); $IF $$my_debug $THEN select data_type into my_data_type from user_arguments where object_name = 'CIRCLE_AREA' and ARGUMENT_NAME = 'RADIUS'; dbms_output.put_line ('Data type of the RADIUS argument is: '|| my_data_type); $END end circle_area; / -- Run procedure circle_area (1) Set $$my_debug: FALSE SQL> alter session set plsql_ccflags = 'my_debug:FALSE'; Session SET altered. SQL> set serveroutput on SQL> exec circle_area(2); Radius: 2.0E+000 Area: 1.2566370614359172E+001 PL/SQL procedure successfully completed. (1) Set $$my_debug: TRUE SQL> alter session set plsql_ccflags = 'my_debug:TRUE'; Session SET altered. -- You need to recompile the procedure circle_area so it can -- see the change in the session SQL> alter procedure circle_area compile; Procedure altered. SQL> set serveroutput on SQL> exec circle_area(2); Radius: 2.0E+000 Area: 1.2566370614359172E+001 Data type of the RADIUS argument is: BINARY_DOUBLE PL/SQL procedure successfully completed.
Using
DBMS_PREPROCESSOR
to print source text- You can use the dbms_preprocessor package to print the source text executed after
the conditional compilation directives were all processed. - For the procedure circle_area above, you can retrieve the source code processed when the
debug directive ($$my_debug) is set to TRUE. - Subprograms
DBMS_PREPROCESSOR.PRINT_POST_PROCESSED_SOURCE
DBMS_PREPROCESSOR.GET_POST_PROCESSED_SOURCE
(overloaded (3))
SQL> call dbms_preprocessor.print_post_processed_source 2 ('PROCEDURE', 'DEV2', 'CIRCLE_AREA'); procedure circle_area (radius my_pkg.my_real) IS my_area my_pkg.my_real; my_data_type varchar2(30); begin my_area := my_pkg.my_pi * (radius**2); dbms_output.put_line ('Radius: '|| to_char(radius) || ' Area: '|| to_char(my_area)); select data_type into my_data_type from user_arguments where object_name = 'CIRCLE_AREA' and ARGUMENT_NAME = 'RADIUS'; dbms_output.put_line ('Data type of the RADIUS argument is: '|| my_data_type); end circle_area; Call completed.
CC Restrictions:
- You cannot have a CC directive within a
CREATE TYPE
statement. (Why? Attributes of a TYPE will determine the physical structure of dependent tables.) - Package Spec, Package body, type body, procedure/function w/o parameters: first conditional compilation directive cannot appear before the keyword IS or AS.
- subprogram with at least one formal parameter: the first conditional compilation directive cannot appear before the left parenthesis that follows the subprogram name.
- Trigger or an anonymous block, the first conditional compilation directive cannot appear before the keyword DECLARE or BEGIN, whichever comes first.
On 11g R2: Compiling PL/SQL Units for Native Execution |
- PL/SQL units can be compiled into native code (processor-dependent system code), which is stored in the SYSTEM tablespace.
- Any PL/SQL unit of any type can be natively compiled, including Oracle supplied ones.
- Natively compiled program units work in all server environments.
- Greatest performance gains: for computation-intensive procedural operations. (i.e. Data warehouse and applications with extensive server-side transformations of data for display).
- Least performance gains: for PL/SQL subprograms that spend most of their time running SQL.
- The
PLSQL_CODE_TYPE
compilation parameter determines whether PL/SQL code is natively compiled or interpreted. ALTER PROCEDURE my_proc COMPILE PLSQL_CODE_TYPE=NATIVE REUSE SETTINGS;
- Run this query to determine how many objects are compiled NATIVE and INTERPRETED:
SELECT TYPE, PLSQL_CODE_TYPE, COUNT(*) FROM DBA_PLSQL_OBJECT_SETTINGS WHERE PLSQL_CODE_TYPE IS NOT NULL GROUP BY TYPE, PLSQL_CODE_TYPE ORDER BY TYPE, PLSQL_CODE_TYPE;
Obfuscation and wrapping |