Friday, September 30, 2011

Golden Gate Database Setup Part 2

In the first post, we did a bit on setting up the schemas for GG Replication both on the source and target databases.
Now we have to do a few things at the source database.

Connect as sqlplus '/ as sysdba'
--enable supplemental logging
alter database add supplemental log data
/
--switch logfile so that the next logfile has supplemental logging
alter system switch logfile
/
--check whether supplemental login was really enabled.As if you did not even bother to read output of step 1.
select supplemental_log_data_min from v$database
/

Now the fun part, login to golden gate on the source side
./ggsci


dblogin userid ggddlusr,password welcome123

add schematrandata SCOTT

The above will enable logging at schema level and makes your life easier compared to
ADD TRANDATA <table name>, as new tables are automatically picked up from the schema SCOTT.
By the way , the above command is supported on 11gR2 and needs PATCH 10423000.
To verify whether the above command worked
select * from table(logmnr$always_suplog_columns('owner in upper case','table in upper case'))

Or I have neatly scripted the below


declare
l_schema varchar2(20) := upper('PINNACLE') ;
l_table_name varchar2(200);
x_ctr number(5);
x_refcur sys_refcursor;
begin
open x_refcur for 'select table_name from dba_tables where owner='''||l_schema||''' order by table_name';
loop
fetch x_refcur into l_table_name;
exit when x_refcur%NOTFOUND;

execute immediate 'select count(*) from table(logmnr$always_suplog_columns(:1,:2))'
into x_ctr using l_schema,l_table_name ;
if x_ctr =0 then

dbms_output.put_line('Analyzing  '||l_table_name||' ..**ERROR**');
else
dbms_output.put_line('Analyzing  '||l_table_name||' ..OK');
end if;

end loop;
end;
/



As of now, this patch is not available on our platform HP-UX Itanium and we have to still use
ADD TRANDATA which is very painful.

But here is the script to generate the script

set echo off
set verify off
set pagesize 2000
set linesize 250
set trim on
set heading off
set feedback off
spool /tmp/scott.obey
select 'add trandata SCOTT.'||table_name
from dba_tables where owner = 'SCOTT' ;
spool off


Once the above file is generated , go back in ggsci and type

obey /tmp/scott.obey

By the way , a small note on the above step ---


1. The "add trandata" does something like "alter table <name> add supplemental log group ggs_<table name>_object id (comma seperated columns) ALWAYS"
Its just to give you an idea and the actual "alter table" sql executed behind the scenes might differ depending on the db versions etc

2. To drop the table level supplemental logging you could either do it from ggsci or sqlplus

ggsci> dblogin userid <id> password <pw>
ggsci> info trandata schema.table_name
ggsci> DELETE TRANDATA schema.table_name

or

sql> select * from dba_log_groups where table_name=upper('&table_name') and owner=upper('&owner');
sql> alter table <table name> drop supplemental log group <log group name>;

By the way, if you want to add table level logging for all columns, you use 'supplemental log data' instead of supplemental log group -

ALTER TABLE tab1 ADD SUPPLEMENTAL LOG DATA (ALL) COLUMNS;
The valid options are ALL,PRIMARY,UNIQUE and FOREIGN KEY.
The above sql creates a supplemental log group starting with SYS_C instead of GGS_xxxx

To drop , you can use
ALTER TABLE tab1 DROP SUPPLEMENTAL LOG DATA (ALL) COLUMNS;
OR
ALTER TABLE tab1 DROP SUPPLEMENTAL LOG GROUP SYS_Cxxxxxx

/



**PLEASE NOTE, IF YOU HAD USED ADD SCHEMATRANDATA THERE WILL BE NO ENTRIES IN DBA_LOG_GROUPS TABLE.YOU WILL HAVE TO USE THE BELOW QUERY
select * from table(logmnr$always_suplog_columns('owner in upper case','table in upper case'))
**


We are now all done and go to the next step.


Golden Gate Schema Setup Part 1

The idea behind this post is not simply write one more post about GG Schema setup. There are tons of information about them from the official oracle documentation which is very good and some excellent blogs on this especially the Gavinsoorma Blogs, from which I learnt myself.

This posting is just to fill the gaps in-between and and I still suggest that you go to the other resources if you are a newbie.

To be honest, I like to keep things simple and prefer to run a standard script at both the source and destination eventhough a few privs are more specific to source and some to destination.

There are a few things which i do differently. One of them is creating a checkpoint table (obviously this is at the destination) for each REPLICAT process instead of a common checkpoint table per destination. This is just to ensure that one DBA does not mess-up with the checkpoint information of an other DBA in environments where there is a common reporting database and multiple people work on setting up their own replicat process.


Without further ado , let me jump into the gory syntax stuff.

COMMON STUFF AT BOTH SOURCE AND DESTINATION
=============================================

create tablespace ggdata          
datafile '+DATA' size 100m
autoextend on maxsize 4096m
extent management local uniform size 1m
/
create user ggddlusr identified by welcome123
/

alter user ggddlusr
default tablespace ggdata
temporary tablespace temp
quota unlimited on ggdata
/
grant dba to ggddlusr
/

--The below is needed at the source only -read my note below
--but I run them at source and target -keep things simple policy.
exec dbms_goldengate_auth.grant_admin_privilege('GGDDLUSR')
/

That is it....I just skip stuff like  execute on flashback_database etc.
I just grant dba privilege and that is all I do....As I said I keep things simple as you wouldn't expect anybody else except the DBA team to use ggddlusr.

Also please note that

Manager (RMAN) works with Extract to retain the archive logs that Extract needs for
recovery. The special privileges are required for interaction with an underlying Oracle
Streams Capture and with RMAN.

For this to happen , you need to make sure that at the source you need to do the following based on the oracle version.

Oracle EE version Privileges
10.2 1. Run package to grant Oracle Streams admin privilege.
exec dbms_streams_auth.grant_admin_privilege('<user>')
2. Grant INSERT into logmnr_restart_ckpt$.
grant insert on system.logmnr_restart_ckpt$ to <user>;
3. Grant UPDATE on streams$_capture_process.
grant update on sys.streams$_capture_process to <user>;
4. Grant the 'become user' privilege.
grant become user to <user>;
11.1 and 11.2.0.1 1. Run package to grant Oracle Streams admin privilege.
exec dbms_streams_auth.grant_admin_privilege('<user>')
2. Grant the 'become user' privilege.
grant become user to <user>;
11.2.0.2 and later Run package to grant Oracle Streams admin privilege.
exec dbms_goldengate_auth.grant_admin_privilege('<user>');

Since I am running the above on 11.2.0.2 release, I just had to run


exec dbms_goldengate_auth.grant_admin_privilege('GGDDLUSR')
/
Please make sure that you do what is appropriate for your version.







Now go to Golden Gate Home, and open the file called GLOBALS (if not there, feel free to create a new file called GLOBALS) and put the following entry
GGSCHEMA <schema name>

In our case the <schema name> is GGDDLUSR.






Now that we have the common user GGDDLUSER setup in both source and target , let us go into some source and target specific actions.


STEPS AT SOURCE DATABASE
==========================
This step is to add DDL support to make sure that DDL changes like new tables, modified tables (ALTER TABLE...) at the source is pushed to destination side automatically. I just do this as this is a common requirement for all my replication needs.nowadays I just do this as a matter of habit.i recommend you do it too.
Just go to golden gate home directory and invoke

sqlplus '/ as sysdba' and run the following ----

--SETUP DDL USER
@marker_setup.sql
--when prompted type in ggddlusr
@ddl_setup.sql
--when prompted type in ggddlusr AND INITIALSETUP
@role_setup.sql
--when prompted type in ggddlusr
grant GGS_GGSUSER_ROLE to ggddlusr
/
@ddl_enable.sql
@ddl_pin ggddlusr

It is a very good idea to check whether all the above commands really setup the DDL replication by running
the following command.
  ddl_status.sql

whew!!!We are almost done except that we will create a checkpoint table at our target database to enable REPLICAT to use it to know where it was !!!


STEPS AT TARGET DATABASE
=========================

 Login to Golden Gate, by typing ggsci
 and do the following

dblogin userid ggddlusr,password welcome123
add checkpointtable ggddlusr.ckptpubs

Let me jump ahead and tell you the command to make sure how the above checkpoint table can be made to be used by the replicat process
add replicat pubsrep, exttrail /oracle/software/goldengate/11.1.1.1/dirdat/cc, CHECKPOINTTABLE ggddlusr.ckptpubs

There you got it!!!!We are all done except that we have to prepare the source database by adding supplemental logging and few other stuff.

I am going to discuss that in the next blog.Hope you find things easy.