Use the <command>RAISE</command> statement to report messages and
raise errors.
RAISE文を使用してメッセージを報告し、エラーを発生することができます。
RAISE [level] 'format' [,expression[, ... ]] [ USINGoption{ = | := }expression[, ... ] ]; RAISE [level]condition_name[ USINGoption{ = | := }expression[, ... ] ]; RAISE [level] SQLSTATE 'sqlstate' [ USINGoption{ = | := }expression[, ... ] ]; RAISE [level] USINGoption{ = | := }expression[, ... ]; RAISE ;
The <replaceable class="parameter">level</replaceable> option specifies
the error severity. Allowed levels are <literal>DEBUG</literal>,
<literal>LOG</literal>, <literal>INFO</literal>,
<literal>NOTICE</literal>, <literal>WARNING</literal>,
and <literal>EXCEPTION</literal>, with <literal>EXCEPTION</literal>
being the default.
<literal>EXCEPTION</literal> 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
<xref linkend="guc-log-min-messages"/> and
<xref linkend="guc-client-min-messages"/> configuration
variables. See <xref linkend="runtime-config"/> for more
information.
levelオプションはエラーの深刻度を指定します。
使用可能なレベルはDEBUG、LOG、INFO、NOTICE、WARNINGおよびEXCEPTIONで、EXCEPTIONがデフォルトです。
EXCEPTIONはエラーを発生させ、現在のトランザクションをアボートします。
他のレベルは異なる優先度レベルのメッセージを生成するだけです。
特定の優先度のエラーメッセージがクライアントに報告するか、サーバログに書き込むか、またはその両方はlog_min_messagesおよびclient_min_messages設定変数によって制御されます。
詳細については、第19章を参照してください。
In the first syntax variant,
after the <replaceable class="parameter">level</replaceable> if any,
write a <replaceable class="parameter">format</replaceable> string
(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, <literal>%</literal> is replaced by the
string representation of the next optional argument's value. Write
<literal>%%</literal> to emit a literal <literal>%</literal>.
The number of arguments must match the number of <literal>%</literal>
placeholders in the format string, or an error is raised during
the compilation of the function.
最初の構文の形式では、もしlevelがあればその後にformat文字列を記述します(これは評価式ではなく、単純な文字列リテラルでなければなりません)。
書式文字列は報告されるエラーメッセージテキストを指定します。
書式文字列の後には、メッセージに挿入される省略可能な引数の式を指定できます。
書式文字列内では、%は次の省略可能な引数の値の文字列表現で書き換えられます。
%%と記述することで%リテラルを表すことができます。
引数の数は書式文字列のプレースホルダ%の数と一致しなければいけません。さもなくば、関数のコンパイル時にエラーが起きます。
In this example, the value of <literal>v_job_id</literal> will replace the
<literal>%</literal> in the string:
以下の例では、v_job_idの値は文字列内の%を置き換えます。
RAISE NOTICE 'Calling cs_create_job(%)', v_job_id;
In the second and third syntax variants,
<replaceable class="parameter">condition_name</replaceable> and
<replaceable class="parameter">sqlstate</replaceable> specify an
error condition name or a five-character SQLSTATE code, respectively.
See <xref linkend="errcodes-appendix"/> for the valid error condition
names and the predefined SQLSTATE codes.
2番目と3番目の構文の形式では、condition_nameとsqlstateはそれぞれエラー条件名と5文字のSQLSTATEコードを指定します。
有効なエラー条件名と定義済みのSQLSTATEコードについては付録Aを参照してください。
Here are examples
of <replaceable class="parameter">condition_name</replaceable>
and <replaceable class="parameter">sqlstate</replaceable> usage:
以下は、condition_nameとsqlstateの使用方法の例です。
RAISE division_by_zero; RAISE WARNING SQLSTATE '22012';
In any of these syntax variants,
you can attach additional information to the error report by writing
<literal>USING</literal> followed by <replaceable
class="parameter">option</replaceable> = <replaceable
class="parameter">expression</replaceable> items. Each
<replaceable class="parameter">expression</replaceable> can be any
string-valued expression. The allowed <replaceable
class="parameter">option</replaceable> key words are:
いずれの構文の形式でも、USINGの後にoption = expressionの項目を記述することで、エラー報告に追加の情報を加えることができます。
各expressionは、どのような文字列による式も可能です。
使用可能なoptionキーワードは以下です。
MESSAGE #エラーメッセージテキストを指定します。 このオプションは、既にメッセージが指定されている最初の構文の形式では使用できません。
DETAIL #エラー詳細メッセージを出力します。
HINT #ヒントメッセージを出力します。
ERRCODE #報告するエラーコード(SQLSTATE)を、付録Aで示されている条件名で指定するか、または5文字のSQLSTATEコードで直接指定します。 このオプションは、既にエラーコードが指定されている2番目と3番目の構文の形式では使用できません。
COLUMNCONSTRAINTDATATYPETABLESCHEMA #関連するオブジェクト名を出力します。
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: 以下の2つの例は、SQLSTATEを設定する等価な方法を示しています。
RAISE 'Duplicate user ID: %', user_id USING ERRCODE = 'unique_violation'; RAISE 'Duplicate user ID: %', user_id USING ERRCODE = '23505';
Another way to produce the same result is: 以下は、同じ結果になる別の方法です。
RAISE unique_violation USING MESSAGE = 'Duplicate user ID: ' || user_id;
As shown in the fourth syntax variant, it is also possible to
write <literal>RAISE USING</literal> or <literal>RAISE
<replaceable class="parameter">level</replaceable> USING</literal> and put
everything else into the <literal>USING</literal> list.
4番目の構文の形式で示されているように、RAISE USINGまたはRAISE と記述して、全て一括してlevel USINGUSINGリスト内に書き加えることも可能です。
The last variant of <command>RAISE</command> has no parameters at all.
This form can only be used inside a <literal>BEGIN</literal> block's
<literal>EXCEPTION</literal> clause;
it causes the error currently being handled to be re-thrown.
最後のRAISE亜種はパラメータを全く取りません。
この形式はBEGINブロックのEXCEPTION句で使用されるのみです。
これは、現在処理中のエラーを再発生させます。
Before <productname>PostgreSQL</productname> 9.1, <command>RAISE</command> without
parameters was interpreted as re-throwing the error from the block
containing the active exception handler. Thus an <literal>EXCEPTION</literal>
clause nested within that handler could not catch it, even if the
<command>RAISE</command> was within the nested <literal>EXCEPTION</literal> clause's
block. This was deemed surprising as well as being incompatible with
Oracle's PL/SQL.
PostgreSQL 9.1より前のバージョンでは、パラメータのないRAISEは稼働している例外ハンドラを含むブロックからのエラーの再発生と解釈されました。
したがって、例外ハンドラの中で入れ子となったEXCEPTION句は、RAISEが入れ子となったEXCEPTION句のブロック内にあるときでも、エラーを捕捉できないことになりました。
これは驚くべきことであり、オラクルの PL/SQLと非互換でした。
If no condition name nor SQLSTATE is specified in a
<command>RAISE EXCEPTION</command> command, the default is to use
<literal>raise_exception</literal> (<literal>P0001</literal>).
If no message text is specified, the default is to use the condition
name or SQLSTATE as message text.
RAISE EXCEPTIONコマンド内で状況名もSQLSTATEも指定されない場合、デフォルトはraise_exception (P0001)を使用します。
メッセージテキストが指定されない場合、デフォルトは状況名、またはSQLSTATEをメッセージテキストとして使用します。
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 <literal>00000</literal>. 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.
SQLSTATEコードでエラーコードを指定する場合、事前に定義されたエラーコードに制約されることはありません。
00000以外の5桁の数字かASCIIの大文字からなるどんなエラーコードも選択できます。
3つのゼロで終わるエラーコードの出力を避けるように推奨されています。
と言うのは、そこには分類コードがあり、それらは全ての分類から捕捉することによってのみ補足可能だからです。
The <command>ASSERT</command> statement is a convenient shorthand for
inserting debugging checks into <application>PL/pgSQL</application>
functions.
ASSERT文は、PL/pgSQL関数にデバッグ用検査を差し込むための便利な省略形です。
ASSERTcondition[ ,message];
The <replaceable class="parameter">condition</replaceable> is a Boolean
expression that is expected to always evaluate to true; if it does,
the <command>ASSERT</command> statement does nothing further. If the
result is false or null, then an <literal>ASSERT_FAILURE</literal> exception
is raised. (If an error occurs while evaluating
the <replaceable class="parameter">condition</replaceable>, it is
reported as a normal error.)
conditionは常に真と評価されると想定される論理値式で、結果が真ならASSERT文がさらに何かすることはありません。
結果が偽かNULLなら、ASSERT_FAILURE例外が発生します。
(もし、conditionを評価する間にエラーが生じた場合、それは通常のエラーと同様に報告されます。)
If the optional <replaceable class="parameter">message</replaceable> is
provided, it is an expression whose result (if not null) replaces the
default error message text <quote>assertion failed</quote>, should
the <replaceable class="parameter">condition</replaceable> fail.
The <replaceable class="parameter">message</replaceable> expression is
not evaluated in the normal case where the assertion succeeds.
省略可能なmessageが与えられた場合、その式の結果で(NULLでないなら)、conditionに失敗した際のデフォルトエラーメッセージ文「assertion failed」が置き換えられます。
message式はアサートに成功する通常の場合には評価されません。
Testing of assertions can be enabled or disabled via the configuration
parameter <literal>plpgsql.check_asserts</literal>, which takes a Boolean
value; the default is <literal>on</literal>. If this parameter
is <literal>off</literal> then <command>ASSERT</command> statements do nothing.
アサート検査は、設定パラメータplpgsql.check_assertsで有効または無効にできます。設定値は真偽値で、デフォルトはonです。
offのときには、ASSERT文は何もしません。
Note that <command>ASSERT</command> is meant for detecting program
bugs, not for reporting ordinary error conditions. Use
the <command>RAISE</command> statement, described above, for that.
ASSERTはプログラムのバグを見つけるためのものであって、通常のエラー条件を報告するものではないことに注意してください。
そのためには前述のRAISEを使ってください。