Error: could not execute revoke query: pq: tuple concurrently updated.
Hey guys, today I’m gonna to share with you about a case when using Terraform.
Error: could not execute revoke query: pq: tuple concurrently updated
Table of Contents
I’m using Terraform with Postgres provider to create a bunch of database users and grant the permissions to them.
Well, everything looking good until I run the terraform apply
. A lot of error logs that scared me. What is that error look likes?
│ Error: could not execute revoke query: pq: tuple concurrently updated
│
│ with module.user-rds-postgres-trade.postgresql_grant.rds_postgres_grant_table["user.01"],
│ on ../../modules/user-rds-postgres/main.tf line 90, in resource "postgresql_grant" "rds_postgres_grant_table":
│ 90: resource "postgresql_grant" "rds_postgres_grant_table" {
│
╵
╷
│ Error: could not execute revoke query: pq: tuple concurrently updated
│
│ with module.user-rds-postgres-trade.postgresql_grant.rds_postgres_grant_table["user.02"],
│ on ../../modules/user-rds-postgres/main.tf line 90, in resource "postgresql_grant" "rds_postgres_grant_table":
│ 90: resource "postgresql_grant" "rds_postgres_grant_table" {
│
╵
╷
│ Error: could not execute revoke query: pq: tuple concurrently updated
│
│ with module.user-rds-postgres-trade.postgresql_grant.rds_postgres_grant_table["user.03"],
│ on ../../modules/user-rds-postgres/main.tf line 90, in resource "postgresql_grant" "rds_postgres_grant_table":
│ 90: resource "postgresql_grant" "rds_postgres_grant_table" {
│
╵
Releasing state lock. This may take a few moments...
Why the error “pq: tuple concurrently updated” happened?
After searched for a while in the internet, finally I got the information from provider’s github https://github.com/cyrilgdn/terraform-provider-postgresql/issues/178
What does the issue means?
They wrote and discussed a lots but I can tell you that simply the Postgres provider doesn’t supporting to run GRANT
the permissions for multi users as the same time.
And that’s an issue belong to the provider, nothing wrong with the Terraform code or your Postgres instance.
How we can resolve the error “pq: tuple concurrently updated”?
And now, how we’re gonna to resolve this error?
Really simple with a trick. Use a parameter like -parallelism=1
. How the Terraform command will be?
You like to change from this:
terraform apply
To this:
terraform apply -parallelism=1
What does that parameter means? According to the Terraform docs https://developer.hashicorp.com/terraform/cli/commands/apply then that parameter means “Limit the number of concurrent operation”. Default values is 10 and we down it to 1.
Like at one time, Terraform just run Postgres grant for only 1 user. That’s all, we can passed the problem.
Conclusion
With just a simple trick above, I think you can save a bit time when facing the error exactly like that. Even myself, few months ago, I wrote and applied the Terraform code and faced it. Then I forgot to use -parallelism=1
and faced the problem again. I gotta to write a post to take a note about this case and also hoping it’s helpful to you guys.