Friday, November 7, 2008

ORA-12091: cannot online redefine table with materialized views

Problem Description
While I attempt to reorganize a table online whenever I do with DBMS_REDEFINITION.can_redef_table or DBMS_REDEFINITION.START_REDEF_TABLE procedure on the table it fails with error ORA-12091 as below.

SQL> exec dbms_redefinition.can_redef_table('CR_2', 'OUT_CDR');
BEGIN dbms_redefinition.can_redef_table('CR_2', 'OUT_CDR'); END;

*
ERROR at line 1:
ORA-12091: cannot online redefine table "CR_2"."OUT_CDR" with materialized views
ORA-06512: at "SYS.DBMS_REDEFINITION", line 137
ORA-06512: at "SYS.DBMS_REDEFINITION", line 1478
ORA-06512: at line 1

Cause of the Problem
If you unsuccessfully launched the DBMS_REDEFINITION.START_REDEF_TABLE procedure
once, the materialized view would be created on the interim table and still be remained there. So subsequent run of the DBMS_REDEFINITION.CAN_REDEF_TABLE BMS_REDEFINITION.START_REDEF_TABLE will return error until you remove the materalized view against the table.

Solution of the Problem
Solution 01:
Remove the snapshot log and materialized view created during the unsuccessful
first execution of the DBMS_REDEFINITION.START_REDEF_TABLE procedure.
You can define the snapshot log and materialized view log of current table by,
SQL> select log_table from user_snapshot_logs;

LOG_TABLE
------------------------------
MLOG$_IN_CDR
MLOG$_OUT_CDR

SQL> select master,log_table from user_mview_logs;

MASTER LOG_TABLE
------------------------------ ------------------------------
IN_CDR MLOG$_IN_CDR
OUT_CDR MLOG$_OUT_CDR

Remove it by,
SQL> DROP MATERIALIZED VIEW LOG ON IN_CDR;
Materialized view log dropped.

SQL> DROP MATERIALIZED VIEW LOG ON OUT_CDR;
Materialized view log dropped.

You can use the keyword SNAPSHOT in place of MATERIALIZED VIEW. The keyword SNAPSHOT is supported in place of MATERIALIZED VIEW for backward compatibility.

Now execute your statement like,
SQL> exec dbms_redefinition.can_redef_table('CR_2', 'OUT_CDR');
PL/SQL procedure successfully completed.
And it works.

Solution 02:
Run the dbms_redefinition.abort_redef_table procedure which will automatically do the clean up task. This procedure will remove the temporary objects that are created by the redefinition process such as materialized view logs.
SQL> exec dbms_redefinition.abort_redef_table('CR_2', 'IN_CDR', 'IN_CDR_');
PL/SQL procedure successfully completed.

Related Documents
How to Convert Long data type to Lob

How to convert non-partitioned table to partition table using re-definition

We will do partition of table OUT_CDR which reside on CR_2 schema. We will do partition on column CDATE using RANGE partitioning technique. We will not change any table structure other than partition. All indexes, constraints, triggers, privileges defined on the table will be remain same.

Step 01: Let's have a look at the table on which we will do partitioning.

SQL> set pagesize 200
SQL> set long 999999
SQL> set linesize 150
SQL> select dbms_metadata.get_ddl('TABLE','OUT_CDR','CR_2') from dual;


DBMS_METADATA.GET_DDL('TABLE','OUT_CDR','CR_2')
--------------------------------------------------------------------------------

