Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

remote-state/pg: Cannot lock workspace #33217

Open
hashi825 opened this issue May 19, 2023 · 4 comments
Open

remote-state/pg: Cannot lock workspace #33217

hashi825 opened this issue May 19, 2023 · 4 comments
Labels
backend/pg bug new new issue not yet triaged

Comments

@hashi825
Copy link

hashi825 commented May 19, 2023

Terraform Version

Terrafrom v1.4.5
on linux_amd64

Terraform Configuration Files

Debug Output

Expected Behavior

When using the pg backend terraform should allow locking for states in different schemas without blocking.

Actual Behavior

Currently it appears if two users sharing the same databases but operating on projects in different schemas, terraform is unable to acquire the state lock when someone else is performing a plan/apply on an unrelated project in a different schema.

This can be reproduced by manually running acquiring the lock and running plan/apply

Steps to Reproduce

  1. Acquire advisory lock for pg side for test
select try pg_try_advisory_lock(-1);
pg_try_advisory_lock | t
  1. Run a plan
terraform plan

Error:  Error acquiring the state lock
Error message: Cannot lock workspace; already locked for workspace creation: default
Lock Info:
ID:        xxx
Path:      
Operation: OperationTypePlan
Who:       xxx
Version:   1.4.5
Created:  xxx
Info:      
Terraform acquires a state lock to protect the state from being written
by multiple users at the same time. Please resolve the issue above and try
again. For most commands, you can disable locking with the "-lock=false"
flag, but this is not recommended.
  1. Unlock from pg side
select pg_advisory_unlock(-1);
pg_advisory_unlock | t
  1. Terraform plan works successfully

Additional Context

Seems be related to https://github.com/hashicorp/terraform/blob/13cb0d03c7d6cca7b22966428c6032ab1ac4c574/internal/backend/remote-state/pg/client.go#LL92C110-L92C110 which tries to acquire a lock on both the states table id and (-1).

References

No response

@hashi825 hashi825 added bug new new issue not yet triaged labels May 19, 2023
@crw
Copy link
Collaborator

crw commented May 19, 2023

Just FYI, @remilapeyre

@naustra
Copy link

naustra commented Jan 23, 2024

up

@FlawaCLV
Copy link

Same issue!

@jnielsendotnet
Copy link

I have a proposed fix for this issue in #35069 . Instead of using "-1" for the create lock (which makes it impossible for two different users to plan or apply a new state at the same time, even if they are completely independent), the create lock name is derived from the schema name.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backend/pg bug new new issue not yet triaged
Projects
None yet
Development

No branches or pull requests

6 participants