Skip to content

Commit a889eb1

Browse files
author
Mohit Joshi
committed
Added pg_rewind tests
1 parent fa38a1b commit a889eb1

File tree

7 files changed

+678
-4
lines changed

7 files changed

+678
-4
lines changed

postgresql/bugs/PG-1658/PG-1658.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
#!/bin/bash
22

3-
INSTALL_DIR=$HOME/postgresql/pg_tde/bld_tde/install
3+
INSTALL_DIR=$HOME/postgresql/bld_tde/install
44
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
55
data_dir=$INSTALL_DIR/data
66
source "$(dirname "${BASH_SOURCE[0]}")/helper_scripts/initialize_server.sh"
7+
source "$(dirname "${BASH_SOURCE[0]}")/helper_scripts/start_server.sh"
78
rm /tmp/key_holder.pem
89

910
start_server() {
@@ -27,7 +28,8 @@ echo "..Add a Global key provider"
2728
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_add_global_key_provider_file('file_provider','/tmp/key_holder.pem');"
2829

2930
echo "..Create a Global Default Principal key"
30-
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_set_default_key_using_global_key_provider('key1','file_provider','true');"
31+
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_create_key_using_global_key_provider('key1','file_provider');"
32+
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_set_default_key_using_global_key_provider('key1','file_provider');"
3133

3234
echo "..Create encrypted table"
3335
$INSTALL_DIR/bin/psql -d postgres -c"CREATE TABLE t1(a INT) USING tde_heap;"

postgresql/bugs/PG-1727/PG-1727.sh

Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
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

postgresql/bugs/PG-1811/PG-1811.sh

Lines changed: 144 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,144 @@
1+
#!/bin/bash
2+
3+
# Setup paths and variables
4+
INSTALL_DIR=$HOME/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+
PG_CTL="$INSTALL_DIR/bin/pg_ctl"
10+
PG_REWIND="$INSTALL_DIR/bin/pg_rewind"
11+
PG_BASEBACKUP="$INSTALL_DIR/bin/pg_basebackup"
12+
PSQL="$INSTALL_DIR/bin/psql"
13+
SYSBENCH="/usr/bin/sysbench"
14+
15+
PORT_PRIMARY=5433
16+
PORT_REPLICA=5434
17+
REPL_USER=repl
18+
REPL_PASS=replica
19+
DB_NAME=postgres
20+
DB_USER=$(whoami)
21+
22+
# Clean slate
23+
pkill -9 postgres
24+
rm -rf "$PRIMARY_DATA" "$REPLICA_DATA"
25+
rm -rf /tmp/primary_keyfile
26+
27+
# Step 1: Init primary
28+
$INSTALL_DIR/bin/initdb -D "$PRIMARY_DATA"
29+
30+
# Configure primary
31+
cat >> "$PRIMARY_DATA/postgresql.conf" <<EOF
32+
shared_preload_libraries = 'pg_tde'
33+
default_table_access_method = 'tde_heap'
34+
port = $PORT_PRIMARY
35+
wal_level = replica
36+
wal_log_hints = on
37+
wal_keep_size = 1024MB
38+
max_replication_slots = 2
39+
max_wal_senders = 10
40+
listen_addresses = 'localhost'
41+
logging_collector = on
42+
log_directory = 'log'
43+
log_filename = 'primary.log'
44+
EOF
45+
46+
echo "host replication $REPL_USER 127.0.0.1/32 trust" >> "$PRIMARY_DATA/pg_hba.conf"
47+
echo "host all all 127.0.0.1/32 trust" >> "$PRIMARY_DATA/pg_hba.conf"
48+
49+
echo "=> Step 1: Start primary"
50+
echo "#########################"
51+
$PG_CTL -D "$PRIMARY_DATA" -o "-p $PORT_PRIMARY" -l "$PRIMARY_LOGFILE" start
52+
sleep 3
53+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "CREATE EXTENSION pg_tde;"
54+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "SELECT pg_tde_add_global_key_provider_file('file_provider','/tmp/primary_keyfile');"
55+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "SELECT pg_tde_create_key_using_global_key_provider('key1','file_provider');"
56+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "SELECT pg_tde_set_server_key_using_global_key_provider('key1','file_provider');"
57+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "SELECT pg_tde_set_key_using_global_key_provider('key1','file_provider');"
58+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "ALTER SYSTEM SET pg_tde.wal_encrypt=ON;"
59+
# Restart primary
60+
$PG_CTL -D "$PRIMARY_DATA" -o "-p $PORT_PRIMARY" -l "$PRIMARY_LOGFILE" restart
61+
sleep 3
62+
63+
# Create replication user
64+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "CREATE ROLE $REPL_USER WITH LOGIN REPLICATION SUPERUSER PASSWORD '$REPL_PASS';"
65+
66+
echo "=> Step 2: Take base backup for replica"
67+
echo "#######################################"
68+
$PG_BASEBACKUP -D "$REPLICA_DATA" -X stream -R -h localhost -p $PORT_PRIMARY -U $REPL_USER
69+
70+
# Configure replica
71+
cat > "$REPLICA_DATA/postgresql.conf" <<EOF
72+
shared_preload_libraries = 'pg_tde'
73+
default_table_access_method = 'tde_heap'
74+
port = $PORT_REPLICA
75+
hot_standby = on
76+
logging_collector = on
77+
log_directory = 'log'
78+
log_filename = 'replica.log'
79+
wal_keep_size=1024MB
80+
max_wal_senders = 10
81+
EOF
82+
83+
echo "=>Step 3: Start replica"
84+
echo "########################"
85+
$PG_CTL -D "$REPLICA_DATA" -o "-p $PORT_REPLICA" -l "$REPLICA_LOGFILE" start
86+
sleep 5
87+
$PSQL -p $PORT_REPLICA -d $DB_NAME -c "ALTER SYSTEM SET pg_tde.wal_encrypt=ON;"
88+
# Restart replica
89+
$PG_CTL -D "$REPLICA_DATA" -o "-p $PORT_REPLICA" -l "$REPLICA_LOGFILE" restart
90+
sleep 5
91+
92+
echo "=>Step 3: Stop primary (simulate crash)"
93+
echo "#######################################"
94+
echo "Simulating primary crash..."
95+
$PG_CTL -D "$PRIMARY_DATA" -m immediate stop
96+
97+
echo "=> Step 4: Promote replica"
98+
echo "##########################"
99+
rm -f $REPLICA_DATA/postgresql.auto.conf
100+
$PG_CTL -D "$REPLICA_DATA" promote
101+
sleep 5
102+
103+
echo "=>Step 6: Use sysbench to generate WAL on promoted replica"
104+
echo "##########################################################"
105+
echo "Generating WAL with sysbench..."
106+
$SYSBENCH --db-driver=pgsql --pgsql-host=127.0.0.1 --pgsql-port=$PORT_REPLICA \
107+
--pgsql-user=$DB_USER --pgsql-db=$DB_NAME \
108+
--threads=10 --tables=1000 --table-size=1000 \
109+
/usr/share/sysbench/oltp_write_only.lua prepare
110+
111+
$SYSBENCH --db-driver=pgsql --pgsql-host=127.0.0.1 --pgsql-port=$PORT_REPLICA \
112+
--pgsql-user=$DB_USER --pgsql-db=$DB_NAME \
113+
--threads=10 --tables=1000 --table-size=1000 --time=300 --report-interval=5 \
114+
/usr/share/sysbench/oltp_write_only.lua run
115+
116+
# Backing up the Original config file
117+
cp $PRIMARY_DATA/postgresql.conf /tmp/postgresql_bk.conf
118+
echo "=> Step 7: Rewind old primary"
119+
echo "############################"
120+
echo "Rewinding old primary..."
121+
$PG_REWIND --target-pgdata="$PRIMARY_DATA" \
122+
--source-server="host=localhost port=$PORT_REPLICA user=$REPL_USER dbname=$DB_NAME"
123+
124+
# Restoring the Original config file
125+
mv /tmp/postgresql_bk.conf $PRIMARY_DATA/postgresql.conf
126+
127+
echo "=> Step 8: Configure old primary as standby"
128+
echo "###########################################"
129+
touch $PRIMARY_DATA/standby.signal
130+
cat >> "$PRIMARY_DATA/postgresql.conf" <<EOF
131+
primary_conninfo = 'host=localhost port=$PORT_REPLICA user=$REPL_USER password=$REPL_PASS'
132+
EOF
133+
134+
echo "=> Step 9: Start rewound primary"
135+
echo "################################"
136+
$PG_CTL -D "$PRIMARY_DATA" -o "-p $PORT_PRIMARY" -l "$PRIMARY_LOGFILE" start
137+
sleep 5
138+
139+
# Done
140+
echo -e "\n✅ Old primary successfully rewound and rejoined as standby."
141+
$PSQL -p $PORT_PRIMARY -d $DB_NAME -c "SELECT * FROM pg_stat_wal_receiver;"
142+
143+
# Tail logs
144+
tail -n 10 "$PRIMARY_DATA/log/primary.log"

0 commit comments

Comments
 (0)