@@ -11,13 +11,36 @@ export PGPORT=5432
1111export PGPASSWORD=pgdog
1212
1313cleanup () {
14+ if [ -n " ${BENCH_PID} " ]; then
15+ kill ${BENCH_PID} 2> /dev/null || true
16+ wait ${BENCH_PID} 2> /dev/null || true
17+ fi
1418 if [ -n " ${REPL_PID} " ]; then
1519 kill ${REPL_PID} 2> /dev/null || true
1620 wait ${REPL_PID} 2> /dev/null || true
1721 fi
1822}
1923trap cleanup EXIT
2024
25+ start_pgbench () {
26+ (
27+
28+ pgbench -h 127.0.0.1 -p 5432 -U pgdog pgdog \
29+ -t 100000000 -c 3 --protocol extended \
30+ -f " ${SCRIPT_DIR} /pgbench.sql" -P 1
31+
32+ ) &
33+ BENCH_PID=$!
34+ }
35+
36+ stop_pgbench () {
37+ if [ -n " ${BENCH_PID} " ]; then
38+ kill ${BENCH_PID} 2> /dev/null || true
39+ wait ${BENCH_PID} 2> /dev/null || true
40+ BENCH_PID=" "
41+ fi
42+ }
43+
2144pushd ${SCRIPT_DIR}
2245
2346psql -f init.sql
@@ -26,42 +49,46 @@ psql -f init.sql
2649# 0 -> 2
2750#
2851${PGDOG_BIN} schema-sync --from-database source --to-database destination --publication pgdog
29- ${PGDOG_BIN} data-sync --sync-only --from-database source --to-database destination --publication pgdog --replication-slot copy_data
30- ${PGDOG_BIN} schema-sync --from-database source --to-database destination --publication pgdog --cutover
52+ start_pgbench
53+ ${PGDOG_BIN} data-sync --from-database source --to-database destination --publication pgdog &
54+ REPL_PID=$!
3155
32- # Check row counts: source (pgdog) vs destination (pgdog1 + pgdog2)
33- echo " Checking row counts: source -> destination..."
34- SHARDED_TABLES=" copy_data.users copy_data.orders copy_data.order_items copy_data.log_actions copy_data.with_identity"
35- OMNI_TABLES=" copy_data.countries copy_data.currencies copy_data.categories"
56+ # Give replication a moment to connect.
57+ sleep 2
3658
37- for TABLE in ${SHARDED_TABLES} ; do
38- SRC=$( psql -d pgdog -tAc " SELECT COUNT(*) FROM ${TABLE} " )
39- DST1=$( psql -d pgdog1 -tAc " SELECT COUNT(*) FROM ${TABLE} " )
40- DST2=$( psql -d pgdog2 -tAc " SELECT COUNT(*) FROM ${TABLE} " )
41- DST=$(( DST1 + DST2 ))
42- if [ " ${SRC} " -ne " ${DST} " ]; then
43- echo " MISMATCH ${TABLE} : source=${SRC} destination=${DST} (shard0=${DST1} shard1=${DST2} )"
44- exit 1
45- fi
46- echo " OK ${TABLE} : ${SRC} rows"
47- done
59+ # Check that the replication process is still alive.
60+ if ! kill -0 ${REPL_PID} 2> /dev/null; then
61+ echo " ERROR: replication process exited early"
62+ wait ${REPL_PID}
63+ exit $?
64+ fi
4865
49- for TABLE in ${OMNI_TABLES} ; do
50- SRC=$( psql -d pgdog -tAc " SELECT COUNT(*) FROM ${TABLE} " )
51- DST1=$( psql -d pgdog1 -tAc " SELECT COUNT(*) FROM ${TABLE} " )
52- DST2=$( psql -d pgdog2 -tAc " SELECT COUNT(*) FROM ${TABLE} " )
53- if [ " ${SRC} " -ne " ${DST1} " ] || [ " ${SRC} " -ne " ${DST2} " ]; then
54- echo " MISMATCH ${TABLE} : source=${SRC} shard0=${DST1} shard1=${DST2} (expected ${SRC} on each shard)"
55- exit 1
56- fi
57- echo " OK ${TABLE} : ${SRC} rows on each shard"
58- done
66+ # Let data sync and replication catch up
67+ echo " Letting replication run for 20 seconds..."
68+ sleep 20
69+
70+ # Stop replication and capture its exit code.
71+ kill ${REPL_PID} 2> /dev/null || true
72+ set +e
73+ wait ${REPL_PID}
74+ REPL_EXIT=$?
75+ set -e
76+ REPL_PID=" "
77+
78+ # 0, 130 (SIGINT), 143 (SIGTERM) are all normal shutdown codes.
79+ if [ ${REPL_EXIT} -ne 0 ] && [ ${REPL_EXIT} -ne 130 ] && [ ${REPL_EXIT} -ne 143 ]; then
80+ echo " ERROR: replication process exited with code ${REPL_EXIT} "
81+ exit ${REPL_EXIT}
82+ fi
83+
84+ stop_pgbench
85+ ${PGDOG_BIN} schema-sync --from-database source --to-database destination --publication pgdog --cutover
5986
6087#
61- # 2 -> 2
88+ # 2 -- > 2
6289#
6390${PGDOG_BIN} schema-sync --from-database destination --to-database destination2 --publication pgdog
64- ${PGDOG_BIN} data-sync --sync-only --from-database destination --to-database destination2 --publication pgdog --replication-slot copy_data
91+ ${PGDOG_BIN} data-sync --sync-only --from-database destination --to-database destination2 --publication pgdog --replication-slot copy_data_2
6592${PGDOG_BIN} schema-sync --from-database destination --to-database destination2 --publication pgdog --cutover
6693
6794# Check row counts: destination (pgdog1 + pgdog2) vs destination2 (shard_0 + shard_1)
@@ -91,43 +118,6 @@ for TABLE in ${OMNI_TABLES}; do
91118 echo " OK ${TABLE} : ${SRC} rows on each shard"
92119done
93120
94- # Start replication in the background.
95- ${PGDOG_BIN} data-sync --replicate-only --from-database source --to-database destination --publication pgdog &
96- REPL_PID=$!
97-
98- # Give replication a moment to connect.
99- sleep 2
100-
101- # Check that the replication process is still alive.
102- if ! kill -0 ${REPL_PID} 2> /dev/null; then
103- echo " ERROR: replication process exited early"
104- wait ${REPL_PID}
105- exit $?
106- fi
107-
108- # Run pgbench against the source database — writes land on the source and
109- # get replicated to the destination shards via logical replication.
110- pgbench -h 127.0.0.1 -p 5432 -U pgdog pgdog \
111- -t 1000 -c 3 --protocol extended \
112- -f " ${SCRIPT_DIR} /pgbench.sql" -P 1
113-
114- # Let replication catch up.
115- sleep 3
116-
117- # Stop replication and capture its exit code.
118- kill ${REPL_PID} 2> /dev/null || true
119- set +e
120- wait ${REPL_PID}
121- REPL_EXIT=$?
122- set -e
123- REPL_PID=" "
124-
125- # 0, 130 (SIGINT), 143 (SIGTERM) are all normal shutdown codes.
126- if [ ${REPL_EXIT} -ne 0 ] && [ ${REPL_EXIT} -ne 130 ] && [ ${REPL_EXIT} -ne 143 ]; then
127- echo " ERROR: replication process exited with code ${REPL_EXIT} "
128- exit ${REPL_EXIT}
129- fi
130-
131121psql -f init.sql
132122
133123popd
0 commit comments