Skip to content

Commit 8852bea

Browse files
author
Mohit Joshi
committed
Added new PG WAL tests
1 parent 722e61f commit 8852bea

File tree

3 files changed

+157
-2
lines changed

3 files changed

+157
-2
lines changed

postgresql/tests/import_vault_keys.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
INSTALL_DIR=$HOME/postgresql/pg_tde/bld_tde/install
3+
INSTALL_DIR=$HOME/postgresql/bld_tde/install
44
DATA_DIR=$INSTALL_DIR/data
55
EXPORT_DIR=vault_export
66
PORT=5432
@@ -89,6 +89,7 @@ start_server
8989
start_vault_server
9090

9191
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_add_database_key_provider_vault_v2('local_vault_provider', '$vault_url', '$secret_mount_point', '$filename', '$vault_ca')"
92+
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_create_key_using_database_key_provider('local_key','local_vault_provider')"
9293
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_set_key_using_database_key_provider('local_key','local_vault_provider')"
9394
$INSTALL_DIR/bin/psql -d postgres -c"CREATE TABLE t1(a INT) USING tde_heap"
9495
$INSTALL_DIR/bin/psql -d postgres -c"INSERT INTO t1 VALUES (100)"

postgresql/tests/import_vault_keys_cli_tool.sh

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
#!/bin/bash
22

3-
INSTALL_DIR=$HOME/postgresql/pg_tde/bld_tde/install
3+
INSTALL_DIR=$HOME/postgresql/bld_tde/install
44
DATA_DIR=$INSTALL_DIR/data
55
EXPORT_DIR=vault_export
66
PORT=5432
@@ -89,6 +89,7 @@ start_vault_server
8989

