Skip to content
Troubleshoot Out of Shared Memory Errors

Troubleshoot Out of Shared Memory Errors

Find out why a Managed PostgreSQL service raises out of shared memory, identify the transactions that fill the lock table, and raise max_locks_per_transaction knowing what the change costs.

The Error

A statement fails with:

ERROR:  out of shared memory
HINT:  You might need to increase "max_locks_per_transaction".

When the statement uses parallel workers, the same error can come from a worker and carries CONTEXT: parallel worker. The errors are also in the service log:

exo dbaas logs SERVICE_NAME -z ZONE -l 200 \
  --output-template '{{range $l := .Logs}}{{println $l.Message}}{{end}}' | grep 'out of shared memory'

Parameters:

  • SERVICE_NAME: name of the Managed PostgreSQL service, for example my-postgresql.
  • ZONE: zone the service runs in, for example ch-gva-2.
  • -l: number of log lines to retrieve, most recent first. Raise it to look further back.

This is not a lack of RAM, and a plan with more memory does not fix it by itself. The memory that runs out is the lock table, a fixed area of shared memory allocated when PostgreSQL starts. The HINT naming max_locks_per_transaction confirms it. It is a different problem from out of memory or from temporary file size exceeds "temp_file_limit".

How the Lock Table Is Sized

Every relation a transaction touches, each table, partition, index and TOAST table, takes one entry in the lock table until the transaction ends. The table is sized at startup from max_locks_per_transaction times the number of server processes and prepared transactions, which the PostgreSQL documentation approximates as:

max_locks_per_transaction × (max_connections + max_prepared_transactions)

Read the figures of your service:

SELECT current_setting('max_locks_per_transaction')::int AS locks_per_xact,
       current_setting('max_connections')::int           AS max_connections,
       current_setting('max_prepared_transactions')::int AS prepared_xacts,
       current_setting('max_locks_per_transaction')::int
         * (current_setting('max_connections')::int
            + current_setting('max_prepared_transactions')::int) AS approx_lock_entries,
       (SELECT count(*) FROM pg_locks) AS locks_held_now;

Two consequences explain most incidents:

  • The budget is shared. max_locks_per_transaction is not a per-transaction cap: one transaction can take far more locks than that as long as the others leave room. A batch job can succeed every night and fail the night it overlaps with another lock-heavy job.
  • The formula is a lower bound. The table also has room for background processes and can use some spare shared memory, so a transaction can hold more locks than approx_lock_entries before the error appears. The exact point depends on what else is running, which is why the error comes without warning.

Workloads that take many locks:

  • Partitioned tables. A query that the planner cannot narrow down to a few partitions locks every partition and every index of every partition. 4000 partitions with one index each are 8000 locks for a single SELECT count(*). Even EXPLAIN of such a query takes the locks.
  • Schema-wide operations. pg_dump locks every table it dumps, in one transaction.
  • Large DDL transactions. A migration that creates, alters or truncates hundreds of tables in one transaction keeps all those locks until it commits.
  • Sessions left idle in transaction. A transaction keeps its locks until it ends, for example when the application opened a transaction and the code path that should have committed it never ran.

Find What Holds the Locks

Locks per backend, with the transaction age and the statement:

SELECT l.pid,
       count(*)                                        AS locks,
       count(*) FILTER (WHERE l.locktype = 'relation') AS relation_locks,
       a.usename,
       a.state,
       now() - a.xact_start                            AS xact_age,
       left(a.query, 80)                               AS query
FROM pg_locks l
LEFT JOIN pg_stat_activity a ON a.pid = l.pid
GROUP BY l.pid, a.usename, a.state, a.xact_start, a.query
ORDER BY locks DESC
LIMIT 20;

The relations one backend has locked, to confirm that partitions are the cause:

SELECT l.relation::regclass AS relation, l.mode, l.granted
FROM pg_locks l
WHERE l.pid = PID
  AND l.locktype = 'relation'
ORDER BY 1;

Parameters:

  • PID: the backend from the previous query.

Sessions sitting in an open transaction:

SELECT pid, usename, application_name,
       now() - xact_start AS xact_age,
       left(query, 80)    AS last_query
FROM pg_stat_activity
WHERE state = 'idle in transaction'
ORDER BY xact_start
LIMIT 20;

avnadmin is a member of pg_signal_backend and can end a session of another database user. This rolls back its transaction and releases its locks:

SELECT pg_terminate_backend(PID);

Raise max_locks_per_transaction

exo dbaas update SERVICE_NAME -z ZONE -f \
  --pg-settings='{"max_locks_per_transaction":256}'

Parameters:

  • max_locks_per_transaction: integer from 64 to 6400. A new service uses 64.
  • -f: apply without the confirmation prompt. Without it, nothing is applied in a script.

Warning

Changing max_locks_per_transaction restarts the service: every connection, direct or pooled, is dropped and the service refuses connections for a short time. Apply it in a window you control.

Check the value in force:

SHOW max_locks_per_transaction;

How to size it:

  • Start from the largest number of relation locks one of your transactions needs, the relation_locks figure above, and divide by the number of connections that may run such a transaction at the same time. Doubling the current value is a reasonable first step.
  • max_connections comes with the plan and is not a setting. Going from 64 to 256 on a service with max_connections 100 raises the approximate lock table from 6400 to 25600 entries.
  • Each entry takes a small amount of shared memory. Do not raise the value far beyond what you need.

SERIALIZABLE transactions use a separate table sized by max_pred_locks_per_transaction, 64 to 5120, which also restarts the service when changed. If the HINT names that parameter, raise that one instead.

Take Fewer Locks

Let the planner prune partitions. A condition on the partition key with a constant value lets the planner keep only the matching partitions, which are the only ones locked. A condition it cannot evaluate while planning, such as one that depends on now() or current_date, makes it plan and lock every partition. Compare with EXPLAIN:

EXPLAIN (COSTS OFF)
SELECT count(*) FROM measurements WHERE recorded_at >= DATE '2026-07-01';

If the plan lists every partition, the query locks every partition.

Keep the number of partitions reasonable. Detach and archive old partitions instead of keeping thousands attached. pg_partman can automate it, see Use pg_partman.

Commit large DDL in batches. Locks are released at commit.

Close idle transactions automatically with a service-wide timeout, which applies without a restart:

exo dbaas update SERVICE_NAME -z ZONE -f \
  --pg-settings='{"idle_in_transaction_session_timeout":300000}'

Parameters:

  • idle_in_transaction_session_timeout: milliseconds a session may stay idle in an open transaction, 0 (disabled) to 604800000. A new service uses 86400000, 24 hours. 300000 is five minutes.

It can also be set per role or database, for example ALTER ROLE batch_worker SET idle_in_transaction_session_timeout = '5min';. See Manage long-running queries.

Related Pages

Last updated on