=========================================
  Replication setting with Java toolkit:
-----------------------------------------

# Check procedures:
select oid,proname,prosrc from pg_proc where proname like 'lastchg_addto%';
select oid,proname,prosrc from pg_proc where proname like '%rserv%';

# Check triggers:

SELECT cl.relname, tr.tgname AS triggername, tr.tgenabled, fn.proname
        AS func_name
        FROM pg_trigger AS tr, pg_class AS cl, pg_proc AS fn
        WHERE tr.tgrelid=cl.oid AND tr.tgfoid=fn.oid
        and tr.tgname like '_rserv%';

# Check tables:

SELECT * from _RSERV_TABLES_;

select pgc.oid, pga.attnum from pg_class pgc, pg_attribute pga
where pgc.relname = 'test'
 and pgc.oid = pga.attrelid
  and pga.attname = 'k';

============================================
 Test real thing:
--------------------------------------------

select account_id, balance from billing_account;
update billing_account set balance=3.62 where account_id=1; 

psql  -c "drop database master_g" template1; psql  -c "drop database slave_g" template1
psql  -c "create database master_g" template1; psql  -c "create database slave_g" template1

pg_dump gennady > gennady_dump.sql
pg_dump -Ft -b gennady > gennady_dump.tar
pg_restore -d master_g -s gennady_dump.tar

mkdir $SQL_TMP_DIR
$JAVA  $SQL_GEN_CLASS  -mode add_marker $MASTER_J > $SQL_TMP_DIR/add_marker_master.sql
$PSQL $MASTER < $SQL_TMP_DIR/add_marker_master.sql
$JAVA  $SQL_GEN_CLASS  -mode set_marker_def $MASTER_J > $SQL_TMP_DIR/set_marker_def.sql

psql master_g < gennady_dump.sql
psql slave_g < gennady_dump.sql

=============================================
 Setting up the test:
---------------------------------------------

pre- steps:
a) quick test settings:
        start replication source generator:
          . /home/gennady/system/postgresql/jdbcsystem/env.sh



1) InitRservTest

2) Remove unnecessary things:

  --------------------------------------------------------------------------------------------
   java  com.postgres.replic.JDBCSystem  -mode drop_trigger -db master > drop_trigger.sql
   java  com.postgres.replic.JDBCSystem  -mode marker_proc_remove -db master > drop_proc.sql

    psql  master <  drop_trigger.sql

    psql  master <  drop_proc.sql

    psql master < lastchg_remove.sql

    psql master < cleanup_master.sql

    psql slave < cleanup_slave.sql



  --------------------------------------------------------------------------------------------
 499174
3)

        java com.postgres.replic.JDBCSystem -db master -mode script  > master_add.sh
        java com.postgres.replic.JDBCSystem -db slave -mode script -slave > slave_add.sh
        java  com.postgres.replic.JDBCSystem  -mode marker_proc_add -db master > add_proc.sql
        java  com.postgres.replic.JDBCSystem  -mode seq -db master > add_seq.sql
        java  com.postgres.replic.JDBCSystem  -mode add_marker -db master > add_marker_master.sql
        java  com.postgres.replic.JDBCSystem  -mode add_marker -db slave -slave > add_marker_slave.sql

        psql master < add_marker_master.sql
        psql slave < add_marker_slave.sql

        java  com.postgres.replic.JDBCSystem  -mode update_marker_col -db master > update_marker_master.sql

        update test set _rserv_ts = l;
         or
        psql master < update_marker_master.sql

        # psql slave :

        delete from test;
        delete from testoid;
        delete from teststr;
        # dump the master:
        pg_dump master > master_dump.sql
        psql slave < master_dump.sql
        psql slave2 < slave_dump.sql

        createlang -U postgres -d master -L /usr/local/pgsql/lib plpgsql
        psql master < lastchg_install.sql
        psql master < add_proc.sql
        psql master < add_seq.sql

        psql master < master.sql

        psql slave < slave.sql

        master_add.sh
        slave_add.sh


4) Try it:

   update test set i=102, k=102 where l=100;
   select * from _RSERV_LOG_;
   Replicate master slave;
   select * from test where l=100;

   update test set i=999, k=999 where l=99;
   select * from _RSERV_LOG_;
   Replicate master slave;
   select * from test where l=99;

   insert into test values (111, 111, 111);
   Replicate master slave;
   select * from test where l=111;

   delete from test  where l=111;
   Replicate master slave;
   select * from test where l=111;


