Você está na página 1de 8

PRAGMA directives in Oracle PL/SQL

In this post we’re going to analyze all the five PRAGMA directives of Oracle’s PL/SQL. Four of them
exist since Oracle8i while the last one has been introduced with Oracle11g.

Let’s begin with PRAGMA EXCEPTION_INIT.


This directive allows us to associate an ORA error code to an user-defined PL/SQL exception.
Once the association as been done we’ll be able to manage the exception in our code as it was a
predefined exception (just like NO_DATA_FOUND or TOO_MANY_ROWS).
Let’s see an example.

We need a function that converts a string to a date using the ‘YYYY-MM-DD’ format:

1
2 SQL> create or replace function string2date (str in varchar2) return
3 date is
4 2 retDate date;
3 begin
5 4 retDate := to_date(str,'yyyy-mm-dd');
6 5 return retDate;
7 6 end;
8 7 /
9
10
11 SQL> select string2date('2010-01-31')
2 from dual;
12
13 STRING2DA
14 ---------
15 31-JAN-10
16
17 SQL> select string2date('werrwer')
2 from dual;
18 select string2date('werrwer')
19 *
20 ERROR at line 1:
21 ORA-01841: (full) year must be between -4713 and +9999, and not be 0
22 ORA-06512: at "MAXR.STRING2DATE", line 4
23

As the example shows, if the input string does not conform to the format we get the ORA-1841
error.
We want to manage this error using the PRAGMA EXCEPTION_INIT directive:

1 SQL> create or replace function string2date (str in varchar2) return date is


