standby維護命令手冊(轉)

ningzi82發表於2010-09-17

standby維護命令手冊

連結:
站內相關文章|Related Articles




1.檢視logical standby上當前正在apply的redo log
COLUMN DICT_BEGIN FORMAT A15;
COLUMN FILE_NAME FORMAT A30;
SET NUMF 9999999;
COL FCHANGE# format 9999999999999;
COL NCHANGE# for 999999999999999999999;
SET line 200
SELECT file_name, sequence# AS seq#, first_change# AS fchange#,
next_change# AS nchange#, TIMESTAMP, dict_begin AS beg,
dict_end AS END, thread# AS thr#, applied
FROM dba_logstdby_log
ORDER BY thread#;
[@more@]

DataGuard Logical

2. 檢視logical standby節點apply程式狀態
select sid,type,status_code,status from v$logstdby_process;

檢視applyer程式的個數
SELECT COUNT(*) AS APPLIER_COUNT FROM V$LOGSTDBY_PROCESS WHERE TYPE = 'APPLIER';

檢視空閒的applyer程式
SELECT COUNT(*) AS IDLE_APPLIER
FROM V$LOGSTDBY_PROCESS WHERE TYPE = 'APPLIER' and status_code = 16166;

3. 調整logical standby的apply程式數
ALTER DATABASE STOP LOGICAL STANDBY APPLY; --- Stop SQL Apply
EXECUTE DBMS_LOGSTDBY.APPLY_SET('APPLY_SERVERS', 20); --- 調整apply程式數為20,預設為5個
ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE; --- Start real-time Apply

確認logical standby上的空閒APPLIER程式

SELECT COUNT (*) AS idle_applier
FROM v$logstdby_process
WHERE TYPE = 'APPLIER' AND status_code = 16166;

注:status_code = 16166 表示程式是空閒狀態,可以看到"STATS"為"ORA-16116: no work available"

如何根據系統表現調整APPLIER程式的個數,統計transactions的apply狀態,

SQL> SELECT NAME, VALUE FROM V$LOGSTDBY_STATS WHERE NAME LIKE 'transactions%';

NAME VALUE
-------------------------- ----------------------------
transactions ready 159
transactions applied 159

如果ready(等待apply)和applied(已經apply)的值基本同步,則設定的APPLIER程式合適或偏多。根據 IDLE_APPLIER的程式數,可減少APPLIER程式數目.如果transactions ready - transactions applied的差比APPLIER程式數的2倍還多,則需要增加APPLIER程式數目了。

4. 調整PREPARER(調製機)的程式數
logical standby上有很多transactions等待apply, 但是還有空閒的applyer程式,且已經沒有idle狀態的PREPARER程式,這時需要增加PREPARER(調製機)的程式數。如下:

ALTER DATABASE STOP LOGICAL STANDBY APPLY; --- Stop SQL Apply
EXECUTE DBMS_LOGSTDBY.APPLY_SET('PREPARE_SERVERS', 4); --- 調整PREPARER程式數為4
ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE; --- Start real-time Apply

5. 計算logical standby的apply速度
可以透過v$logstdby_stats檢視統計logical standby的apply速度,計算公式如下:
apply_rate = bytes of redo processed / (coordinator uptime - seconds system is idle)
比如:

SELECT NAME, VALUE
FROM v$logstdby_stats
WHERE NAME IN
('coordinator uptime',
'seconds system is idle',
'bytes of redo processed'
);

NAME VALUE
--------------------------- ---------------------------------
coordinator uptime 78717
bytes of redo processed 7954813012
seconds system is idle 40

logical standby.apply_rate = 7954813012/(78717-40)/1024/1024

整理成1條sql語句計算出apply_rate如下:

SELECT c.VALUE / (a.VALUE - b.VALUE) / 1024 / 1024 AS "APPLY_RATE"
FROM v$logstdby_stats a, v$logstdby_stats b, v$logstdby_stats c
WHERE a.NAME = 'coordinator uptime'
AND b.NAME = 'seconds system is idle'
AND c.NAME = 'bytes of redo processed';


