バージョンごとのドキュメント一覧

43.6. 制御構造 #

<title>Control Structures</title>

Control structures are probably the most useful (and important) part of <application>PL/pgSQL</application>. With <application>PL/pgSQL</application>'s control structures, you can manipulate <productname>PostgreSQL</productname> data in a very flexible and powerful way. 制御構造はおそらくPL/pgSQLの最も有用(かつ重要)な部分です。 PL/pgSQLの制御構造を使用して、PostgreSQLのデータを非常に柔軟、強力に操作することができます。

43.6.1. 関数からの復帰 #

<title>Returning from a Function</title>

There are two commands available that allow you to return data from a function: <command>RETURN</command> and <command>RETURN NEXT</command>. 関数からデータを返すために使用できるコマンドが2つあります。 RETURNおよびRETURN NEXTです。

43.6.1.1. RETURN #

RETURN expression;

<command>RETURN</command> with an expression terminates the function and returns the value of <replaceable>expression</replaceable> to the caller. This form is used for <application>PL/pgSQL</application> functions that do not return a set. 式を持つRETURNは関数を終了し、expressionの値を呼び出し元に返します。 この形式は集合を返さないPL/pgSQL関数で使用されます。

In a function that returns a scalar type, the expression's result will automatically be cast into the function's return type as described for assignments. But to return a composite (row) value, you must write an expression delivering exactly the requested column set. This may require use of explicit casting. スカラ型を返す関数において、代入のところで説明したように、式の結果は自動的に関数の戻り値の型にキャストされます。 しかし、複合(行)値を返すためには、要求された列集合を正確に導出する式を記述しなければなりません。 これにより、明示的なキャストの使用が必要となることがあります。

If you declared the function with output parameters, write just <command>RETURN</command> with no expression. The current values of the output parameter variables will be returned. 出力パラメータを持った関数を宣言した時は、式の無いRETURNを記述してください。 その時点における出力パラメータの値が返されます。

If you declared the function to return <type>void</type>, a <command>RETURN</command> statement can be used to exit the function early; but do not write an expression following <command>RETURN</command>. voidを返すように関数を宣言した場合でも、関数を直ちに抜け出すためにRETURNを使用できますが、RETURNの後に式を記述しないでください。

The return value of a function cannot be left undefined. If control reaches the end of the top-level block of the function without hitting a <command>RETURN</command> statement, a run-time error will occur. This restriction does not apply to functions with output parameters and functions returning <type>void</type>, however. In those cases a <command>RETURN</command> statement is automatically executed if the top-level block finishes. 関数の戻り値は未定義とさせたままにすることはできません。 制御が、RETURN文が見つからない状態で関数の最上位のブロックの終わりまで達した時、実行時エラーが発生します。 しかし、この制限は出力パラメータを持った関数及びvoidを返す関数には当てはまりません。 このような場合は最上位のブロックが終わった時、RETURN文が自動的に実行されます。

Some examples: 例を示します。


&#45;- functions returning a scalar type

-- スカラ型を返す関数
RETURN 1 + 2;
RETURN scalar_var;


&#45;- functions returning a composite type

-- 複合型を返す関数
RETURN composite_type_var;

RETURN (1, 2, 'three'::text);  &#45;- must cast columns to correct types

RETURN (1, 2, 'three'::text);  -- 正しい型の列にキャストしなければなりません

43.6.1.2. RETURN NEXTおよびRETURN QUERY #

<title><command>RETURN NEXT</command> and <command>RETURN QUERY</command></title>
RETURN NEXT expression;
RETURN QUERY query;
RETURN QUERY EXECUTE command-string [ USING expression [, ... ] ];

When a <application>PL/pgSQL</application> function is declared to return <literal>SETOF <replaceable>sometype</replaceable></literal>, the procedure to follow is slightly different. In that case, the individual items to return are specified by a sequence of <command>RETURN NEXT</command> or <command>RETURN QUERY</command> commands, and then a final <command>RETURN</command> command with no argument is used to indicate that the function has finished executing. <command>RETURN NEXT</command> can be used with both scalar and composite data types; with a composite result type, an entire <quote>table</quote> of results will be returned. <command>RETURN QUERY</command> appends the results of executing a query to the function's result set. <command>RETURN NEXT</command> and <command>RETURN QUERY</command> can be freely intermixed in a single set-returning function, in which case their results will be concatenated. PL/pgSQL関数がSETOF sometypeを返すように宣言した場合、後続の処理が多少違います。 この場合、戻り値の個々の項目は、RETURN NEXTコマンドまたはRETURN QUERYコマンドで指定されます。 そして、引数のない最後のRETURNコマンドにより、関数が実行を終了したことが示されます。 RETURN NEXTは、スカラ型および複合型の両方で使用することができます。 複合型の場合、結果のテーブル全体が返されます。 RETURN QUERYは、問い合わせを実行した結果を関数の結果集合に追加します。 RETURN NEXTRETURN QUERYは、単一の集合を返す関数の中で自由に混合できます。 この場合、連結されたものが結果となります。

<command>RETURN NEXT</command> and <command>RETURN QUERY</command> do not actually return from the function &mdash; they simply append zero or more rows to the function's result set. Execution then continues with the next statement in the <application>PL/pgSQL</application> function. As successive <command>RETURN NEXT</command> or <command>RETURN QUERY</command> commands are executed, the result set is built up. A final <command>RETURN</command>, which should have no argument, causes control to exit the function (or you can just let control reach the end of the function). 実際には、RETURN NEXTおよびRETURN QUERYは関数から戻りません。 単に関数の結果集合に行を追加しているだけです。 そして、その実行はPL/pgSQL関数内の次の文に継続します。 RETURN NEXTまたはRETURN QUERYコマンドが連続して実行されると、結果集合が作成されます。 最後の、引数を持ってはならないRETURNにより、関数の終了を制御します (または制御を関数の最後に移すことができます)。

<command>RETURN QUERY</command> has a variant <command>RETURN QUERY EXECUTE</command>, which specifies the query to be executed dynamically. Parameter expressions can be inserted into the computed query string via <literal>USING</literal>, in just the same way as in the <command>EXECUTE</command> command. RETURN QUERYにはRETURN QUERY EXECUTEという亜種があり、それは問い合わせが動的に実行されることを指定します。 パラメータ式を、EXECUTEコマンド内と全く同じように、USINGによって演算された問い合わせ文字列に挿入することができます。