2 2 retDate date;
3 3 not_valid_date exception;
4 PRAGMA EXCEPTION_INIT(not_valid_date,-1841);
4 5 begin
5 6 retDate := to_date(str,'yyyy-mm-dd');
6 7 return retDate;
7 8 exception
9 when not_valid_date then
8 10 dbms_output.put_line('Error: the string '||str||' cannot be converted
9 to a date!');
10 11 return null;
11 12 end;
12 13 /
13
SQL> set serverout on
14 SQL> select string2date('werrwer')
15 2 from dual;
16
17 STRING2DA
18 ---------
19
20
Error: the string werrwer cannot be converted to a date!
21
22
23

We’re defining a new exception not_valid_date, but it will be never called if we don’t associate it
to the ORA-1841 error using the PRAGMA.
Once we have made the association Oracle knows that, in case of the ORA-1841 error, the
not_valid_date exception must be raised.

PRAGMA RESTRICT_REFERENCES allows us to explicitly declare that a PL/SQL program doesn’t


read/write in db objects or in package variables.

In some situations, only functions that guarantee those restrictions can be used.
The following is a simple example:
Let’s define a package made of a single function that updates a db table and returns a number:

1 SQL> create or replace package pack is


2 function a return number;
2 3 end;
3 4 /
4
5 SQL> create or replace package body pack is
6 2 function a return number is
7 3 begin
4 update emp set empno=0 where 1=2;
8 5 return 2;
9 6 end;
10 7 end;
11 8 /
12
13

If we try to use the function pack.a in a query statement we’ll get an error:

1 SQL> select pack.a from dual;


2 select pack.a from dual
*
3 ERROR at line 1:
4 ORA-14551: cannot perform a DML operation inside a
5 query
6 ORA-06512: a "MAXR.PACK", line 4

PL/SQL functions can be used inside a query statement only if they don’t modify neither the db
nor packages’ variables.

This error can be descovered only at runtime, when the select statement is executed.
How can we check for this errors at compile time? We can use PRAGMA RESTRICT_REFERENCES!
If we know that the function will be used in SQL we can define it as follows:

1 SQL> create or replace package pack is


2 2 function a return number;
3 3 pragma restrict_references(a,'WNDS');
4 4 end;
5 /
5

Declaring that the function A will not modify the database state (WNDS stands for WRITE NO
DATABASE STATE).
Once we have made this declaration, if a programmer, not knowing that the function has to be
used in a query statement, tries to write code for A that violates the PRAGMA:

1 SQL> create or replace package body pack is


2 2 function a return number is
3 begin
3 4 update emp set empno=0 where 1=2;
4 5 return 2;
5 6 end;
6 7 end;
8 /
7
8 Warning: Package Body created with compilation errors.
9
10 SVIL>sho err
11 Errors for PACKAGE BODY PACK:
12
13 LINE/COL ERROR
14 -------- -------------------------------------------------------
----------
15 2/1 PLS-00452: Subprogram 'A' violates its associated
16 pragma
17

He(She)’ll get an error at compile time…

Pragma RESTRICT_REFERENCE is deprecated and could be removed from future versions of Oracle.

PRAGMA SERIALLY_REUSABLE tells to the compiler that the package’s variables are needed for
a single use. After this single use Oracle can free the associated memory. It’s really useful to save
memory when a packages uses large temporary space just once in the session.
Let’s see an example.

Let’s define a package with a single numeric variable “var” not initialized:

1 SQL> create or replace package pack is


2 2 var number;
3 3 end;
4 4 /

If we assign a value to var, this will preserve that value for the whole session:

1 SQL> begin
2 2 pack.var := 1;
3 3 end;
4 4 /
5
6 SQL> exec dbms_output.put_line('Var='||pack.var);
Var=1
7

If we use the PRAGMA SERIALLY_REUSABLE, var will preserve the value just inside the program
that initializes it, but is null in the following calls:

1 SQL> create or replace package pack is


2 PRAGMA SERIALLY_REUSABLE;
2 3 var number;
3 4 end;
4 5 /
5
6 SQL> begin
7 2 pack.var := 1;
3 dbms_output.put_line('Var='||pack.var);
8 4 end;
9 5 /
10 Var=1
11
12 SQL> exec dbms_output.put_line('Var='||pack.var);
Var=
13
14
15

PRAGMA SERIALLY_REUSABLE is a way to change the default behavior of package variables that
is as useful as heavy for memory.

PRAGMA AUTONOMOUS_TRANSACTION declare to the compiler that a given program has to


run into a dedicated transaction, ignoring all uncommitted data changes made into the original
transaction of the calling program.

The sum of salaries in EMP is:

1 SQL> select sum(sal) from emp;


2
3 SUM(SAL)
4 ----------
5 29025

Let’s define two functions that do exactly the same thing, read and return the sum of salaries of
EMP:

1 SQL> create or replace function getsal return number


2 is
3 2 s number;
3 begin
4 4 select sum(sal) into s from emp;
5 5 return s;
6 6 end;
7 7 /
8
9 SQL> create or replace function getsal_AT return
number is
10 2 PRAGMA AUTONOMOUS_TRANSACTION;
11 3 s number;
12 4 begin
13 5 select sum(sal) into s from emp;
6 return s;
14 7 end;
15 8 /
16
17 SQL> select sum(sal), getsal, getsal_AT
18 2 from emp;
19
20 SUM(SAL) GETSAL GETSAL_AT
21 ---------- ---------- ----------
22 29025 29025 29025
23

The second one uses the PRAGMA AUTONOMOUS_TRANSACTION. Now let’s cut all the salaries:

1 SQL> update emp set sal=10;


2
3 SQL> select sum(sal), getsal, getsal_AT
4 2 from emp;
5
6 SUM(SAL) GETSAL GETSAL_AT
7 ---------- ---------- ----------
140 140 29025
8

GETSAL is seeing uncommitted changed data while GETSAL_AT, defined using PRAGMA
AUTONOMOUS_TRANSACTION, reads data as they where before the UPDATE statement…

The only PRAGMA recently added (in Oracle11g) is PRAGMA INLINE.

In Oracle11g has been added a new feature that optimizer can use to get better performances, it’s
called Subprogram Inlining.
Optimizer can (autonomously or on demand) choose to replace a subprogram call with a local copy
of the subprogram.

For example, assume the following code:

1 declare
2 total number;
3 begin
4 total := calculate_nominal + calculate_interests;
end;
5

Where calculate_nominal and calculate_interests are two functions defined as follows:

function calculate_nominal return number is


1
s number;
2 begin
3 select sum(nominal)
4 into s
5 from deals;
6
return s;
7 end;
8
9 function calculate_interests return number is
10 s number;
11 begin
select sum(interest)
12 into s
13 from deals;
14
15 return s;
16 end;
17
18
19

Optimizer can change the code to something like this:

1
declare
2 total number;
3 v_calculate_nominal number;
4 v_calculate_interests number;
5 begin
6 select sum(nominal)
into v_calculate_nominal
7 from deals;
8
9 select sum(interest)
10 into v_calculate_interests
11 from deals;
12
total := v_calculate_nominal +
13
v_calculate_interests;
14 end;
15

Including a copy of the subprograms into the calling program.

PRAGMA INLINE is the tool that we own to drive this new feature.
If we don’t want such an optimization we can do:

1
declare
2 total number;
3 begin
4 PRAGMA INLINE(calculate_nominal,'NO');
5 PRAGMA INLINE(calculate_interests,'NO');
total := calculate_nominal + calculate_interests;
6 end;
7

If we do want subprogram inlining on calculate_nominal we do:

declare
1 total number;
2 begin
3 PRAGMA INLINE(calculate_nominal,'YES');
total := calculate_nominal + calculate_interests;
4 end;
5
6

Subprogram inlining behave differently depending on the level of optimization defined through the
db initialization variable PLSQL_OPTIMIZE_LEVEL.
If this variable is set to 2 (that’s the default value) optimizer never uses subprogram inlining unless
the programmer requests it using PRAGMA INLINE YES.
If PLSQL_OPTIMIZE_LEVEL=3 optimizer can autonomously decide whether to use subprogram
inlining or not. In this case PRAGMA INLINE YES does not force the optimizer, it’s just an hint.

That’s all about pragmas by now.

Você também pode gostar