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

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

Wiki / SQLSTATE

39P01 — trigger_protocol_violated

触发器违反了返回值或调用协议。

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

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

aliases
未知
class name
External Routine Invocation Exception
condition name
trigger_protocol_violated
lang
en
sqlstate
39P01

版本定义 PG 18

39P01

速览 {#at-a-glance}

39P01 是 trigger_protocol_violated。固定源码确认它同时出现在核心 trigger manager、contrib/tcn 以及 PL/Perl trigger 返回值检查中,消息和严重级别取决于具体路径。

含义 {#meaning}

这是 trigger 调用契约不成立:核心管理器可能报告 trigger function %u returned null value 或 BEFORE STATEMENT trigger cannot return a value;tcn 函数在错误的 timing/level/参数条件下被调用,或表没有主键;PL/Perl DELETE trigger 的修改可能被 WARNING 忽略,非法返回值则是 ERROR。

诊断 {#diagnosis}

先按 message 区分来源:核心管理器检查 isnull 标志和 BEFORE STATEMENT 返回契约;tcn 要核对 AFTER、FOR EACH ROW、参数个数和主键;PL/Perl 要核对返回是否为 undef、SKIP 或 MODIFY。DELETE 修改被忽略的报文是 WARNING,非法返回报文是 ERROR;不要把所有 39P01 都归为同一个 trigger 类型。

处置 {#response}

修正 trigger 的 timing、level、事件、参数和返回值契约;对 tcn 还要核对表主键和调用顺序。PL/Perl 的 WARNING 表示 DELETE 行修改被忽略,非法返回值则使调用失败;先确认原操作是否完成和业务是否接受该结果,再决定后续写入。修复契约后才重新执行。

版本 {#versions}

锁定目录从 7.4 记录该条件,并在正式快照及 19beta3 中出现;源码路径取 PostgreSQL 18.6。

相关条件 {#related}

39P02, 39P03, 2F005

来源 {#sources}

可直接阅读固定的 trigger.c、tcn.c 和 plperl.c 路径;完整范围见结构化证据记录。

比较版本

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

claims · identity
{
  "claim_id": "identity",
  "limits": "",
  "method": "Read the fixed definition row.",
  "position": 0,
  "runtime": [],
  "sources": [
    "src.errcodes.18.6"
  ],
  "statement": "39P01 is trigger_protocol_violated in Class 39."
}
claims · core-trigger
{
  "claim_id": "core-trigger",
  "limits": "",
  "method": "Read each fixed trigger-manager context and its message.",
  "position": 1,
  "runtime": [],
  "sources": [
    "src.path.trigger"
  ],
  "statement": "The core trigger manager reports this code when a trigger function sets the isnull flag, and for invalid BEFORE STATEMENT return values."
}
claims · tcn-contract
{
  "claim_id": "tcn-contract",
  "limits": "",
  "method": "Read the complete tcn checks around each report.",
  "position": 2,
  "runtime": [],
  "sources": [
    "src.path.tcn"
  ],
  "statement": "contrib/tcn checks trigger invocation context, AFTER timing, row level, argument count, and primary-key setup with this code."
}
claims · language-severity
{
  "claim_id": "language-severity",
  "limits": "",
  "method": "Compare ereport levels and exact messages.",
  "position": 3,
  "runtime": [],
  "sources": [
    "src.path.plperl"
  ],
  "statement": "PL/Perl has a WARNING variant for an ignored DELETE-row modification and an ERROR variant for an invalid trigger return value."
}
messages · message.trigger-null
{
  "limits": "",
  "message_id": "message.trigger-null",
  "path": "",
  "position": 0,
  "raw": {
    "id": "message.trigger-null",
    "primary_template": "trigger function %u returned null value",
    "severity_source": "ERROR",
    "sources": [
      "src.path.trigger"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.trigger"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "trigger function returned null value",
      "position": 0,
      "role": "",
      "template": "trigger function %u returned null value"
    }
  ]
}
messages · message.before-statement
{
  "limits": "",
  "message_id": "message.before-statement",
  "path": "",
  "position": 1,
  "raw": {
    "id": "message.before-statement",
    "primary_template": "BEFORE STATEMENT trigger cannot return a value",
    "severity_source": "ERROR",
    "sources": [
      "src.path.trigger"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.trigger"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "BEFORE STATEMENT trigger cannot return a value",
      "position": 0,
      "role": "",
      "template": "BEFORE STATEMENT trigger cannot return a value"
    }
  ]
}
messages · message.tcn-called-as-trigger
{
  "limits": "",
  "message_id": "message.tcn-called-as-trigger",
  "path": "",
  "position": 2,
  "raw": {
    "id": "message.tcn-called-as-trigger",
    "primary_template": "triggered_change_notification: must be called as trigger",
    "severity_source": "ERROR",
    "sources": [
      "src.path.tcn"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.tcn"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "triggered_change_notification: must be called as trigger",
      "position": 0,
      "role": "",
      "template": "triggered_change_notification: must be called as trigger"
    }
  ]
}
messages · message.tcn-after-change
{
  "limits": "",
  "message_id": "message.tcn-after-change",
  "path": "",
  "position": 3,
  "raw": {
    "id": "message.tcn-after-change",
    "primary_template": "triggered_change_notification: must be called after the change",
    "severity_source": "ERROR",
    "sources": [
      "src.path.tcn"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.tcn"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "triggered_change_notification: must be called after the change",
      "position": 0,
      "role": "",
      "template": "triggered_change_notification: must be called after the change"
    }
  ]
}
messages · message.tcn-each-row
{
  "limits": "",
  "message_id": "message.tcn-each-row",
  "path": "",
  "position": 4,
  "raw": {
    "id": "message.tcn-each-row",
    "primary_template": "triggered_change_notification: must be called for each row",
    "severity_source": "ERROR",
    "sources": [
      "src.path.tcn"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.tcn"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "triggered_change_notification: must be called for each row",
      "position": 0,
      "role": "",
      "template": "triggered_change_notification: must be called for each row"
    }
  ]
}
messages · message.tcn-one-parameter
{
  "limits": "",
  "message_id": "message.tcn-one-parameter",
  "path": "",
  "position": 5,
  "raw": {
    "id": "message.tcn-one-parameter",
    "primary_template": "triggered_change_notification: must not be called with more than one parameter",
    "severity_source": "ERROR",
    "sources": [
      "src.path.tcn"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.tcn"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "triggered_change_notification: must not be called with more than one parameter",
      "position": 0,
      "role": "",
      "template": "triggered_change_notification: must not be called with more than one parameter"
    }
  ]
}
messages · message.tcn-primary-key
{
  "limits": "",
  "message_id": "message.tcn-primary-key",
  "path": "",
  "position": 6,
  "raw": {
    "id": "message.tcn-primary-key",
    "primary_template": "triggered_change_notification: must be called on a table with a primary key",
    "severity_source": "ERROR",
    "sources": [
      "src.path.tcn"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.tcn"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "triggered_change_notification: must be called on a table with a primary key",
      "position": 0,
      "role": "",
      "template": "triggered_change_notification: must be called on a table with a primary key"
    }
  ]
}
messages · message.plperl-delete-warning
{
  "limits": "",
  "message_id": "message.plperl-delete-warning",
  "path": "",
  "position": 7,
  "raw": {
    "id": "message.plperl-delete-warning",
    "primary_template": "ignoring modified row in DELETE trigger",
    "severity_source": "WARNING",
    "sources": [
      "src.path.plperl"
    ],
    "sqlstate": "39P01"
  },
  "severity": "WARNING",
  "sources": [
    "src.path.plperl"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "ignoring modified row in DELETE trigger",
      "position": 0,
      "role": "",
      "template": "ignoring modified row in DELETE trigger"
    }
  ]
}
messages · message.plperl-invalid-return
{
  "limits": "",
  "message_id": "message.plperl-invalid-return",
  "path": "",
  "position": 8,
  "raw": {
    "id": "message.plperl-invalid-return",
    "primary_template": "result of PL/Perl trigger function must be undef, \"SKIP\", or \"MODIFY\"",
    "severity_source": "ERROR",
    "sources": [
      "src.path.plperl"
    ],
    "sqlstate": "39P01"
  },
  "severity": "ERROR",
  "sources": [
    "src.path.plperl"
  ],
  "templates": [
    {
      "kind": "primary",
      "literal": "result of PL/Perl trigger function must be undef, \"SKIP\", or \"MODIFY\"",
      "position": 0,
      "role": "",
      "template": "result of PL/Perl trigger function must be undef, \"SKIP\", or \"MODIFY\""
    }
  ]
}
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#L311"
}
sources · src.calls.REL_18_6
{
  "commit": "",
  "docs_url": "",
  "kind": "local_artifact",
  "location": "",
  "path": "raw/calls/REL_18_6.jsonl",
  "position": 1,
  "sha256": "9ee8a0e81d8f0825c5c1ae45583439859a26e602bdd4ce2f2a62aa278867ccbf",
  "source_id": "src.calls.REL_18_6",
  "tag": "",
  "url": ""
}
sources · src.path.trigger
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/backend/commands/trigger.c",
  "position": 2,
  "sha256": "0a539af85b0de1a04779f92202e7bfc77d85ae6da1747ea32527c67f39084fbd",
  "source_id": "src.path.trigger",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/backend/commands/trigger.c#L2388-L2394"
}
sources · src.path.tcn
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "contrib/tcn/tcn.c",
  "position": 3,
  "sha256": "fd1b0dfa65a4b3f15bddc5ae6d4b71505e448a5febdaa20c5da8839d2b54350d",
  "source_id": "src.path.tcn",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/contrib/tcn/tcn.c#L77-L110"
}
sources · src.path.plperl
{
  "commit": "724edf9bde9d356724ad384a2e196edc3c9f80f7",
  "docs_url": "",
  "kind": "upstream_source",
  "location": "",
  "path": "src/pl/plperl/plperl.c",
  "position": 4,
  "sha256": "90789190fd0bf5b53773b57b8c3287c88c35fbebdfa8310acab3f2d89910e4ba",
  "source_id": "src.path.plperl",
  "tag": "REL_18_6",
  "url": "https://github.com/postgres/postgres/blob/724edf9bde9d356724ad384a2e196edc3c9f80f7/src/pl/plperl/plperl.c#L2607-L2618"
}

来源引用

完整定义与证据 JSON

定义来源

center · PostgreSQL 18 · english-manuals:78c5cae4e0aa0c243deee6fbbaea42866d5af54907a6fc35db6b2e62bc1d0f3c

正文语言: zh-Hans · english-manuals:78c5cae4e0aa0c243deee6fbbaea42866d5af54907a6fc35db6b2e62bc1d0f3c