If you declared the function with output parameters, write just <command>RETURN NEXT</command> with no expression. On each execution, the current values of the output parameter variable(s) will be saved for eventual return as a row of the result. Note that you must declare the function as returning <literal>SETOF record</literal> when there are multiple output parameters, or <literal>SETOF <replaceable>sometype</replaceable></literal> when there is just one output parameter of type <replaceable>sometype</replaceable>, in order to create a set-returning function with output parameters. 出力パラメータを持つ関数を宣言した時は、式の無いRETURN NEXTだけを記述してください。 実行の度に、その時点における出力パラメータの値が、関数からの戻り値のために結果の行として保存されます。 出力パラメータを持つ集合を返す関数を作成するためには、出力パラメータが複数の時はSETOF recordを返すように関数を宣言し、単一のsometype型の出力パラメータの時はSETOF sometypeを返すように関数を宣言しなければならないことに注意してください。

Here is an example of a function using <command>RETURN NEXT</command>: RETURN NEXTを使用する関数の例を以下に示します。

CREATE TABLE foo (fooid INT, foosubid INT, fooname TEXT);
INSERT INTO foo VALUES (1, 2, 'three');
INSERT INTO foo VALUES (4, 5, 'six');

CREATE OR REPLACE FUNCTION get_all_foo() RETURNS SETOF foo AS
$BODY$
DECLARE
    r foo%rowtype;
BEGIN
    FOR r IN
        SELECT * FROM foo WHERE fooid > 0
    LOOP

        &#45;- can do some processing here
        RETURN NEXT r; &#45;- return current row of SELECT

        -- ここで処理を実行できます
        RETURN NEXT r; -- SELECTの現在の行を返します
    END LOOP;
    RETURN;
END;
$BODY$
LANGUAGE plpgsql;

SELECT * FROM get_all_foo();

Here is an example of a function using <command>RETURN QUERY</command>: RETURN QUERYを使用する関数の例を以下に示します。

CREATE FUNCTION get_available_flightid(date) RETURNS SETOF integer AS
$BODY$
BEGIN
    RETURN QUERY SELECT flightid
                   FROM flight
                  WHERE flightdate >= $1
                    AND flightdate < ($1 + 1);


    &#45;- Since execution is not finished, we can check whether rows were returned
    &#45;- and raise exception if not.

-- 実行が終わっていないので、行が返されたか検査して、行がなければ例外を発生させます。
    IF NOT FOUND THEN
        RAISE EXCEPTION 'No flight at %.', $1;
    END IF;

    RETURN;
 END;
$BODY$
LANGUAGE plpgsql;


&#45;- Returns available flights or raises exception if there are no
&#45;- available flights.

-- 利用できるフライトを返し、フライトがない場合は例外を発生させます。
SELECT * FROM get_available_flightid(CURRENT_DATE);

注記

The current implementation of <command>RETURN NEXT</command> and <command>RETURN QUERY</command> stores the entire result set before returning from the function, as discussed above. That means that if a <application>PL/pgSQL</application> function produces a very large result set, performance might be poor: data will be written to disk to avoid memory exhaustion, but the function itself will not return until the entire result set has been generated. A future version of <application>PL/pgSQL</application> might allow users to define set-returning functions that do not have this limitation. Currently, the point at which data begins being written to disk is controlled by the <xref linkend="guc-work-mem"/> configuration variable. Administrators who have sufficient memory to store larger result sets in memory should consider increasing this parameter. 上記のように、RETURN NEXTおよびRETURN QUERYの現在の実装では、関数から返される前に結果集合全体を保管します。 これにより、PL/pgSQL関数が非常に大量の結果集合を返した場合、性能が低下する可能性があります。 メモリの枯渇を避けるため、データはディスクに書き込まれます。 しかし、関数自体は結果集合全体が生成されるまでは戻りません。 将来のPL/pgSQLのバージョンでは、この制限を受けずに集合を返す関数をユーザが定義できるようになるかもしれません。 現在、データがディスクに書き込まれ始まる時点はwork_mem設定変数によって制御されています。 大量の結果集合を保管するのに十分なメモリがある場合、管理者はこのパラメータの値を大きくすることを考慮すべきです。

43.6.2. プロシージャからの戻り #

<title>Returning from a Procedure</title>

A procedure does not have a return value. A procedure can therefore end without a <command>RETURN</command> statement. If you wish to use a <command>RETURN</command> statement to exit the code early, write just <command>RETURN</command> with no expression. プロシージャは戻り値を持ちません。 したがって、プロシージャはRETURN文なしで終了できます。 早期にコードを抜けるためにRETURN文を使いたいときには、式を伴わないRETURNだけを書いてください。

If the procedure has output parameters, the final values of the output parameter variables will be returned to the caller. プロシージャが出力パラメータを持っている場合、出力パラメータ変数の最終の値が呼び出し元に返されます。

43.6.3. プロシージャを呼び出す #

<title>Calling a Procedure</title>

A <application>PL/pgSQL</application> function, procedure, or <command>DO</command> block can call a procedure using <command>CALL</command>. Output parameters are handled differently from the way that <command>CALL</command> works in plain SQL. Each <literal>OUT</literal> or <literal>INOUT</literal> parameter of the procedure must correspond to a variable in the <command>CALL</command> statement, and whatever the procedure returns is assigned back to that variable after it returns. For example: PL/pgSQLの関数、プロシージャ、DOブロックは、CALLを使ってプロシージャを呼び出しできます。 CALLを普通のSQLで実行する場合とは、出力パラメータの扱いが異なります。 プロシージャの各OUTもしくはINOUTパラメータはCALL文の変数と対応しなければならず、プロシージャが返すものはすべて、CALL文が返った後にこの変数に書き戻されます。 以下に例を示します。

CREATE PROCEDURE triple(INOUT x int)
LANGUAGE plpgsql
AS $$
BEGIN
    x := x * 3;
END;
$$;

DO $$
DECLARE myvar int := 5;
BEGIN
  CALL triple(myvar);
  RAISE NOTICE 'myvar = %', myvar;  -- prints 15
END;
$$;

The variable corresponding to an output parameter can be a simple variable or a field of a composite-type variable. Currently, it cannot be an element of an array. 出力パラメータに対応する変数は、単純な変数または複合型の変数のフィールドです。 今のところ配列の要素は使えません。

43.6.4. 条件分岐 #

<title>Conditionals</title>

<command>IF</command> and <command>CASE</command> statements let you execute alternative commands based on certain conditions. <application>PL/pgSQL</application> has three forms of <command>IF</command>: IFCASE文はある条件に基づいて代わりのコマンドを実行させます。 PL/pgSQLには、以下のような3つのIFの形式があります。

  • IF ... THEN ... END IF

  • IF ... THEN ... ELSE ... END IF

  • IF ... THEN ... ELSIF ... THEN ... ELSE ... END IF

and two forms of <command>CASE</command>: また、以下のような2つのCASEの形式があります。

  • CASE ... WHEN ... THEN ... ELSE ... END CASE

  • CASE WHEN ... THEN ... ELSE ... END CASE

