pg_input_error_info() 是一个系统函数,它会返回当提供的值为指定数据类型无效时会发出的错误消息。
pg_input_error_info() 添加于 PostgreSQL 16。
用法
pg_input_error_info (stringtext,typetext) →
record (messagetext,detailtext,hinttext,sql_error_codetext)
如果提供的字符串对于指定的数据类型无效,则会发出 PostgreSQL 会返回的消息,但不会引发 ERROR。
如果提供的字符串对于指定的数据类型有效,则返回 NULL。
pg_input_is_valid() 提供了一个简单的布尔有效性测试。
变更历史
示例
pg_input_error_info() 的用法示例
postgres=# SELECT * FROM pg_input_error_info('3.14', 'integer')\gx
-[ RECORD 1 ]--+----------------------------------------------
message | invalid input syntax for type integer: "3.14"
detail |
hint |
sql_error_code | 22P02
postgres=# SELECT * FROM pg_input_error_info('3.14', 'numeric(1,2)')\gx
-[ RECORD 1 ]--+-----------------------------------------------------------------------------------
message | numeric field overflow
detail | A field with precision 1, scale 2 must round to an absolute value less than 10^-1.
hint |
sql_error_code | 22003
如果提供的字符串对于指定的数据类型有效,则所有返回的字段均为 NULL。
postgres=# SELECT * FROM pg_input_error_info('3.14', 'text');
message | detail | hint | sql_error_code
---------+--------+------+----------------
| | |
(1 row)
使用 pg_input_is_valid() 对值进行简单的布尔有效性检查。
参考资料
- PostgreSQL documentation: Data Validity Checking Functions
