|
#!/bin/bash |
|
|
|
set -e |
|
set -u |
|
|
|
DIR="/tmp/lime3-debug/" |
|
|
|
prepare(){ |
|
mkdir -p $DIR |
|
cd $DIR |
|
sudo pg_ctlcluster 14 lime3 start |
|
} |
|
|
|
cleanup(){ |
|
rm -rf $DIR |
|
} |
|
|
|
deduplicate_csv(){ |
|
cat $DIR/out.csv | sort | uniq > $DIR/out-dedup.csv |
|
mv $DIR/out-dedup.csv $DIR/out.csv |
|
} |
|
|
|
# Timestamp format: 2025-05-04 HH:mm:ss.000000+00 |
|
current_timestamp(){ |
|
sudo cat /etc/postgresql/14/lime3/postgresql.conf | grep recovery_target_time | grep -v "#recovery_target_timeline" | sed "s/#.*//;s/recovery_target_time =//;s/[']//g;s/[\t]$//g;s/^[ \t]*//;" |
|
} |
|
|
|
# Timestamp in format HH:mm:ss |
|
formatted_timestamp() { |
|
echo $(current_timestamp) | sed 's/2025-05-04 //;s/.000000+00//' |
|
} |
|
|
|
# TODO include heders when first row |
|
print_row(){ |
|
psql -U postgres -p 5434 lime3 -c "COPY(SELECT '$(formatted_timestamp)', * FROM lime_survey_651468 WHERE id = 1968) TO STDOUT WITH (FORMAT CSV, FORCE_QUOTE *);" |
|
} |
|
|
|
print_headers(){ |
|
psql -U postgres -p 5434 lime3 -c "COPY(SELECT * FROM lime_survey_651468 WHERE false) TO STDOUT WITH (FORMAT CSV, HEADER TRUE, FORCE_QUOTE *);" |
|
} |
|
|
|
# Export row from db. Filename is current timestamp. |
|
export_row() { |
|
sudo su - postgres -c "echo $(print_row)" >> $DIR/out.csv |
|
} |
|
|
|
update_timestamp(){ |
|
new_timestamp="$@" |
|
|
|
if [[ -z "$new_timestamp" ]]; then |
|
echo "No timestamp provided. Exiting." |
|
exit 1 |
|
fi |
|
|
|
echo "Updating version to $new_timestamp" |
|
|
|
sudo sed -i "s/recovery_target_time = '.*'/recovery_target_time = '$new_timestamp'/" /etc/postgresql/14/lime3/postgresql.conf |
|
} |
|
|
|
restart_postgres(){ |
|
sudo pg_ctlcluster 14 lime3 stop && sudo pg_ctlcluster 14 lime3 start |
|
} |
|
|
|
wait_postgres_ready() { |
|
set +e |
|
|
|
while true; do |
|
echo "Verifica stato PostgreSQL..." |
|
|
|
pg_isready -h localhost -p 5434 -U postgres > /dev/null 2>&1 |
|
pg_ready=$? |
|
|
|
sudo pg_ctlcluster 14 lime3 status | grep -q "esecuzione" |
|
cluster_status=$? |
|
|
|
if [[ $pg_ready -eq 0 && $cluster_status -eq 0 ]]; then |
|
echo "✅ PostgreSQL è pronto e il cluster è online." |
|
break |
|
else |
|
echo "⏳ In attesa che PostgreSQL sia pronto... (ready: $pg_ready, cluster: $cluster_status)" |
|
sleep 5 |
|
fi |
|
done |
|
|
|
set -e |
|
} |
|
|
|
run_for_timestamp(){ |
|
timestamp=$1 |
|
|
|
if [[ -z "$timestamp" ]]; then |
|
echo "No timestamp provided. Exiting." |
|
exit 1 |
|
fi |
|
|
|
echo "Running for timestamp: $timestamp." |
|
|
|
update_timestamp "$timestamp" |
|
restart_postgres |
|
|
|
wait_postgres_ready |
|
|
|
export_row |
|
} |
|
|
|
run_from_file(){ |
|
prepare |
|
|
|
file=$1 |
|
echo "Running for file: $file" |
|
|
|
exec 3< "$file" |
|
|
|
print_headers > $DIR/out.csv |
|
|
|
while IFS= read -r line <&3; do |
|
run_for_timestamp "$line" |
|
# read -p "Are you sure? " -n 1 -r |
|
# echo # (optional) move to a new line |
|
# if [[ $REPLY =~ ^[Yy]$ ]] |
|
# then |
|
# echo "AA Running for timestamp: $line" |
|
# fi |
|
done |
|
|
|
# Chiude il file descriptor |
|
exec 3<&- |
|
} |
|
|
|
fresh_start(){ |
|
sudo service postgresql stop |
|
sudo rm -rf /var/lib/postgresql/14/lime3/ |
|
sudo cp -r '/var/lib/postgresql/14/lime3-2025-05-04 13:49:00.085883+02' /var/lib/postgresql/14/lime3 |
|
sudo chown -R postgres:postgres /var/lib/postgresql/14/lime3 |
|
update_timestamp "2025-05-04 11:49:50.000000+00" |
|
cleanup |
|
} |
|
|
|
# ################ |
|
# Preparation: |
|
# ################ |
|
# - Create a base backup located at /var/lib/postgresql/14/lime3-2025-05-04 13:49:00.085883+02 |
|
# - configure /etc/postgresql/14/lime3/postgresql.conf with the following lines: |
|
# max_connections = 500 |
|
# max_locks_per_transaction = 512 |
|
# recovery_target_action = pause |
|
# recovery_target_time = '2025-05-04 11:50:05.000000+00' |
|
# restore_command = 'wal-g wal-fetch %f %p >> /tmp/wal.log 2>&1' |
|
# shared_buffers = 128MB |
|
|
|
# (still preparation) |
|
# ./debug-db.sh fresh_start |
|
|
|
|
|
# ################ |
|
# Usage: |
|
# ################ |
|
# ./debug-db.sh run_from_file /tmp/timestamps |
|
|
|
$@ |