43.6.4.1. IF-THEN #

IF boolean-expression THEN
    statements
END IF;

<literal>IF-THEN</literal> statements are the simplest form of <literal>IF</literal>. The statements between <literal>THEN</literal> and <literal>END IF</literal> will be executed if the condition is true. Otherwise, they are skipped. IF-THEN文は最も単純なIFの形式です。 THENEND IFの間の文が条件が真の場合に実行されます。 さもなければそれらは飛ばされます。

Example: 例:

IF v_user_id <> 0 THEN
    UPDATE users SET email = v_email WHERE user_id = v_user_id;
END IF;

43.6.4.2. IF-THEN-ELSE #

IF boolean-expression THEN
    statements
ELSE
    statements
END IF;

<literal>IF-THEN-ELSE</literal> statements add to <literal>IF-THEN</literal> by letting you specify an alternative set of statements that should be executed if the condition is not true. (Note this includes the case where the condition evaluates to NULL.) IF-THEN-ELSE文はIF-THENに加え、条件評価が偽の場合に実行すべき代替となる文の集合を指定することができます。 (これには条件がNULLと評価した場合も含まれることに注意してください。)

Examples: 例:

IF parentid IS NULL OR parentid = ''
THEN
    RETURN fullname;
ELSE
    RETURN hp_true_filename(parentid) || '/' || fullname;
END IF;

IF v_count > 0 THEN
    INSERT INTO users_count (count) VALUES (v_count);
    RETURN 't';
ELSE
    RETURN 'f';
END IF;

43.6.4.3. IF-THEN-ELSIF #

IF boolean-expression THEN
    statements
[ ELSIF boolean-expression THEN
    statements
[ ELSIF boolean-expression THEN
    statements
    ...
]
]
[ ELSE
    statements ]
END IF;

Sometimes there are more than just two alternatives. <literal>IF-THEN-ELSIF</literal> provides a convenient method of checking several alternatives in turn. The <literal>IF</literal> conditions are tested successively until the first one that is true is found. Then the associated statement(s) are executed, after which control passes to the next statement after <literal>END IF</literal>. (Any subsequent <literal>IF</literal> conditions are <emphasis>not</emphasis> tested.) If none of the <literal>IF</literal> conditions is true, then the <literal>ELSE</literal> block (if any) is executed. 選択肢が2つだけではなくより多くになる場合があります。 IF-THEN-ELSIFは、順番に複数の代替手段を検査する、より便利な方法を提供します。 IF条件は最初の真である結果が見つかるまで連続して検査されます。 そして関連した文が実行され、その後END IF以降の次の文に制御が渡されます。 (以降にあるIF条件の検査はすべて実行されません。) 全てのIF条件が真でない場合、ELSEブロックが(もし存在すれば)実行されます。

Here is an example: 以下に例を示します。

IF number = 0 THEN
    result := 'zero';
ELSIF number > 0 THEN
    result := 'positive';
ELSIF number < 0 THEN
    result := 'negative';
ELSE

    &#45;- hmm, the only other possibility is that number is null

    -- ふうむ、残る唯一の可能性はその値がNULLであることだ
    result := 'NULL';
END IF;

The key word <literal>ELSIF</literal> can also be spelled <literal>ELSEIF</literal>. ELSIFキーワードはELSEIFのように書くことができます。

An alternative way of accomplishing the same task is to nest <literal>IF-THEN-ELSE</literal> statements, as in the following example: 同じ作業を遂行する別の方法は、以下の例のようにIF-THEN-ELSE文を入れ子にすることです。

IF demo_row.sex = 'm' THEN
    pretty_sex := 'man';
ELSE
    IF demo_row.sex = 'f' THEN
        pretty_sex := 'woman';
    END IF;
END IF;

However, this method requires writing a matching <literal>END IF</literal> for each <literal>IF</literal>, so it is much more cumbersome than using <literal>ELSIF</literal> when there are many alternatives. しかし、この方法はそれぞれのIFに対応するEND IFの記述が必要です。 従って、多くの選択肢がある場合ELSIFを使用するよりも厄介です。

43.6.4.4. 単純なCASE #

<title>Simple <literal>CASE</literal></title>
CASE search-expression
    WHEN expression [, expression [ ... ]] THEN
      statements
  [ WHEN expression [, expression [ ... ]] THEN
      statements
    ... ]
  [ ELSE
      statements ]
END CASE;

The simple form of <command>CASE</command> provides conditional execution based on equality of operands. The <replaceable>search-expression</replaceable> is evaluated (once) and successively compared to each <replaceable>expression</replaceable> in the <literal>WHEN</literal> clauses. If a match is found, then the corresponding <replaceable>statements</replaceable> are executed, and then control passes to the next statement after <literal>END CASE</literal>. (Subsequent <literal>WHEN</literal> expressions are not evaluated.) If no match is found, the <literal>ELSE</literal> <replaceable>statements</replaceable> are executed; but if <literal>ELSE</literal> is not present, then a <literal>CASE_NOT_FOUND</literal> exception is raised. CASEの単純な形式はオペランドの等価性にもとづく条件的実行を提供します。 search-expressionは(一度だけ)評価され、その後WHEN句内のそれぞれのexpressionと比較されます。 一致するものが見つかると、関連したstatementsが実行され、END CASEの次の文に制御が渡されます。 (以降のWHEN式は評価されません。) 一致するものが見つからない場合、ELSE statementsが実行されますが、ELSEが無いときはCASE_NOT_FOUND例外を引き起こします。

Here is a simple example: 以下は簡単な例です。

CASE x
    WHEN 1, 2 THEN
        msg := 'one or two';
    ELSE
        msg := 'other value than one or two';
END CASE;

43.6.4.5. 検索付きCASE #

<title>Searched <literal>CASE</literal></title>
CASE
    WHEN boolean-expression THEN
      statements
  [ WHEN boolean-expression THEN
      statements
    ... ]
  [ ELSE
      statements ]
END CASE;

The searched form of <command>CASE</command> provides conditional execution based on truth of Boolean expressions. Each <literal>WHEN</literal> clause's <replaceable>boolean-expression</replaceable> is evaluated in turn, until one is found that yields <literal>true</literal>. Then the corresponding <replaceable>statements</replaceable> are executed, and then control passes to the next statement after <literal>END CASE</literal>. (Subsequent <literal>WHEN</literal> expressions are not evaluated.) If no true result is found, the <literal>ELSE</literal> <replaceable>statements</replaceable> are executed; but if <literal>ELSE</literal> is not present, then a <literal>CASE_NOT_FOUND</literal> exception is raised. CASEの検索された形式は論理値式の真の結果に基づく条件付き実行を提供します。 それぞれのWHEN句のboolean-expressiontrueとなる1つが見つかるまで順番に評価されます。 その後、関連するstatementsが実行され、その結果END CASEの次の文に制御が渡されます。 (以降のWHEN式は評価されません。) 真となる結果が見つからない場合、ELSE statementsが実行されますが、ELSEが存在しないときはCASE_NOT_FOUND例外を引き起こします。

