|
| 1 | +#!/bin/bash |
| 2 | + |
| 3 | +# Set variable |
| 4 | +INSTALL_DIR=/home/mohit.joshi/postgresql/bld_tde/install |
| 5 | +PRIMARY_DATA=$INSTALL_DIR/primary_data |
| 6 | +REPLICA_DATA=$INSTALL_DIR/replica_data |
| 7 | +PRIMARY_LOGFILE=$PRIMARY_DATA/server.log |
| 8 | +REPLICA_LOGFILE=$REPLICA_DATA/server.log |
| 9 | +TABLES=1000 |
| 10 | + |
| 11 | +# initate the database |
| 12 | +initialize_server() { |
| 13 | + PG_PIDS=$(lsof -ti :5433 -ti :5434 2>/dev/null) || true |
| 14 | + if [[ -n "$PG_PIDS" ]]; then |
| 15 | + echo "Killing PostgreSQL processes: $PG_PIDS" |
| 16 | + kill -9 $PG_PIDS |
| 17 | + fi |
| 18 | + rm -rf $PRIMARY_DATA $REPLICA_DATA |
| 19 | + $INSTALL_DIR/bin/initdb -D $PRIMARY_DATA > /dev/null 2>&1 |
| 20 | + cat > "$PRIMARY_DATA/postgresql.conf" <<SQL |
| 21 | +port=5433 |
| 22 | +listen_addresses='*' |
| 23 | +shared_preload_libraries = 'pg_tde' |
| 24 | +logging_collector = on |
| 25 | +log_directory = '$PRIMARY_DATA' |
| 26 | +log_filename = 'server.log' |
| 27 | +log_statement = 'all' |
| 28 | +wal_level = 'logical' |
| 29 | +default_table_access_method = 'tde_heap' |
| 30 | +SQL |
| 31 | + |
| 32 | + cat >> "$PRIMARY_DATA/pg_hba.conf" <<SQL |
| 33 | +# Allow replication connections |
| 34 | +host replication repuser 127.0.0.1/32 trust |
| 35 | +SQL |
| 36 | +} |
| 37 | + |
| 38 | +start_primary() { |
| 39 | + $INSTALL_DIR/bin/pg_ctl -D $PRIMARY_DATA start -l $PRIMARY_LOGFILE > $PRIMARY_LOGFILE 2>&1 |
| 40 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"CREATE USER repuser replication;" |
| 41 | +} |
| 42 | + |
| 43 | +stop_server() { |
| 44 | + datadir=$1 |
| 45 | + $INSTALL_DIR/bin/pg_ctl -D $datadir stop |
| 46 | +} |
| 47 | + |
| 48 | +start_server() { |
| 49 | + datadir=$1 |
| 50 | + $INSTALL_DIR/bin/pg_ctl -D $datadir start |
| 51 | +} |
| 52 | + |
| 53 | +start_replica() { |
| 54 | + $INSTALL_DIR/bin/pg_basebackup -h localhost -U repuser --checkpoint=fast -D $REPLICA_DATA -R --slot=somename -C --port=5433 |
| 55 | + sleep 5 |
| 56 | + cat >> "$REPLICA_DATA/postgresql.conf" <<SQL |
| 57 | +port=5434 |
| 58 | +logging_collector = on |
| 59 | +log_directory = '$REPLICA_DATA' |
| 60 | +log_filename = 'server.log' |
| 61 | +log_statement = 'all' |
| 62 | +SQL |
| 63 | + $INSTALL_DIR/bin/pg_ctl -D $REPLICA_DATA -l $REPLICA_LOGFILE start |
| 64 | + |
| 65 | +} |
| 66 | + |
| 67 | +enable_tde_and_create_load() { |
| 68 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"CREATE EXTENSION pg_tde;" |
| 69 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_add_database_key_provider_file('local_key_provider','$PRIMARY_DATA/keyring.file');" |
| 70 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_create_key_using_database_key_provider('local_key','local_key_provider');" |
| 71 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_set_key_using_database_key_provider('local_key','local_key_provider');" |
| 72 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_add_global_key_provider_file('global_key_provider','$PRIMARY_DATA/keyring.file');" |
| 73 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_create_key_using_global_key_provider('global_key','global_key_provider');" |
| 74 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"SELECT pg_tde_set_server_key_using_global_key_provider('global_key','global_key_provider');" |
| 75 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c"ALTER SYSTEM SET pg_tde.wal_encrypt=ON" |
| 76 | + |
| 77 | + stop_server $PRIMARY_DATA |
| 78 | + start_server $PRIMARY_DATA |
| 79 | + |
| 80 | + echo "Create some tables on Primary Node" |
| 81 | + sysbench /usr/share/sysbench/oltp_insert.lua --pgsql-user=`whoami` --pgsql-db=postgres --db-driver=pgsql --pgsql-port=5433 --threads=5 --tables=$TABLES --table-size=1000 prepare |
| 82 | +} |
| 83 | + |
| 84 | +rotate_server_key() { |
| 85 | + echo "Rotating server keys for 30 seconds..." |
| 86 | + |
| 87 | + local start_time=$(date +%s) |
| 88 | + local duration=30 |
| 89 | + |
| 90 | + while [[ $(($(date +%s) - start_time)) -lt $duration ]]; do |
| 91 | + RAND=$RANDOM |
| 92 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c "SELECT pg_tde_create_key_using_global_key_provider('global_key$RAND','global_key_provider');" >/dev/null |
| 93 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c "SELECT pg_tde_set_server_key_using_global_key_provider('global_key$RAND','global_key_provider');" >/dev/null |
| 94 | + sleep 2 # optional: adjust to control key rotation rate |
| 95 | + done |
| 96 | +} |
| 97 | + |
| 98 | +verify_streaming_replication() { |
| 99 | + echo "Creating verification table on primary..." |
| 100 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c "CREATE TABLE verify_replication(id INT PRIMARY KEY, val TEXT);" || exit 1 |
| 101 | + $INSTALL_DIR/bin/psql -d postgres -p 5433 -c "INSERT INTO verify_replication VALUES (1, 'streaming_test');" || exit 1 |
| 102 | + |
| 103 | + echo "Waiting for replication to apply..." |
| 104 | + sleep 5 |
| 105 | + |
| 106 | + echo "Checking data on replica..." |
| 107 | + result=$($INSTALL_DIR/bin/psql -d postgres -p 5434 -Atc "SELECT val FROM verify_replication WHERE id=1;" 2>/dev/null) |
| 108 | + |
| 109 | + if [[ "$result" == "streaming_test" ]]; then |
| 110 | + echo "Streaming replication is working correctly" |
| 111 | + else |
| 112 | + echo "Streaming replication failed or is delayed" |
| 113 | + exit 1 |
| 114 | + fi |
| 115 | +} |
| 116 | + |
| 117 | +verify_logical_replication() { |
| 118 | + echo "Verifying logical replication of sysbench tables..." |
| 119 | + |
| 120 | + local retries=5 |
| 121 | + local delay=5 |
| 122 | + local all_tables_replicated=true |
| 123 | + |
| 124 | + for ((i=1; i<=TABLES; i++)); do |
| 125 | + table_name="sbtest$i" |
| 126 | + echo "Checking table: $table_name" |
| 127 | + |
| 128 | + attempt=1 |
| 129 | + while (( attempt <= retries )); do |
| 130 | + primary_count=$($INSTALL_DIR/bin/psql -d postgres -p 5433 -Atc "SELECT count(*) FROM $table_name;" 2>/dev/null) |
| 131 | + replica_count=$($INSTALL_DIR/bin/psql -d postgres -p 5434 -Atc "SELECT count(*) FROM $table_name;" 2>/dev/null) |
| 132 | + |
| 133 | + if [[ "$primary_count" == "$replica_count" ]]; then |
| 134 | + echo "Table $table_name replicated successfully with $replica_count rows" |
| 135 | + break |
| 136 | + else |
| 137 | + echo "Mismatch (Attempt $attempt): Primary=$primary_count, Replica=$replica_count" |
| 138 | + ((attempt++)) |
| 139 | + sleep $delay |
| 140 | + fi |
| 141 | + done |
| 142 | + |
| 143 | + if (( attempt > retries )); then |
| 144 | + echo "Table $table_name replication failed after $retries attempts" |
| 145 | + all_tables_replicated=false |
| 146 | + fi |
| 147 | + done |
| 148 | + |
| 149 | + if $all_tables_replicated; then |
| 150 | + echo "All sysbench tables successfully replicated via logical replication" |
| 151 | + else |
| 152 | + echo "One or more sysbench tables failed to replicate correctly" |
| 153 | + exit 1 |
| 154 | + fi |
| 155 | +} |
| 156 | + |
| 157 | +run_workload_during_conversion() { |
| 158 | + echo "Running workload on primary during pg_createsubscriber execution..." |
| 159 | + |
| 160 | + sysbench /usr/share/sysbench/oltp_write_only.lua \ |
| 161 | + --pgsql-user=$(whoami) \ |
| 162 | + --pgsql-db=postgres \ |
| 163 | + --db-driver=pgsql \ |
| 164 | + --pgsql-port=5433 \ |
| 165 | + --threads=10 \ |
| 166 | + --tables=$TABLES \ |
| 167 | + --table-size=1000 \ |
| 168 | + --time=30 \ |
| 169 | + run > /tmp/workload.log 2>&1 & |
| 170 | + WORKLOAD_PID=$! |
| 171 | +} |
| 172 | + |
| 173 | +run_pg_createsubscriber() { |
| 174 | + echo "Running pg_createsubscriber..." |
| 175 | + |
| 176 | + stop_server $REPLICA_DATA |
| 177 | + |
| 178 | + $INSTALL_DIR/bin/pg_createsubscriber \ |
| 179 | + -d postgres \ |
| 180 | + -D $REPLICA_DATA \ |
| 181 | + --subscriber-port=5434 \ |
| 182 | + --subscriber-username=$(whoami) \ |
| 183 | + --publisher-server="host=127.0.0.1 port=5433 dbname=postgres user=$(whoami)" \ |
| 184 | + --publication=mypub \ |
| 185 | + --subscription=mysub \ |
| 186 | + --verbose |
| 187 | + |
| 188 | + start_server $REPLICA_DATA |
| 189 | +} |
| 190 | + |
| 191 | +# Actual test starts here... |
| 192 | + |
| 193 | +echo "1=>Create Data Directory" |
| 194 | +initialize_server |
| 195 | + |
| 196 | +echo "2=>Start Primary Server" |
| 197 | +start_primary |
| 198 | + |
| 199 | +echo "3=>Start Replica Server" |
| 200 | +start_replica |
| 201 | + |
| 202 | +echo "4=>Enable pg_tde on Primary Server" |
| 203 | +enable_tde_and_create_load |
| 204 | + |
| 205 | +echo "5=>Verifying Streaming Replication" |
| 206 | +verify_streaming_replication |
| 207 | + |
| 208 | +echo "6=>Run workload in parallel on Primary Server" |
| 209 | +rotate_server_key & |
| 210 | +run_workload_during_conversion |
| 211 | + |
| 212 | +echo "7=>Convert physical replica into logical replica" |
| 213 | +run_pg_createsubscriber |
| 214 | + |
| 215 | +#echo "8=>Verifying Logical Replication" |
| 216 | +#verify_logical_replication |
0 commit comments