Restore statement_timeout after syncing sequences

What does this MR do and why?

without_statement_timeout in lib/gitlab/database/sync_sequences_with_table_data.rb set the statement timeout to 0, ran the block, then called RESET statement_timeout.

RESET restores the cluster default from postgresql.conf, not the value that database.yml set for the session. The cluster default is usually 0, so after this method returned, the connection kept running with no statement timeout at all.

The method now reads the current value with SHOW statement_timeout before changing it and restores that exact value afterwards. A regression test sets the timeout to 15s, runs the sync, and checks it is still 15s.

This surfaced in CI: the spec for this class is tagged :delete, so it does not run inside a transaction and the leaked setting stayed on the connection for the rest of the RSpec process. The new timeouts check in gitlab:db:diagnostics reports statement_timeout = 0 as an error, so when the sequence spec ran before spec/tasks/gitlab/db/diagnostics_rake_spec.rb in the same job, the diagnostics example failed and the next two spec files failed on the aborted connection.

References

Screenshots or screen recordings

Not applicable. This MR has no UI change.

How to set up and validate locally

Minimal reproduction. Save this as repro.rb and run it with RAILS_ENV=test bin/rails runner repro.rb:

connection = ApplicationRecord.connection
puts "before: #{connection.select_value('SHOW statement_timeout')}"
Gitlab::Database::SyncSequencesWithTableData.new(logger: Logger.new(nil), only_sequences: ['does_not_exist']).execute
puts "after:  #{connection.select_value('SHOW statement_timeout')}"

On master:

before: 2min
after:  0

On this branch:

before: 2min
after:  2min

The CI failure itself reproduces by running the two spec files in a fixed order. On master the diagnostics example fails, on this branch all examples pass:

bin/rspec --order defined spec/lib/gitlab/database/sync_sequences_with_table_data_spec.rb spec/tasks/gitlab/db/diagnostics_rake_spec.rb

MR acceptance checklist

Evaluate this MR against the MR acceptance checklist. It helps you analyze changes to reduce risks in quality, performance, reliability, security, and maintainability.

🤖 Generated with Claude Code

Edited by Rutger Wessels

Merge request reports

Loading
Loading