Skip to content

Granting / revoking privileges on tables through "objects" is not atomic #208

@jan-wilhelm

Description

@jan-wilhelm

Hi there,

Thank you for opening an issue. Please provide the following information:

Terraform Version

Terraform v1.1.9
on darwin_amd64

  • provider registry.terraform.io/cyrilgdn/postgresql v1.16.0

Affected Resource(s)

  • postgresql_grant

Terraform Configuration Files

resource "postgresql_grant" "test-grant" {
  database    = "db"
  role        = "test-role"
  schema      = "public"

  object_type = "table"
  objects     = ["table-one"]
  privileges  = ["SELECT"]
}

Expected Behavior

When changing the objects from ["table-one"] to ["table-one", "table-two"], the permissions should be revoked / granted atomatically, so that existing systems querying the database don't temporarily see errors.

Actual Behavior

Because of the way objects was implemented in #105 (specifically, the ForceNew schema flag), Terraform forces the plan to destroy and recreate the grant which does not happen within the same postgres transaction. Therefore, postgres roles using the "SELECT" permission on "table-one" temporarily see errors for a little while when Terraform has deleted but not yet recreated the permission grants.

Steps to Reproduce

  1. create a postgres db with a test schema
  2. create a testing role and apply the tf code from above
  3. Connect to postgres using the test role
  4. Continously run statements such as select count(*) from "table-one";
  5. change the objects = ["table-one"] line to objects = ["table-one", "table-two"] and apply the changes through terraform
  6. Keep running your count(*) statement, you will see some errors for ~1 second while the system revokes / issues new permissions.

Question

Are there any smart workarounds for this? For testing and dev purposes, this behaviour is OK, but for production systems that are running hundreds of queries per second through a role managed with this provider, seeing errors for about a second is something I would definitely like to avoid. Thank you!

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions