↑↓ 选择↵ 打开⌫ 切换范围完整搜索

PG.CENTER 连接 PostgreSQL 文档、百科与生态知识。由 Pigsty 维护。

Wiki / SQLSTATE

F0001 — lock_file_exists

数据目录的锁文件已存在,实例可能已在运行。

英文手册覆盖始于 PostgreSQL 8.1. 来源历史与运行验证各自保留独立版本边界。

当前阅读 PG 18·选择有来源记录的版本

aliases
未知
class name
Configuration File Error
condition name
lock_file_exists
lang
en
sqlstate
F0001

版本定义 PG 18

F0001

速览 {#at-a-glance}

F0001 是启动时的所有权冲突。PostgreSQL 同时检查目标数据目录的 lock file 和共享内存身份;活跃所有者与崩溃留下的残留物是不同情况。

含义 {#meaning}

CreateLockFile 用 O_EXCL 原子创建锁文件。已有文件若包含 PID,会用 kill(pid, 0) 区分进程仍活跃还是已经消失;数据目录锁文件还会读取记录的共享内存标识并调用 PGSharedMemoryIsInUse。该辅助函数只把与目标 DataDir 关联的共享内存段视为相关,因为共享内存 ID 可能偶然重合。空锁文件是独立的 F0001 分支,可能表示另一个服务器正在启动,也可能是上次启动崩溃的残留。

SysV 共享内存启动路径也有同样边界:仍有进程附着或分析结果不确定的既有共享内存段报告仍在使用;与数据目录相关但无进程附着的共享内存段才可清理后重建。这些 guard 防止两个 postmaster 认领同一集群。

消息 {#messages}

  • FATAL,SQLSTATE F0001:lock file "%s" already exists。
    • 数据目录提示:Is another postgres (PID %d) running in data directory "%s"?(编码所有者为 postmaster 时会使用对应提示文案)。
    • socket lock 变体会写 using socket file "%s"。
  • FATAL,SQLSTATE F0001:lock file "%s" is empty。
    • 提示:Either another server is starting, or the lock file is the remnant of a previous server startup crash.
  • FATAL,SQLSTATE F0001:pre-existing shared memory block (key %lu, ID %lu) is still in use。
    • 提示:Terminate any old server processes associated with data directory "%s".

其他锁文件 I/O 失败使用 errcode_for_file_access(),可能得到不同 SQLSTATE,不能都归入 F0001。

诊断 {#diagnosis}

确认精确 PGDATA、socket 目录、PID、启动时间、记录的 device/inode 身份、进程所有者和 postmaster 日志。kill(pid, 0) 成功,或失败 errno 既不是 ESRCH(进程不存在)也不是 EPERM(进程存在但调用者无权发送信号),会保持活跃进程分支。guard 将 EPERM 排除在活跃进程分支之外,是因为 checkDataDir() 以及 0600/0640 锁文件权限足以排除不同 UID 的进程作为竞争 postmaster;它只允许继续做身份和共享内存检查,并不授权直接删除锁文件。PID 消失或被视为不同 UID 候选后,还要检查数据目录共享内存 ID 及是否仍有孤儿后端附着,再考虑清理。空锁文件在源码中明确是歧义状态,不代表一定残留。

处理 {#response}

这是启动阶段的 FATAL,没有客户端事务可恢复;解决所有权冲突后启动服务器,再使用新连接。若另有 postmaster/后端存活,应按正常管理流程协调或停止。只有确认集群、进程身份和共享内存状态后,才能按文档处理陈旧锁文件。绝不能仅凭路径、PID 文本或数字 ID 删除锁文件/共享内存;所有者活跃时删除可能造成双主冲突(两个 postmaster 认领同一集群)和数据丢失。

版本 {#versions}

锁定目录从 7.4 记录此条件;固定启动与共享内存路径来自 PostgreSQL 18.6。本源码条目没有诱发进程、lock-file 或共享内存故障。

相关 {#related}

F0000、57P03、58P01

来源 {#sources}

src/backend/utils/init/miscinit.c#L1262-L1427

src/backend/port/sysv_shmem.c#L306-L335

src/backend/port/sysv_shmem.c#L765-L835

结构化证据记录保存活动/陈旧检查、固定 FATAL 消息和源码/运行边界。

比较版本

SQLSTATE 证据与已记录的运行版本

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read locked definition snapshot.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "F0001 is lock_file_exists in SQLSTATE Class F0."
}
claims · resolved-paths
{
  "claim_id": "resolved-paths",
  "limits": "Source confirmation is not natural runtime.",
  "method": "Read complete PID, shared-memory identity, and startup cleanup guards.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.0",
    "src.path.1"
  ],
  "statement": "Startup distinguishes a live lock owner, an empty or stale lock, and shared memory associated with the intended data directory; only the live/in-use branches emit the fixed F0001 FATAL messages."
}
claims · history
{
  "claim_id": "history",
  "limits": "",
  "method": "Use locked catalogue boundary.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked catalogue records F0001 from 7.4; it does not prove the exact introduction rationale."
}
messages · message.0
{
  "limits": "",
  "message_id": "message.0",
  "path": "",
  "position": 0,
  "raw": {
    "hint_template": "Is another postgres (PID %d) running in data directory \"%s\"?",
    "hint_variants": [
      "Is another postmaster (PID %d) running in data directory \"%s\"?",
      "Is another postgres (PID %d) using socket file \"%s\"?",
      "Is another postmaster (PID %d) using socket file \"%s\"?"
    ],
    "id": "message.0",
    "primary_template": "lock file \"%s\" already exists",
    "severity_source": "FATAL",
    "sources": [
      "src.path.0"
    ],
    "sqlstate": "F0001"
  },
  "severity": "FATAL",
  "sources": [
    "src.path.0"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "lock file \" \" already exists",
      "position": 0,
      "role": "",
      "template": "lock file \"%s\" already exists"
    },
    {
      "kind": "hint",
      "literal": "Is another postgres (PID ) running in data directory \" \"?",
      "position": 1,
      "role": "",
      "template": "Is another postgres (PID %d) running in data directory \"%s\"?"
    },
    {
      "kind": "hint",
      "literal": "Is another postmaster (PID ) running in data directory \" \"?",
      "position": 2,
      "role": "",
      "template": "Is another postmaster (PID %d) running in data directory \"%s\"?"
    },
    {
      "kind": "hint",
      "literal": "Is another postgres (PID ) using socket file \" \"?",
      "position": 3,
      "role": "",
      "template": "Is another postgres (PID %d) using socket file \"%s\"?"
    },
    {
      "kind": "hint",
      "literal": "Is another postmaster (PID ) using socket file \" \"?",
      "position": 4,
      "role": "",
      "template": "Is another postmaster (PID %d) using socket file \"%s\"?"
    }
  ]
}
messages · message.1
{
  "limits": "",
  "message_id": "message.1",
  "path": "",
  "position": 1,
  "raw": {
    "hint_template": "Either another server is starting, or the lock file is the remnant of a previous server startup crash.",
    "id": "message.1",
    "primary_template": "lock file \"%s\" is empty",
    "severity_source": "FATAL",
    "sources": [
      "src.path.0"
    ],
    "sqlstate": "F0001"
  },
  "severity": "FATAL",
  "sources": [
    "src.path.0"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "lock file \" \" is empty",
      "position": 0,
      "role": "",
      "template": "lock file \"%s\" is empty"
    },
    {
      "kind": "hint",
      "literal": "Either another server is starting, or the lock file is the remnant of a previous server startup crash.",
      "position": 1,
      "role": "",
      "template": "Either another server is starting, or the lock file is the remnant of a previous server startup crash."
    }
  ]
}
messages · message.2
{
  "limits": "",
  "message_id": "message.2",
  "path": "",
  "position": 2,
  "raw": {
    "hint_template": "Terminate any old server processes associated with data directory \"%s\".",
    "id": "message.2",
    "primary_template": "pre-existing shared memory block (key %lu, ID %lu) is still in use",
    "severity_source": "FATAL",
    "sources": [
      "src.path.0",
      "src.path.1"
    ],
    "sqlstate": "F0001"
  },
  "severity": "FATAL",
  "sources": [
    "src.path.0",
    "src.path.1"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "pre-existing shared memory block (key u, ID u) is still in use",
      "position": 0,
      "role": "",
      "template": "pre-existing shared memory block (key %lu, ID %lu) is still in use"
    },
    {
      "kind": "hint",
      "literal": "Terminate any old server processes associated with data directory \" \".",
      "position": 1,
      "role": "",
      "template": "Terminate any old server processes associated with data directory \"%s\"."
    }
  ]
}
sources · src.errcodes.18.6
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/errcodes.txt",
  "position": 0,
  "sha256": "6e8de346643ba84aa3c9c6a73360acfc7b2dfb89162c06c08ce9bf5bcd5bbcba",
  "source_id": "src.errcodes.18.6",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/errcodes.txt#L454"
}
sources · src.path.0
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/utils/init/miscinit.c",
  "position": 1,
  "sha256": "54bdb859c143a6c70d86067ccf2e124f59aa0852e5705bb653652e41a60af39a",
  "source_id": "src.path.0",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/utils/init/miscinit.c#L1262"
}
sources · src.path.1
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/port/sysv_shmem.c",
  "position": 2,
  "sha256": "150a1143ebffbc726243cb1925109b3237173100814633ee3822f4470ca488de",
  "source_id": "src.path.1",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/port/sysv_shmem.c#L306"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:f648381640bf00f42cebe299bfbb2e4648eefa1b71e1c1193cbdb6dd556963ae

正文语言: zh-Hans · english-manuals:f648381640bf00f42cebe299bfbb2e4648eefa1b71e1c1193cbdb6dd556963ae