-
Notifications
You must be signed in to change notification settings - Fork 278
Use object-level locks for concurrent grants to improve parallelism #595
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
Open
benoittgt
wants to merge
6
commits into
cyrilgdn:main
Choose a base branch
from
benoittgt:lock-grants
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 5 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
9eacc7e
Fix for `Error: could not execute revoke query: pq: tuple concurrentl…
7ab23a1
Revert changes to master
gesanderson 5520ed1
Wrap postgresql_grant resource in a lock
gesanderson 458a910
Improve test setup
gesanderson edcadd7
Use object-level locks for concurrent grants to improve parallelism
benoittgt 167bdcf
Re add set statement_timeout like for pgLockRole
benoittgt File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,6 @@ | ||
| provider_installation { | ||
| dev_overrides { | ||
| "cyrilgdn/postgresql" = "../../../" | ||
| } | ||
| direct {} | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| locals { | ||
| read_only_users = toset([for i in range(var.user_ro_count) : "user_ro_${i}"]) | ||
| read_write_users = toset([for i in range(var.user_rw_count) : "user_rw_${i}"]) | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,191 @@ | ||
| terraform { | ||
| required_providers { | ||
| docker = { | ||
| source = "kreuzwerker/docker" | ||
| version = ">= 3.0.2" | ||
| } | ||
| postgresql = { | ||
| source = "cyrilgdn/postgresql" | ||
| version = ">= 1.25" | ||
| } | ||
| } | ||
| } | ||
|
|
||
| provider "docker" { | ||
| host = var.docker_host | ||
| } | ||
|
|
||
| resource "docker_image" "postgres" { | ||
| name = var.postgres_image | ||
| keep_locally = var.keep_image | ||
| } | ||
|
|
||
| resource "docker_container" "postgres" { | ||
| image = docker_image.postgres.image_id | ||
| name = "postgres" | ||
| wait = true | ||
| ports { | ||
| internal = var.POSTGRES_PORT | ||
| external = var.POSTGRES_PORT | ||
| } | ||
| env = [ | ||
| "POSTGRES_PASSWORD=${var.POSTGRES_PASSWORD}" | ||
| ] | ||
| healthcheck { | ||
| test = ["CMD-SHELL", "pg_isready"] | ||
| interval = "5s" | ||
| timeout = "5s" | ||
| retries = 5 | ||
| start_period = "2s" | ||
| } | ||
| upload { | ||
| file = "/docker-entrypoint-initdb.d/mock-tables.sql" | ||
| content = <<EOS | ||
| CREATE DATABASE "test" OWNER "${var.POSTGRES_DBNAME}"; | ||
| \connect ${var.POSTGRES_DBNAME} | ||
|
|
||
| DO $$ | ||
| DECLARE | ||
| table_count int := ${var.table_count}; | ||
| BEGIN | ||
| FOR count IN 0..table_count LOOP | ||
| EXECUTE format('CREATE TABLE table_%s (test int)', count); | ||
| END LOOP; | ||
| END $$; | ||
| EOS | ||
| } | ||
| } | ||
|
|
||
| provider "postgresql" { | ||
| scheme = "postgres" | ||
| host = var.POSTGRES_HOST | ||
| port = docker_container.postgres.ports[0].external | ||
| database = var.POSTGRES_PASSWORD | ||
| username = var.POSTGRES_PASSWORD | ||
| password = var.POSTGRES_PASSWORD | ||
| sslmode = "disable" | ||
| superuser = false | ||
| lock_grants = true | ||
| } | ||
|
|
||
| resource "postgresql_role" "readonly_role" { | ||
| name = "readonly" | ||
| login = false | ||
| superuser = false | ||
| create_database = false | ||
| create_role = false | ||
| inherit = false | ||
| replication = false | ||
| connection_limit = -1 | ||
| } | ||
|
|
||
| resource "postgresql_role" "readwrite_role" { | ||
| name = "readwrite" | ||
| login = false | ||
| superuser = false | ||
| create_database = false | ||
| create_role = false | ||
| inherit = false | ||
| replication = false | ||
| connection_limit = -1 | ||
| } | ||
|
|
||
| resource "postgresql_grant" "readonly_role" { | ||
| database = var.POSTGRES_DBNAME | ||
| role = postgresql_role.readonly_role.name | ||
| object_type = "table" | ||
| schema = "public" | ||
| privileges = ["SELECT"] | ||
| with_grant_option = false | ||
| } | ||
|
|
||
| resource "postgresql_grant" "readwrite_role" { | ||
| database = var.POSTGRES_DBNAME | ||
| role = postgresql_role.readwrite_role.name | ||
| object_type = "table" | ||
| schema = "public" | ||
| privileges = ["SELECT", "INSERT", "UPDATE", "DELETE"] | ||
| with_grant_option = false | ||
| } | ||
|
|
||
| resource "postgresql_role" "readonly_users" { | ||
| for_each = local.read_only_users | ||
| name = each.value | ||
| roles = [postgresql_role.readonly_role.name] | ||
| login = true | ||
| superuser = false | ||
| create_database = false | ||
| create_role = false | ||
| inherit = true | ||
| replication = false | ||
| connection_limit = -1 | ||
| } | ||
|
|
||
| resource "postgresql_role" "readwrite_users" { | ||
| for_each = local.read_write_users | ||
| name = each.value | ||
| roles = [postgresql_role.readonly_role.name] | ||
| login = true | ||
| superuser = false | ||
| create_database = false | ||
| create_role = false | ||
| inherit = true | ||
| replication = false | ||
| connection_limit = -1 | ||
| } | ||
|
|
||
| resource "postgresql_grant" "connect_db_readonly_role" { | ||
| for_each = postgresql_role.readonly_users | ||
| database = var.POSTGRES_DBNAME | ||
| object_type = "database" | ||
| privileges = ["CREATE", "CONNECT"] | ||
| role = each.value.name | ||
| } | ||
|
|
||
| resource "postgresql_grant" "connect_db_readwrite_role" { | ||
| for_each = postgresql_role.readwrite_users | ||
| database = var.POSTGRES_DBNAME | ||
| object_type = "database" | ||
| privileges = ["CREATE", "CONNECT"] | ||
| role = each.value.name | ||
| } | ||
|
|
||
| resource "postgresql_grant" "usage_readonly_role" { | ||
| for_each = postgresql_role.readonly_users | ||
| database = var.POSTGRES_DBNAME | ||
| role = each.value.name | ||
| object_type = "schema" | ||
| schema = "public" | ||
| privileges = ["USAGE"] | ||
| with_grant_option = false | ||
| } | ||
|
|
||
| resource "postgresql_grant" "usage_readwrite_role" { | ||
| for_each = postgresql_role.readwrite_users | ||
| database = var.POSTGRES_DBNAME | ||
| role = each.value.name | ||
| object_type = "schema" | ||
| schema = "public" | ||
| privileges = ["USAGE"] | ||
| with_grant_option = false | ||
| } | ||
|
|
||
| resource "postgresql_grant" "select_readonly_role" { | ||
| for_each = postgresql_role.readonly_users | ||
| database = var.POSTGRES_DBNAME | ||
| role = each.value.name | ||
| object_type = "table" | ||
| schema = "public" | ||
| privileges = ["SELECT"] | ||
| with_grant_option = false | ||
| } | ||
|
|
||
| resource "postgresql_grant" "crud_readwrite_role" { | ||
| for_each = postgresql_role.readwrite_users | ||
| database = var.POSTGRES_DBNAME | ||
| role = each.value.name | ||
| object_type = "table" | ||
| schema = "public" | ||
| privileges = ["SELECT", "UPDATE", "INSERT", "DELETE"] | ||
| with_grant_option = false | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| run "concurrent_grants" { | ||
| command = apply | ||
| } |
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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,73 @@ | ||
| variable "postgres_image" { | ||
| description = "Which postgres docker image to use" | ||
| default = "postgres:17" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "docker_host" { | ||
| description = "Socket path to docker host to use for testing" | ||
| default = "unix:///var/run/docker.sock" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "table_count" { | ||
| description = "Number of mock tables to create" | ||
| default = 300 | ||
| type = number | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "user_ro_count" { | ||
| description = "Number of mock RO users to create" | ||
| default = 30 | ||
| type = number | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "user_rw_count" { | ||
| description = "Number of mock RW users to create" | ||
| default = 30 | ||
| type = number | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "POSTGRES_DBNAME" { | ||
| default = "postgres" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "POSTGRES_USER" { | ||
| default = "postgres" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "POSTGRES_PASSWORD" { | ||
| description = "Password for docker POSTGRES_USER" | ||
| default = "postgres" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "POSTGRES_HOST" { | ||
| default = "127.0.0.1" | ||
| type = string | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "POSTGRES_PORT" { | ||
| description = "Which port postgres should listen on." | ||
| default = 5432 | ||
| type = number | ||
| sensitive = false | ||
| } | ||
|
|
||
| variable "keep_image" { | ||
| description = "If true, then the Docker image won't be deleted on destroy operation. If this is false, it will delete the image from the docker local storage on destroy operation." | ||
| default = true | ||
| type = bool | ||
| sensitive = false | ||
| } |
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
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.