Here is an example: 以下は簡単な例です。

CASE
    WHEN x BETWEEN 0 AND 10 THEN
        msg := 'value is between zero and ten';
    WHEN x BETWEEN 11 AND 20 THEN
        msg := 'value is between eleven and twenty';
END CASE;

This form of <command>CASE</command> is entirely equivalent to <literal>IF-THEN-ELSIF</literal>, except for the rule that reaching an omitted <literal>ELSE</literal> clause results in an error rather than doing nothing. この形式のCASEは、判定基準が省略されたELSE句に達した場合に何もしないのではなくエラーなる点を除き、IF-THEN-ELSIFと全く同一です。

43.6.5. 単純なループ #

<title>Simple Loops</title>

With the <literal>LOOP</literal>, <literal>EXIT</literal>, <literal>CONTINUE</literal>, <literal>WHILE</literal>, <literal>FOR</literal>, and <literal>FOREACH</literal> statements, you can arrange for your <application>PL/pgSQL</application> function to repeat a series of commands. LOOPEXITCONTINUEWHILEFORFOREACH文を使用して、PL/pgSQL関数で、一連のコマンドを繰り返すことができます。

43.6.5.1. LOOP #

[ <<label>> ]
LOOP
    statements
END LOOP [ label ];

<literal>LOOP</literal> defines an unconditional loop that is repeated indefinitely until terminated by an <literal>EXIT</literal> or <command>RETURN</command> statement. The optional <replaceable>label</replaceable> can be used by <literal>EXIT</literal> and <literal>CONTINUE</literal> statements within nested loops to specify which loop those statements refer to. LOOPは、EXIT文またはRETURN文によって終了されるまで無限に繰り返される、条件なしのループを定義します。 省略可能なlabelは、ネステッドループにおいてEXITおよびCONTINUE文がどのレベルの入れ子を参照するかを指定するために使用されます。

43.6.5.2. EXIT #

EXIT [ label ] [ WHEN boolean-expression ];

If no <replaceable>label</replaceable> is given, the innermost loop is terminated and the statement following <literal>END LOOP</literal> is executed next. If <replaceable>label</replaceable> is given, it must be the label of the current or some outer level of nested loop or block. Then the named loop or block is terminated and control continues with the statement after the loop's/block's corresponding <literal>END</literal>. labelが指定されない場合、最も内側のループを終わらせ、END LOOPの次の文がその後に実行されます。 labelが指定された場合、それは現在またはその上位のネステッドループやブロックのラベルである必要があります。 その後、指名されたループまたはブロックを終わらせ、そのループまたはブロックの対応するENDの次の文に制御を移します。

If <literal>WHEN</literal> is specified, the loop exit occurs only if <replaceable>boolean-expression</replaceable> is true. Otherwise, control passes to the statement after <literal>EXIT</literal>. WHENが指定された場合、boolean-expressionが真の場合のみループの終了が起こります。 さもなければ、EXITの後の行に制御が移ります。

<literal>EXIT</literal> can be used with all types of loops; it is not limited to use with unconditional loops. EXITは、すべての種類のループと共に使用できます。 条件なしのループでの使用に限定されません。

When used with a <literal>BEGIN</literal> block, <literal>EXIT</literal> passes control to the next statement after the end of the block. Note that a label must be used for this purpose; an unlabeled <literal>EXIT</literal> is never considered to match a <literal>BEGIN</literal> block. (This is a change from pre-8.4 releases of <productname>PostgreSQL</productname>, which would allow an unlabeled <literal>EXIT</literal> to match a <literal>BEGIN</literal> block.) BEGINブロックと共に使用した時、EXITによりブロックの次の文に制御が移ります。 この目的のためにラベルが使用されなければならないことに注意してください。 ラベル無しのEXITBEGINブロックに対応するとは決して考えられません。 (これは、ラベル無しのEXITBEGINブロックに対応することを許容するPostgreSQLの8.4より前のリリースからの変更です。)

Examples: 例:

LOOP

    &#45;- some computations

    -- 何らかの演算
    IF count > 0 THEN

        EXIT;  &#45;- exit loop

        EXIT;  -- ループを抜け出す
    END IF;
END LOOP;

LOOP

    &#45;- some computations
    EXIT WHEN count &gt; 0;  &#45;- same result as previous example

-- 何らかの演算
    EXIT WHEN count > 0;  -- 上例と同じ結果
END LOOP;

<<ablock>>
BEGIN

    &#45;- some computations

    -- 何らかの演算
    IF stocks > 100000 THEN

        EXIT ablock;  &#45;- causes exit from the BEGIN block

        EXIT ablock;  -- これによりBEGINブロックを抜け出す
    END IF;

    &#45;- computations here will be skipped when stocks &gt; 100000

    -- stocks > 100000 であればここでの演算は省略
END;

43.6.5.3. CONTINUE #

CONTINUE [ label ] [ WHEN boolean-expression ];

If no <replaceable>label</replaceable> is given, the next iteration of the innermost loop is begun. That is, all statements remaining in the loop body are skipped, and control returns to the loop control expression (if any) to determine whether another loop iteration is needed. If <replaceable>label</replaceable> is present, it specifies the label of the loop whose execution will be continued. labelが無い場合、すぐ外側のループの次の繰り返しが開始されます。 すなわち、ループ本体の残りの文は飛ばされて、他のループの繰り返しが必要かどうかを決めるため、制御がループ制御式(もし存在すれば)に戻ります。 labelが存在する場合、実行を継続するループのラベルを指定します。

If <literal>WHEN</literal> is specified, the next iteration of the loop is begun only if <replaceable>boolean-expression</replaceable> is true. Otherwise, control passes to the statement after <literal>CONTINUE</literal>. WHENが指定された場合、boolean-expressionが真の場合のみループにおける次の繰り返しが始まります。 さもなければ、CONTINUEの後の行に制御が移ります。

<literal>CONTINUE</literal> can be used with all types of loops; it is not limited to use with unconditional loops. CONTINUEは全ての種類のループで使用可能です。 条件なしのループに限定されません。

Examples:

LOOP

    &#45;- some computations

    -- 何らかの演算
    EXIT WHEN count > 100;
    CONTINUE WHEN count < 50;

    &#45;- some computations for count IN [50 .. 100]

    -- 50から100を数える、何らかの演算
