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

43.1. 概要 #

<title>Overview</title>

<application>PL/pgSQL</application> is a loadable procedural language for the <productname>PostgreSQL</productname> database system. The design goals of <application>PL/pgSQL</application> were to create a loadable procedural language that PL/pgSQLは、PostgreSQLデータベースシステム用の読み込み可能な手続き言語です。 PL/pgSQLの設計目的は、次のような読み込み可能な手続き言語でした。

Functions created with <application>PL/pgSQL</application> can be used anywhere that built-in functions could be used. For example, it is possible to create complex conditional computation functions and later use them to define operators or use them in index expressions. PL/pgSQLで作成した関数は、組み込み関数が使えるところであれば、どこでも使用できます。 例えば、複雑な条件のある演算処理関数の作成が可能ですし、作成した関数を使用して演算子を定義することも、インデックス式にその関数を使用することも可能です。

In <productname>PostgreSQL</productname> 9.0 and later, <application>PL/pgSQL</application> is installed by default. However it is still a loadable module, so especially security-conscious administrators could choose to remove it. PostgreSQL 9.0以降ではPL/pgSQLはデフォルトでインストールされます。 しかしこれはまだロード可能なモジュールですので、特にセキュリティに厳しい管理者は削除することもできます。

43.1.1. PL/pgSQLを使用することの利点 #

<title>Advantages of Using <application>PL/pgSQL</application></title>

<acronym>SQL</acronym> is the language <productname>PostgreSQL</productname> and most other relational databases use as query language. It's portable and easy to learn. But every <acronym>SQL</acronym> statement must be executed individually by the database server. SQLPostgreSQLおよびその他のほとんどのリレーショナルデータベースが問い合わせ言語として使用している言語です。 移植性があり、習得が容易です。 しかし、あらゆるSQL文はデータベースサーバによって個々に実行されなければいけません。

That means that your client application must send each query to the database server, wait for it to be processed, receive and process the results, do some computation, then send further queries to the server. All this incurs interprocess communication and will also incur network overhead if your client is on a different machine than the database server. これはクライアントアプリケーションに対して以下のようなことを要求しています。 まず、データベースサーバに問い合わせを送信します。 次にそれが処理されるのを待ちます。 次に、結果を取得して処理します。 次に若干の計算を行います。 そして、サーバに次の問い合わせを送信します。 クライアントがデータベースサーバマシンと異なるマシンの場合、これが招いたプロセス間通信により、ネットワークオーバーヘッドを起こすでしょう。

With <application>PL/pgSQL</application> you can group a block of computation and a series of queries <emphasis>inside</emphasis> the database server, thus having the power of a procedural language and the ease of use of SQL, but with considerable savings of client/server communication overhead. PL/pgSQLを使うことで、計算と複数の問い合わせをデータベースサーバ内部でひとまとめに実行することができます。 このように、手続き言語の能力とSQLの使いやすさを持ち合わせているにもかかわらず、クライアント/サーバ通信のオーバーヘッドをかなり節約できます。

<listitem><para> Extra round trips between client and server are eliminated </para></listitem>
  • クライアント・サーバ間の余計なやり取りを排除する。

  • <listitem><para> Intermediate results that the client does not need do not have to be marshaled or transferred between server and client </para></listitem>
  • クライアントサーバ間において、クライアントに不必要な中間結果の整理と転送を不要とする。

  • <listitem><para> Multiple rounds of query parsing can be avoided </para></listitem>
  • 一連の問い合わせに、複数の解析が不要である。

<para> This can result in a considerable performance increase as compared to an application that does not use stored functions.

これにより、ストアドプロシージャを使用しないアプリケーションに比較して、かなり性能を向上させることができます。

Also, with <application>PL/pgSQL</application> you can use all the data types, operators and functions of SQL. また、PL/pgSQLではSQL全てのデータ型、演算子、関数を使用することができます。

43.1.2. 引数と結果データ型のサポート #

<title>Supported Argument and Result Data Types</title>

Functions written in <application>PL/pgSQL</application> can accept as arguments any scalar or array data type supported by the server, and they can return a result of any of these types. They can also accept or return any composite type (row type) specified by name. It is also possible to declare a <application>PL/pgSQL</application> function as accepting <type>record</type>, which means that any composite type will do as input, or as returning <type>record</type>, which means that the result is a row type whose columns are determined by specification in the calling query, as discussed in <xref linkend="queries-tablefunctions"/>. PL/pgSQLで作成された関数は、サーバでサポートされる任意のスカラデータ型や配列データ型を引数として受け付けることができ、また、これらの型を結果として返すことができます。 また、任意の、名前で指定された複合型(行型)を受け付けたり、返したりすることもできます。 さらに、7.2.1.4で説明されているように、PL/pgSQL関数がrecordを受け入れるように、すなわち、任意の複合型を入力としたりrecordを返すように宣言することも可能です。recordを返す場合の結果は、その各列が呼び出す問い合わせの中での指定で決まる行型です。

<application>PL/pgSQL</application> functions can be declared to accept a variable number of arguments by using the <literal>VARIADIC</literal> marker. This works exactly the same way as for SQL functions, as discussed in <xref linkend="xfunc-sql-variadic-functions"/>. PL/pgSQL関数はVARIADIC記号を使用して可変長の引数を受け付けられるように宣言することができます。 これは38.5.6で論議したように、SQL関数と全く同じ方法で動作します。

<application>PL/pgSQL</application> functions can also be declared to accept and return the polymorphic types described in <xref linkend="extend-types-polymorphic"/>, thus allowing the actual data types handled by the function to vary from call to call. Examples appear in <xref linkend="plpgsql-declaration-parameters"/>. また、PL/pgSQL関数を、38.2.5で説明されている多様型を受け付けたり、返したりするように宣言することもできます。 これにより、関数によって処理される実際のデータ型は呼び出しごとに変動することができます。 例を43.3.1に示します。

<application>PL/pgSQL</application> functions can also be declared to return a <quote>set</quote> (or table) of any data type that can be returned as a single instance. Such a function generates its output by executing <command>RETURN NEXT</command> for each desired element of the result set, or by using <command>RETURN QUERY</command> to output the result of evaluating a query. PL/pgSQL関数は、1つのインスタンスとして返すことができる任意のデータ型の集合(テーブル)を返すように宣言できます。 こうした関数は、結果集合の必要な要素に対してRETURN NEXTを実行すること、または問い合わせの評価結果を得るためにRETURN QUERYを使用することで、その出力を生成します。

Finally, a <application>PL/pgSQL</application> function can be declared to return <type>void</type> if it has no useful return value. (Alternatively, it could be written as a procedure in that case.) 最後に、有用な戻り値を持たない場合、PL/pgSQL関数は、voidを返すように宣言することができます。 (あるいは、この場合はプロシージャとして書くこともできます)

<application>PL/pgSQL</application> functions can also be declared with output parameters in place of an explicit specification of the return type. This does not add any fundamental capability to the language, but it is often convenient, especially for returning multiple values. The <literal>RETURNS TABLE</literal> notation can also be used in place of <literal>RETURNS SETOF</literal>. PL/pgSQL関数は戻り値の型を明確に指定する代わりに、出力パラメータと共に宣言することもできます。 これは言語に対して基本的な能力を追加するものではありませんが、特に複数の値を返す時にしばしば便利です。 RETURNS TABLE表記はRETURNS SETOFの代わりとして使用できます。

Specific examples appear in <xref linkend="plpgsql-declaration-parameters"/> and <xref linkend="plpgsql-statements-returning"/>. 関連する例は43.3.1および43.6.1にあります。