6. 統計logical standby上是否有報錯資訊
SELECT xidusn, xidslt, xidsqn, status, status_code
FROM dba_logstdby_events
WHERE event_time = (SELECT MAX (event_time)
FROM dba_logstdby_events);

7. 調整MAX_SGA - 防止Pageouts
SQL> select value bytes from v$logstdby_stats where name='bytes paged out';

注:如果以上查詢結果在增長,則查到當前MAX_SGA的大小:
SQL> select value from v$logstdby_stats where name = 'maximum SGA for LCR cache';
VALUE
------------------------
30

增大MAX_SGA,備註:10gR2,MAX_SGA可以增大到4095 MB.

SQL> alter database stop logical standby apply;
Database altered.
SQL> execute dbms_logstdby.apply_set('MAX_SGA',1000);
PL/SQL procedure successfully completed.
SQL> alter database start logical standby apply immediate;
Database altered.

邏輯備庫需要將redo記錄解析成LCR,會在shared pool裡分配一部分空間來作為LCR Cache,如果cache太小,就會像OS的虛擬記憶體管理一樣,需要做page out,這會嚴重影響應用日誌的效能。預設情況下,LCR Cache為Shared pool的四分之一,最少不少於30M,否則SQL Apply不能啟動。如果機器的記憶體足夠,建議將LCR Cache儘量設大一點,當然,同時share pool也要足夠大。如果機器記憶體有限,那可以考慮將buffer cache減少一點來給LCR Cache騰出空間。

8.跳過特定表或Schema的DML或DDL事務
alter database stop logical standby apply;
execute dbms_logstdby.skip (stmt => 'DML', schema_name => 'EYGLE',
object_name => 'SALES', proc_name => null);
execute dbms_logstdby.skip (stmt => 'SCHEMA_DDL', schema_name => 'EYGLE',
object_name => 'SALES', proc_name => null);
execute dbms_logstdby.skip (stmt => 'DML',
schema_name => 'EYGLE', object_name => '%', proc_name => null);
execute dbms_logstdby.skip (stmt => 'SCHEMA_DDL',
schema_name => 'EYGLE', object_name => '%', proc_name => null);
alter database start logical standby apply;

9.如何重新初始化資料表 透過以下查詢確認當前的skip規則:
select * from dba_logstdby_skip;

建議取消Skip之後,再重新初始化:
alter database stop logical standby apply;
execute dbms_logstdby.unskip('DML','EYGLE','SALES');
exec dbms_logstdby.instantiate_table('EYGLE','SALES','dblink_name');
alter database start logical standby apply;

10. Starting Real-time Apply

To start real-time apply on the logical standby database to immediately recover redo data from the standby redo log files on the logical standby database, include the IMMEDIATE keyword as shown in the following statement:

SQL>  ALTER DATABASE START LOGICAL STANDBY APPLY IMMEDIATE;

11.Stopping Log Apply Services on a Logical Standby Database

To stop SQL Apply, issue the following statement on the logical standby database:

SQL> ALTER DATABASE STOP LOGICAL STANDBY APPLY;

When you issue this statement, SQL Apply waits until it has committed all complete transactions that were in the process of being applied. Thus, this command may not stop the SQL Apply processes immediately.

If you want to stop SQL Apply immediately, issue the following statement:

SQL> ALTER DATABASE ABORT LOGICAL STANDBY APPLY;

Oracle文件參考:

INSTANTIATE_TABLE Procedure

This procedure creates and populates a table in the standby database from a corresponding table in the primary database. The table requires the name of the database link (dblink) as an input parameter.

Use the INSTANTIATE_TABLE procedure to:

  • Add a table to a standby database
  • Re-create a table in a standby database

Syntax

DBMS_LOGSTDBY.INSTANTIATE_TABLE (
table_name IN VARCHAR2,
schema_name IN VARCHAR2,
dblink IN VARCHAR2);

Parameters

Table 29-4 describes the parameters for the INSTANTIATE_TABLE procedure.

Table 29-4 DBMS_LOGSTDBY.INSTANTIATE_TABLE Procedure Parameters
ParameterDescription

table_name

Name of the table to be created or re-created in the standby database.

schema_name