9090
$INSTALL_DIR/bin/psql -d postgres -c"CREATE EXTENSION pg_tde;"
9191
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_add_database_key_provider_vault_v2('local_vault_provider', '$vault_url', '$secret_mount_point', '$filename', '$vault_ca')"
92+
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_create_key_using_database_key_provider('local_key','local_vault_provider')"
9293
$INSTALL_DIR/bin/psql -d postgres -c"SELECT pg_tde_set_key_using_database_key_provider('local_key','local_vault_provider')"
9394
$INSTALL_DIR/bin/psql -d postgres -c"CREATE TABLE t1(a INT) USING tde_heap"
9495
$INSTALL_DIR/bin/psql -d postgres -c"INSERT INTO t1 VALUES (100)"
Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
1+
#!/bin/bash
2+
3+
INSTALL_DIR="$HOME/postgresql/bld_tde/install"
4+
DATA_DIR_BASE="$INSTALL_DIR/data_segsize"
5+
KEYFILE="/tmp/keyfile.per"
6+
PG_PORT=5432
7+
8+
WAL_SEG_SIZES=(1 16 64) # in MB
9+
10+
rm -f "$KEYFILE"
11+
12+
run_test() {
13+
local segsize_mb=$1
14+
local data_dir="${DATA_DIR_BASE}_${segsize_mb}MB"
15+
local replica_dir="${data_dir}_replica"
16+
local replica_port=$((PG_PORT + 1))
17+
18+
echo "============================="
19+
echo "Testing with --wal-segsize = ${segsize_mb}MB"
20+
echo "Primary: $data_dir"
21+
echo "Replica: $replica_dir"
22+
echo "============================="
23+
24+
pkill -9 postgres || true
25+
rm -rf "$data_dir" "$replica_dir"
26+
27+
"$INSTALL_DIR/bin/initdb" --wal-segsize="$segsize_mb" -D "$data_dir"
28+
29+
cat >> "$data_dir/postgresql.conf" <<EOF
30+
shared_preload_libraries = 'pg_tde'
31+
port = $PG_PORT
32+
logging_collector = on
33+
log_directory = 'log'
34+
log_filename = 'postgresql.log'
35+
wal_level = replica
36+
max_wal_senders = 5
37+
wal_keep_size = 64
38+
hot_standby = on
39+
EOF
40+
41+
"$INSTALL_DIR/bin/pg_ctl" -D "$data_dir" -l "$data_dir/server.log" start
42+
sleep 2
43+
44+
# Enable WAL encryption
45+
"$INSTALL_DIR/bin/psql" -p $PG_PORT -d postgres <<EOF
46+
CREATE EXTENSION pg_tde;
47+
SELECT pg_tde_add_global_key_provider_file('global_provider', '$KEYFILE');
48+
SELECT pg_tde_create_key_using_global_key_provider('server_key1', 'global_provider');
49+
SELECT pg_tde_set_key_using_global_key_provider('server_key1', 'global_provider');
50+
SELECT pg_tde_set_server_key_using_global_key_provider('server_key1', 'global_provider');
51+
ALTER SYSTEM SET pg_tde.wal_encrypt = 'ON';
52+
EOF
53+
54+
"$INSTALL_DIR/bin/pg_ctl" -D "$data_dir" restart
55+
sleep 2
56+
57+
# Setup replica
58+
echo "Setting up replica..."
59+
"$INSTALL_DIR/bin/pg_basebackup" -h 127.0.0.1 -p $PG_PORT -D "$replica_dir" -U $(whoami) --no-password --write-recovery-conf
60+
61+
cat >> "$replica_dir/postgresql.conf" <<EOF
62+
port = $replica_port
63+
hot_standby = on
64+
EOF
65+
66+
echo "Starting replica..."
67+
"$INSTALL_DIR/bin/pg_ctl" -D "$replica_dir" -l "$replica_dir/server.log" start
68+
sleep 2
69+
70+
echo "Replica status:"
71+
"$INSTALL_DIR/bin/psql" -p $replica_port -d postgres -c "SELECT pg_is_in_recovery();"
72+
73+
echo "Creating 100 tables..."
74+
sysbench /usr/share/sysbench/oltp_insert.lua \
75+
--db-driver=pgsql \
76+
--pgsql-db=postgres \
77+
--pgsql-user=$(whoami) \
78+
--pgsql-port=$PG_PORT \
79+
--pgsql-host=127.0.0.1 \
80+
--tables=100 \
81+
--table-size=1000 \
82+
--threads=5 \
83+
prepare
84+
85+
# Run sysbench workload and simulate crash-restart and failover
86+
for phase in {1..4}; do
87+
echo "============================"
88+
echo "Phase $phase: Workload Start"
89+
echo "============================"
90+
91+
if [[ $phase -eq 1 ]]; then
92+
echo "Running on primary (normal state)"
93+
WORKLOAD_PORT=$PG_PORT
94+
95+
elif [[ $phase -eq 2 ]]; then
96+
echo "Simulating failover: stopping primary and promoting replica..."
97+
sleep 5
98+
"$INSTALL_DIR/bin/pg_ctl" -D "$data_dir" stop
99+
sleep 2
100+
"$INSTALL_DIR/bin/pg_ctl" -D "$replica_dir" promote
101+
sleep 3
102+
echo "Replica promoted. Running on promoted replica."
103+
WORKLOAD_PORT=$replica_port
104+
105+
elif [[ $phase -eq 3 ]]; then
106+
echo "Workload continues on promoted replica"
107+
WORKLOAD_PORT=$replica_port
108+
109+
elif [[ $phase -eq 4 ]]; then
110+
echo "Restarting original primary (now demoted or stale)..."
111+
"$INSTALL_DIR/bin/pg_ctl" -D "$data_dir" -l "$data_dir/server.log" start
112+
sleep 5
113+
echo "Original primary restarted. Workload stays on promoted replica."
114+
WORKLOAD_PORT=$replica_port
115+
fi
116+
117+
# Run workload
118+
sysbench /usr/share/sysbench/oltp_read_write.lua \
119+
--db-driver=pgsql \
120+
--pgsql-db=postgres \
121+
--pgsql-user=$(whoami) \
122+
--pgsql-port=$WORKLOAD_PORT \
123+
--pgsql-host=127.0.0.1 \
124+
--threads=5 \
125+
--tables=100 \
126+
--time=30 \
127+
--report-interval=1 \
128+
--events=1870000000 \
129+
run
130+
131+
echo "============================"
132+
echo "Phase $phase: Workload End"
133+
echo "============================"
134+
135+
done
136+
137+
echo "Sample WAL file:"
138+
WAL_FILE=$(find "$data_dir/pg_wal" -type f | head -n 1)
139+
[[ -f "$WAL_FILE" ]] && hexdump -C "$WAL_FILE" | head -n 10 || echo "No WAL found."
140+
141+
"$INSTALL_DIR/bin/pg_ctl" -D "$data_dir" stop
142+
"$INSTALL_DIR/bin/pg_ctl" -D "$replica_dir" stop
143+
144+
echo "Test complete for --wal-segsize = ${segsize_mb}MB"
145+
echo
146+
}
147+
148+
for segsize in "${WAL_SEG_SIZES[@]}"; do
149+
run_test "$segsize"
150+
done
151+
152+
echo "All WAL segsize streaming replication + encryption crash tests complete."
153+

0 commit comments

Comments
 (0)