PostgreSQL error: out of shared memory

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

Despite the alarming wording, this is almost never about the server running out of memory in general. When it comes with the max_locks_per_transaction hint, it means one specific thing: the shared lock table — the fixed-size structure where PostgreSQL tracks locks on tables, indexes and other objects — is full.

What this error means

The lock table is sized at server start, with room for roughly max_locks_per_transaction × (max_connections + max_prepared_transactions) object locks. Two properties explain everything you'll observe:

Every table, partition and index touched by a query needs its own lock entry, held until the end of the transaction. That's the arithmetic that gets you: a query over a table with 2,000 partitions and a few indexes each can need many thousands of entries — while the default sizing (64 × ~100 connections) gives you roughly 6,400 for the whole server.

Common causes

How to diagnose it

While the failing workload runs (or right before it fails):

-- Quanto è pieno il lock table:
SELECT count(*) FROM pg_locks;

-- Chi sta consumando le entry:
SELECT pid, count(*)
FROM pg_locks
GROUP BY pid
ORDER BY count(*) DESC
LIMIT 10;

Compare the total against the formula above with your settings. If one pid holds thousands of locks, look at what it's running — and if partitions are involved, count them: SELECT count(*) FROM pg_inherits WHERE inhparent = 'events'::regclass;

How to fix it

🔍 Related reading: How to read EXPLAIN ANALYZE — the plan shows you whether partition pruning actually happened.