#!/usr/bin/env bash # Одновременная передача в независимых активных каналах DBMS_PIPE. set -euo pipefail PSQL="${PSQL:-psql}" PGHOST="${PGHOST:-/tmp}" PGPORT="${PGPORT:-5432}" PGUSER="${PGUSER:-postgres}" PGDATABASE="${PGDATABASE:-postgres}" channels="${PIPE_ACTIVE_CHANNELS:-4}" messages="${PIPE_ACTIVE_MESSAGES:-40}" if [[ ! "$channels" =~ ^[1-8]$ ]]; then echo "PIPE_ACTIVE_CHANNELS: допустимо 1..8" >&2 exit 2 fi if [[ ! "$messages" =~ ^[0-9]{1,3}$ ]] || ((10#$messages < 1 || 10#$messages > 200)); then echo "PIPE_ACTIVE_MESSAGES: допустимо 1..200" >&2 exit 2 fi messages=$((10#$messages)) run_id="$$" tmpdir="$(mktemp -d /tmp/dbms_pipe_active.XXXXXX)" pids=() sql() { "$PSQL" -XAtq -v ON_ERROR_STOP=1 \ -h "$PGHOST" -p "$PGPORT" -U "$PGUSER" -d "$PGDATABASE" "$@" } pipe_name() { printf 'DBMS_PIPE_ACTIVE_%s_%s' "$run_id" "$1" } cleanup() { local pid i trap - EXIT for pid in "${pids[@]}"; do kill "$pid" 2>/dev/null || true wait "$pid" 2>/dev/null || true done for ((i=0; i/dev/null 2>&1 || true done rm -rf -- "$tmpdir" } trap cleanup EXIT for ((i=0; i/dev/null sql -c "SELECT dbms_pipe_create_pipe('$pipe'::varchar2,65536,false,false,0)" >/dev/null done # Сначала запустить всех получателей и дождаться их реального wait-event. for ((i=0; i"$tmpdir/receiver_$i.log" 2>&1 & pids+=("$!") done ready=0 for ((attempt=0;attempt<200;attempt++)); do count="$(sql -c "SELECT count(*) FROM pg_stat_activity WHERE application_name LIKE 'pipe-active-$run_id-%' AND wait_event_type = 'Extension'")" if ((count == channels)); then ready=1 break fi sleep 0.02 done if ((ready != 1)); then echo "FAIL: только $count из $channels получателей находятся в ожидании" >&2 exit 1 fi # Один независимый producer на канал; все producers выполняются параллельно. for ((i=0; i"$tmpdir/sender_$i.log" 2>&1 & pids+=("$!") done for pid in "${pids[@]}"; do wait "$pid" done pids=() for ((i=0; i