terraform-provider-postgresql icon indicating copy to clipboard operation
terraform-provider-postgresql copied to clipboard

Fix concurrency issue when granting privileges on databases

Open timothegenzmer opened this issue 3 years ago • 6 comments

This fixes #223

timothegenzmer avatar Jun 29 '22 13:06 timothegenzmer

Any news on getting this merged?

philip-harvey avatar Aug 22 '22 03:08 philip-harvey

@cyrilgdn any chance of this getting merged?

brokenjacobs avatar Sep 13 '22 19:09 brokenjacobs

Hey, any news ? It would be nice to merge it.

BartoszGiza avatar Oct 06 '22 09:10 BartoszGiza

BTW: i think i have workaround for this problem. I have added max_connections = 1 to provider block. Seems to work. Probably it will slow down plans :(

BartoszGiza avatar Oct 06 '22 09:10 BartoszGiza

BTW: i think i have workaround for this problem. I have added max_connections = 1 to provider block. Seems to work. Probably it will slow down plans :(

@BartoszGiza That does work in some cases. Unfortunately that also has another side effect that it can cause the provider to hang indefinitely.

seanamos avatar Oct 17 '22 13:10 seanamos

BTW: i think i have workaround for this problem. I have added max_connections = 1 to provider block. Seems to work. Probably it will slow down plans :(

@BartoszGiza That does work in some cases. Unfortunately that also has another side effect that it can cause the provider to hang indefinitely.

Hi, I think i just hit that problem. I have problems with hanging during refreshing states. Also for some reason i can't delete database which provider created.

BartoszGiza avatar Oct 18 '22 06:10 BartoszGiza

Hey @cyrilgdn any updates on this ?

vr avatar Nov 17 '22 11:11 vr

@cyrilgdn thank You

BartoszGiza avatar Nov 17 '22 13:11 BartoszGiza

Will there be a release with this PR included? Thanks.

philip-harvey avatar Nov 17 '22 15:11 philip-harvey

Thanks for merging. We have tested this change in production for a while and it resolved the issue for us without any side effects

timothegenzmer avatar Nov 18 '22 09:11 timothegenzmer

@cyrilgdn when do You plan to release new version ?

BartoszGiza avatar Nov 25 '22 12:11 BartoszGiza

This has been released in v1.18.0

cyrilgdn avatar Nov 27 '22 17:11 cyrilgdn

I tested 1.18 and am still getting concurrency issues on grants so unfortunately #223 is not fixed

philip-harvey avatar Nov 28 '22 22:11 philip-harvey

@philip-harvey Are you sure that you are facing the same issue? This PR only fixes an concurrency issue if you simultaneously grant two different roles privileges on a database. See the example code in the linked issue

timothegenzmer avatar Nov 29 '22 10:11 timothegenzmer

I am getting errors such as the below when updating grants if I don't have max_connections = 1

│ Error: could not get advisory lock for members of role pdr_ro_rl: pq: deadlock detected │ │ with module.int_pdr.postgresql_grant.objects["b8e4ec3df0d0c7e7d493c4e904bc820e"], │ on ../../modules/pdr-config/main.tf line 235, in resource "postgresql_grant" "objects": │ 235: resource "postgresql_grant" "objects" { │ ╵ ╷ │ Error: could not execute revoke query: pq: tuple concurrently updated │ │ with module.int_pdr.postgresql_grant.objects["027c01cac83a9d668d9fe39e210a6278"], │ on ../../modules/pdr-config/main.tf line 235, in resource "postgresql_grant" "objects": │ 235: resource "postgresql_grant" "objects" { │ ╵ ╷ │ Error: could not execute revoke query: pq: tuple concurrently updated │ │ with module.int_pdr.postgresql_grant.objects["117188f8b95fd3ede43ec4bc981c8abb"], │ on ../../modules/pdr-config/main.tf line 235, in resource "postgresql_grant" "objects": │ 235: resource "postgresql_grant" "objects" { │ ╵ ╷ │ Error: could not execute revoke query: pq: tuple concurrently updated │ │ with module.int_pdr.postgresql_grant.objects["de1c9bcc954536336bb466b2a011beef"], │ on ../../modules/pdr-config/main.tf line 235, in resource "postgresql_grant" "objects": │ 235: resource "postgresql_grant" "objects" {

philip-harvey avatar Nov 29 '22 15:11 philip-harvey

Can you provide the relevant terraform code? Just from the error it is hard to say that you are affected by the same issue, as all are just postgresql_grant.

Also the first error: could not get advisory lock for members of role pdr_ro_rl: pq: deadlock detected looks worrisome. Is this a new error since you updated to 1.18? Could you verify that the same error happened on 1.17.1?

timothegenzmer avatar Nov 30 '22 19:11 timothegenzmer

This is the specific part of the code that is still failing due to concurrency issues.

# Grant each role rights to the objects in the schema
resource "postgresql_grant" "objects" {
  for_each    = local.grants_map
  role        = each.value.rolename
  database    = local.database_name
  schema      = each.value.schema
  object_type = each.value.object_type
  objects     = each.value.objects
  privileges  = each.value.privileges
  depends_on = [
    postgresql_role.roles,
    postgresql_schema.schema
  ]
}

AFAIK this issue has existed on all previous and current versions of the provider and the only known way to work around the issue currently is to set max_connections = 1

In this particular use case objects_type = table for most of the grants. Is the merged fix only valid if object_type = database? The exact same issue occurs for tables and schemas as well as databases, and any combination of object_types. e.g. #178

philip-harvey avatar Nov 30 '22 21:11 philip-harvey

This is the most generic code that you could have provided :D

Yes this PR only fixes object_type = database

If you want to know which query is actually responsible for your issues you can have a look at the postgres logs.

timothegenzmer avatar Dec 02 '22 15:12 timothegenzmer

I've had to downgrade as this change is causing all sorts of havoc. The biggest one is that its "leaking" advisory locks on databases. I'm often left with multiple locks that I have to manually cleanup.

seanamos avatar Jan 06 '23 13:01 seanamos

Hey @seanamos, Sorry for the late response.

I have tested my code again, but I was not able to leak any locks I have tested this via while :; do terraform apply --auto-approve && terraform destroy --auto-approve; done and I have checked this with select relation::regclass, * from pg_locks;

from what I get pg_advisory_xact_lock should not leak any locks when the transaction is not leaked and the transaction should not be leaked as it is released via defer

Do you have an example code to reproduce the issue or could you share the leaked locks here so that I can investigate further?

timothegenzmer avatar Feb 27 '23 18:02 timothegenzmer

Any chance the change in #224 can be rolled back? In the past we could set max_connections = 1 and it would work, now it's just totally broken for versions > 1.1.7

philip-harvey avatar Apr 17 '23 16:04 philip-harvey

Are you able to provide a terraform configuration that has this issue? I have tested this again and again with the config I shared in #223 that previously would throw and error and this PR fixed this configuration.

The fix that I provided might have had some side effects that I am not aware of. I am happy to provide a fix for them if I am able to reproduce the issue

timothegenzmer avatar Apr 19 '23 14:04 timothegenzmer

I have tested 1.21.1-beta.1 and concurrency issues are not fixed.

The fix provided by giner works. This one not. Ref: https://github.com/giner/terraform-provider-postgresql/commits/fix_getting_stuck_on_refreshing_postgresql_grant

pagalba-com avatar Dec 11 '23 10:12 pagalba-com