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

psql  -c "drop database master" template1
psql  -c "drop database slave" template1
psql  -c "drop database slave2" template1

psql  -c "create database master" template1
psql  -c "create database slave" template1
psql  -c "create database slave2" template1

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                  gettables
psql master_g < gennady_dump.sql
psql slave_g < gennady_dump.sql




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


psql master < master.sql
psql slave < slave.sql


MasterAddTable master test k
MasterAddTable master testoid oid
MasterAddTable master teststr i

or just use InitRservTest  (modified version writing to /tmp/__t... file)

or new :

master_add.sh
slave_add.sh


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

2) 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';

===============================================================

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

 select relname, reltype from pg_class where oid in (230048,230114,230143,230475);

 select * from _rserv_log_;
 select * from _rserv_sync_;

 (17,20,191,192,18,21,193,194);


create trigger _RSERV_TRIGGER_T_1 after
insert or update or delete on test
 for each row execute procedure
  _rserv_log_('2');

select * from test where l=100;
update test set i=101 where l=100;

select * from _RSERV_LOG_ ;

Replicate master slave:

1) Prepare snapshot:

select pgc.oid, pgc.relname, pga.attname
 from _RSERV_TABLES_ rt, pg_class pgc, pg_attribute pga
  where pgc.oid = rt.reloid and pga.attrelid = rt.reloid
   and pga.attnum = rt.key;

2) Read last succeeded sync:

select syncid, synctime, minid, maxid, active from _RSERV_SYNC_
 where server = 0 and syncid = (select max(syncid) from
  _RSERV_SYNC_ where server = 0 and status > 0);

3) get deleted rows:

select l.reloid, l.key from _RSERV_LOG_ l
 where l.deleted = 1 order by l.reloid;



===============================================
   SETUP THE REPLICATION
-----------------------------------------------

1) make create  triggers script:

  java com.postgres.replic.JDBCSystem -db master -mode script  > master_add.sh

2) set up structures for a slave database. Does not include triggers,
   but only bookkeeping tables:

  java com.postgres.replic.JDBCSystem -db slave -mode script -slave > slave_add.sh

3) set up structures and user-defined functions for a master
   database.

   MasterInit master

   alterntive) it is may be better to use:

   psql master < master.sql



4) sets up structures for a slave database. Does not include triggers,
   but only bookkeeping tables.

   SlaveInit slave

   or

   psql slave < slave.sql

5)
    ./master_add.sh

6)  ./slave_add.sh

7) Replicate start server (or cronjob) running the following command at certain
   time interval:

   Replicate master_dbname slave_dbname masterhost=localhost masteruser=gennady masterpassword=gena slavehost=localhost slaveuser=gennady slavepassword=gena
   Replicate master slave masterhost=localhost masteruser=gennady masterpassword=gena slavehost=localhost slaveuser=gennady slavepassword=gena

   "masterhost=s", "slavehost=s", "host=s",
		     "masteruser=s", "slaveuser=s", "user=s",
		     "masterpassword=s", "slavepassword=s", "password=s"

===========================================================================================
   RESET THE DATABASES:
-------------------------------------------------------------------------------------------


----------------
        a) quick test settings:
        start replication source generator:
          . /home/gennady/system/postgresql/jdbcsystem/env.sh
          java  com.postgres.replic.JDBCSystem -db master -mode trigger_drop -verbose


1) java  com.postgres.replic.JDBCSystem  -mode drop_trigger -db master > drop_trigger.sql

2) psql  master <  drop_trigger.sql

3) psql master < cleanup_master.sql

4) psql slave < cleanup_slave.sql

-------------------------------------------------------------------------------------------

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

alter table test add ts timestamp DEFAULT current_timestamp;
update test set ts=current_timestamp;

select lastchg_addto('test', 'ts');
select * from test where l=100;
update test set i=100 where l=100;
select * from test where l=100;

MasterAddTable master test ts
select * from _RSERV_LOG_;
delete from _RSERV_LOG_;
delete from _RSERV_SYNC_;

update test set k=100 where l=100;
Replicate master slave;


--------------------------------------------------------------------------------------------

InitRservTest
. /home/gennady/system/postgresql/jdbcsystem/env.sh
1) java  com.postgres.replic.JDBCSystem  -mode drop_trigger -db master > drop_trigger.sql

