Systemd could not open shared memory segment

Ellie Huxtable

Systemd 无法打开共享内存段

我在 EC2 实例上运行 PostgreSQL 有一段时间了。我们遇到过一个奇怪的问题:有时查询会失败,并报错

could not open shared memory segment "/PostgreSQL.271741757"

对于非系统用户(UID < 1000),logind 默认会在注销时清除共享内存文件。对于数据库这类应用来说,这完全不是我们想要的行为。需要注意的是,如果你从不以数据库用户的身份登录,这种情况就不太可能发生。

/etc/systemd/logind.conf 中设置 RemoveIPC=no 应该可以解决这个问题。理论上,你可以在系统运行时重启 logind。我做了一些测试,但我觉得最稳妥的做法可能还是重启系统。

手册页:

RemoveIPC=
           Controls whether System V and POSIX IPC objects belonging to
           the user shall be removed when the user fully logs out. Takes
           a boolean argument. If enabled, the user may not consume IPC
           resources after the last of the user's sessions terminated.
           This covers System V semaphores, shared memory and message
           queues, as well as POSIX shared memory and message queues.
           Note that IPC objects of the root user and other system users
           are excluded from the effect of this setting. Defaults to
           "yes".

修改之后,完整重启一次系统即可确保配置被加载。另外,重启 logind 也应该能解决这个问题:systemctl restart systemd-logind.service

有用的链接:

  • https://github.com/systemd/systemd/issues/4532
  • https://github.com/systemd/systemd/issues/2039
  • https://stackoverflow.com/questions/49065733/redhat-or-centos7-systemd-may-remove-user-ipc-resources-unexpectedly
  • https://stackoverflow.com/questions/73368759/why-systemd-remove-my-shm-file-but-not-postgresqls

原文由 Ellie Huxtable 发布

本文章由 stealth/ox-alpha 进行翻译