Alt 3)  (for test start with InitRservTest)

  Setup Env:
  ---------------

     ========== Following portion will be set with shell scrips: ==================
        POSTGRES_HOME=/usr/local/pgsql
        SU=postgres

        MASTER_HOST=localhost
        MASTER_PORT=5432
        MASTER_USER=gennady
        MASTER_DB=master

        SLAVE_HOST=localhost
        SLAVE_PORT=5432
        SLAVE_USER=gennady
        SLAVE_DB=slave

        SQL_GEN_CLASS=com.postgres.replic.JDBCSystem
        MASTER="-h $MASTER_HOST -p $MASTER_PORT -d $MASTER_DB -U $MASTER_USER"
        SLAVE="-h $SLAVE_HOST -p $SLAVE_PORT -d $SLAVE_DB -U $SLAVE_USER"
        MASTER_J="-host $MASTER_HOST -db $MASTER_DB -port $MASTER_PORT -user $MASTER_USER"
        SLAVE_J="-host $SLAVE_HOST -db $SLAVE_DB -port $SLAVE_PORT -user $SLAVE_USER"
        PSQL_BIN=$POSTGRES_HOME/bin
        PSQL=$PSQL_BIN/psql
        JAVA=java
     ================================================================================

  mkdir $SQL_TMP_DIR

  # Setup Master:
  # ---------------
        $JAVA  $SQL_GEN_CLASS $MASTER_J -mode script  > $SQL_TMP_DIR/master_add.sh
        $JAVA  $SQL_GEN_CLASS  -mode marker_proc_add $MASTER_J > $SQL_TMP_DIR/add_proc.sql
        $JAVA  $SQL_GEN_CLASS  -mode seq $MASTER_J > $SQL_TMP_DIR/add_seq.sql
        $JAVA  $SQL_GEN_CLASS  -mode add_marker $MASTER_J > $SQL_TMP_DIR/add_marker_master.sql

        $PSQL $MASTER < $SQL_TMP_DIR/add_marker_master.sql
        $JAVA $SQL_GEN_CLASS  -mode update_marker_col $MASTER_J > $SQL_TMP_DIR/update_marker_master.sql
        $PSQL $MASTER < $SQL_TMP_DIR/update_marker_master.sql

        # dump the master:
        $PSQL_BIN/pg_dump --host $MASTER_HOST --port $MASTER_PORT $MASTER_DB > $SQL_TMP_DIR/master_dump.sql

        $JAVA  $SQL_GEN_CLASS  -mode set_marker_def $MASTER_J > $SQL_TMP_DIR/set_marker_def.sql
        $PSQL $MASTER < $SQL_DIR/set_marker_def.sql
        $PSQL_BIN/createlang -U $SU -h $MASTER_HOST -p $MASTER_PORT -d $MASTER_DB -L $POSTGRES_HOME/lib plpgsql
        $PSQL $MASTER < $SQL_DIR/lastchg_install.sql
        $PSQL $MASTER < $SQL_TMP_DIR/add_proc.sql
        $PSQL $MASTER < $SQL_TMP_DIR/add_seq.sql
        $PSQL $MASTER < $SQL_DIR/master.sql
        $SQL_TMP_DIR/master_add.sh

  # Setup Slave:
  # ------------
        $PSQL  -c "drop database $SLAVE_DB" template1      // replace following 2 lines with create_database.sql script
        $PSQL  -c "create database $SLAVE_DB" template1    //
        $PSQL $SLAVE < $SQL_TMP_DIR/master_dump.sql
        $JAVA $SQL_GEN_CLASS $SLAVE_J -mode script -slave > $SQL_TMP_DIR/slave_add.sh
        #### Following 2 steps are necessary if you do not use dump of the master:
        #   $JAVA $SQL_GEN_CLASS  -mode add_marker $SLAVE_J -slave > $SQL_TMP_DIR/add_marker_slave.sql
        #   $PSQL $SLAVE < $SQL_TMP_DIR/add_marker_slave.sql
        ####

        $PSQL $SLAVE < $SQL_DIR/slave.sql
        $SQL_TMP_DIR/slave_add.sh


 # Alt 2) Remove unnecessary things:
 #
 # --------------------------------------------------------------------------------------------
   $JAVA $SQL_GEN_CLASS -mode drop_trigger $MASTER_J > $SQL_TMP_DIR/drop_trigger.sql
   $JAVA $SQL_GEN_CLASS -mode marker_proc_remove $MASTER_J > $SQL_TMP_DIR/drop_proc.sql

    $PSQL $MASTER <  $SQL_TMP_DIR/drop_trigger.sql

    $PSQL $MASTER <  $SQL_TMP_DIR/drop_proc.sql

    $PSQL $MASTER < $SQL_DIR/lastchg_remove.sql

    $PSQL $MASTER < $SQL_DIR/cleanup_master.sql

    $PSQL $SLAVE < $SQL_DIR/cleanup_slave.sql


 ---------------------------------------
  TEST
  -------------------------------------
  InitRservTest
  reset.sh
  setup_master.sh
  setup_slave.sh
  pg_dump slave > slave_dump.sql
  psql  -c "drop database slave2" template1
  psql  -c "create database slave2" template1
  psql slave2 < slave_dump.sql
  select * from _rserv_slave_tables_;

  // on slave run:
  delete from _rserv_slave_tables_ where tname='teststr';

  // on slave2 run:
  delete from _rserv_slave_tables_ where tname='test';

  // on master:
  update test set i=102, k=102 where l=100;
   select * from _RSERV_LOG_;
   Replicate master slave;
   select * from test where l=100;