2) psql  master <  drop_trigger.sql

3) psql master < cleanup_master.sql

4) psql slave < cleanup_slave.sql


drop sequence _rserv_test_seq_;
create sequence _rserv_test_seq_ minvalue 101;

alter table test add ts integer;
alter table test  alter column ts set  DEFAULT nextval('_rserv_test_seq_');

# alter table test add ts timestamp DEFAULT now;
# alter table test alter column ts set DEFAULT 'now';
# update test set ts=current_timestamp;

update test set ts = l;


psql slave :
 # alter table test add ts timestamp;
 alter table test add ts integer;
 delete from test;
 delete from testoid;
 delete from teststr;
# dump the master:
pg_dump master > master_dump.sql
psql slave < master_dump.sql

createlang -U postgres -d master -L /usr/local/pgsql/lib plpgsql
# psql master < lastchg_remove.sql
psql master < lastchg_install.sql
on master:

# 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%';

========================================================================
   create function _rserv_test_ts_lcupdate() returns opaque as '
   begin
        NEW.ts = nextval(''_rserv_test_seq_'');
        return NEW;
   end;
   'language 'plpgsql';

   create trigger _rserv_test_ts_lctrig
   before update on test
   for each row execute procedure _rserv_test_ts_lcupdate() ;
=========================================================================

        select lastchg_addto('test', 'ts');
        execute  '' select lastchg_addto(''''test'''', ''''ts'''') '';
        # select lastchg_remove('test', 'ts');

psql master < master.sql
psql slave < slave.sql
MasterAddTable master test ts
SlaveAddTable slave test ts
on master:
        update test set k=101 where l=100;
select * from _RSERV_LOG_;
Replicate master slave;
select * from test where l=100;

update test set i=112 where l=100;
select * from test where l=100;
update test set i=102, k=102 where l=100;

insert into test (i, k, l, ts) values (111, 111, 111);
insert into test (i, k, l, ts) values (222, 222, 222);
select * from test where l=111;
select * from test where l=222;

delete from test  where l=111;

# check defaults:
select pgc.oid, pga.attname, pga.atthasdef from pg_class pgc, pg_attribute pga where pgc.relname = 'test' and pgc.oid = pga.attrelid;

select syncid, synctime, minid, maxid, active from _RSERV_SYNC_ where server = 0 and syncid = (select max(syncid) from _RSERV_SYNC_ where server = 0 and status > 0);
select l.reloid, l.key from _RSERV_LOG_ l where l.deleted = 1  order by l.reloid ;
select  _test.* from _RSERV_LOG_ l, test _test where l.reloid = 125858 and l.deleted = 0  and l.key = _test._rserv_ts::text;
select pgc.oid, pgc.relname, pga.attname, rt.key from _RSERV_SLAVE_TABLES_ rt, pg_class pgc, pg_attribute pga where pgc.oid = rt.reloid and pga.attrelid = rt.reloid and pga.attnum = rt.key ;

-------------------------------------------------------------------------------------------------


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%';


 SELECT tr.tgname AS triggername, tr.tgenabled, fn.proname
        AS func_name
        FROM pg_trigger AS tr, pg_proc AS fn
        WHERE tr.tgfoid=fn.oid
        and tr.tgname = '_rserv_trigger_t_';

 SELECT tr.tgname AS triggername, tr.tgfoid, tr.tgenabled
        FROM pg_trigger AS tr
        WHERE tr.tgname = '_rserv_trigger_t_';



=================================================
  HOW IT WORKS:
-------------------------------------------------

1) Set up master system tables:

   _RSERV_SERVERS_;

   _RSERV_TABLES_;

   _RSERV_LOG_;

   _RSERV_SYNC_;

2)  Set up slave tables:

    _RSERV_SLAVE_TABLES_;

    _RSERV_SLAVE_SYNC_;


3) Create triggers on master database that after  insert or update or delete on
   each table & column will insert/ update  _RSERV_LOG_ table.


4) Prepare snapshot of modified data:

   4.1 Read last succeeded sync
   4.2 Prepare snaphot of changed data including one w/o succeeded sync id

5) Apply Changes:

   5.1 Apply all the changes from previous unsuccessful attemps (w/o succeeded sync)
   5.2 Apply new changes
   5.3 Propagate success from slave to master