END LOOP;

43.6.5.4. WHILE #

[ <<label>> ]
WHILE boolean-expression LOOP
    statements
END LOOP [ label ];

The <literal>WHILE</literal> statement repeats a sequence of statements so long as the <replaceable>boolean-expression</replaceable> evaluates to true. The expression is checked just before each entry to the loop body. WHILE文はboolean-expressionの評価が真である間、一連の文を繰り返します。 条件式は、ループ本体に入る前にのみ検査されます。

For example: 以下に例を示します。

WHILE amount_owed > 0 AND gift_certificate_balance > 0 LOOP

    &#45;- some computations here

    -- ここで演算をいくつか行います。
END LOOP;

WHILE NOT done LOOP

    &#45;- some computations here

    -- ここで演算をいくつか行います。
END LOOP;

43.6.5.5. 整数FORループ #

<title><literal>FOR</literal> (Integer Variant)</title>
[ <<label>> ]
FOR name IN [ REVERSE ] expression .. expression [ BY expression ] LOOP
    statements
END LOOP [ label ];

This form of <literal>FOR</literal> creates a loop that iterates over a range of integer values. The variable <replaceable>name</replaceable> is automatically defined as type <type>integer</type> and exists only inside the loop (any existing definition of the variable name is ignored within the loop). The two expressions giving the lower and upper bound of the range are evaluated once when entering the loop. If the <literal>BY</literal> clause isn't specified the iteration step is 1, otherwise it's the value specified in the <literal>BY</literal> clause, which again is evaluated once on loop entry. If <literal>REVERSE</literal> is specified then the step value is subtracted, rather than added, after each iteration. この形式のFORは整数値の範囲内で繰り返すループを生成します。 name変数はinteger型として自動的に定義され、ループ内部のみで存在します (ループ外部で定義しても、ループ内部では全て無視されます)。 範囲の下限、上限として与えられる2つの式はループに入った時に一度だけ評価されます。 BY句を指定しない時の繰り返し刻みは1ですが、BY句を用いて指定でき、ループに入った時に一度だけ評価されます。 REVERSEが指定された場合、繰り返し刻みの値は加算されるのではなく、繰り返しごとに減算されます。

Some examples of integer <literal>FOR</literal> loops: 整数FORループの例を以下に示します。

FOR i IN 1..10 LOOP

    &#45;- i will take on the values 1,2,3,4,5,6,7,8,9,10 within the loop

    -- i はループ内で 1、2、3、4、5、6、7、8、9、10 の値を取ります。
END LOOP;

FOR i IN REVERSE 10..1 LOOP

    &#45;- i will take on the values 10,9,8,7,6,5,4,3,2,1 within the loop

    -- i はループ内で 10、9、8、7、6、5、4、3、2、1 の値を取ります。
END LOOP;

FOR i IN REVERSE 10..1 BY 2 LOOP

    &#45;- i will take on the values 10,8,6,4,2 within the loop

    -- i はループ内で 10、8、6、4、2 の値を取ります。
END LOOP;

If the lower bound is greater than the upper bound (or less than, in the <literal>REVERSE</literal> case), the loop body is not executed at all. No error is raised. 下限が上限よりも大きい(REVERSEの場合はより小さい)場合、ループ本体はまったく実行されません。 エラーは発生しません。

If a <replaceable>label</replaceable> is attached to the <literal>FOR</literal> loop then the integer loop variable can be referenced with a qualified name, using that <replaceable>label</replaceable>. labelFORループに付加することにより、labelを用いて修飾した名前の整数ループ変数を参照できます。

43.6.6. 問い合わせ結果による繰り返し #

<title>Looping through Query Results</title>

Using a different type of <literal>FOR</literal> loop, you can iterate through the results of a query and manipulate that data accordingly. The syntax is: 別の種類のFORループを使用して、問い合わせの結果を繰り返し、そのデータを扱うことができます。 以下に構文を示します。

[ <<label>> ]
FOR target IN query LOOP
    statements
END LOOP [ label ];

The <replaceable>target</replaceable> is a record variable, row variable, or comma-separated list of scalar variables. The <replaceable>target</replaceable> is successively assigned each row resulting from the <replaceable>query</replaceable> and the loop body is executed for each row. Here is an example: targetは、レコード変数、行変数またはカンマで区切ったスカラ変数のリストです。 targetには順次、queryの結果の全ての行が代入され、各行に対してループ本体が実行されます。 以下に例を示します。

CREATE FUNCTION refresh_mviews() RETURNS integer AS $$
DECLARE
    mviews RECORD;
BEGIN
    RAISE NOTICE 'Refreshing all materialized views...';

    FOR mviews IN
       SELECT n.nspname AS mv_schema,
              c.relname AS mv_name,
              pg_catalog.pg_get_userbyid(c.relowner) AS owner
         FROM pg_catalog.pg_class c
    LEFT JOIN pg_catalog.pg_namespace n ON (n.oid = c.relnamespace)
        WHERE c.relkind = 'm'
     ORDER BY 1
    LOOP


        &#45;- Now "mviews" has one record with information about the materialized view

        -- ここで"mviews"はマテリアライズドビューの情報の1つのレコードを持ちます

        RAISE NOTICE 'Refreshing materialized view %.% (owner: %)...',
                     quote_ident(mviews.mv_schema),
                     quote_ident(mviews.mv_name),
                     quote_ident(mviews.owner);
        EXECUTE format('REFRESH MATERIALIZED VIEW %I.%I', mviews.mv_schema, mviews.mv_name);
    END LOOP;

    RAISE NOTICE 'Done refreshing materialized views.';
    RETURN 1;
END;
$$ LANGUAGE plpgsql;

If the loop is terminated by an <literal>EXIT</literal> statement, the last assigned row value is still accessible after the loop. このループがEXIT文で終了した場合、最後に割り当てられた行の値はループを抜けた後でもアクセスすることができます。

The <replaceable>query</replaceable> used in this type of <literal>FOR</literal> statement can be any SQL command that returns rows to the caller: <command>SELECT</command> is the most common case, but you can also use <command>INSERT</command>, <command>UPDATE</command>, or <command>DELETE</command> with a <literal>RETURNING</literal> clause. Some utility commands such as <command>EXPLAIN</command> will work too. この種類のFOR文のqueryとしては、呼び出し元に行を返すSQLコマンドをすべて使用できます。 通常はSELECTですが、RETURNING句を持つINSERTUPDATEまたはDELETEも使用できます。 EXPLAINなどのユーティリティコマンドも作動します。

