[20181002]DBMS_FLASHBACK與函式.txt
[20181002]DBMS_FLASHBACK與函式.txt
http://berxblog.blogspot.com/2018/10/seing-your-db-as-it-was-some-minutes-ago.html
--//重複測試:
DBMS_FLASHBACK Overview
DBMS_FLASHBACK provides an interface for the user to view the database at a particular time in the past, with the
additional capacity provided by transaction back out features that allow for selective removal of the effects of
individual transactions. This is different from a flashback database which moves the database back in time.
When DBMS_FLASHBACK is enabled, the user session uses the Flashback version of the database, and applications can
execute against the Flashback version of the database.
DBMS_FLASHBACK is relevant only for the session in which it's called, so it doesn't change the database.
But it's unclear what's meant by database in this context.
1.環境:
SCOTT@test01p> @ ver1
PORT_STRING VERSION BANNER CON_ID
------------------------------ -------------- -------------------------------------------------------------------------------- ----------
IBMPC/WIN_NT64-9.1.0 12.1.0.1.0 Oracle Database 12c Enterprise Edition Release 12.1.0.1.0 - 64bit Production 0
drop table x1 purge;
create table x1 (u number);
insert into x1 (u) values (1);
CREATE OR REPLACE FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 1
BEGIN
IF ( i = 1 ) THEN
RETURN 'red';
ELSE
RETURN 'blue';
END IF;
END return_something;
/
2.測試:
SCOTT@test01p> select sysdate now, u, return_something(u) c10,current_scn from x1,v$database;
NOW U C10 CURRENT_SCN
------------------- ---------- ---------- -----------
2018-10-03 19:46:02 1 red 29429234
SCOTT@test01p> host sleep 60
SCOTT@test01p> update x1 set u = 2 where u = 1;
1 row updated.
CREATE OR REPLACE FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
/
SCOTT@test01p> select sysdate now, u, return_something(u) c10,current_scn from x1,v$database;
NOW U C10 CURRENT_SCN
------------------- ---------- ---------- -----------
2018-10-03 19:50:35 2 zwoa 29429620
3.使用DBMS_FLASHBACK.
--//EXEC dbms_flashback.enable_at_time(to_timestamp('2018-10-03 19:46:02','YYYY-MM-DD HH24:MI:SS'));
SCOTT@test01p> exec dbms_flashback.enable_at_system_change_number(29429234);
PL/SQL procedure successfully completed.
SCOTT@test01p> select sysdate now, u, return_something(u) c10,current_scn from x1,v$database;
NOW U C10 CURRENT_SCN
------------------- ---------- ---------- -----------
2018-10-03 19:52:24 1 oans 29429665
--//注意函式return_something(u)的返回,使用新定義的函式.sysdate,CURRENT_SCN也是在走.
SCOTT@test01p> col text for A50
SCOTT@test01p> select text from dba_source where owner=user and name='RETURN_SOMETHING' order by LINE asc;
TEXT
--------------------------------------------------
FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
11 rows selected.
--//我這裡看到與作者不同,函式的內容是新定義的.開啟另外會話:
SYS@test01p> select text c100 from dba_source as of scn 29429234 where owner='SCOTT' and name='RETURN_SOMETHING' order by LINE asc;
C100
----------------------------------------------------
FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
11 rows selected.
--//奇怪,12c不能看到前面的版本.
--//連結:http://berxblog.blogspot.com/2018/10/seing-your-db-as-it-was-some-minutes-ago.html
Here we can see how DBMS_FLASHBACK.ENABLE_AT_TIME is set to a time between the 1st insert & create function and the 2nd
block.
With this setting, the content of table X1 is as expected. Also DBA_SOURCE shows the code of RETURN_SOMETHING.
But the function itself is not changed in memory and works as of it's state NOW, not at the given flashback time.
The flashback time version of RETURN_SOMETHING is even visible when you open it in SQLDeveloper (you have to believe me
or test it).
Don't forget to clean up after the tests:
SCOTT@test01p> exec dbms_flashback.disable;
PL/SQL procedure successfully completed.
SCOTT@test01p> select sysdate now, u, return_something(u) c10,current_scn from x1,v$database;
NOW U C10 CURRENT_SCN
------------------- ---------- ---------- -----------
2018-10-03 20:01:04 2 zwoa 29431158
For this testcase no COMMIT was used. ;-)
4.收尾:
SCOTT@test01p> drop table x1 purge;
Table dropped.
SCOTT@test01p> drop FUNCTION "RETURN_SOMETHING";
Function dropped.
5.總結:
其它不解析,但是我的這個12c版本,函式看不到舊版本,重複測試看看.
CREATE OR REPLACE FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 1
BEGIN
IF ( i = 1 ) THEN
RETURN 'red';
ELSE
RETURN 'blue';
END IF;
END return_something;
/
SCOTT@test01p> select current_scn,sysdate from v$database ;
CURRENT_SCN SYSDATE
----------- -------------------
29431442 2018-10-03 20:07:15
SCOTT@test01p> select text from dba_source as of scn 29431442 where owner=user and name='RETURN_SOMETHING' order by LINE asc;
select text from dba_source as of scn 29431442 where owner=user and name='RETURN_SOMETHING' order by LINE asc
*
ERROR at line 1:
ORA-01031: insufficient privileges
--//沒有許可權看dba_source檢視.
SCOTT@test01p> select text from user_source where name='RETURN_SOMETHING' order by LINE asc;
no rows selected
SCOTT@test01p> select text from user_source as of scn 29431442 where name='RETURN_SOMETHING' order by LINE asc;
select text from user_source as of scn 29431442 where name='RETURN_SOMETHING' order by LINE asc
*
ERROR at line 1:
ORA-01031: insufficient privileges
--//換一個使用者SYS.
SYS@test01p> select text from dba_source as of timestamp to_date('2018-10-03 20:07:15','yyyy-mm-dd hh24:mi:ss') where owner=user and name='RETURN_SOMETHING' order by LINE asc;
no rows selected
--//再次回到原來會話:
CREATE OR REPLACE FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
/
SCOTT@test01p> select text c50 from user_source where name='RETURN_SOMETHING' order by LINE asc;
C50
--------------------------------------------------
FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
11 rows selected.
SCOTT@test01p> select text c50 from dba_source where name='RETURN_SOMETHING' order by LINE asc;
C50
--------------------------------------------------
FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
11 rows selected.
SYS@test01p> select text c50 from dba_source as of scn 29431442 where name='RETURN_SOMETHING' order by LINE asc;
C50
--------------------------------------------------
FUNCTION "RETURN_SOMETHING" (
i NUMBER
) RETURN VARCHAR2 AS
-- Version 2
BEGIN
IF ( i = 1 ) THEN
RETURN 'oans';
ELSE
RETURN 'zwoa';
END IF;
END return_something;
11 rows selected.
--//看到的是新定義的函式.
--//很明顯,oracle 12c這個版本有問題.
來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/267265/viewspace-2215417/,如需轉載,請註明出處,否則將追究法律責任。
相關文章
- [20231123]函式與bash shell呼叫.txt函式
- [20180531]函式呼叫與遞迴.txt函式遞迴
- [20190827]函式索引與選擇率.txt函式索引
- [20241012]cursor_sharing=force與函式索引.txt函式索引
- [20180626]函式與標量子查詢14.txt函式
- [20180612]函式與標量子查詢10.txt函式
- [20180611]函式與標量子查詢9.txt函式
- [20180607]函式與標量子查詢8.txt函式
- [20180602]函式與標量子查詢4.txt函式
- [20180602]函式與標量子查詢3.txt函式
- 20180601]函式與標量子查詢2.txt函式
- [20200213]函式nullif使用.txt函式Null
- [20211231]函式索引測試.txt函式索引
- [20180509]函式索引問題.txt函式索引
- [20180917]關於分析函式的range與rows的區別.txt函式
- [20191002]函式dump的bug.txt函式
- [20190728]分析函式LAST_VALUE.txt函式AST
- [20190401]關於semtimedop函式呼叫.txt函式
- Python函式與lambda 表示式(匿名函式)Python函式
- [20190918]關於函式索引問題.txt函式索引
- 建構函式與解構函式函式
- 函式節流與函式防抖函式
- 回撥函式 與 函式閉包函式
- Oracle分析函式與視窗函式Oracle函式
- 何時使用函式表示式與函式宣告函式
- echo與函式函式
- webgl內建函式--幾何函式與矩陣函式Web函式矩陣
- webgl內建函式--向量函式與紋理查詢函式Web函式
- 函式與極限 第一節 對映與函式函式
- [20190402]關於semtimedop函式呼叫2.txt函式
- 普通函式與函式模板呼叫規則函式
- 箭頭函式與普通函式區別函式
- 函式外與函式內的變數函式變數
- python內建函式-eval()函式與exec()函式的區別Python函式
- 生成函式與多項式函式
- 函式遞迴與生成式函式遞迴
- 函式宣告與函式表示式有什麼區別?函式
- 普通函式與函式模板呼叫規則2函式