{
  "id": "database/postgresql-too-many-snapshots",
  "signature": "psycopg2.OperationalError: ERROR: out of shared memory HINT: You might need to increase max_locks_per_transaction.",
  "signature_zh": "psycopg2.OperationalError：错误：共享内存不足 提示：您可能需要增加 max_locks_per_transaction。",
  "regex": "out of shared memory",
  "domain": "database",
  "category": "resource_error",
  "subcategory": null,
  "root_cause": "PostgreSQL's shared memory for lock management is exhausted, typically because too many concurrent transactions hold locks or snapshots, exceeding the max_locks_per_transaction * max_connections limit.",
  "root_cause_type": "generic",
  "root_cause_zh": "PostgreSQL 用于锁管理的共享内存耗尽，通常是因为太多并发事务持有锁或快照，超过了 max_locks_per_transaction * max_connections 的限制。",
  "versions": [
    {
      "version": "PostgreSQL 14",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PostgreSQL 15",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    },
    {
      "version": "PostgreSQL 16",
      "introduced": null,
      "deprecated": null,
      "removed": null,
      "behavior_change": null,
      "status": "active"
    }
  ],
  "os_specific": {},
  "dead_ends": [
    {
      "action": "Restart PostgreSQL to clear the shared memory, assuming it's a temporary leak",
      "why_fails": "If the workload pattern (e.g., many concurrent long-running transactions) remains the same, the error will recur quickly after restart.",
      "fail_rate": 0.85,
      "condition": "",
      "sources": []
    },
    {
      "action": "Increase max_connections instead of max_locks_per_transaction",
      "why_fails": "max_connections increases the number of possible connections but does not increase the lock space per connection; the shared memory limit is still bounded by max_locks_per_transaction * max_connections.",
      "fail_rate": 0.75,
      "condition": "",
      "sources": []
    }
  ],
  "workarounds": [
    {
      "action": "Increase max_locks_per_transaction in postgresql.conf (e.g., to 128 or 256), then restart PostgreSQL. Verify with SHOW max_locks_per_transaction;. Also consider reducing the number of concurrent long-running transactions.",
      "success_rate": 0.85,
      "how": "Increase max_locks_per_transaction in postgresql.conf (e.g., to 128 or 256), then restart PostgreSQL. Verify with SHOW max_locks_per_transaction;. Also consider reducing the number of concurrent long-running transactions.",
      "condition": "",
      "sources": []
    },
    {
      "action": "Identify and terminate idle-in-transaction sessions that hold locks: SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND xact_start < now() - interval '10 minutes';",
      "success_rate": 0.8,
      "how": "Identify and terminate idle-in-transaction sessions that hold locks: SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND xact_start < now() - interval '10 minutes';",
      "condition": "",
      "sources": []
    }
  ],
  "workarounds_zh": [
    "Increase max_locks_per_transaction in postgresql.conf (e.g., to 128 or 256), then restart PostgreSQL. Verify with SHOW max_locks_per_transaction;. Also consider reducing the number of concurrent long-running transactions.",
    "Identify and terminate idle-in-transaction sessions that hold locks: SELECT pg_terminate_backend(pid) FROM pg_stat_activity WHERE state = 'idle in transaction' AND xact_start < now() - interval '10 minutes';"
  ],
  "transition_graph": {
    "leads_to": [],
    "preceded_by": [],
    "frequently_confused_with": []
  },
  "official_doc_url": "https://www.postgresql.org/docs/16/runtime-config-resource.html#GUC-MAX-LOCKS-PER-TRANSACTION",
  "official_doc_section": null,
  "error_code": null,
  "verification_tier": "ai_generated",
  "confidence": 0.89,
  "fix_success_rate": 0.78,
  "resolvable": "true",
  "first_seen": "2024-04-18",
  "last_confirmed": "2024-06-01",
  "last_updated": "2024-06-01",
  "evidence_count": 1,
  "tags": [],
  "locale": "en",
  "aliases": []
}