<application>PL/pgSQL</application> variables are replaced by query parameters, and the query plan is cached for possible re-use, as discussed in detail in <xref linkend="plpgsql-var-subst"/> and <xref linkend="plpgsql-plan-caching"/>. PL/pgSQL変数は問い合わせパラメータで置き換えられます。 問い合わせ計画は、43.11.1および43.11.2で述べたように、再利用のためにキャッシュされます。

The <literal>FOR-IN-EXECUTE</literal> statement is another way to iterate over rows: FOR-IN-EXECUTE文は行を繰り返すもう1つの方法です。

[ <<label>> ]
FOR target IN EXECUTE text_expression [ USING expression [, ... ] ] LOOP
    statements
END LOOP [ label ];

This is like the previous form, except that the source query is specified as a string expression, which is evaluated and replanned on each entry to the <literal>FOR</literal> loop. This allows the programmer to choose the speed of a preplanned query or the flexibility of a dynamic query, just as with a plain <command>EXECUTE</command> statement. As with <command>EXECUTE</command>, parameter values can be inserted into the dynamic command via <literal>USING</literal>. この方法は、問い合わせのソースが文字列式で指定される点を除き、前の形式と似ています。 この式はFORループの各項目で評価され、再計画が行われます。 これにより、プログラマは、通常のEXECUTE文と同じように事前に計画された問い合わせによる高速性と、動的な問い合わせの持つ柔軟性を選択することができます。 EXECUTEの場合と同様、パラメータ値はUSINGにより動的コマンドに挿入できます。

Another way to specify the query whose results should be iterated through is to declare it as a cursor. This is described in <xref linkend="plpgsql-cursor-for-loop"/>. 結果を通して繰り返さなければならない問い合わせを指定するもう1つの方法として、カーソルの宣言があります。 これは43.7.4で説明します。

43.6.7. 配列を巡回 #

<title>Looping through Arrays</title>

The <literal>FOREACH</literal> loop is much like a <literal>FOR</literal> loop, but instead of iterating through the rows returned by an SQL query, it iterates through the elements of an array value. (In general, <literal>FOREACH</literal> is meant for looping through components of a composite-valued expression; variants for looping through composites besides arrays may be added in future.) The <literal>FOREACH</literal> statement to loop over an array is: FOREACHループはFORループにとてもよく似ています。 しかし、SQL 問い合わせが抽出した行を繰り返す代わりに、配列の要素を繰り返します。 (一般的にFOREACHは、複合値で表現される構成要素の巡回を意味しますが、配列でない複合値も巡回する亜種が将来は追加されるかもしれません。) 配列を巡回するFOREACH文を示します。

[ <<label>> ]
FOREACH target [ SLICE number ] IN ARRAY expression LOOP
    statements
END LOOP [ label ];

Without <literal>SLICE</literal>, or if <literal>SLICE 0</literal> is specified, the loop iterates through individual elements of the array produced by evaluating the <replaceable>expression</replaceable>. The <replaceable>target</replaceable> variable is assigned each element value in sequence, and the loop body is executed for each element. Here is an example of looping through the elements of an integer array: SLICEがない、またはSLICE 0が指定された場合、ループはexpressionによって評価されて作成された配列の各要素を繰り返します。 target変数が各要素の値に順次割り当てられ、各要素に対してループ本体が実行されます。 整数配列の要素を巡回する例を示します。

CREATE FUNCTION sum(int[]) RETURNS int8 AS $$
DECLARE
  s int8 := 0;
  x int;
BEGIN
  FOREACH x IN ARRAY $1
  LOOP
    s := s + x;
  END LOOP;
  RETURN s;
END;
$$ LANGUAGE plpgsql;

The elements are visited in storage order, regardless of the number of array dimensions. Although the <replaceable>target</replaceable> is usually just a single variable, it can be a list of variables when looping through an array of composite values (records). In that case, for each array element, the variables are assigned from successive columns of the composite value. 配列の次元数に関係なく、要素は格納した順番で処理されます。 通常targetは単一の変数ですが、複合値(レコード)の配列を巡回するときは、変数のリストも可能です。 その場合、配列の各要素に対して、変数は複合値(レコード)の列から連続的に割り当てられます。

With a positive <literal>SLICE</literal> value, <literal>FOREACH</literal> iterates through slices of the array rather than single elements. The <literal>SLICE</literal> value must be an integer constant not larger than the number of dimensions of the array. The <replaceable>target</replaceable> variable must be an array, and it receives successive slices of the array value, where each slice is of the number of dimensions specified by <literal>SLICE</literal>. Here is an example of iterating through one-dimensional slices: 正のSLICE値を持つ場合、FOREACHは単一の要素ではなく多次元配列の低次元部分配列を通して繰り返します。 SLICE値は、配列の次元数より小さい整数定数でなければなりません。 target変数は配列でなければなりません。 この変数は、配列値から連続した部分配列を受けとります ここで部分配列はSLICEで指定した次数となります。 以下に1次元の部分配列を通した繰り返しの例を示します。

CREATE FUNCTION scan_rows(int[]) RETURNS void AS $$
DECLARE
  x int[];
BEGIN
  FOREACH x SLICE 1 IN ARRAY $1
  LOOP
    RAISE NOTICE 'row = %', x;
  END LOOP;
END;
$$ LANGUAGE plpgsql;

SELECT scan_rows(ARRAY[[1,2,3],[4,5,6],[7,8,9],[10,11,12]]);

NOTICE:  row = {1,2,3}
NOTICE:  row = {4,5,6}
NOTICE:  row = {7,8,9}
NOTICE:  row = {10,11,12}

43.6.8. エラーの捕捉 #

<title>Trapping Errors</title>

By default, any error occurring in a <application>PL/pgSQL</application> function aborts execution of the function and the surrounding transaction. You can trap errors and recover from them by using a <command>BEGIN</command> block with an <literal>EXCEPTION</literal> clause. The syntax is an extension of the normal syntax for a <command>BEGIN</command> block: デフォルトでは、PL/pgSQL関数の内部でエラーが発生すると関数とそれを囲むトランザクションをアボートします。 BEGINブロックおよびEXCEPTION句を使用すれば、エラーを捕捉してその状態から回復できます。 その構文は通常のBEGINブロックの構文を拡張したものです。

[ <<label>> ]
[ DECLARE
    declarations ]
BEGIN
    statements
EXCEPTION
    WHEN condition [ OR condition ... ] THEN
        handler_statements
    [ WHEN condition [ OR condition ... ] THEN
          handler_statements
      ... ]
END;

