由 PostgreSQL 伺服器發出的所有訊息都會被分配一個五個字元的錯誤代碼,該代碼遵循 SQL 標準的 “SQLSTATE” 代碼慣例。 需要知道發生了哪個錯誤情況的應用程式通常應該測試錯誤代碼,而不是查看文字錯誤訊息。 錯誤代碼在 PostgreSQL 版本之間不太可能更改,並且也不會因錯誤訊息的本地化而發生更改。 請注意,由 PostgreSQL 產生的錯誤代碼中,有些(但不是全部)是由 SQL 標準定義的; 對於標準未定義的情況,已發明了一些額外的錯誤代碼或從其他資料庫借用。
根據標準,錯誤代碼的前兩個字元表示一類錯誤,而後三個字元表示該類別中的特定情況。 因此,無法識別特定錯誤代碼的應用程式可能仍然能夠從錯誤類別中推斷出該怎麼做。
表 A.1 列出了 PostgreSQL 17.2 中定義的所有錯誤代碼。 (有些目前實際上並未使用,但由 SQL 標準定義。) 也顯示了錯誤類別。 對於每個錯誤類別,都有一個 “標準” 錯誤代碼,其後三個字元為 000
。 此代碼僅用於屬於該類別但沒有分配更具體代碼的錯誤情況。
在 “條件名稱” 欄中顯示的符號是在 PL/pgSQL 中使用的條件名稱。 條件名稱可以用大寫或小寫書寫。 (請注意,PL/pgSQL 不會識別警告,而不是錯誤,條件名稱; 這些是 00、01 和 02 類別。)
對於某些類型的錯誤,伺服器會報告與該錯誤相關的資料庫物件(表、表欄位、資料類型或約束)的名稱; 例如,導致 unique_violation
錯誤的唯一約束的名稱。 這些名稱在錯誤報告訊息的單獨欄位中提供,因此應用程式無需嘗試從訊息的可能已本地化的可讀文字中提取它們。 從 PostgreSQL 9.3 開始,此功能的完整涵蓋範圍僅適用於 SQLSTATE 類別 23(完整性約束違規)中的錯誤,但這可能會在未來擴展。
表 A.1. PostgreSQL 錯誤代碼
錯誤代碼 | 條件名稱 |
---|---|
Class 00 — 成功完成 | |
00000 |
successful_completion |
Class 01 — 警告 | |
01000 |
warning |
0100C |
dynamic_result_sets_returned |
01008 |
implicit_zero_bit_padding |
01003 |
null_value_eliminated_in_set_function |
01007 |
privilege_not_granted |
01006 |
privilege_not_revoked |
01004 |
string_data_right_truncation |
01P01 |
deprecated_feature |
Class 02 — 沒有資料 (這也是 SQL 標準中的警告類別) | |
02000 |
no_data |
02001 |
no_additional_dynamic_result_sets_returned |
Class 03 — SQL 語句尚未完成 | |
03000 |
sql_statement_not_yet_complete |
Class 08 — 連線例外 | |
08000 |
connection_exception |
08003 |
connection_does_not_exist |
08006 |
connection_failure |
08001 |
sqlclient_unable_to_establish_sqlconnection |
08004 |
sqlserver_rejected_establishment_of_sqlconnection |
08007 |
transaction_resolution_unknown |
08P01 |
protocol_violation |
Class 09 — 觸發動作例外 | |
09000 |
triggered_action_exception |
Class 0A — 不支援的功能 | |
0A000 |
feature_not_supported |
Class 0B — 無效的交易啟動 | |
0B000 |
invalid_transaction_initiation |
Class 0F — 定位器例外 | |
0F000 |
locator_exception |
0F001 |
invalid_locator_specification |
Class 0L — 無效的授權者 | |
0L000 |
invalid_grantor |
0LP01 |
invalid_grant_operation |
Class 0P — 無效的角色規範 | |
0P000 |
invalid_role_specification |
Class 0Z — 診斷例外 | |
0Z000 |
diagnostics_exception |
0Z002 |
stacked_diagnostics_accessed_without_active_handler |
Class 20 — 找不到案例 | |
20000 |
case_not_found |
Class 21 — 基數違規 | |
21000 |
cardinality_violation |
Class 22 — 資料例外 | |
22000 |
data_exception |
2202E |
array_subscript_error |
22021 |
character_not_in_repertoire |
22008 |
datetime_field_overflow |
22012 |
division_by_zero |
22005 |
error_in_assignment |
2200B |
escape_character_conflict |
22022 |
indicator_overflow |
22015 |
interval_field_overflow |
2201E |
invalid_argument_for_logarithm |
22014 |
invalid_argument_for_ntile_function |
22016 |
invalid_argument_for_nth_value_function |
2201F |
invalid_argument_for_power_function |
2201G |
invalid_argument_for_width_bucket_function |
22018 |
invalid_character_value_for_cast |
22007 |
invalid_datetime_format |
22019 |
invalid_escape_character |
2200D |
invalid_escape_octet |
22025 |
invalid_escape_sequence |
22P06 |
nonstandard_use_of_escape_character |
22010 |
invalid_indicator_parameter_value |
22023 |
invalid_parameter_value |
22013 |
invalid_preceding_or_following_size |
2201B |
invalid_regular_expression |
2201W |
invalid_row_count_in_limit_clause |
2201X |
invalid_row_count_in_result_offset_clause |
2202H |
invalid_tablesample_argument |
2202G |
invalid_tablesample_repeat |
22009 |
invalid_time_zone_displacement_value |
2200C |
invalid_use_of_escape_character |
2200G |
most_specific_type_mismatch |
22004 |
null_value_not_allowed |
22002 |
null_value_no_indicator_parameter |
22003 |
numeric_value_out_of_range |
2200H |
sequence_generator_limit_exceeded (序列產生器超出限制) |
22026 |
string_data_length_mismatch (字串資料長度不符) |
22001 |
string_data_right_truncation |
22011 |
substring_error (子字串錯誤) |
22027 |
trim_error (修剪錯誤) |
22024 |
unterminated_c_string (未終止的 C 字串) |
2200F |
zero_length_character_string (零長度字元字串) |
22P01 |
floating_point_exception (浮點數例外) |
22P02 |
invalid_text_representation (無效的文字表示法) |
22P03 |
invalid_binary_representation (無效的二進位表示法) |
22P04 |
bad_copy_file_format (不正確的複製檔案格式) |
22P05 |
untranslatable_character (無法翻譯的字元) |
2200L |
not_an_xml_document (不是 XML 文件) |
2200M |
invalid_xml_document (無效的 XML 文件) |
2200N |
invalid_xml_content (無效的 XML 內容) |
2200S |
invalid_xml_comment (無效的 XML 註解) |
2200T |
invalid_xml_processing_instruction (無效的 XML 處理指示) |
22030 |
duplicate_json_object_key_value (重複的 JSON 物件鍵值) |
22031 |
invalid_argument_for_sql_json_datetime_function (SQL JSON 日期時間函數的無效引數) |
22032 |
invalid_json_text (無效的 JSON 文字) |
22033 |
invalid_sql_json_subscript (無效的 SQL JSON 下標) |
22034 |
more_than_one_sql_json_item (超過一個 SQL JSON 項目) |
22035 |
no_sql_json_item (沒有 SQL JSON 項目) |
22036 |
non_numeric_sql_json_item (非數值的 SQL JSON 項目) |
22037 |
non_unique_keys_in_a_json_object (JSON 物件中存在非唯一鍵) |
22038 |
singleton_sql_json_item_required (需要單例 SQL JSON 項目) |
22039 |
sql_json_array_not_found (找不到 SQL JSON 陣列) |
2203A |
sql_json_member_not_found (找不到 SQL JSON 成員) |
2203B |
sql_json_number_not_found (找不到 SQL JSON 數字) |
2203C |
sql_json_object_not_found (找不到 SQL JSON 物件) |
2203D |
too_many_json_array_elements (JSON 陣列元素過多) |
2203E |
too_many_json_object_members (JSON 物件成員過多) |
2203F |
sql_json_scalar_required (需要 SQL JSON 純量) |
2203G |
sql_json_item_cannot_be_cast_to_target_type (SQL JSON 項目無法轉換為目標類型) |
Class 23 — Integrity Constraint Violation (類別 23 — 完整性約束違規) | |
23000 |
integrity_constraint_violation (完整性約束違規) |
23001 |
restrict_violation (限制違規) |
23502 |
not_null_violation (非空值違規) |
23503 |
foreign_key_violation (外鍵違規) |
23505 |
unique_violation (唯一性違規) |
23514 |
check_violation (檢查違規) |
23P01 |
exclusion_violation (排除違規) |
Class 24 — Invalid Cursor State (類別 24 — 無效的游標狀態) | |
24000 |
invalid_cursor_state (無效的游標狀態) |
Class 25 — Invalid Transaction State (類別 25 — 無效的交易狀態) | |
25000 |
invalid_transaction_state (無效的交易狀態) |
25001 |
active_sql_transaction (作用中的 SQL 交易) |
25002 |
branch_transaction_already_active (分支交易已在作用中) |
25008 |
held_cursor_requires_same_isolation_level (保留的游標需要相同的隔離等級) |
25003 |
inappropriate_access_mode_for_branch_transaction (分支交易的存取模式不適當) |
25004 |
inappropriate_isolation_level_for_branch_transaction (分支交易的隔離等級不適當) |
25005 |
no_active_sql_transaction_for_branch_transaction (分支交易沒有作用中的 SQL 交易) |
25006 |
read_only_sql_transaction (唯讀 SQL 交易) |
25007 |
schema_and_data_statement_mixing_not_supported (不支援混合綱要和資料陳述式) |
25P01 |
no_active_sql_transaction (沒有作用中的 SQL 交易) |
25P02 |
in_failed_sql_transaction (在失敗的 SQL 交易中) |
25P03 |
idle_in_transaction_session_timeout (交易中閒置的會話逾時) |
25P04 |
transaction_timeout (交易逾時) |
Class 26 — Invalid SQL Statement Name (類別 26 — 無效的 SQL 陳述式名稱) | |
26000 |
invalid_sql_statement_name (無效的 SQL 陳述式名稱) |
Class 27 — Triggered Data Change Violation (類別 27 — 觸發的資料變更違規) | |
27000 |
triggered_data_change_violation (觸發的資料變更違規) |
Class 28 — Invalid Authorization Specification (類別 28 — 無效的授權規範) | |
28000 |
invalid_authorization_specification (無效的授權規範) |
28P01 |
invalid_password (無效的密碼) |
Class 2B — Dependent Privilege Descriptors Still Exist (類別 2B — 仍存在相依的權限描述符) | |
2B000 |
dependent_privilege_descriptors_still_exist (仍存在相依的權限描述符) |
2BP01 |
dependent_objects_still_exist (仍存在相依的物件) |
Class 2D — Invalid Transaction Termination (類別 2D — 無效的交易終止) | |
2D000 |
invalid_transaction_termination (無效的交易終止) |
Class 2F — SQL Routine Exception (類別 2F — SQL 例程例外) | |
2F000 |
sql_routine_exception (SQL 例程例外) |
2F005 |
function_executed_no_return_statement (函數已執行但沒有傳回陳述式) |
2F002 |
modifying_sql_data_not_permitted (不允許修改 SQL 資料) |
2F003 |
prohibited_sql_statement_attempted (嘗試了禁止的 SQL 陳述式) |
2F004 |
reading_sql_data_not_permitted (不允許讀取 SQL 資料) |
Class 34 — Invalid Cursor Name (類別 34 — 無效的游標名稱) | |
34000 |
invalid_cursor_name (無效的游標名稱) |
Class 38 — External Routine Exception (類別 38 — 外部例程例外) | |
38000 |
external_routine_exception (外部例程例外) |
38001 |
containing_sql_not_permitted (不允許包含 SQL) |
38002 |
modifying_sql_data_not_permitted (不允許修改 SQL 資料) |
38003 |
prohibited_sql_statement_attempted (嘗試了禁止的 SQL 陳述式) |
38004 |
reading_sql_data_not_permitted (不允許讀取 SQL 資料) |
Class 39 — External Routine Invocation Exception (類別 39 — 外部例程調用例外) | |
39000 |
external_routine_invocation_exception (外部例程調用例外) |
39001 |
invalid_sqlstate_returned (傳回無效的 SQLSTATE) |
39004 |
null_value_not_allowed |
39P01 |
trigger_protocol_violated (觸發程序協定違規) |
39P02 |
srf_protocol_violated (SRF 協定違規) |
39P03 |
event_trigger_protocol_violated (事件觸發程序協定違規) |
Class 3B — Savepoint Exception (類別 3B — 儲存點例外) | |
3B000 |
savepoint_exception (儲存點例外) |
3B001 |
invalid_savepoint_specification (無效的儲存點規範) |
Class 3D — Invalid Catalog Name (類別 3D — 無效的目錄名稱) | |
3D000 |
invalid_catalog_name (無效的目錄名稱) |
Class 3F — Invalid Schema Name (類別 3F — 無效的綱要名稱) | |
3F000 |
invalid_schema_name (無效的綱要名稱) |
Class 40 — Transaction Rollback (類別 40 — 交易回滾) | |
40000 |
transaction_rollback (交易回滾) |
40002 |
transaction_integrity_constraint_violation (交易完整性約束違規) |
40001 |
serialization_failure (序列化失敗) |
40003 |
statement_completion_unknown (陳述式完成狀態不明) |
40P01 |
deadlock_detected (偵測到死鎖) |
Class 42 — Syntax Error or Access Rule Violation (類別 42 — 語法錯誤或存取規則違規) | |
42000 |
syntax_error_or_access_rule_violation (語法錯誤或存取規則違規) |
42601 |
syntax_error (語法錯誤) |
42501 |
insufficient_privilege (權限不足) |
42846 |
cannot_coerce (無法強制轉換) |
42803 |
grouping_error (分組錯誤) |
42P20 |
windowing_error (視窗函數錯誤) |
42P19 |
invalid_recursion (無效的遞迴) |
42830 |
invalid_foreign_key (無效的外鍵) |
42602 |
invalid_name (無效的名稱) |
42622 |
name_too_long (名稱過長) |
42939 |
reserved_name (保留名稱) |
42804 |
datatype_mismatch (資料類型不符) |
42P18 |
indeterminate_datatype (不確定的資料類型) |
42P21 |
collation_mismatch (定序不符) |
42P22 |
indeterminate_collation (不確定的定序) |
42809 |
wrong_object_type (錯誤的物件類型) |
428C9 |
generated_always (總是產生的) |
42703 |
undefined_column (未定義的欄位) |
42883 |
undefined_function (未定義的函數) |
42P01 |
undefined_table (未定義的表格) |
42P02 |
undefined_parameter (未定義的參數) |
42704 |
undefined_object (未定義的物件) |
42701 |
duplicate_column (重複的欄位) |
42P03 |
duplicate_cursor (重複的游標) |
42P04 |
duplicate_database (重複的資料庫) |
42723 |
duplicate_function (重複的函數) |
42P05 |
duplicate_prepared_statement (重複的預備陳述式) |
42P06 |
duplicate_schema (重複的綱要) |
42P07 |
duplicate_table (重複的表格) |
42712 |
duplicate_alias (重複的別名) |
42710 |
duplicate_object (重複的物件) |
42702 |
ambiguous_column (不明確的欄位) |
42725 |
ambiguous_function (不明確的函數) |
42P08 |
ambiguous_parameter (不明確的參數) |
42P09 |
ambiguous_alias (不明確的別名) |
42P10 |
invalid_column_reference (無效的欄位參考) |
42611 |
invalid_column_definition (無效的欄位定義) |
42P11 |
invalid_cursor_definition (無效的游標定義) |
42P12 |
invalid_database_definition (無效的資料庫定義) |
42P13 |
invalid_function_definition (無效的函數定義) |
42P14 |
invalid_prepared_statement_definition (無效的預備陳述式定義) |
42P15 |
invalid_schema_definition (無效的綱要定義) |
42P16 |
invalid_table_definition (無效的表格定義) |
42P17 |
invalid_object_definition (無效的物件定義) |
Class 44 — WITH CHECK OPTION Violation (類別 44 — WITH CHECK OPTION 違規) | |
44000 |
with_check_option_violation (WITH CHECK OPTION 違規) |
Class 53 — Insufficient Resources (類別 53 — 資源不足) | |
53000 |
insufficient_resources (資源不足) |
53100 |
disk_full (磁碟已滿) |
53200 |
out_of_memory (記憶體不足) |
53300 |
too_many_connections (連線數過多) |
53400 |
configuration_limit_exceeded (超出配置限制) |
Class 54 — Program Limit Exceeded (類別 54 — 超出程式限制) | |
54000 |
program_limit_exceeded (超出程式限制) |
54001 |
statement_too_complex (陳述式過於複雜) |
54011 |
too_many_columns (欄位過多) |
54023 |
too_many_arguments (引數過多) |
Class 55 — Object Not In Prerequisite State (類別 55 — 物件不在必要狀態) | |
55000 |
object_not_in_prerequisite_state (物件不在必要狀態) |
55006 |
object_in_use (物件正在使用中) |
55P02 |
cant_change_runtime_param (無法變更執行時參數) |
55P03 |
lock_not_available (鎖定不可用) |
55P04 |
unsafe_new_enum_value_usage (不安全的新列舉值使用) |
Class 57 — Operator Intervention (類別 57 — 操作員干預) | |
57000 |
operator_intervention (操作員干預) |
57014 |
query_canceled (查詢已取消) |
57P01 |
admin_shutdown (管理員關閉) |
57P02 |
crash_shutdown (崩潰關機) |
57P03 |
cannot_connect_now (現在無法連線) |
57P04 |
database_dropped (資料庫已刪除) |
57P05 |
idle_session_timeout (閒置會話逾時) |
Class 58 — System Error (errors external to PostgreSQL itself) (類別 58 — 系統錯誤 (PostgreSQL 本身以外的錯誤)) | |
58000 |
system_error (系統錯誤) |
58030 |
io_error (I/O 錯誤) |
58P01 |
undefined_file (未定義的檔案) |
58P02 |
duplicate_file (重複的檔案) |
Class F0 — Configuration File Error (類別 F0 — 組態檔錯誤) | |
F0000 |
config_file_error (組態檔錯誤) |
F0001 |
lock_file_exists (鎖定檔案存在) |
Class HV — Foreign Data Wrapper Error (SQL/MED) (類別 HV — 外部資料包裝器錯誤 (SQL/MED)) | |
HV000 |
fdw_error (FDW 錯誤) |
HV005 |
fdw_column_name_not_found (找不到 FDW 欄位名稱) |
HV002 |
fdw_dynamic_parameter_value_needed (需要 FDW 動態參數值) |
HV010 |
fdw_function_sequence_error (FDW 函數序列錯誤) |
HV021 |
fdw_inconsistent_descriptor_information (FDW 描述符資訊不一致) |
HV024 |
fdw_invalid_attribute_value (FDW 無效的屬性值) |
HV007 |
fdw_invalid_column_name (FDW 無效的欄位名稱) |
HV008 |
fdw_invalid_column_number (FDW 無效的欄位編號) |
HV004 |
fdw_invalid_data_type (FDW 無效的資料類型) |
HV006 |
fdw_invalid_data_type_descriptors (FDW 無效的資料類型描述符) |
HV091 |
fdw_invalid_descriptor_field_identifier (FDW 無效的描述符欄位識別碼) |
HV00B |
fdw_invalid_handle (FDW 無效的句柄) |
HV00C |
fdw_invalid_option_index (FDW 無效的選項索引) |
HV00D |
fdw_invalid_option_name (FDW 無效的選項名稱) |
HV090 |
fdw_invalid_string_length_or_buffer_length (FDW 無效的字串長度或緩衝區長度) |
HV00A |
fdw_invalid_string_format (FDW 無效的字串格式) |
HV009 |
fdw_invalid_use_of_null_pointer (FDW 無效的使用空指標) |
HV014 |
fdw_too_many_handles (FDW 句柄過多) |
HV001 |
fdw_out_of_memory (FDW 記憶體不足) |
HV00P |
fdw_no_schemas (FDW 沒有綱要) |
HV00J |
fdw_option_name_not_found (找不到 FDW 選項名稱) |
HV00K |
fdw_reply_handle (FDW 回覆句柄) |
HV00Q |
fdw_schema_not_found (找不到 FDW 綱要) |
HV00R |
fdw_table_not_found (找不到 FDW 表格) |
HV00L |
fdw_unable_to_create_execution (FDW 無法建立執行) |
HV00M |
fdw_unable_to_create_reply (FDW 無法建立回覆) |
HV00N |
fdw_unable_to_establish_connection (FDW 無法建立連線) |
Class P0 — PL/pgSQL Error (類別 P0 — PL/pgSQL 錯誤) | |
P0000 |
plpgsql_error (PL/pgSQL 錯誤) |
P0001 |
raise_exception (引發例外) |
P0002 |
no_data_found (找不到資料) |
P0003 |
too_many_rows (列數過多) |
P0004 |
assert_failure (斷言失敗) |
Class XX — Internal Error (類別 XX — 內部錯誤) | |
XX000 |
internal_error (內部錯誤) |
XX001 |
data_corrupted (資料已損毀) |
XX002 |
index_corrupted (索引已損毀) |