Edition-Based Redefinition Janina Patolla Trivadis AG, Basel Basel Baden Bern Brugg Lausanne Zurich Düsseldorf Frankfurt/M. Freiburg i. Br. Hamburg Munich Stuttgart Vienna
Introduction Upgrading critical applications can be very difficult. Problems: long downtimes cannot be scheduled periodically For such applications we'd prefer online upgrades. Up to Oracle 11g Release 1 no real support With Oracle 11g Release 2 and Edition-based Redefinition, real support for implementing online upgrades. Oracle Database New Features 11g for Developers Edition Based Redefinition 2
Agenda Editions Editioned Objects Editioning Views Data are always part of the game. Crossedition Triggers Limitations Oracle Database New Features 11g for Developers Edition Based Redefinition 3
Concept As of Oracle Database 11g Release 2 an object is identified by: its name the user (schema) it belongs to nothing new so far its EDITION The aim of the edition support several copies (editions) of a given object Objects with several copies editioned objects. Objects that cannot have several copies noneditioned objects, have no edition. In data dictionary, edition is set to NULL. Oracle Database New Features 11g for Developers Edition Based Redefinition 4
@ Online Upgrade Using Editions EMAIL May.Ling@xx.com Tom.Kerr@xx.com Ina.Ries@ght.de RECIPIENT May.Ling Tom.Kerr Ina.Ries DOMAIN xx.com xx.com ght.de PERSONS_TAB HR_PKG PERSONS Ora$Base PERS_PKG HR_PKG PERS_PKG PERSONS FCT RCT Edi_1 Oracle Database New Features 11g for Developers Edition Based Redefinition 5
Editionable Object Types Editionable object types FUNCTION LIBRARY PACKAGE and PACKAGE BODY PROCEDURE SYNONYM TRIGGER TYPE and TYPE BODY VIEW Noneditionable object types do not support editions TABLE PUBLIC SYNONYM Oracle Database New Features 11g for Developers Edition Based Redefinition 6
Enabling Editions for a User Editioned objects can only be owned by users that have been explicitly enabled to do so. SQL> ALTER USER cha ENABLE EDITIONS; The operation of enabling a user to create editioned objects is irreversible! Oracle Database New Features 11g for Developers Edition Based Redefinition 7
Creating Editions (1) Every database must have at least one edition. A database supports several editions organized as a "hierarchy" with one root edition (by default ORA$BASE) and a parent-child relationship between every other edition. ORA$BASE REL1 REL2 REL3 The CREATE EDITION statement is used to create an edition. It requires the CREATE ANY EDITION system privilege. SQL> CREATE EDITION rel1 AS CHILD OF ora$base; SQL> CREATE EDITION rel2 AS CHILD OF rel1; SQL> CREATE EDITION rel3 AS CHILD OF rel2; Oracle Database New Features 11g for Developers Edition Based Redefinition 8
Creating Editions (2) Default: new edition can only be used by the user who created it by SYS. Grant USE ON EDITION privilege for other users SQL> GRANT USE ON EDITION rel2 TO cha; SQL> GRANT USE ON EDITION rel3 TO PUBLIC; Oracle Database New Features 11g for Developers Edition Based Redefinition 9
Selecting Editions Specify session edition when connection is created, APIs (e.g. OCI and JDBC) tools (e.g. SQL*Plus and Data Pump) Property DEFAULT_EDITION specifies session default By default, the property is set to ORA$BASE. Session edition can also be modified ALTER SESSION statement SQL> ALTER SESSION SET EDITION = rel1; May be used with ON-LOGON Triggers Oracle Database New Features 11g for Developers Edition Based Redefinition 10
Dropping Editions The DROP EDITION is used to drop an edition. It requires the DROP ANY EDITION system privilege. In addition, the following conditions must be met: The edition is not the latest database edition. The edition is not the default database edition. The edition is not in use as session edition. The edition is either the root or the leaf of the hierarchy. If the edition is the leaf of the hierarchy and it still has associated objects, the CASCADE option must be specified. If the edition is the root of the hierarchy, the child edition must not inherit objects from the root edition. Dropping an edition that is no longer used is optional. Oracle Database New Features 11g for Developers Edition Based Redefinition 11
Agenda Editions Editioned Objects Editioning Views Data are always part of the game. Crossedition Triggers Limitations Oracle Database New Features 11g for Developers Edition Based Redefinition 12
Creating Editioned Objects Editioned objects are created like any other object. Set edition before creating object SQL> ALTER SESSION SET EDITION = rel1; SQL> CREATE OR REPLACE PROCEDURE hello IS 2 BEGIN 3 dbms_output.put_line('hello from REL1'); 4 END; 5 / Enabling editions for a user with existing object: objects are associated with the root edition Oracle Database New Features 11g for Developers Edition Based Redefinition 13
Redefining Editioned Objects (1) The typical steps that are carried out to redefine editioned objects are the following. Create a new edition and set it as session edition. Redefine the editioned objects and ensure that all objects are valid. Check whether the application works as expected with the new edition. Permanently switch to the new edition. Oracle Database New Features 11g for Developers Edition Based Redefinition 14
Redefining Editioned Objects (2) When a new edition is created, all objects that are associated with the parent edition are inherited by the child edition. objects not associated with the child edition only made visible to the child edition. To redefine editioned objects, select the new edition alter, add, drop or recompile editioned object (DDL statements) SQL> ALTER SESSION SET EDITION = rel2; SQL> CREATE OR REPLACE PROCEDURE hello IS 2 BEGIN 3 dbms_output.put_line('hello from REL2'); 4 END; 5 / Oracle Database New Features 11g for Developers Edition Based Redefinition 15
Agenda Editions Editioned Objects Editioning Views Data are always part of the game. Crossedition Triggers Limitations Oracle Database New Features 11g for Developers Edition Based Redefinition 16
Editioning Views (1) Tables are not editionable. If data is part of revisioning, every table should be covered with an editioning view in the application all references to tables are replaced with references to editioning views. An editioning views is a regular view with a few special characteristics: It only supports projections (no restrictions, joins, aggregations, ). It supports DML triggers. It does not support INSTEAD OF triggers. Oracle Database New Features 11g for Developers Edition Based Redefinition 17
Editioning Views (2) To create an editioning view, use EDITIONING keyword in the CREATE VIEW statement. SQL> DESCRIBE persons Name Null? Type ----------------------- -------- ---------------- ID NOT NULL NUMBER(10) FIRST_NAME NOT NULL VARCHAR2(100) LAST_NAME NOT NULL VARCHAR2(100) EMAIL NOT NULL VARCHAR2(100) SQL> RENAME persons TO persons_tab; SQL> CREATE EDITIONING VIEW persons AS 2 SELECT id, first_name AS firstname, 3 last_name AS lastname, email 4 FROM persons_tab; Oracle Database New Features 11g for Developers Edition Based Redefinition 18
Agenda Editions Editioned Objects Editioning Views Data are always part of the game. Crossedition Triggers Limitations Oracle Database New Features 11g for Developers Edition Based Redefinition 19
Crossedition Triggers (1) During upgrade, editioning views may be set in read-only editioned views based on tables that have to be redefined should be set in read-only mode. Whenever possible If setting editioned views to read-only is not an option, crossedition triggers have to be used Oracle Database New Features 11g for Developers Edition Based Redefinition 20
Forward Crossedition Triggers (1) Forward crossedition trigger transform a row from the old structure into the new structure Forward crossedition trigger is created in the new edition The old one should never be modified Fires when a DML statement is executed by a session using an ancestor edition of the trigger edition Forward crossedition trigger are explicitly defined specifying the FORWARD CROSSEDITION clause Oracle Database New Features 11g for Developers Edition Based Redefinition 21
Forward Crossedition Triggers (2) SQL> ALTER TABLE persons_tab ADD ( 2 email_recipient VARCHAR2(100), 3 email_domain VARCHAR2(100) 4 ); SQL> CREATE TRIGGER persons_fc_trg 2 BEFORE INSERT OR UPDATE ON persons_tab FOR EACH ROW 3 FORWARD CROSSEDITION 4 DISABLE 5 BEGIN 6 :new.email_recipient := 7 regexp_substr(:new.email,'(.*)@',1,1,null,1); 8 :new.email_domain := 9 regexp_substr(:new.email,'@(.*)',1,1,null,1); 10 END; 11 / Oracle Database New Features 11g for Developers Edition Based Redefinition 22
Forward Crossedition Triggers (3) Data Migration The data already stored in the redefined table has to be converted in the new structure as well. Idea: fire the trigger for every row already stored in the table Reuse logic Performance? DBMS_SQL.PARSE function for trigger application To avoid an inconsistent state: Enable the forward edition trigger. Wait for every transaction on the redefined table to be committed or rolled back (use DBMS_UTILITY.WAIT_ON_PENDING_DML). Start a PL/SQL block that fires the trigger for every row. Oracle Database New Features 11g for Developers Edition Based Redefinition 23
Forward Crossedition Triggers (4) Data Migration SQL> DECLARE 2 c INTEGER; 3 r INTEGER; 4 BEGIN 5 c := dbms_sql.open_cursor; 6 dbms_sql.parse( 7 c => c, 8 statement => 'UPDATE persons' 9 'SET email_domain = email_domain', 10 language_flag => dbms_sql.native, 11 apply_crossedition_trigger => 'persons_fc_trg' 12 ); 13 r := dbms_sql.execute(c); 14 dbms_sql.close_cursor(c); 15 COMMIT; 16 END; 17 / Oracle Database New Features 11g for Developers Edition Based Redefinition 24
Reverse Crossedition Triggers (1) Reverse crossedition trigger are used when old and new edition are used concurrently Reverse crossedition trigger transforms a row from new structure to old structure. Reverse crossedition trigger is created in new edition and fires when a DML statement is executed by a session using the trigger edition(or a descendent) Reverse crossedition trigger are explicitly defined specifying the REVERSE CROSSEDITION clause. Oracle Database New Features 11g for Developers Edition Based Redefinition 25
Reverse Crossedition Triggers (2) SQL> CREATE TRIGGER persons_rc_trg 2 BEFORE INSERT OR UPDATE ON persons_tab FOR EACH ROW 3 REVERSE CROSSEDITION 4 DISABLE 5 BEGIN 6 :new.email := :new.email_recipient 7 '@' 8 :new.email_domain; 9 END; 10 / Oracle Database New Features 11g for Developers Edition Based Redefinition 26
@ Online Upgrade Final Steps EMAIL May.Ling@xx.com Tom.Kerr@xx.com Ina.Ries@ght.de RECIPIENT May.Ling Tom.Kerr Ina.Ries DOMAIN xx.com xx.com ght.de PERSONS_TAB HR_PKG PERSONS Ora$Base PERS_PKG HR_PKG PERS_PKG PERSONS FCT RCT Edi_1 Oracle Database New Features 11g for Developers Edition Based Redefinition 27
Agenda Editions Editioned Objects Editioning Views Data are always part of the game. Crossedition Triggers Limitations Oracle Database New Features 11g for Developers Edition Based Redefinition 28
Limitations There are two important implementation limitations related to the hierarchies that can be built with editions: Every edition can have at most one child. Every database has one, and only one, root edition. VER11 CRM10 CRM11 CRM12 ORA$BASE VER10 VER20 HR1 HR2 HR3 The possibility of tying the default edition to a database service is not available. Indexes and constraints can only be created on tables. Hence, there is no way to take advantage of editions with them. Oracle Database New Features 11g for Developers Edition Based Redefinition 29
Edition-Based Redefinition Core Messages Data are always part of the game. Edition-based redefinition provides completely new features that make online upgrades possible. The complexity of such upgrades should not be underestimated. Oracle Database New Features 11g for Developers Edition Based Redefinition 30