If no error occurs, this form of block simply executes all the <replaceable>statements</replaceable>, and then control passes to the next statement after <literal>END</literal>. But if an error occurs within the <replaceable>statements</replaceable>, further processing of the <replaceable>statements</replaceable> is abandoned, and control passes to the <literal>EXCEPTION</literal> list. The list is searched for the first <replaceable>condition</replaceable> matching the error that occurred. If a match is found, the corresponding <replaceable>handler_statements</replaceable> are executed, and then control passes to the next statement after <literal>END</literal>. If no match is found, the error propagates out as though the <literal>EXCEPTION</literal> clause were not there at all: the error can be caught by an enclosing block with <literal>EXCEPTION</literal>, or if there is none it aborts processing of the function. エラーが発生しない時、この形式のブロックは単に全てのstatementsを実行し、ENDの次の文に制御が移ります。 しかし、statementsの内部でエラーが発生すると、それ以後のstatementsの処理は中断され、EXCEPTIONリストに制御が移ります。 そしてリストの中から、発生したエラーと合致する最初のconditionを探します。 合致するものがあれば、対応するhandler_statementsを実行し、ENDの次の文に制御が移ります。 合致するものがなければ、EXCEPTION句が存在しないのと同じで、エラーは外側に伝播します。 EXCEPTIONを含んだ外側のブロックはエラーを捕捉できますが、失敗すると関数の処理は中断されます。

The <replaceable>condition</replaceable> names can be any of those shown in <xref linkend="errcodes-appendix"/>. A category name matches any error within its category. The special condition name <literal>OTHERS</literal> matches every error type except <literal>QUERY_CANCELED</literal> and <literal>ASSERT_FAILURE</literal>. (It is possible, but often unwise, to trap those two error types by name.) Condition names are not case-sensitive. Also, an error condition can be specified by <literal>SQLSTATE</literal> code; for example these are equivalent: 全てのconditionの名前は付録Aに示したもののいずれかを取ることができます。 分類名はそこに分類される全てのエラーに合致します。 OTHERSという特別の状態名はQUERY_CANCELEDASSERT_FAILUREを除く全てのエラーに合致します。 (QUERY_CANCELEDASSERT_FAILUREを名前で捕捉することは可能ですが、賢明ではありません。) 状態名は大文字小文字を区別しません。 同時に、エラー状態はSQLSTATEコードで指定可能です。 例えば以下は等価です。

WHEN division_by_zero THEN ...
WHEN SQLSTATE '22012' THEN ...

If a new error occurs within the selected <replaceable>handler_statements</replaceable>, it cannot be caught by this <literal>EXCEPTION</literal> clause, but is propagated out. A surrounding <literal>EXCEPTION</literal> clause could catch it. エラーが該当するhandler_statements内部で新たに発生した時、EXCEPTION句はそのエラーを捕捉できずエラーは外側に伝播します。 なお、上位のEXCEPTION句はそのエラーを捕捉できます。

When an error is caught by an <literal>EXCEPTION</literal> clause, the local variables of the <application>PL/pgSQL</application> function remain as they were when the error occurred, but all changes to persistent database state within the block are rolled back. As an example, consider this fragment: EXCEPTION句がエラーを捕捉した時、PL/pgSQL関数のローカル変数はエラーが起こった後の状態を保ちます。 しかし、ブロック内部における永続的なデータベースの状態は、ロールバックされます。 そのような例を以下に示します。

INSERT INTO mytab(firstname, lastname) VALUES('Tom', 'Jones');
BEGIN
    UPDATE mytab SET firstname = 'Joe' WHERE lastname = 'Jones';
    x := x + 1;
    y := x / 0;
EXCEPTION
    WHEN division_by_zero THEN
        RAISE NOTICE 'caught division_by_zero';
        RETURN x;
END;

When control reaches the assignment to <literal>y</literal>, it will fail with a <literal>division_by_zero</literal> error. This will be caught by the <literal>EXCEPTION</literal> clause. The value returned in the <command>RETURN</command> statement will be the incremented value of <literal>x</literal>, but the effects of the <command>UPDATE</command> command will have been rolled back. The <command>INSERT</command> command preceding the block is not rolled back, however, so the end result is that the database contains <literal>Tom Jones</literal> not <literal>Joe Jones</literal>. 制御が変数yの代入に移ると、division_by_zeroエラーとなり、EXCEPTION句がそのエラーを捕捉します。 RETURN文による関数の戻り値は、1を加算した後のxの値となりますが、UPDATEコマンドによる結果はロールバックされます。 しかし、前のブロックのINSERTコマンドはロールバックされません。 したがって、データベースの内容の最終結果はTom Jonesであり、Joe Jonesではありません。

ヒント

A block containing an <literal>EXCEPTION</literal> clause is significantly more expensive to enter and exit than a block without one. Therefore, don't use <literal>EXCEPTION</literal> without need. EXCEPTION句を含んだブロックの実行に要する時間は、含まないブロックに比べてとても長くなります。 したがって、必要のない時にEXCEPTIONを使用してはいけません。

例43.2 UPDATE/INSERTの例外

<title>Exceptions with <command>UPDATE</command>/<command>INSERT</command></title>

This example uses exception handling to perform either <command>UPDATE</command> or <command>INSERT</command>, as appropriate. It is recommended that applications use <command>INSERT</command> with <literal>ON CONFLICT DO UPDATE</literal> rather than actually using this pattern. This example serves primarily to illustrate use of <application>PL/pgSQL</application> control flow structures: これはUPDATEまたはINSERTの実行における例外処理を使用した適当な例題です。 アプリケーションでは実際にこの方式を使うよりも、ON CONFLICT DO UPDATEを伴ったINSERTを使うことが推奨されます。本例は主としてPL/pgSQLの制御構造の使い方を示すものです。

CREATE TABLE db (a INT PRIMARY KEY, b TEXT);

CREATE FUNCTION merge_db(key INT, data TEXT) RETURNS VOID AS
$$
BEGIN
    LOOP

        &#45;- first try to update the key

        -- 最初にキーを更新する
        UPDATE db SET b = data WHERE a = key;
        IF found THEN
            RETURN;
        END IF;

        &#45;- not there, so try to insert the key
        &#45;- if someone else inserts the same key concurrently,
        &#45;- we could get a unique-key failure

        -- キーが存在しないので、キーの挿入を試行する
        -- 他者がすでに同一のキーを挿入していたならば
        -- 一意性に違反する欠陥となります
        BEGIN
            INSERT INTO db(a,b) VALUES (key, data);
            RETURN;
        EXCEPTION WHEN unique_violation THEN

            &#45;- Do nothing, and loop to try the UPDATE again.

            -- 何もしないで、更新を再試行します
        END;
    END LOOP;
END;
$$
LANGUAGE plpgsql;

SELECT merge_db(1, 'david');
SELECT merge_db(1, 'dennis');

