From 7690d2ca6dd804d65ab6725fea8f4953359fcc6a Mon Sep 17 00:00:00 2001 From: Kevin Veen-Birkenbach Date: Tue, 22 Sep 2026 15:52:16 +0200 Subject: [PATCH] fix(postgres): raise max_locks_per_transaction to 256 GitLab's migration on its dedicated postgres sidecar failed in CI run 35674334334 with "ERROR: out of shared memory" and the hint to raise max_locks_per_transaction. The default of 64 locks per transaction cannot cover a schema that locks thousands of tables and indexes in one transaction: a reproduction on postgres:18 failed at table 7453 with the default and held 20000 tables at 256. The value sits next to the other postgres tuning constants in the resource lookup, is exposed as POSTGRES_MAX_LOCKS_PER_TRANSACTION, and reaches both the central svc-db-postgres command and the per-app sidecar. The sidecar's new command keeps the image entrypoint and only adds the flag. The shared lock table grows to about 256 x max_connections entries, roughly 30 MB at the 400-connection cap. Co-Authored-By: Claude Opus 5 (1M context) --- group_vars/all/17_resource.yml | 1 + plugins/lookup/resource.py | 2 ++ roles/svc-db-postgres/templates/compose.yml.j2 | 2 ++ roles/sys-svc-rdbms/templates/services/postgres.yml.j2 | 4 ++++ 4 files changed, 9 insertions(+) diff --git a/group_vars/all/17_resource.yml b/group_vars/all/17_resource.yml index 1d2ecaf5e9..f807f39bad 100644 --- a/group_vars/all/17_resource.yml +++ b/group_vars/all/17_resource.yml @@ -15,6 +15,7 @@ POSTGRES_SUPERUSER_RESERVED_CONNECTIONS: "{{ lookup('resource', 'postgres_superu POSTGRES_SHARED_BUFFERS: "{{ lookup('resource', 'postgres_shared_buffers') }}" POSTGRES_WORK_MEM: "{{ lookup('resource', 'postgres_work_mem') }}" POSTGRES_MAINTENANCE_WORK_MEM: "{{ lookup('resource', 'postgres_maintenance_work_mem') }}" +POSTGRES_MAX_LOCKS_PER_TRANSACTION: "{{ lookup('resource', 'postgres_max_locks_per_transaction') }}" POSTGRES_DELAY: "{{ lookup('resource', 'postgres_delay') }}" TIMEOUT_FACTOR: 1 diff --git a/plugins/lookup/resource.py b/plugins/lookup/resource.py index 60da705972..91b2a9d1a3 100644 --- a/plugins/lookup/resource.py +++ b/plugins/lookup/resource.py @@ -21,6 +21,7 @@ _HOST_RESERVE_MEM = 4 _PIDS_LIMIT = 512 _POSTGRES_SUPERUSER_RESERVED_CONNECTIONS = 3 _POSTGRES_DELAY = 2 +_POSTGRES_MAX_LOCKS_PER_TRANSACTION = 256 _CONTAINER_PREFIX_REGEX = r"^(web-|svc-).*" @@ -192,6 +193,7 @@ def _build( "postgres_maintenance_work_mem_mb": maintenance_work_mem_mb, "postgres_maintenance_work_mem": f"{maintenance_work_mem_mb}MB", "postgres_delay": _POSTGRES_DELAY, + "postgres_max_locks_per_transaction": _POSTGRES_MAX_LOCKS_PER_TRANSACTION, } if containers is not None: diff --git a/roles/svc-db-postgres/templates/compose.yml.j2 b/roles/svc-db-postgres/templates/compose.yml.j2 index 5b0170dfda..3136594e5a 100644 --- a/roles/svc-db-postgres/templates/compose.yml.j2 +++ b/roles/svc-db-postgres/templates/compose.yml.j2 @@ -16,6 +16,8 @@ - "work_mem={{ POSTGRES_WORK_MEM }}" - "-c" - "maintenance_work_mem={{ POSTGRES_MAINTENANCE_WORK_MEM }}" + - "-c" + - "max_locks_per_transaction={{ POSTGRES_MAX_LOCKS_PER_TRANSACTION }}" {% include 'roles/sys-svc-container/templates/base.yml.j2' %} {{ lookup('container_healthcheck', service_name) | indent(4) }} diff --git a/roles/sys-svc-rdbms/templates/services/postgres.yml.j2 b/roles/sys-svc-rdbms/templates/services/postgres.yml.j2 index 8336da1ace..41d69882a2 100644 --- a/roles/sys-svc-rdbms/templates/services/postgres.yml.j2 +++ b/roles/sys-svc-rdbms/templates/services/postgres.yml.j2 @@ -15,6 +15,10 @@ {% else %} {{ lookup('container_image', 'svc-db-postgres', 'postgres', image=lookup('database', application_id, 'image'), version=lookup('database', application_id, 'version')) }} {% endif %} + command: + - "postgres" + - "-c" + - "max_locks_per_transaction={{ POSTGRES_MAX_LOCKS_PER_TRANSACTION }}" env_file: - {{ lookup('database', application_id, 'env') }} {{ lookup('compose_restart') }}