#!/bin/sh # # Create logical slots with test_decoding while other sessions run DDL, and # count what goes wrong decoding from them. Uses a throwaway cluster in a temp # directory, and only initdb, pg_ctl, psql, pgbench and contrib/test_decoding. # # slot_under_ddl.sh [rounds] [port] [pgbench script] # # Exit status 1 means something went wrong in a round. Exit status 2 means the # DDL load stopped early, so the counts mean nothing. BIN=$1; ROUNDS=${2:-300}; PORT=${3:-57123}; SCRIPT=$4 D=$(mktemp -d "${TMPDIR:-/tmp}/slotddl.XXXXXX") || exit 3 "$BIN/initdb" -D "$D/data" -A trust -U postgres -E UTF8 --locale=C --no-sync >/dev/null || exit 3 cat >> "$D/data/postgresql.conf" </dev/null || exit 3 trap '[ -n "$LOAD" ] && { kill "$LOAD"; wait "$LOAD"; } 2>/dev/null; "$BIN/pg_ctl" -D "$D/data" -m immediate -w stop >/dev/null; rm -rf "$D"' EXIT # Every insert into t_N happens after the table has column b, so a decoded # insert without b was decoded with the catalog as it was before the ALTER. The # insert's transaction stays open long enough to overlap other clients' commits. cat > "$D/ddl.sql" <<'SQL' CREATE TABLE t_:client_id (id int, a text); ALTER TABLE t_:client_id ADD COLUMN b int; BEGIN; INSERT INTO t_:client_id VALUES (1, 'x', 2); \sleep 2 ms COMMIT; DROP TABLE t_:client_id; SQL [ -n "$SCRIPT" ] && cp "$SCRIPT" "$D/ddl.sql" "$BIN/pgbench" -n -h "$D" -p "$PORT" -U postgres -c 6 -T 86400 -f "$D/ddl.sql" postgres >/dev/null 2>&1 & LOAD=$! # Each round makes a slot, decodes from it twice and drops it. psql carries on # after an error. The decoding queries print the number of inserts without b. DECODE="SELECT count(*) FILTER (WHERE data LIKE 'table public.t%: INSERT:%' AND data NOT LIKE '% b[integer]:%') FROM pg_logical_slot_get_changes('s', NULL, NULL);" i=0 while [ "$i" -lt "$ROUNDS" ]; do printf '%s\n' "SELECT pg_create_logical_replication_slot('s', 'test_decoding') IS NULL;" \ "$DECODE" "SELECT pg_sleep(0.005) IS NULL;" "$DECODE" \ "SELECT pg_drop_replication_slot('s') IS NULL;" i=$((i + 1)) done | "$BIN/psql" -X -q -At -h "$D" -p "$PORT" -U postgres -d postgres >"$D/out" 2>"$D/err" "$BIN/postgres" --version kill -0 "$LOAD" 2>/dev/null || { echo "the DDL load stopped early"; exit 2; } SILENT=$(grep -E '^[0-9]+$' "$D/out" | awk '{ s += $1 } END { print s + 0 }') echo "$ROUNDS rounds, $SILENT inserts decoded without column b, errors:" sed -nE 's/^.*ERROR: *//p' "$D/err" | sed -E 's/[0-9]+/N/g' | sort | uniq -c | sort -rn [ "$SILENT" = 0 ] && ! grep -q ERROR "$D/err"