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

43.2. PL/pgSQLの構造 #

<title>Structure of <application>PL/pgSQL</application></title>

Functions written in <application>PL/pgSQL</application> are defined to the server by executing <xref linkend="sql-createfunction"/> commands. Such a command would normally look like, say, PL/pgSQLで書かれた関数はCREATE FUNCTIONコマンドを実行することでサーバに定義されます。 そのようなコマンドは通常、例えば次のようになります。

CREATE FUNCTION somefunc(integer, text) RETURNS integer
AS 'function body text'
LANGUAGE plpgsql;

The function body is simply a string literal so far as <command>CREATE FUNCTION</command> is concerned. It is often helpful to use dollar quoting (see <xref linkend="sql-syntax-dollar-quoting"/>) to write the function body, rather than the normal single quote syntax. Without dollar quoting, any single quotes or backslashes in the function body must be escaped by doubling them. Almost all the examples in this chapter use dollar-quoted literals for their function bodies. 関数本体はCREATE FUNCTIONにとっては単なる文字列リテラルです。 関数本体を書くのには、普通の単一引用符構文よりは、ドル引用符(4.1.2.4を参照)を使うのが、多くの場合役に立ちます。 ドル引用符でなければ、関数本体内の単一引用符やバックスラッシュをすべて二重化してエスケープしなければなりません。 この章のほぼすべての例では、関数本体にドル記号で括られたリテラルを使っています。

<application>PL/pgSQL</application> is a block-structured language. The complete text of a function body must be a <firstterm>block</firstterm>. A block is defined as: PL/pgSQLはブロック構造の言語です。 関数本体のテキスト全体はブロックでなければなりません。 ブロックは以下のように定義されます。

[ <<label>> ]
[ DECLARE
    declarations ]
BEGIN
    statements
END [ label ];

Each declaration and each statement within a block is terminated by a semicolon. A block that appears within another block must have a semicolon after <literal>END</literal>, as shown above; however the final <literal>END</literal> that concludes a function body does not require a semicolon. ブロック内の宣言や文はそれぞれ、セミコロンで終わります。 上に示したように、他のブロック内に出現するブロックのENDの後にはセミコロンが必要ですが、関数本体を完結する最後のENDには不要です。

ヒント

A common mistake is to write a semicolon immediately after <literal>BEGIN</literal>. This is incorrect and will result in a syntax error. BEGINの直後にセミコロンを書くことも、同じように間違いです。 これは不正であり、構文エラーとなります。

A <replaceable>label</replaceable> is only needed if you want to identify the block for use in an <literal>EXIT</literal> statement, or to qualify the names of the variables declared in the block. If a label is given after <literal>END</literal>, it must match the label at the block's beginning. labelが必要となるのは、EXIT文が使用されるブロックを特定したい場合、またはブロック内で宣言された変数名を修飾したい場合だけです。 ENDの後にラベルを配置する時は、そのブロックの先頭ラベルと一致させなければなりません。

All key words are case-insensitive. Identifiers are implicitly converted to lower case unless double-quoted, just as they are in ordinary SQL commands. 全てのキーワードは大文字と小文字を区別しません。 識別子は二重引用符でくくられていない限り、通常のSQLコマンドと同様に、暗黙的に小文字に変換されます。

Comments work the same way in <application>PL/pgSQL</application> code as in ordinary SQL. A double dash (<literal>&#45;-</literal>) starts a comment that extends to the end of the line. A <literal>/*</literal> starts a block comment that extends to the matching occurrence of <literal>*/</literal>. Block comments nest. PL/pgSQLコード内では、通常のSQLと同じ方法のコメントが動作します。 二重のダッシュ(--)はその行末までをコメントとするコメントを開始します。 /*はコメントブロックの始まりを意味し、次に*/が現れるまでをコメントとします。 ブロックコメントは入れ子になります。

Any statement in the statement section of a block can be a <firstterm>subblock</firstterm>. Subblocks can be used for logical grouping or to localize variables to a small group of statements. Variables declared in a subblock mask any similarly-named variables of outer blocks for the duration of the subblock; but you can access the outer variables anyway if you qualify their names with their block's label. For example: ブロックの文節内の全ての文は副ブロックになることができます。 副ブロックは論理的なグループ分けや変数を文の小さな集まりに局所化するのに使用できます。 副ブロックにおいて宣言された変数は、副ブロック内部では外側のブロックにおける同名の変数を遮蔽しますが、外側のラベルを変数名に付加して指定すればアクセスできます。 以下に例を示します。

CREATE FUNCTION somefunc() RETURNS integer AS $$
<< outerblock >>
DECLARE
    quantity integer := 30;
BEGIN

    RAISE NOTICE 'Quantity here is %', quantity;  &#45;- Prints 30

    RAISE NOTICE 'Quantity here is %', quantity;  -- Quantity here is 30と表示
    quantity := 50;
    --

    &#45;- Create a subblock

    -- 副ブロックの作成
    --
    DECLARE
        quantity integer := 80;
    BEGIN

        RAISE NOTICE 'Quantity here is %', quantity;  &#45;- Prints 80
        RAISE NOTICE 'Outer quantity here is %', outerblock.quantity;  &#45;- Prints 50

        RAISE NOTICE 'Quantity here is %', quantity;  -- Quantity here is 80と表示
        RAISE NOTICE 'Outer quantity here is %', outerblock.quantity;  -- Quantity here is 50と表示
    END;


    RAISE NOTICE 'Quantity here is %', quantity;  &#45;- Prints 50

    RAISE NOTICE 'Quantity here is %', quantity;  -- Quantity here is 50と表示

    RETURN quantity;
END;
$$ LANGUAGE plpgsql;

注記

There is actually a hidden <quote>outer block</quote> surrounding the body of any <application>PL/pgSQL</application> function. This block provides the declarations of the function's parameters (if any), as well as some special variables such as <literal>FOUND</literal> (see <xref linkend="plpgsql-statements-diagnostics"/>). The outer block is labeled with the function's name, meaning that parameters and special variables can be qualified with the function's name. PL/pgSQL関数の本体を囲む、隠れた外側のブロックが存在します。 この隠れたブロックにおいて、関数のパラメータがあれば宣言をして、同様にFOUNDのような特殊な変数(43.5.5を参照)を提供します。 この外側のブロックのラベルは関数名となります。 つまりパラメータと特殊な変数は関数名によって修飾することを意味します。

It is important not to confuse the use of <command>BEGIN</command>/<command>END</command> for grouping statements in <application>PL/pgSQL</application> with the similarly-named SQL commands for transaction control. <application>PL/pgSQL</application>'s <command>BEGIN</command>/<command>END</command> are only for grouping; they do not start or end a transaction. See <xref linkend="plpgsql-transactions"/> for information on managing transactions in <application>PL/pgSQL</application>. Also, a block containing an <literal>EXCEPTION</literal> clause effectively forms a subtransaction that can be rolled back without affecting the outer transaction. For more about that see <xref linkend="plpgsql-error-trapping"/>. PL/pgSQLにおける文をまとめるためのBEGIN/ENDとトランザクション制御用の同名のSQLコマンドとを取り違えないようにすることが重要です。 PL/pgSQLBEGIN/ENDは単にまとめるためのもので、トランザクションを始めたり終わらせたりしません。 トランザクションをPL/pgSQL内で制御するための情報に関しては、43.8を参照してください。 また、EXCEPTION句を含むブロックは外側のトランザクションに影響しないでロールバックできるサブトランザクションを、実質的に作成できます。 これについては43.6.8を参照してください。