CREATE TABLE "CR_2"."OUT_CDR"
( "ID" NUMBER(32,0) NOT NULL ENABLE,
"CDATE" DATE NOT NULL ENABLE,
"DDATE" DATE NOT NULL ENABLE,
"ACCTSESSIONID" VARCHAR2(100),
"CALLINGNO" VARCHAR2(100),
"CALLEDNO" VARCHAR2(100) NOT NULL ENABLE,
"AREACODE" VARCHAR2(100),
"PREFIX" VARCHAR2(100),
"SESSIONTIME" NUMBER(32,0),
"BILLABLETIME" NUMBER(32,0),
"RATE" NUMBER(32,4),
"CALL_COST" NUMBER(32,4),
"CURRENTBILL" NUMBER(32,4),
"DISCONNECTCAUSE" VARCHAR2(50),
"SOURCEIP" VARCHAR2(100),
"DESTIP" VARCHAR2(100),
"BILLABLE" NUMBER(32,0) NOT NULL ENABLE,
"LESS" NUMBER(32,0) NOT NULL ENABLE,
"ACCID" NUMBER(32,0),
"IN_DDATE" DATE,
"IN_PREFIX" VARCHAR2(100),
"IN_SESSIONTIME" NUMBER(32,0),
"IN_BILLABLETIME" NUMBER(32,0),
"IN_RATE" NUMBER(32,4),
"IN_CALL_COST" NUMBER(32,4),
"IN_MONEYLEFT" NUMBER(32,4),
"IN_DISCONNECTCAUSE" VARCHAR2(50),
"IN_BILLABLE" NUMBER(32,0),
"IN_LESS" NUMBER(32,0),
"SWITCH_ID" NUMBER(32,0) NOT NULL ENABLE,
"USER_ID" NUMBER(32,0) NOT NULL ENABLE,
"IN_USER_ID" NUMBER(32,0),
"PROCESSED" NUMBER(1,0),
CONSTRAINT "OUT_CDR_PK" PRIMARY KEY ("ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 168820736 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "CDR_INDX_SPC" ENABLE,
CONSTRAINT "OUT_CDR_UQ" UNIQUE ("CDATE", "CALLEDNO", "USER_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 522190848 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "CDR_INDX_SPC" ENABLE,
CONSTRAINT "OUT_CDR_UQ_2" UNIQUE ("DDATE", "CALLEDNO", "USER_ID")
USING INDEX PCTFREE 10 INITRANS 2 MAXTRANS 255 COMPUTE STATISTICS
STORAGE(INITIAL 521142272 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "CDR_INDX_SPC" ENABLE
) PCTFREE 10 PCTUSED 40 INITRANS 1 MAXTRANS 255 NOCOMPRESS LOGGING
STORAGE(INITIAL 2013265920 NEXT 1048576 MINEXTENTS 1 MAXEXTENTS 2147483645
PCTINCREASE 0 FREELISTS 1 FREELIST GROUPS 1 BUFFER_POOL DEFAULT)
TABLESPACE "OUT_CDR_NEW_SPC"

Step 02: Let's determine if the table OUT_CDR can be redefined online.
SQL> exec dbms_redefinition.can_redef_table('CR_2', 'OUT_CDR');
PL/SQL procedure successfully completed.

Step 03: Create a interim table which holds the same structure as the original table except constraints, indexes, triggers but add the partitioning attribute.
I named the interim table as OUT_CDR_. Later we may drop it.

SQL> CREATE TABLE "CR_2"."OUT_CDR_"
2 ( "ID" NUMBER(32,0),
3 "CDATE" DATE ,
4 "DDATE" DATE ,
5 "ACCTSESSIONID" VARCHAR2(100),
6 "CALLINGNO" VARCHAR2(100),
7 "CALLEDNO" VARCHAR2(100) ,
8 "AREACODE" VARCHAR2(100),
9 "PREFIX" VARCHAR2(100),
10 "SESSIONTIME" NUMBER(32,0),
11 "BILLABLETIME" NUMBER(32,0),
12 "RATE" NUMBER(32,4),
13 "CALL_COST" NUMBER(32,4),
14 "CURRENTBILL" NUMBER(32,4),
15 "DISCONNECTCAUSE" VARCHAR2(50),
16 "SOURCEIP" VARCHAR2(100),
17 "DESTIP" VARCHAR2(100),
18 "BILLABLE" NUMBER(32,0) ,
19 "LESS" NUMBER(32,0) ,
20 "ACCID" NUMBER(32,0),
21 "IN_DDATE" DATE,
22 "IN_PREFIX" VARCHAR2(100),
23 "IN_SESSIONTIME" NUMBER(32,0),
24 "IN_BILLABLETIME" NUMBER(32,0),
25 "IN_RATE" NUMBER(32,4),
26 "IN_CALL_COST" NUMBER(32,4),
27 "IN_MONEYLEFT" NUMBER(32,4),
28 "IN_DISCONNECTCAUSE" VARCHAR2(50),
29 "IN_BILLABLE" NUMBER(32,0),
30 "IN_LESS" NUMBER(32,0),
31 "SWITCH_ID" NUMBER(32,0) ,
32 "USER_ID" NUMBER(32,0) ,
33 "IN_USER_ID" NUMBER(32,0),
34 "PROCESSED" NUMBER(1,0)
35 ) TABLESPACE "OUT_CDR_NEW_SPC"
36 Partition by range(cdate)
37 (
38 partition P08152008 values less than (to_date('15-AUG-2008','DD-MON-YYYY')),
39 partition P09012008 values less than (to_date('01-SEP-2008','DD-MON-YYYY')),
40 partition P09152008 values less than (to_date('15-SEP-2008','DD-MON-YYYY')),
41 partition P10012008 values less than (to_date('01-OCT-2008','DD-MON-YYYY')),
42 partition P10152008 values less than (to_date('15-OCT-2008','DD-MON-YYYY')),
43 partition P11012008 values less than (to_date('01-NOV-2008','DD-MON-YYYY')),
44 partition P11152008 values less than (to_date('15-NOV-2008','DD-MON-YYYY')),
45 partition P12012008 values less than (to_date('01-DEC-2008','DD-MON-YYYY')),
46 partition P12152008 values less than (to_date('15-DEC-2008','DD-MON-YYYY')),
47 partition P01012009 values less than (to_date('01-JAN-2009','DD-MON-YYYY')),
48 partition P01152009 values less than (to_date('15-JAN-2009','DD-MON-YYYY')),
49 partition P02012009 values less than (to_date('01-FEB-2009','DD-MON-YYYY')),
50 partition PMAX values less than (maxvalue));

Table created.

Step 04: Initiates the redefinition process by calling dbms_redefinition.start_redef_table procedure.

SQL> exec dbms_redefinition.start_redef_table('CR_2', 'OUT_CDR', 'OUT_CDR_');

PL/SQL procedure successfully completed.

Step 05: Copies the dependent objects of the original table onto the interim table. The COPY_TABLE_DEPENDENTS Procedure clones the dependent objects of the table being redefined onto the interim table and registers the dependent objects. But this procedure does not clone the already registered dependent objects.

In fact COPY_TABLE_DEPENDENTS Procedure is used to clone the dependent objects like grants, triggers, constraints and privileges from the table being redefined to the interim table which in facr represents the post-redefinition table.

SQL> declare
2 error_count pls_integer := 0;
3 BEGIN
4 dbms_redefinition.copy_table_dependents('CR_2', 'OUT_CDR', 'OUT_CDR_',1, true, true, true, false,error_count);
5 dbms_output.put_line('errors := ' || to_char(error_count));
6 END;
7 /

PL/SQL procedure successfully completed.

Step 06: Completes the redefinition process by calling FINISH_REDEF_TABLE Procedure.
SQL> exec dbms_redefinition.finish_redef_table('CR_2', 'OUT_CDR', 'OUT_CDR_');

PL/SQL procedure successfully completed.

Step 07: Check the partitioning validation by,

SQL> Select partition_name, high_value from user_tab_partitions where table_name='OUT_CDR';

PARTITION_NAME HIGH_VALUE
------------------------------ ---------------------------------------------------------------------------------------------------
P01012009 TO_DATE(' 2009-01-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P01152009 TO_DATE(' 2009-01-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P02012009 TO_DATE(' 2009-02-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P08152008 TO_DATE(' 2008-08-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P09012008 TO_DATE(' 2008-09-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P09152008 TO_DATE(' 2008-09-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P10012008 TO_DATE(' 2008-10-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P10152008 TO_DATE(' 2008-10-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P11012008 TO_DATE(' 2008-11-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P11152008 TO_DATE(' 2008-11-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P12012008 TO_DATE(' 2008-12-01 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
P12152008 TO_DATE(' 2008-12-15 00:00:00', 'SYYYY-MM-DD HH24:MI:SS', 'NLS_CALENDAR=GREGORIA
PMAX MAXVALUE

13 rows selected.

Check index status by,

SQL> select index_name , status from user_indexes where table_name='OUT_CDR';


INDEX_NAME STATUS
------------------------------ --------
OUT_CDR_PK VALID
OUT_CDR_UQ VALID
OUT_CDR_UQ_2 VALID

Step 08: Drop the interim table OUT_CDR_.
SQL> DROP TABLE OUT_CDR_;
Table dropped.

Related Documents
ORA-12091: cannot online redefine table with materialized views
How to Convert Long data type to Lob

ORA-01033: ORACLE initialization or shutdown in progress

Error Description
While connecting to database user get the error,
ORA-01033: ORACLE initialization or shutdown in progress

Cause of The Problem
SYSDBA used issued STARTUP command to the database and while starting up database, connecting to database as normal user will get the error ORA-01033.

There may be the scenario that SHUTDOWN command waits a long time.
Solution of The Problem
Scenario 01:
Based on the situation STARTUP may take fewer minutes. As a normal user what more you do is to wait few minutes and try again. You will succeed if database is open state.

Here is the waiting result.
C:\Documents and Settings\Queen>sqlplus arju/a

SQL*Plus: Release 10.2.0.1.0 - Production on Fri Nov 7 17:34:23 2008

Copyright (c) 1982, 2005, Oracle. All rights reserved.

ERROR:
ORA-01033: ORACLE initialization or shutdown in progress


Enter user-name: arju
Enter password:
ERROR:
ORA-01033: ORACLE initialization or shutdown in progress
Enter user-name: arju
Enter password:

Connected to:
Oracle Database 10g Enterprise Edition Release 10.2.0.1.0 - Production
With the Partitioning, OLAP and Data Mining options

At third attempts I became succeed.

Scenario 02:

If you have SYSDBA privilege then connect to database as SYSDBA and see the status of the database.
SQL> conn / as sysdba
Connected.
SQL> select open_mode from v$database;
select open_mode from v$database
*
ERROR at line 1:
ORA-01507: database not mounted

SQL> select open_mode from v$database;
select open_mode from v$database
*
ERROR at line 1:
ORA-01507: database not mounted

SQL> select open_mode from v$database;

OPEN_MODE
----------
MOUNTED

SQL> select open_mode from v$database;

OPEN_MODE
----------
MOUNTED

SQL> select open_mode from v$database;


OPEN_MODE
----------
READ WRITE

As we can see that database gradually became usable state. Someone issued STARTUP and it take some times to be in READ WRITE state.

Scenario 03:
If shutdown takes more times suppose SHUTDOWN NORMAL then issue,
SHUTDOWN ABORT;
and later normal startup,
STARTUP;

Wednesday, November 5, 2008

Post Code of different areas of dhaka city

While filling up any form in website to put your address you are frequently asked to enter the post code of your location. In Bangladesh we usually not familiar ourselves with postal code. In this post, I tried to list the postal code inside Bangladesh.

This list is sorted by places alphabetically.

Dist Thana Suboffice Post Code
----- ----- ----------------- ----------
Dhaka Demra Demra 1360
Dhaka Demra Sarulia 1361
Dhaka Demra Matuail 1362
Dhaka Dhaka GPO Dhaka GPO 1000
Dhaka Dhaka Main Dhaka Main PO 1100
Dhaka Dhaka Sadar Wari TSO 1203
Dhaka Dhaka Sadar Gendaria TSO 1204
Dhaka Dhaka Sadar New Market TSO 1205
Dhaka Dhaka Sadar Dhaka CantonmentTSO 1206
Dhaka Dhaka Sadar Mohammadpur Housing 1207
Dhaka Dhaka Sadar Dhaka Politechnic 1208
Dhaka Dhaka Sadar Jigatala TSO 1209
Dhaka Dhaka Sadar Posta TSO 1211
Dhaka Dhaka Sadar Gulshan Model Town 1212
Dhaka Dhaka Sadar Banani TSO 1213
Dhaka Dhaka Sadar Basabo TSO 1214
Dhaka Dhaka Sadar Tejgaon TSO 1215
Dhaka Dhaka Sadar Mirpur Section-1 1216
Dhaka Dhaka Sadar Shantinagr TSO 1217
Dhaka Dhaka Sadar Mirpur TSO 1218
Dhaka Dhaka Sadar KhilgaonTSO 1219
Dhaka Dhaka Sadar Mirpur Section-12 1221
Dhaka Dhaka Sadar BangabhabanTSO 1222
Dhaka Dhaka Sadar DilkushaTSO 1223
Dhaka Dhaka Sadar Sangsad BhabanTSO 1225
Dhaka Dhaka Sadar KhilkhetTSO 1229
Dhaka Dhaka Sadar Uttara Model TwonTSO 1231
Dhaka Dhaka Sadar Dhania TSO 1232
Dhaka Dhamrai Dhamrai 1350
Dhaka Dhamrai Kamalpur 1351
Dhaka Joypara Joypara 1330
Dhaka Joypara Palamganj 1331
Dhaka Joypara Narisha 1332
Dhaka Keraniganj Keraniganj 1310
Dhaka Keraniganj Dhaka Jute Mills 1311
Dhaka Keraniganj Ati 1312
Dhaka Keraniganj Kalatia 1313
Dhaka Nawabganj Nawabganj 1320
Dhaka Nawabganj Hasnabad 1321
Dhaka Nawabganj Daudpur 1322
Dhaka Nawabganj Agla 1323
Dhaka Nawabganj Khalpar 1324
Dhaka Nawabganj Churain 1325
Dhaka Savar Savar 1340
Dhaka Savar Dairy Farm 1341
Dhaka Savar Jahangirnagar Univer 1342
Dhaka Savar Saver P.A.T.C 1343
Dhaka Savar Savar Canttonment 1344
Dhaka Savar Shimulia 1345
Dhaka Savar Kashem Cotton Mills 1346
Dhaka Savar Rajphulbaria 1347
Dhaka Savar Amin Bazar 1348
Dhaka Savar EPZ 1349

Related Documents
Post Code of Rajshahi Division in Bangladesh
Post Code of Barisal Division in Bangladesh
Post Code of Sylhet Division in Bangladesh
Post Code of Chittagong Division in Bangladesh
Post Code of Dhaka Division in Bangladesh
Post Code of Khulna Division in Bangladesh

Post Code of different districts of Bangladesh

In Bangladesh there are 64 districts. In order to recall easily the postcode of all districts I have note down them in my blog.


Dist Name Postcode
----------- ---------
Bagerhat 9300
Bandarban 4600
Barguna 8700
Barishal 8200
Bhola 8300
Bogra 5800
Bramhan Baria 3400
Chandpur 3600
Chapai
Nawabgonj 6300
Chittagong GPO 4000
Chittagong Head
Office 4100
Chuadanga 7200
Comilla 3500
Cox's Bazar 4700
Dhaka GPO 1000
Dhaka Head
Office 1100
Dinajpur 5200
Faridpur 7800
Feni 3900
Gaibandha 5700
Gazipur
(Joydevpur) 1700
Ghoramara 6100
Gopalgonj 8100
Hobigonj 3300
Jamalpur 2000
Jessore 7400
Jhalokathi 8400
Jhenaidah 7300
Joypurhat 5900
Khagrachhori 4400
Khulna GPO 9000
Khulna Head
Office 9100
Kishoregonj 2300
Kurigram 5600
Kushtia 7000
Lalmonirhat 5500
Luxmipur 3700
Madaripur 7900
Magura 7600
Manikgonj 1800
Meherpur 7100
Moulvibazar 3200
Munshigonj 1500
Mymensingh 2200
Naogaon 6500
Narail 7500
Narashingdi 1600
Narayangonj 1400
Natore 6400
Netrokona 2400
Nilfamari 5300
Noakhali 3800
Pabna 6600
Panchagar 5000
Patuakhali 8600
Pirozpur 8500
Rajbari 7700
Rajshahi City 6200
Rajshahi GPO 6000
Rangamati 4500
Rangpur 5400
Satkhira 9400
Shariatpur 8000
Sherpur 2100
Sirajgonj 6700
Sunamgonj 3000
Tangail 1900
Thakoregaon 5100

Related Documents
Post Code of Rajshahi Division in Bangladesh
Post Code of Barisal Division in Bangladesh
Post Code of Sylhet Division in Bangladesh
Post Code of Chittagong Division in Bangladesh
Post Code of Dhaka Division in Bangladesh
Post Code of Khulna Division in Bangladesh

Tuesday, November 4, 2008

Block or Accept Oracle access by IP Address

You sometimes may wish to access to logon to your database filtered by IP address. Suppose you will allow to connect to database having a list of IP address. Or you like to ban a list of IP addresses in order to deny logon as a database user.

With oracle this scenario can be achieved, however this seems to me a bit of fun.

The secret lies in the SQLNET.ORA file. On UNIX system this file resides in $ORACLE_HOME/network/admin directory along with tnsnames.ora and listener.ora.

In order to put any filtering by IP address open the sqlnet.ora file with any editor and insert the following line,

tcp.validnode_checking = yes

This in fact, turns on the hostname/IP checking for your listeners. After this, with
tcp.invited_nodes /tcp.excluded_nodes you can supply lists of nodes to enable/disable, as such:


tcp.invited_nodes = (hostname1, hostname2)
tcp.excluded_nodes = (192.168.100.101,192.168.100.160)


Note that if you only specify invited nodes with tcp.invited_nodes, all other nodes will be excluded, so there is really no reason to do both. The same is true for excluded nodes. If you put tcp.excluded_nodes = (192.168.100.101,192.168.100.160) then IP containing 192.168.100.101 and 192.168.100.160 will be excluded/denied to connect to database as a database user while allowing others to connect.

Some rules for entering invited/excluded nodes:

1. You cannot use wild cards in your specifications.
2. You must put all invited nodes in one line; likewise for excluded nodes.
3. You should always enter localhost as an invited node.

Once you have set up your rules and enabled valid node checking, you must restart your listeners to reap the benefits.

To do so,
$lsnrctl stop
$lsnrctl start


A simple example: Suppose in your database server you simply allow the host containing IP Address 192.168.100.2 and 192.168.100.3. Then your sqlnet.ora file look like,

tcp.validnode_checking = yes
tcp.invited_nodes = (localhost,192.168.100.2,192.168.100.3)