#!/bin/bash

pub_port=7654
sub_port=7655

pg_ctl -D data_pub stop -m immediate
pg_ctl -D data_sub stop -m immediate
rm -rf data_pub data_sub  sub.log pub.log

initdb -D data_pub -U postgres
initdb -D data_sub -U postgres

cat << EOF >> data_pub/postgresql.conf
wal_level = logical
autovacuum = off
port = $pub_port
logical_decoding_work_mem = 256MB
log_line_prefix = '%m [%p] %b '
log_statement = all
wal_sender_timeout = 0
wal_receiver_timeout = 0
EOF

cat << EOF >> data_sub/postgresql.conf
wal_level = logical
port = $sub_port
log_min_messages = debug1
log_line_prefix = '%m [%p] %b '
log_statement = all
wal_sender_timeout = 0
wal_receiver_timeout = 0
EOF

pg_ctl -D data_pub start -l pub.log
pg_ctl -D data_sub start -l sub.log


psql -d postgres -Upostgres -p $pub_port -c "create table tab(id UUID PRIMARY KEY, data JSONB);"
psql -d postgres -Upostgres -p $pub_port -c "create publication pub for table tab;"

sleep 2

psql -d postgres -Upostgres -p $sub_port -c "create table tab(id UUID PRIMARY KEY, data JSONB);"
psql -d postgres -Upostgres -p $sub_port -c "CREATE SUBSCRIPTION sub CONNECTION 'port=$pub_port dbname=postgres user=postgres' PUBLICATION pub"


sleep 2

psql -d postgres -Upostgres -p $pub_port -c "INSERT INTO tab SELECT gen_random_uuid(), '{\"key1\":\"values1\"}'::jsonb FROM generate_series(1, 1000000) s(i);"


exit 0

<< COMMENTS
# Updating all rows serially like below does not reproduce the problem:

psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$' &
COMMENTS

#<< COMMENTS
# Updating all rows in parallel like below reproduces the problem:

psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$00000000-0000-0000-0000-000000000000$$ AND id < $$10000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$10000000-0000-0000-0000-000000000000$$ AND id < $$20000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$20000000-0000-0000-0000-000000000000$$ AND id < $$30000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$30000000-0000-0000-0000-000000000000$$ AND id < $$40000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$40000000-0000-0000-0000-000000000000$$ AND id < $$50000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$50000000-0000-0000-0000-000000000000$$ AND id < $$60000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$60000000-0000-0000-0000-000000000000$$ AND id < $$70000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$70000000-0000-0000-0000-000000000000$$ AND id < $$80000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$80000000-0000-0000-0000-000000000000$$ AND id < $$90000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$90000000-0000-0000-0000-000000000000$$ AND id < $$a0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$a0000000-0000-0000-0000-000000000000$$ AND id < $$b0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$b0000000-0000-0000-0000-000000000000$$ AND id < $$c0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$c0000000-0000-0000-0000-000000000000$$ AND id < $$d0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$d0000000-0000-0000-0000-000000000000$$ AND id < $$e0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$e0000000-0000-0000-0000-000000000000$$ AND id < $$f0000000-0000-0000-0000-000000000000$$' &
psql -p7654 -Upostgres -d postgres -c 'UPDATE tab SET data = data || $${"test_0": "1", "test_1": "1", "test_2": "1", "test_3": "1", "test_4": "1", "test_5": "1", "test_6": "1", "test_7": "1", "test_8": "1", "test_9": "1", "test_a": "1", "test_b": "1", "test_c": "1", "test_d": "1", "test_e": "1", "test_f": "1"}$$ #- $${"init_0"}$$ WHERE id >= $$f0000000-0000-0000-0000-000000000000$$ AND id <= $$ffffffff-ffff-ffff-ffff-ffffffffffff$$' &
#COMMENTS
