PostgreSQL runs out of shared memory when the lock table exceeds capacity, typically due to excessive table locking or high concurrency. Increase max_locks_per_transaction or adjust connection settings to resolve.
The "out of shared memory" error occurs when PostgreSQL exhausts its fixed-size shared memory area used for tracking locks on database objects. PostgreSQL allocates shared memory for locks based on the formula: max_locks_per_transaction * (max_connections + max_prepared_transactions). This is not about OS-level shared memory configuration, but PostgreSQL's internal lock management space. Once this limit is exceeded, any attempt to lock additional objects fails with this error. Common scenarios include touching thousands of tables in a single transaction, holding many concurrent connections with heavy locking patterns, or creating temporary tables repeatedly within one transaction. Row-level locks are stored on disk and don't consume this shared memory, so the issue is specifically about table-level and object-level locks.
Connect to PostgreSQL and run:
SHOW max_locks_per_transaction;
SHOW max_connections;
SHOW max_prepared_transactions;Calculate total lock capacity: max_locks_per_transaction * (max_connections + max_prepared_transactions). If this is exhausted relative to your workload, proceed to the next step.
Check for idle transactions that may be holding locks:
SELECT pid, usename, application_name, state, state_change
FROM pg_stat_activity
WHERE state = 'idle in transaction'
AND state_change < NOW() - INTERVAL '10 minutes';Terminate these if safe:
SELECT pg_terminate_backend(pid) FROM pg_stat_activity
WHERE pid <> pg_backend_pid() AND state = 'idle in transaction'
AND state_change < NOW() - INTERVAL '10 minutes';Edit your PostgreSQL configuration file (usually /etc/postgresql/*/main/postgresql.conf on Linux):
sudo vi /etc/postgresql/15/main/postgresql.confFind the line with max_locks_per_transaction (or add it if missing) and increase it:
max_locks_per_transaction = 256Start with 256, then increase further if needed (256, 512, 1024). Reload the configuration:
sudo systemctl reload postgresqlVerify the change:
SHOW max_locks_per_transaction;If your application has long-running analytical queries, mark them explicitly as read-only to avoid unnecessary lock tracking:
BEGIN READ ONLY;
-- Your SELECT queries here
COMMIT;Or in application code, use:
SET TRANSACTION READ ONLY;This prevents PostgreSQL from accumulating serialization read locks for transactions that don't modify data.
If max_connections is set very high (hundreds or thousands), consider lowering it:
sudo vi /etc/postgresql/15/main/postgresql.confFind and adjust:
max_connections = 100Instead of increasing raw connections, use a connection pooler like PgBouncer:
sudo apt-get install pgbouncerConfigure PgBouncer to pool connections to PostgreSQL, reducing the actual server-side connection overhead. Then reload PostgreSQL:
sudo systemctl reload postgresqlFor databases with thousands of tables or heavy partitioning, consider database sharding or splitting schemas across multiple PostgreSQL instances. The lock table formula max_locks_per_transaction * (max_connections + max_prepared_transactions) is fixed at startup, so tuning these parameters requires a service restart. On Unix-like systems, use ipcs -m to verify the operating system shared memory configuration isn't the limiting factor. Most "out of shared memory" errors are PostgreSQL lock table exhaustion, not OS kernel shared memory limits. If you have many temporary tables created in loops, consider materialized views or permanent staging tables instead. For managed PostgreSQL services (Heroku, AWS RDS, Azure), you may need to upgrade to a plan with higher lock limits or higher max_connections allocation.
insufficient columns in unique constraint for partition key
How to fix "insufficient columns in unique constraint for partition key" in PostgreSQL
ERROR 42501: must be owner of table
How to fix "must be owner of table" in PostgreSQL
trigger cannot change partition destination
How to fix "Trigger cannot change partition destination" in PostgreSQL
SSL error: certificate does not match host name
SSL error: certificate does not match host name in PostgreSQL
No SSL connection
No SSL connection to PostgreSQL