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

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

Wiki / SQLSTATE

2201W — invalid_row_count_in_limit_clause

LIMIT 行数无效,不能为负。

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

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

aliases
未知
class name
Data Exception
condition name
invalid_row_count_in_limit_clause
lang
en
sqlstate
2201W

版本定义 PG 18

2201W

速览 {#at-a-glance}

LIMIT 或 FETCH 行数触发了特定形式的守卫。固定路径报告 LIMIT must not be negative;解析器另行拒绝 FETCH FIRST ... WITH TIES 中字面 NULL 行数。

代表性报文 {#messages}

代表性守卫使用以下首要文本:

| 路径 | 首要文本 | | --- | --- | | 普通 LIMIT 的负 count | LIMIT must not be negative | | FETCH FIRST ... WITH TIES 中的字面 NULL | row count cannot be null in FETCH FIRST ... WITH TIES clause |

含义 {#meaning}

执行器计算普通 LIMIT count 时,NULL 被解释为没有 count(LIMIT ALL),零值合法并返回零行,负值才报告 2201W。解析器另有 FETCH FIRST ... WITH TIES 裸 NULL 常量守卫;这不是所有可为空 LIMIT 表达式都采用的统一规则。OFFSET 由 2201X 处理。

诊断 {#diagnosis}

先确认报文来自普通 LIMIT 求值还是 WITH TIES 解析规则,再检查表达式实际值和类型。区分无 ties 时的 NULL、with ties 的字面 NULL、零值和负值。源码的 A_Const 检查范围很窄,隐藏在表达式中的 NULL 可能通过解析器,因此应依据实际语句和报文分类。

处理 {#response}

普通 LIMIT 若要不限行可使用 NULL,否则提供非负 count;WITH TIES 应提供该语法接受的非 NULL 行数。如果负 count 或解析器 ERROR 发生在显式事务中,应先 ROLLBACK 或回滚到既有保存点再重试;自动提交可重试修正后的语句。

版本 {#versions}

锁定目录从 8.4.0 起记录该条件;本页固定源码路径为 PostgreSQL 18.6。未声称本页有自然运行观察。

相关 {#related}

2201X、22012

来源 {#sources}

执行器对 NULL/零值/负值的处理在 src/backend/executor/nodeLimit.c#L347-405;字面 WITH TIES NULL 守卫在 src/backend/parser/parse_clause.c#L1890-1907。结构化证据记录保留两种首要角色;本页未运行自然案例。

比较版本

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

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed errcodes row and macro.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "2201W is invalid_row_count_in_limit_clause in SQLSTATE Class 22"
}
claims · resolved-path
{
  "claim_id": "resolved-path",
  "limits": "Source confirmation is not a natural runtime observation.",
  "method": "Read complete source contexts for the representative guards.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.nodeLimit.limit",
    "src.parse_clause.with_ties"
  ],
  "statement": "Fixed PostgreSQL 18.6 executor and parser paths distinguish ordinary LIMIT NULL/zero/negative values from the narrow literal NULL WITH TIES guard."
}
claims · history
{
  "claim_id": "history",
  "limits": "",
  "method": "Use catalogue presence and history boundaries.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "The locked catalogue records 2201W from 8.4.0 without proving an exact implementation introduction date."
}
messages · message.limit-negative
{
  "limits": "",
  "message_id": "message.limit-negative",
  "path": "",
  "position": 0,
  "raw": {
    "id": "message.limit-negative",
    "primary_template": "LIMIT must not be negative",
    "severity_source": "ERROR",
    "sources": [
      "src.nodeLimit.limit"
    ],
    "sqlstate": "2201W"
  },
  "severity": "ERROR",
  "sources": [
    "src.nodeLimit.limit"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "LIMIT must not be negative",
      "position": 0,
      "role": "",
      "template": "LIMIT must not be negative"
    }
  ]
}
messages · message.with-ties-null
{
  "limits": "",
  "message_id": "message.with-ties-null",
  "path": "",
  "position": 1,
  "raw": {
    "id": "message.with-ties-null",
    "primary_template": "row count cannot be null in FETCH FIRST ... WITH TIES clause",
    "severity_source": "ERROR",
    "sources": [
      "src.parse_clause.with_ties"
    ],
    "sqlstate": "2201W"
  },
  "severity": "ERROR",
  "sources": [
    "src.parse_clause.with_ties"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "row count cannot be null in FETCH FIRST ... WITH TIES clause",
      "position": 0,
      "role": "",
      "template": "row count cannot be null in FETCH FIRST ... WITH TIES clause"
    }
  ]
}
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#L188"
}
sources · src.nodeLimit.limit
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/executor/nodeLimit.c",
  "position": 1,
  "sha256": "f4c202d7182d66c6bef222fa50439831691dd99bfa5fd6b4de7f9142635dc305",
  "source_id": "src.nodeLimit.limit",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/executor/nodeLimit.c#L347"
}
sources · src.parse_clause.with_ties
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/parser/parse_clause.c",
  "position": 2,
  "sha256": "fd3fb69d6ea2ed0c220eba1d4479c0b79d8713b39a28c62cc4bf1428491e8ebd",
  "source_id": "src.parse_clause.with_ties",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/parser/parse_clause.c#L1890"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:9d84276424ed0f558b65ebfa5004431a9daeb46776fb838831a06eb86e41ef68

正文语言: zh-Hans · english-manuals:9d84276424ed0f558b65ebfa5004431a9daeb46776fb838831a06eb86e41ef68