hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io
Other
43.13k stars 9.58k forks source link

Postgres backend does not have close function #23638

Open gorpher opened 4 years ago

gorpher commented 4 years ago

Current Terraform Version

terraform 0.12.17

Use-cases

if use terraform cli more than 100 at the same time with pgbackend postgress connenction number limit,terraform execution exception

Attempted Solutions

  1. add close function in the backend/remote-state/pg/backend.go
  2. public sql.DB field DB in the backend/remote-state/pg/backend.go
  3. adjust postgress max connection number

Proposal

I think terraform should added backend close function for second development

References

kinds of backend implement backend/backend.go , but this interface add close method ,It's against the principle of development, i holp terraform may expose pgbackend close function.

remilapeyre commented 4 years ago

Hi @gorpher, I'm not sure when exactly you would like to close this Close() method, it seems like you would like Terraform to call it at the end of the Terraform run, but the connection is already closed when Terraform exit is it not?

bschaatsbergen commented 1 month ago

Hi @gorpher, thank you for raising this issue some time ago.

I'm trying to understand why we aren't calling close, it seems that db.Close() would close the database connection immediately after setup, making the connection unusable for any subsequent database operations in the backend (e.g. locking, unlocking). When we initialize the connection, it’s solely to set up the database and return the connection for future use. Closing it at this point would prevent any further interactions with the database, resulting in runtime errors.

Instead, db.Close() should only be called once we know the database connection is no longer needed. Ideally, this would be in a dedicated function or lifecycle step when the backend is finished doing backend work. Extending the backend base functionalities to handle this would be a significant effort, especially as I don’t see any reason to implement a Close method in the other backends, which could potentially impact them as well.