Errors and Messages

1. Reporting Errors and Messages

Use the RAISE statement to report messages and raise errors.

RAISE [ level ] ’format’ [, expression [, ... ]] [ USING option = expression [, ... ] ];
RAISE [ level ] condition_name [ USING option = expression [, ... ] ];
RAISE [ level ] SQLSTATE ’sqlstate’ [ USING option = expression [, ... ] ];
RAISE [ level ] USING option = expression [, ... ];
RAISE ;

The level option specifies the error severity. Allowed levels are DEBUG, LOG, INFO, NOTICE, WARNING, and EXCEPTION, with EXCEPTION being the default.

EXCEPTION raises an error (which normally aborts the current transaction); the other levels only generate messages of different priority levels.

Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the log_min_messages and client_min_messages configuration variables.

After level if any, you can write a format (which must be a simple string literal, not an expression).

The format string specifies the error message text to be reported. The format string can be followed by optional argument expressions to be inserted into the message.

Inside the format string, % is replaced by the string representation of the next optional argument’s value.

Write %% to emit a literal %. The number of arguments must match the number of % placeholders in the format string, or an error is raised during the compilation of the function.

In this example, the value of v_job_id will replace the % in the string:

RAISE NOTICE ’Calling cs_create_job(%)’, v_job_id;

You can attach additional information to the error report by writing USING followed by option = expression items. Each expression can be any string-valued expression.

The allowed option key words are:

MESSAGE

Sets the error message text. This option can’t be used in the form of RAISE that includes a format

string before USING.

DETAIL

Supplies an error detail message.

HINT

Supplies a hint message.

ERRCODE

Specifies the error code (SQLSTATE) to report, either by condition name or directly as a five-character SQLSTATE code.

COLUMN
CONSTRAINT
DATATYPE
TABLE
SCHEMA

Supplies the name of a related object.

This example will abort the transaction with the given error message and hint:

RAISE EXCEPTION ’Nonexistent ID --> %’, user_id
USING HINT = ’Please check your user ID’;

These two examples show equivalent ways of setting the SQLSTATE:

RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’unique_violation’;
RAISE ’Duplicate user ID: %’, user_id USING ERRCODE = ’23505’;

There is a second RAISE syntax in which the main argument is the condition name or SQLSTATE to be reported, for example:

RAISE division_by_zero;
RAISE SQLSTATE ’22012’;

In this syntax, USING can be used to supply a custom error message, detail, or hint.

Another way to do the earlier example is

RAISE unique_violation USING MESSAGE = ’Duplicate user ID: ’ || user_id;

Still another variant is to write RAISE USING or RAISE level USING and put everything else into the USING list.

The last variant of RAISE has no parameters at all. This form can only be used inside a BEGIN block’s EXCEPTION clause; it causes the error currently being handled to be re-thrown.

If no condition name nor SQLSTATE is specified in a RAISE EXCEPTION command, the default is to use RAISE_EXCEPTION (P0001).

If no message text is specified, the default is to use the condition name or SQLSTATE as message text.

Note: When specifying an error code by SQLSTATE code, you are not limited to the predefined error codes, but can select any error code consisting of five digits and/or upper-case ASCII letters, other than 00000.

It is recommended that you avoid throwing error codes that end in three zeroes, because these are category codes and can only be trapped by trapping the whole category.

2. Checking Assertions

The ASSERT statement is a convenient shorthand for inserting debugging checks into PL/pgSQL functions.

ASSERT condition [ , message ];

The condition is a Boolean expression that is expected to always evaluate to true; if it does, the ASSERT statement does nothing further. If the result is false or null, then an ASSERT_FAILURE exception is raised. (If an error occurs while evaluating the condition, it is reported as a normal error.)

If the optional message is provided, it is an expression whose result (if not null) replaces the default error message text “assertion failed”, should the condition fail. The message expression is not evaluated in the normal case where the assertion succeeds.

Testing of assertions can be enabled or disabled via the configuration parameter plpgsql.check_asserts, which takes a Boolean value; the default is on. If this parameter is off then ASSERT statements do nothing.

Note that ASSERT is meant for detecting program bugs, not for reporting ordinary error conditions. Use the RAISE statement, described above, for that