Name of the schema.

dblink

Name of the database link account that has privileges to read and lock the table in the primary database.

Exceptions

None.

Usage Notes

  • Use this procedure to create and populate a table in a way that keeps the data on the standby database transactionally consistent with the primary database.
  • This procedure assumes that the metadata has been maintained correctly.
  • This table is not safe until the redo log that was current on the primary database at the time of execution is applied to the standby database.

Example

Enter this statement to create and populate a new table on the standby database.

SQL> EXECUTE DBMS_LOGSTDBY.INSTANTIATE_TABLE ('myschema', 'mytable', 'mydblink');
----------------

10gR2大大簡化了logical standby的建立步驟,從physical到logical的轉換步驟異常簡單,不再對搭建過程過多描述。

因為Logical standby是SQL語句的邏輯運用,而standby是block級別的recover,從故障角度上將,Logical standby比standby standby有更多的故障機會出現。
常用的用於維護和排查logical standby的檢視幾乎都是已”dba_logstdby_*”和“v$logstdby_*”開頭,常用的包以”dbms_logstdby.*”開頭,具體含義根據字面意思也不難理解,不贅述(下文有部分提及)。當然,用於physical standby的部分檢視,對排查邏輯備庫也同樣適用。

1. APPLY狀態的檢查和診斷

Apply程式是邏輯SQL運用的核心。Apply程式遇到錯誤會自動終止,需要處理後手工重新啟動。因此,監控apply進度和狀態是監控邏輯備庫同步與否的關鍵,如同物理DG監控日誌同步。

(1) 監控備庫上各個程式的狀態

可以查詢v$logstdby_process檢視,用於得到與SQL APPLY相關的各程式的狀態情況.
如果沒有返回值,表示APPLY程式沒有啟動或者已停止。如果是後者,可以從alert.log檔案或者dba_logstdby_events中得到更多的錯誤資訊用於後續處理。

(2) Apply 進度

監控Apply進度可以得到備庫的恢復進展以及同步狀態。關聯查詢v$logstdby_progress 和dba_logstdby_log,得出目前SQL APPLY的apply scn/time以及log sequence。如果apply程式正常而主備之間同步有很大差異,需要做進一步排查。

2. SQL Apply的異常事務處理

SQL  Apply的異常事務會記錄到dba_logstdby_events檢視中,對於能夠進行手工fix的異常,可以處理後重新執行apply程式。對於某些無法處理或者修復後需要忽略的異常,透過dbms_logstdby.skip*一系列包做標記以便讓apply程式跳過。比如,我們可以對特定的錯誤事務進行標記,透過dbms_logstdby.skip_transaction標記指定的事務而忽略該事務的運用。
在10G中,oracle提供了帶skip failed transaction子句的 SQL apply啟動語句,可以方便的對當前記錄的錯誤事務全部跳過。

SQL>alter database start logical standby apply skip failed transaction;

3.SKIP策略

Logical standby提供skip的策略,可根據實際情況跳過不想被apply的物件或者跳過特定物件上的所有錯誤事務。
dbms_logstdby.skip: 用於指定物件的DML DDL skip
dbms_logstdby.skip_error: 用於指定物件上的錯誤事務 skip
dbms_logstdby.skip_transaction: 用於指定事務 skip

4. UNSKIP與初始化

Unskip是與skip相反的操作,用於已skip操作的撤銷。對於unskip的物件,需要進行手工構造或者初始化。
Oracle提供dbms_logstdby.instantiate_table包來對指定的table初始化。我們也可以手工來構造一個當前apply scn時刻的閃回查詢版本來初始化資料。

5. DDL handle

Logical standby中提供DDL handle的功能,用於自定義的一些DDL語句處理,比如一些表物件的重新命名。
需要注意的是,在邏輯備庫中,db_file_name_convert/ log_file_name_convert引數並不生效。對於路徑不同的主備體系,新增資料檔案和表空間會引起邏輯備庫的apply錯誤,可以透過DDL handle可以實現檔案路徑的重新命名。
ORACLE聯機文件上的一個例子:

CREATE OR REPLACE PROCEDURE SYS.HANDLE_TBS_DDL (
OLD_STMT IN VARCHAR2,
STMT_TYP IN VARCHAR2,
SCHEMA IN VARCHAR2,
NAME IN VARCHAR2,
XIDUSN IN NUMBER,
XIDSLT IN NUMBER,
XIDSQN IN NUMBER,
ACTION OUT NUMBER,
NEW_STMT OUT VARCHAR2
) AS
BEGIN
– All primary file specification that contains a directory
– /usr/orcl/primary/dbs
– should go to /usr/orcl/stdby directory specification
NEW_STMT := REPLACE(OLD_STMT,
‘/usr/orcl/primary/dbs’,
‘/usr/orcl/stdby’);
ACTION := DBMS_LOGSTDBY.SKIP_ACTION_REPLACE;
EXCEPTION
WHEN OTHERS THEN
ACTION := DBMS_LOGSTDBY.SKIP_ACTION_ERROR;
NEW_STMT := NULL;
END HANDLE_TBS_DDL;

然後指向這個ddl handle

SQL>exec dbms_logstdby.skip(stmt => ‘TABLESPACE’, proc_name => ‘SYS.HANDLE_TBS_DDL’);

6.引數設定

透過檢視v$logstdby_stats觀察現有的一些引數設定和狀態。設定/恢復引數使用dbms_logstdby.apply_set/dbms_logstdby. apply_unset包進行。

7. Role Transitions

邏輯switchover的過程要比physical standby複雜一些,需要有一步prepare的過程,注意留意期間主備庫角色的當值狀態。

8.Flashback database

如果開啟了flashback database,對於一個做過failover的原主庫,可以透過flashback database的功能將資料庫閃回到某個scn上,然後轉換成備庫,從而無需重新重新搭建環境。
(1). 在新主庫上,執行如下查詢獲得這個轉換時刻的SCN

SQL>SELECT VALUE FROM DBA_LOGSTDBY_PARAMETERS WHERE NAME = ‘STANDBY_BECAME_PRIMARY_SCN’;

(2). 舊庫上,執行flashback,並resetlogs

SQL>FLASHBACK DATABASE TO SCN became_primary_scn;

(3).角色切換

SQL>ALTER DATABASE START LOGICAL STANDBY APPLY NEW PRIMARY mylink;

9.Guard 模式

預設狀態下的邏輯備庫提供查詢功能和sys使用者下的DDL/DML操作,即standby模式.
Oracle提供database級別的3種模式,即none/standby/all模式,透過查詢v$database.guard_status列獲得當前狀態,可以透過以下SQL在備庫上重新定義:

SQL>alter databsae guard none(standby/all);

NONE模式:

邏輯備庫可以以任意資料庫使用者進行任意資料庫動作(查詢/DML/DDL/DCL等).置於該模式下的邏輯備庫,除了還需要運用SQL APPLY外,可讀寫,同一般的例項無區別.

STANDBY模式:

阻止除SYS使用者外的所有使用者對受SQL apply保護的所有物件的DDL和DDL操作.比如:透過SQL APPLY生成的表,普通使用者不允許對其進行DML/DDL操作,但普通使用者可以建立/操作主庫上不存在的表(物件).

ALL模式:

阻止除SYS使用者外的所有資料庫使用者進行任意更改操作,資料庫只讀.

同時邏輯備庫還支援session級別的guard保護開關,有時候需要進行維護操作,可以臨時將guard關閉掉以支援該session下的DDL/DML操作.

SQL>alter session disable/enable guard;

http://www.easyora.net/blog/logical_standby_daily_maintenance.html

轉自:
參考網址:
資料庫技術/oracle資料庫及相關/201005179/oracle-logical-standby-日常管理常用的一些語句.html
http://hi.baidu.com/edeed/blog/item/3b465aafdc6d81f7fbed50d7.html
http://fangrn.javaeye.com/blog/539004
http://fangrn.javaeye.com/blog/53901
http://blog.tianya.cn/blogger/post_show.asp?BlogID=1995494&PostID=19162830&idWriter=0&Key=0
http://www.easyora.net/blog/logical_standby_performance.html

                                                    

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

相關文章