System Compleat.

Postgresql Slony-I replication

Techs
Postgresql  Replication 구현 및 테스트

1. 테스트 서버

    RH9.
    
    AMD 3800+ , 2G RAM
    Pgsql Version   : 8.2.3
    Slony-I Version : 1.2.8
    
    slony-master, slony-slave 두대 모두 동일한 사양, 동일한 원판.
    
    slony-master -  192.168.3.50/16
    slony-slave  -  192.168.3.175/16
    

2. 설치

    1) Postgresql
    
        configure:
            configure --prefix=/home/postgresql --enable-debug --enable-integer-datetimes --enable-cassert --enable-thread-safety --with-tcl --with-perl --with-pam --with-ldap --with-openssl --with-python
        
        make:    
            gmake && gmake install
        
        DB initialize:
            su - postgres -c '/home/postgresql/bin/initdb -D /home/postgresql/data -E EUC_KR'
                
        postgresql.conf :
        
            listen_addresses = '*'
            port = 9999
            max_connections = 500
            work_mem = 16MB
            maintenance_work_mem = 32MB
            max_fsm_pages = 2048000
            max_fsm_relations = 80000
            
            log_line_prefix = '%t %p %x %i'
            
            
        pg_hba.conf :
            
            # IPv4 local connections:
            host    all         all         192.168.0.0/16       trust
            
        pgsql 구동
        
        
        pgbench 설치 :
        
            cd $PGSQL_SRCDIR/contrib/pgbench ; gmake && gmake install
        
        
    2) Slony-I
    
        configure :
        
            configure --prefix=/home/postgresql/slony --enable-debug --with-gnu-ld --with-pgconfigdir=/home/postgresql/bin --with-pgbindir=/home/postgresql/bin --with-pglibdir=/home/postgresql/lib --with-perltools=/home/postgresql/slony/tools --with-perlsharedir=/home/postgresql/lib
            
        make :
        
            gmake && gmake install
            
        
        create directories :
        
            cd /home/postgresql/slony
            mkdir logs archive scripts check
            
        check scripts copy :
        
            cp -a $SRCDIR/tools/*.sh  $PREFIXDIR/scripts
            

            
3. Replication 구현

    1) test DB 생성
        
        psql -h slony-master -U postgres -d template1 -p 9999
        db=#CREATE ROLE pgbench LOGIN;
        db=#CREATE DATABASE pgbench OWNER pgbench;
        
    2) test table 생성 ( slony-master )
    
        pgbench -i -s 1000 -U pgbench -d pgbench -p 9999
        
    3) slony-slave 에 table 및 기타 해당 DB 동기화
    
        psql -h slony-slave -U postgres -d template1 -p 9999
        db=#CREATE ROLE pgbench LOGIN;
        db=#CREATE DATABASE pgbench OWNER pgbench;
        db=# \q
        
        pg_dump -h slony-master -U postgres -d pgbench -p 9999 -s > schema_nodata.sql ( DDL 생성정보 dump )
        psql -h slony-slave -U postgres -d template1 -p 9999 < schema_nodata.sql
        
    4) slony-i  설정.
    
        set_pgbench_replicate.sh
        
        -----------------------------------------------------------------------------------------------------------                
            #!/bin/sh
            # Set up environment vars for slony: pg replication
            
            
            SLONIK=/home/postgresql/slony/bin/slonik
            
            
            $SLONIK <<_EOF_
            
            
                    # Replication의 이름 지정.  해당 DB에 _pgbench 와 같이 생성됨
                    
                    cluster name = pgbench;   
                    
                    # Replication Node 생성
                    node 1 admin conninfo = 'dbname=pgbench host=slony-master port=9999 user=postgres';
                    node 2 admin conninfo = 'dbname=pgbench host=slony-slave port=9999 user=postgres';
            
            
                    # Replication Init
                    init cluster ( id=1, comment = 'pgbench MASTER Node' );
            
                    # Uniq Constraint 또는 Primary key 가 없는 테이블에 키 지정.
                    # 해당 테이블에 _Slony-I_$CLUSTERNAME_rowID  bigint 컬럼 생성.
                    # primary key 가 없는 테이블을 replication 지정할때 반드시 작업해 주어야함.
                    
                    table add key ( node id = 1, fully qualified name = 'public.history' );
                    
            
                    # Replication 될 Set 및 table 설정.
            
                    create set ( id=1, origin=1, comment='tables in pgbench');
                    set add table ( set id=1, origin=1, id=1, fully qualified name = 'public.accounts', comment='account');
                    set add table ( set id=1, origin=1, id=2, fully qualified name = 'public.branches', comment='branches');
                    set add table ( set id=1, origin=1, id=3, fully qualified name = 'public.history', comment='history');
                    set add table ( set id=1, origin=1, id=4, fully qualified name = 'public.tellers', comment='tellers');
            
            
                    # Table set Ends here.
                    
                    # Create the second node ( SLAVE ) tell the 2 nodes how to connect to each other.
            
                    store node ( id=2, comment = 'pgbench SLAVE Node');
                    store path ( server = 1, client = 2, conninfo='dbname=pgbench host=slony-master port=9999 user=postgres');
                    store path ( server = 2, client = 1, conninfo='dbname=pgbench host=slony-slave port=9999 user=postgres');
                    store listen ( origin=1, provider = 1, receiver = 2);
                    store listen ( origin=2, provider = 2, receiver = 1);
            
            _EOF_        
        
        -----------------------------------------------------------------------------------------------------------                
        
    5) slonik setting 및 확인.
    
        sh set_pgbench_replicate.sh   
        
        # err 발생 유무 확인  에러가 발생하였다면
        psql -h slony-master -U postgres -d pgbench -p 9999 -c "DROP SCHEMA _$CLUSTERNAME CASCADE"
        로 스키마를 DROP 후 스크립트를 수정하여 다시 반복한다.
        
        err가 없었다면 master 및 slave DB 모두에 _$CLUSTERNAME 과 같은 스키마가 생성되었는지 확인한다.
        
        
    6) slon daemon 구동
        
        slony-master:
        /home/postgresql/slony/bin/slon pgbench "dbname=pgbench user=postgres host=slony-master port=9999" -a /home/postgresql/slony/archive -x /home/postgresql/slony/logs -d 2 2&>1 &
        
        slony-slaver:
        /home/postgresql/slony/bin/slon pgbench "dbname=pgbench user=postgres host=slony-slave port=9999" -a /home/postgresql/slony/archive -x /home/postgresql/slony/logs -d 2 2&>1 &
        
        /home/postgresql/slony/archive 에  slony 로 replication 된 sql 파일을 로그로 쌓는다.
        실제 서비스에서는 I/O 감소를 위해 hdc 또는 sdb 와 같은 백업 디스크로 경로를 바꿔 주어야 한다
        
        
        
    7) subscribe replication.
    
        start_pgbench_replication.sh 의 스크립트 제작.
        
        -----------------------------------------------------------------------------------------------------------                
            #!/bin/sh
            
            /home/postgresql/slony/bin/slonik <<_EOF_
            
                    cluster name = pgbench;
            
                    node 1 admin conninfo = 'dbname=pgbench host=slony-master port=9999 user=postgres';
                    node 2 admin conninfo = 'dbname=pgbench host=slony-slave port=9999 user=postgres';
            
                    subscribe set ( id = 1, provider = 1, receiver =2 , forward = no );
            
            _EOF_
        -----------------------------------------------------------------------------------------------------------                
        
    8)  vmstat 및 iptraf 등으로 각 테이블들이 master -> slave 로 sync 되고있는지 확인한다.
    
    
    
4. 관리

    /home/postgresql/slony/tools 에 보면 replication 관리에 필요한 스크립트들이 있다.
    
    .../slony/etc/slon_tools.conf 파일이 있는지 확인하고 없다면 slony의 소스 압축을 푼 디렉토리에서 찾아 해당 위치에 복사해 준다.
    
    
    slon_tools.conf 파일 수정.
    
        $CLUSTER_NAME = 'pgbench';
        $LOGDIR = '/home/postgresql/slony/logs/slony';
        $MASTERNODE = 1;
        
            add_node(node     => 1,
                      host     => 'slony-master',
                      dbname   => 'pgbench',
                      port     => 9999,
                      user     => 'postgres',
                      password => '');
        
    
        $SLONYSET 설정.
        
            "set_id" => 1,
            
            "table_id"    => 1,
            "sequence_id" => 1,
            
            # Primary key가 지정되어있는 테이블 리스트설정.
            "pkeyedtables" => [
                                   'TABLE1',
                                   'table2',
                                 ],
                                 
            # Primary key 는 없으나 Uniq not null 키를 가지고 있는 테이블 및 인덱스 지정.
            "keyedtables" => {
                                'table3' => 'index_on_table3',
                                'table4' => 'index_on_table4',
                            },
                            
                            
            # Primary Key 또는 uniq 키 모두 없는 테이블지정.
            # 지정하지 않고 키를 생성해도 된다.
            
            "serialtables" => ["table5"],
            
            # Replication 되어야할 sequence 지정
            
            "sequences" => ['sequence1',
                            'sequence2',
                           ],
                           
                           
                         
     
    사용 방법
    
        /home/postgresql/slony/tools 의 스크립트를 이용
        
        일부 스크립트는 직접 수행되는 것도 있고 slonik 용 스크립트를 생성해 주는 것도 있다.
        
        직접 수행되는것들로는
        
            slon_*
            
            
        slonik 용 스크립트 생성 툴은
        
            slonik_*  
            
        로 보면 된다.
        
        
        Usage는 잘 설명 되어있으나  --config-file 은 스크립트 수행시 넣어주지 않아도 된다.
        
            ex)  ./slonik_drop_node 1 > /home/postgresql/slony/scripts/drop_node1.slonik
            
                 /home/postgresql/slony/bin/slonik < /home/postgresql/slony/scripts/drop_node1.slonik
                
                
        
        replication 장애시.        
         ./slonik_failover 1 2 > /home/postgresql/slony/scripts/fail_over_masternode_to_2.slonik
        
         /home/postgresql/slony/bin/slonik < /home/postgresql/slony/scripts/fail_over_masternode_to_2.slonik