postgreSQL PL/SQL编程学习笔记(四)的更多相关文章

  1. postgreSQL PL/SQL编程学习笔记(二)

    Control Structures of PL/SQL Control structures are probably the most useful (and important) part of ...

  2. postgreSQL PL/SQL编程学习笔记(六)——杂七杂八

    1 PL/pgSQL Under the Hood This part discusses some implementation details that are frequently import ...

  3. postgreSQL PL/SQL编程学习笔记(五)——触发器(Triggers)

    Trigger Procedures PL/pgSQL can be used to define trigger procedures on data changes or database eve ...

  4. postgreSQL PL/SQL编程学习笔记(三)——游标(Cursors)

    Cursors Rather than executing a whole query at once, it is possible to set up a cursor that encapsul ...

  5. postgreSQL PL/SQL编程学习笔记(一)

    1.Structure of PL/pgSQL The structure of PL/pgSQL is like below: [ <<label>> ] [ DECLARE ...

  6. PL/SQL编程基础(四):程序控制(IF,CASE,FOR,LOOP,WHILE LOOP,EXIT,CONTINUE)

    程序控制 程序结构有分支结构与循环结构: 分支结构语法:IF.CASE: 循环结构:FOR.WHILE LOOP:先执行再判断,至少执行一次: WHILE LOOP:先判断再执行,如果不满足条件,就不 ...

  7. MySql-Mysql技术内幕~SQL编程学习笔记(N)

    1._rowid 类似Oracle的rowid mysql> ; +-------+----+----------------+-------------+---------------+--- ...

  8. MySql-Mysql技术内幕~SQL编程学习笔记(1)

    1.MySQL的历史,一些相关概念. 2.MySQL数据类型 *通常一个页内可以存放尽可能多的行,那么数据库的性能就越好,选择一个正确的数据类型至关重要. 1>UNSIGNED类型: 将数字类型 ...

  9. 【Linux_Shell 脚本编程学习笔记四、监控系统内存并报警企业案例脚本】

    前置知识:awk 参考学习博客:https://www.cnblogs.com/bugingcode/p/8287914.html awk 'BEGIN{ commands } pattern{ co ...

随机推荐

  1. java 高并发解决方案

    对于我们开发的网站,如果网站的访问量非常大的话,那么我们就需要考虑相关的并发访问问题了.而并发问题是绝大部分的程序员头疼的问题, 但话又说回来了,既然逃避不掉,那我们就坦然面对吧~今天就让我们一起来研 ...

  2. MongoDB安全加固方案,防止数据泄露被勒索

    早上起来,发现生产数据库被删了,留下一个数据库名叫“PLEASE_READ”,里面内容如下: "Info" : "Your DB is Backed up at our ...

  3. Autofac 4 netcore with config demo

    Autofac 4+以上和以前的配置不一样的,最近在NetCore中做项目,得到实例时折腾了一番. 使用场景,用autofac实例化数据库类型,避免一个个的去new json配置文件: <?xm ...

  4. 【280】◀▶ ArcPy 常用工具说明

    目录: 一.相关技巧 二.工具说明 一.相关技巧 技巧1:将工具从工具箱拖拽到 Python 窗体中自动会生成相应的函数,于是可以快速定位函数名称! 技巧2:将通过工具箱实现的操作结果拖拽到 Pyth ...

  5. 6410裸板程序,led、蜂鸣器、按键…

    //***************************************************************** //作者:昊天 // //功能:在ok6410板子上跑裸板程序, ...

  6. c++ 多态问题(在虚函数里调用虚函数)

    最近在看cocos2d-x的源码,非常感激cocos2d作者的开源精神.在看代码的过程中感觉两个方向让我受益,1.把之前从书中看到的c++知识,明白了怎么运用.2.学习作者驾驭代码的巧妙方法. 看co ...

  7. 性能优化之_android内存

    优化内存使用主要是三个原则: CPU如何操纵特定的数据类型 数据和指令需要占用多少存储空间 数据在内存中的布局方式 处理大量数据时,使用可以满足要求的最小字节数据类型,能用short就不用int,能用 ...

  8. Apache htdigest命令

    一.简介 htdigest命令是Apache的Web服务器内置工具,用于创建和更新储存用户名.域和用于摘要认证的密码文件.服务器上的资源可以被限制为仅允许由htdigest建立的文件中的用户访问.使用 ...

  9. IP协议、ARP协议等之温故知新

    今天才知道: 1.IP协议的固定部分长度为20字节.(貌似有一家运维工程师面试我的时候,问过我这个问题呢.) 2.IP数据包首部中的协议?? 答:协议:占8位,指出此数据报携带的数据使用何种协议以便目 ...

  10. PHP学习笔记之continue与break

    百度中有人这样解释:break是结束整个循环体,continue是结束单个循环体.昨天看燕十八老师PHP视频,讲到break,continue时,举了一个例子,理解更容易.天龙八部中,西夏国公主选婿, ...