This coding assumes the <literal>unique_violation</literal> error is caused by the <command>INSERT</command>, and not by, say, an <command>INSERT</command> in a trigger function on the table. It might also misbehave if there is more than one unique index on the table, since it will retry the operation regardless of which index caused the error. More safety could be had by using the features discussed next to check that the trapped error was the one expected. このコーディングではunique_violationエラーの原因がINSERTによるものであり、テーブルのトリガ関数内部のINSERTによるものでないと仮定します。 また、テーブルに2つ以上の一意インデックスが存在した場合、どちらのインデックスがエラーの原因になろうと操作を再試行するので、誤作動となります。 捕捉したエラーが予測したものであるか検証するために、次節で記述するエラー情報を利用すれば、より安全となります。


43.6.8.1. エラーに関する情報の取得 #

<title>Obtaining Information about an Error</title>

Exception handlers frequently need to identify the specific error that occurred. There are two ways to get information about the current exception in <application>PL/pgSQL</application>: special variables and the <command>GET STACKED DIAGNOSTICS</command> command. 例外ハンドラはしばしば、起こった特定のエラーを識別する必要があります。 PL/pgSQLで現在の例外に関する情報を取得する方法は2つあります。 特殊な変数とGET STACKED DIAGNOSTICSコマンドです。

Within an exception handler, the special variable <varname>SQLSTATE</varname> contains the error code that corresponds to the exception that was raised (refer to <xref linkend="errcodes-table"/> for a list of possible error codes). The special variable <varname>SQLERRM</varname> contains the error message associated with the exception. These variables are undefined outside exception handlers. 例外ハンドラの内部では、特殊な変数SQLSTATE変数が起こった例外に対応したエラーコード(表 A.1のエラーコード表を参照してください)を保有します。 特殊な変数SQLERRMは例外に関連したエラーメッセージを保有します。 これらの変数は、例外ハンドラの外部では定義されていません。

Within an exception handler, one may also retrieve information about the current exception by using the <command>GET STACKED DIAGNOSTICS</command> command, which has the form: 例外ハンドラの内部では、GET STACKED DIAGNOSTICSコマンドを使用して、現在の例外に関する情報を取り出すこともできます。 次のようなやり方となります。

GET STACKED DIAGNOSTICS variable { = | := } item [ , ... ];

Each <replaceable>item</replaceable> is a key word identifying a status value to be assigned to the specified <replaceable>variable</replaceable> (which should be of the right data type to receive it). The currently available status items are shown in <xref linkend="plpgsql-exception-diagnostics-values"/>. itemは、指定されたvariable(これは受け取るために正しいデータ型でなければなりません)に代入される状態値を識別するキーワードです。 現在使用可能なステータス項目は表 43.2に表示されています。

表43.2 エラーの診断値

<title>Error Diagnostics Items</title>
名前説明
RETURNED_SQLSTATEtext例外のSQLSTATEエラーコード
COLUMN_NAMEtext例外に関する列名
CONSTRAINT_NAMEtext例外に関する制約名
PG_DATATYPE_NAMEtext例外に関するデータ型名
MESSAGE_TEXTtext例外の主要なメッセージのテキスト
TABLE_NAMEtext例外に関するテーブル名
SCHEMA_NAMEtext例外に関するスキーマ名
PG_EXCEPTION_DETAILtext例外の詳細なメッセージのテキスト、存在する場合
PG_EXCEPTION_HINTtext例外のヒントとなるメッセージのテキスト、存在する場合
PG_EXCEPTION_CONTEXTtext例外時における呼び出しスタックを記述するテキストの行(43.6.9を参照)

If the exception did not set a value for an item, an empty string will be returned. 例外が項目の値を設定しない場合、空文字列が返されます。

Here is an example: 以下に例を示します。

DECLARE
  text_var1 text;
  text_var2 text;
  text_var3 text;
BEGIN

  &#45;- some processing which might cause an exception

  -- 例外を引き起こす処理
  ...
EXCEPTION WHEN OTHERS THEN
  GET STACKED DIAGNOSTICS text_var1 = MESSAGE_TEXT,
                          text_var2 = PG_EXCEPTION_DETAIL,
                          text_var3 = PG_EXCEPTION_HINT;
END;

43.6.9. 実行位置情報の取得 #

<title>Obtaining Execution Location Information</title>

The <command>GET DIAGNOSTICS</command> command, previously described in <xref linkend="plpgsql-statements-diagnostics"/>, retrieves information about current execution state (whereas the <command>GET STACKED DIAGNOSTICS</command> command discussed above reports information about the execution state as of a previous error). Its <literal>PG_CONTEXT</literal> status item is useful for identifying the current execution location. <literal>PG_CONTEXT</literal> returns a text string with line(s) of text describing the call stack. The first line refers to the current function and currently executing <command>GET DIAGNOSTICS</command> command. The second and any subsequent lines refer to calling functions further up the call stack. For example: 以前、43.5.5に記載されていたGET DIAGNOSTICSコマンドは、現在の実行状態に関する情報を取得します(対して、前述のGET STACKED DIAGNOSTICSコマンドは一つ前のエラー時点の実行状態を報告します)。 これのPG_CONTEXTステータス項目は現在の実行位置を識別するのに役立ちます。 PG_CONTEXTは呼び出しスタックを記述したテキスト行を含むテキスト文字列を返します。 最初の行は現在の関数と現在実行中のGET DIAGNOSTICSコマンドを参照します。 次行および後の行は、呼び出しスタック上の呼び出し関数を参照します。 例を示します。

CREATE OR REPLACE FUNCTION outer_func() RETURNS integer AS $$
BEGIN
  RETURN inner_func();
END;
$$ LANGUAGE plpgsql;

CREATE OR REPLACE FUNCTION inner_func() RETURNS integer AS $$
DECLARE
  stack text;
BEGIN
  GET DIAGNOSTICS stack = PG_CONTEXT;
  RAISE NOTICE E'--- Call Stack ---\n%', stack;
  RETURN 1;
END;
$$ LANGUAGE plpgsql;

SELECT outer_func();

NOTICE:  --- Call Stack ---
PL/pgSQL function inner_func() line 5 at GET DIAGNOSTICS
PL/pgSQL function outer_func() line 3 at RETURN
CONTEXT:  PL/pgSQL function outer_func() line 3 at RETURN
 outer_func
 ------------
           1
(1 row)

<literal>GET STACKED DIAGNOSTICS ... PG_EXCEPTION_CONTEXT</literal> returns the same sort of stack trace, but describing the location at which an error was detected, rather than the current location. GET STACKED DIAGNOSTICS ... PG_EXCEPTION_CONTEXTは同種のスタックトレースを返しますが、現在の位置ではなく、エラーが検出されたところの位置を記述します。