Integrate object-level locks for concurrent grants (PR #595)#2
Merged
Integrate object-level locks for concurrent grants (PR #595)#2
Conversation
Replace database-wide locks with hash-based object locks. Enables parallel grant operations on different objects while preventing catalog conflicts. Uses hashtext() for deterministic lock IDs like "grant:table:db.schema.table". Signed-off-by: Benoit Tigeot <benoit.tigeot@lifen.fr>
* pr-595/lock-grants: Re add set statement_timeout like for pgLockRole Use object-level locks for concurrent grants to improve parallelism Improve test setup Wrap postgresql_grant resource in a lock Revert changes to master Fix for `Error: could not execute revoke query: pq: tuple concurrently updated`
dba8167 to
4ee720b
Compare
jbdelpech
approved these changes
Dec 22, 2025
|
Hello Happy to see this merged here. Curious about the feedbacks. Is it working as expected ? |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Overview
This PR integrates upstream PR #595 which implements object-level advisory locks for concurrent grant operations, significantly improving parallelism and preventing "tuple concurrently updated" errors.
Problem Solved
When running multiple
postgresql_grantresources concurrently in Terraform, users would frequently encounter:This happened because the provider used database-wide locks, creating unnecessary contention between unrelated grant operations.
Solution
Replace database-wide locks with hash-based object-level locks using PostgreSQL's advisory lock mechanism:
hashtext()for deterministic lock IDs (e.g.,grant:table:db.schema.table)Changes
Core Implementation
postgresql/config.go: AddLockGrantsconfiguration field andIsLockGrants()methodspostgresql/helpers.go:generateGrantLockID()- generates deterministic lock identifiers per object typepgLockGrantTarget()- acquires advisory locks at object levelpostgresql/provider.go: Addlock_grantsprovider configuration parameter (default:false)postgresql/resource_postgresql_grant.go: Replace database-wide locks with object-level locking when enabledTesting & Examples
examples/issues/178/: Complete test setup with Docker PostgreSQL and concurrent grant scenarioslock_grantsparameterDocumentation
README.md: Mark PR Use object-level locks for concurrent grants to improve parallelism cyrilgdn/terraform-provider-postgresql#595 as integrated ✅Usage
Enable object-level locking in your provider configuration:
Performance Impact
false)Testing
The example configuration in
examples/issues/178/can be used to verify concurrent grant behavior:Credits
Original implementation by @benoittgt in cyrilgdn/terraform-provider-postgresql#595
Related