Oracle 19C Data Guard基礎運維-08 DML重定向

chenoracle發表於2020-04-23

Oracle 19C Data Guard 基礎運維 -08 DML 重定向

DML 重定向說明:

Active Data Guard Standby Databases 執行 DML 操作

Performing DML Operations on Active Data Guard Standby Databases

您可以在ADG 備庫端執行 DML 操作。這使您能夠在備用資料庫上執行以讀為主的應用程式,這些應用程式偶爾執行 DML

You can run DML operations on Active Data Guard standby databases. This   enables you to run read-mostly applications, which occasionally execute DMLs, on the standby database.

standby 資料庫上的 DML 操作可以透明地重定向到 primary 資料庫並在 primary 資料庫上執行。這包括作為 PL/SQL 塊一部分的 DML 語句。

DML operations on a standby can be transparently redirected to and run on the   primary database. This includes DML statements that are part of PL/SQL blocks.

ADG 會話將等待,直到將相應的更改傳送到並應用到 ADG standby 。在DML 操作期間保持讀取一致性,執行 DML standby 資料庫可以檢視其未提交的更改。但是,所有其他 standby 資料庫例項只能在事務提交後才能檢視這些更改。

The Active Data Guard session waits until the corresponding changes are shipped to and applied to the Active Data Guard standby. Read consistency is maintained during the DML operation and the standby database on which the DML is run can view its uncommitted changes. However, all the other standby database instances can view these changes only after the transaction is committed.

Note:

避免在ADG 端執行過多的 DML ,因為執行最終是在主庫端執行,過多的 DML 操作會影響主庫效能。

Avoid running too may DML operations on Active Data Guard standby databases. Because the operations are actually performed on the primary, too many DMLs may impact the performance of the primary.

ALTER SESSION ENABLE ADG_REDIRECT_DML;

ALTER S YSTEM  ENABLE ADG_REDIRECT_DML;

Example 10-1 Performing DML Operations on a Physical Standby Database

SQL> ALTER SESSION ENABLE ADG_REDIRECT_DML;

SQL> INSERT INTO employees VALUES (.......);

此時,更改後的資料僅對執行該命令的備用資料庫可見。在主資料庫上提交插入操作之後,將把更改發回並應用於所有備用資料庫。

At this point, the changed data is visible only to the standby database on which the   command was run. After the insert operation is committed on the primary database, the changes are shipped back and applied to all the standby databases.

實驗過程如下:

主庫cjcdb: 建立測試資料

SQL> create user c##cjc identified by oracle;

SQL> grant connect,resource,dba to c##cjc;

SQL> create table c##cjc.t1 as select level as id from dual connect by level<=10;

備庫:chendb

SQL> select * from c##cjc.t1;

ID

----------

 1

 2

......

10

10 rows selected.

備庫chendb 啟用會話級別 adg_redirect_dml

SQL> conn c##cjc/oracle

SQL> show parameter ADG_REDIRECT_DML

NAME          TYPE  VALUE

------------------------------------ ----------- ------------------------------

adg_redirect_dml        boolean  FALSE

SQL> alter session enable adg_redirect_dml;

SQL> select open_mode from v$database;

OPEN_MODE

--------------------

READ ONLY WITH APPLY

SQL> show user

USER is "C##CJC"

啟用10046 事件,跟蹤當前會話

SQL> alter session set tracefile_identifier='10046 C ';

SQL> alter session set events '10046 trace name context forever ,level 12';

SQL> insert into t1 values(1);

資料提交之前,檢視主庫事務資訊

1 主庫:透過v$transaction 查詢出當前事務對應的會話地址

SQL> select  addr from  v$transaction ;

2 主庫:透過上一步的會話地址,在 v$session 中找到對應 sql_id

SQL> select  prev_hash_value , prev_sql_id from  v$session where  taddr = '000000007BE7CF20' ;

3 主庫:透過上一步的 sql_id ,在 v$sql 中查詢出正在執行的 SQL

SQL> select   *   from  v$sql where  sql_id = '66xyn363mxuzu' ;

--- INSERT  INTO "T1" "A1" ("ID") VALUES (1)

SQL> select sql_text,sql_id,hash_value,service from v$sql where sql_id='66xyn363mxuzu';

結論: 備庫端執行的DML 語句,實際上重定位到主庫上執行了,具體是怎麼實現的呢?

五:備庫:提交事務,生成10046 跟蹤檔案

SQL> commit;

SQL> alter session set events '10046 trace name context off';

[oracle@cjcos02 trace]$ pwd

/u01/app/oracle/diag/rdbms/chendb/chendb/trace

檢視10046 跟蹤檔案

[oracle@cjcos02 trace]$ vim chendb_ora_3590_10046C.trc

等待事件:和dblink 有關

SQL*Net message to dblink

SQL*Net message from dblink

格式化跟蹤檔案:

[oracle@cjcos02 trace]$ tkprof chendb_ora_3590_10046C.trc 10046.trc

遞迴 SQL 也和 dblink 有關

SQL ID: b3zgx1xckrwxu Plan Hash: 1278882766

select host,userid,password,flag,authusr,authpwd, passwordx, authpwdx

from   link$ where owner#=:1 and name=:2

結論: 在備庫端執行 DML 時,備庫端透過 dblink 遠端連線到主庫,在主庫端執行 DML 語句,待主庫執行成功並將資料同步回備庫端後,備庫端顯示 DML 語句執行完成。

七:常見問題

備庫執行DML 語句時,報如下錯誤:

ORA-16397: statement redirection from Oracle Active Data Guard standby database

問題原因:

不支援SYS 使用者會話級別啟用 DML 重定向。

解決方案:

使用非SYS 使用者啟用和測試。

錯誤重現過程如下:

SQL>  show user

USER is "SYS"

SQL> alter session set tracefile_identifier='10046';

SQL> alter session set events '10046 trace name context forever ,level 12';

SQL> insert into test1 values(1);

insert into test1 values(1)

            *

ERROR at line 1:

ORA-16397: statement redirection from Oracle Active Data Guard standby database

to primary database failed

歡迎關注我的微信公眾號"IT小Chen",共同學習,共同成長!!!

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29785807/viewspace-2687855/,如需轉載,請註明出處,否則將追究法律責任。

相關文章