Skip to content

Commit

Permalink
Merge pull request #7 from crztssr/main
Browse files Browse the repository at this point in the history
Fixes #6 for pg_repack invalid leftovers
  • Loading branch information
vitabaks authored Dec 10, 2022
2 parents a379487 + e657d48 commit b7d7784
Showing 1 changed file with 15 additions and 7 deletions.
22 changes: 15 additions & 7 deletions pg_auto_reindexer
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,14 @@ for db in $DBNAME; do
info " completed repack index $index (size after: $index_size_after MB)"
break
else
# check for invalid temporary indexes with the suffix "index_"
invalid_index=$(psql -h "${PGHOST}" -p "${PGPORT}" -U "${DBUSER}" -d "$db" -tAXc "SELECT string_agg(quote_ident(schemaname)||'.'||quote_ident(indexrelname), ', ') FROM pg_stat_user_indexes sui JOIN pg_index i USING (indexrelid) WHERE NOT indisvalid AND indexrelname like 'index_%'")
if [ -n "$invalid_index" ]; then
warnmsg " A temporary index apparently created by pg_repack has been left behind."
warnmsg " failed to repack index \"$index\". Skipping"
failed_reindex_count=$((failed_reindex_count+1))
continue 2
fi
retry_n=$((retry_n+1))
# Skipping repack after all attempts
if [[ $delay -eq 60 ]]; then
Expand All @@ -368,13 +376,6 @@ for db in $DBNAME; do
fi
fi
done
# check for invalid temporary indexes with the suffix "index_"
invalid_index=$(psql -h "${PGHOST}" -p "${PGPORT}" -U "${DBUSER}" -d "$db" -tAXc "SELECT string_agg(quote_ident(schemaname)||'.'||quote_ident(indexrelname), ', ') FROM pg_stat_user_indexes sui JOIN pg_index i USING (indexrelid) WHERE NOT indisvalid AND indexrelname like 'index_%'")
if [ -n "$invalid_index" ]; then
warnmsg "A temporary index apparently created by pg_repack has been left behind, and we do not want to risk dropping this index ourselves. If the index was in fact created by an old pg_repack job which didn't get cleaned up, you should just use DROP INDEX CONCURRENTLY and try the pg_auto_reindexer again."
warnmsg "invalid indexes: $invalid_index"
continue 2
fi
db_maintenance_benefit=$((db_maintenance_benefit+index_size_before-index_size_after))
sleep 2s
done
Expand Down Expand Up @@ -428,6 +429,13 @@ for db in $DBNAME; do
if [[ -n "$bloat_indexes" ]] && [[ $failed_reindex_count -lt $FAILED_REINDEX_LIMIT ]]; then
info "Completed index maintenance for database: $db (released: $db_maintenance_benefit MB)"
fi
invalid_index_drop_commands=$(psql -h "${PGHOST}" -p "${PGPORT}" -U "${DBUSER}" -d "$db" -tAXc "SELECT string_agg('DROP INDEX CONCURRENTLY '||quote_ident(schemaname)||'.'||quote_ident(indexrelname), '; ')||';' FROM pg_stat_user_indexes sui JOIN pg_index i USING (indexrelid) WHERE NOT indisvalid AND indexrelname like 'index_%'")
if [ -n "$invalid_index_drop_commands" ]; then
info "A temporary index(es) apparently created by pg_repack has been left behind, and we do not want to risk dropping this index ourselves."
info "If the index was in fact created by an old pg_repack job which didn't get cleaned up, you should just use next commands:"
info "$invalid_index_drop_commands"
info "and run pg_auto_reindexer again."
fi
done
info "Total amount released during maintenance: $total_maintenance_benefit MB"
exit

0 comments on commit b7d7784

Please sign in to comment.