Chapter 11. 手続き言語

Table of Contents
手続き言語のインストール
PL/pgSQL
PL/Tcl

Beginning with the release of version 6.3, Postgres supports the definition of procedural languages. In the case of a function or trigger procedure defined in a procedural language, the database has no builtin knowlege how to interpret the functions source text. Instead, the calls are passed into a handler that knows the details of the language. The handler itself is a special programming language function compiled into a shared object and loaded on demand.

バージョン 6.3 のリリースから、Postgres は手続き言語の定義をサポートするようになりました。手続き言語で 定義された関数、または、トリガプロシージャについて、PostgreSQL は、関数のソーステキストをどのように解釈するかについての仕組み がありません。代わりに、呼び出しをその言語の詳細を理解している ハンドラに渡します。ハンドラ自体は、共有オブジェクトとしてコン パイルされ、必要に応じて読み込まれる、特別なプログラミング言語 関数です。

手続き言語のインストール

手続き言語のインストール方法

A procedural language is installed in the database in three steps.

手続き言語は次の 3 段階でデータベースにインストールされます。

  1. The shared object for the language handler must be compiled and installed. By default the handler for PL/pgSQL is built and installed into the database library directory. If Tcl/Tk support is configured in, the handler for PL/Tcl is also built and installed in the same location.

    まずはその言語の、ハンドラ用の共有オブジェクトがコンパ イルされインストールされている必要があります。デフォル トでは PL/pgSQL 用のハンドラはコンパイルされ、デー タベースライブラリディレクトリにインストールされます。 Tcl/Tk サポートを設定している場合は、PL/Tcl 用のハンド ラもまた、コンパイルされ、同じ場所にインストールされます。

    Writing a handler for a new procedural language (PL) is outside the scope of this manual.

    新しい手続き言語( PL )用のハンドラを記述する事はこの マニュアルの範囲外です。

  2. The handler must be declared with the command

        CREATE FUNCTION handler_function_name () RETURNS OPAQUE AS
            'path-to-shared-object' LANGUAGE 'C';
    		
    The special return type of OPAQUE tells the database, that this function does not return one of the defined base- or composite types and is not directly usable in SQL statements.

    ハンドラは次のコマンドで宣言されなければいけません。

        CREATE FUNCTION handler_function_name () RETURNS OPAQUE AS
            'path-to-shared-object' LANGUAGE 'C';
    		
    特別な OPAQUE という戻り値の型は データベースに、この関数が定義済みの基本型、複合型の どれかを返さず、そして、この関数は SQL 文で直接使用できないことを伝 えます。

  3. The PL must be declared with the command

        CREATE [ TRUSTED ] PROCEDURAL LANGUAGE 'language-name'
            HANDLER handler_function_name
    	LANCOMPILER 'description';
    		
    The optional keyword TRUSTED tells if ordinary database users that have no superuser privileges can use this language to create functions and trigger procedures. Since PL functions are executed inside the database backend it should only be used for languages that don't gain access to database backends internals or the filesystem. The languages PL/pgSQL and PL/Tcl are known to be trusted.

    PL は次のコマンドで宣言されなければいけません。

        CREATE [ TRUSTED ] PROCEDURAL LANGUAGE 'language-name'
            HANDLER handler_function_name
    	LANCOMPILER 'description';
    		
    オプションである TRUSTED キーワード は、スーパーユーザの権利を持たない一般ユーザがこの言語 を使って関数、トリガプロシージャを作成できるかどうか を知らせます。PL 関数はデータベースバックエンドの内側で 実行されますので、データベースバックエンド内部やファイ ルシステムへのアクセスを持たない言語のみが使われるべき です。PL/pgSQL と PL/Tcl は信頼できるものとされています。

  1. The following command tells the database where to find the shared object for the PL/pgSQL languages call handler function.

    次のコマンドはデータベースに PL/pgSQL 言語呼び出しハンドラ関数 用の共有オブジェクトがどこにあるかを伝えます。

        CREATE FUNCTION plpgsql_call_handler () RETURNS OPAQUE AS
            '/usr/local/pgsql/lib/plpgsql.so' LANGUAGE 'C';
        
  2. The command

        CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
            HANDLER plpgsql_call_handler
            LANCOMPILER 'PL/pgSQL';
        

    then defines that the previously declared call handler function should be invoked for functions and trigger procedures where the language attribute is 'plpgsql'.

    そして次のコマンドの LANGUAGE 属性 'plpgsql' のところで、

        CREATE TRUSTED PROCEDURAL LANGUAGE 'plpgsql'
            HANDLER plpgsql_call_handler
            LANCOMPILER 'PL/pgSQL';
        

    前に宣言された呼び出しハンドラ関数が、関数、トリガプロシージャ 用に呼び出されるべきであることを定義します。

    PL handler functions have a special call interface that is different from regular C language functions. One of the arguments given to the handler is the object ID in the pg_proc tables entry for the function that should be executed. The handler examines various system catalogs to analyze the functions call arguments and it's return data type. The source text of the functions body is found in the prosrc attribute of pg_proc. Due to this, in contrast to C language functions, PL functions can be overloaded like SQL language functions. There can be multiple different PL functions having the same function name, as long as the call arguments differ.

    PL ハンドラ関数は、標準的な C 言語関数とは異なる、特別な呼び出 し用インタフェースを持ちます。ハンドラに与えられる引数の一つは、 pg_proc テーブル内の実行される関数のエント リのオブジェクト ID です。ハンドラは、関数呼び出し時の引数とその 戻り値の型を分析するために、各種システムカタログを検査します。関 数本体のソーステキストは pg_proc の prosrc 属性にあります。このため、C 言語関数とは対象的に、PL 関数は SQL 言語関数のように上書きできます。呼び出し用引数が異なっている限り、 同一関数名をもった複数の異なる PL 関数が存在しても構いません。

    Procedural languages defined in the template1 database are automatically defined in all subsequently created databases. So the database administrator can decide which languages are available by default.

    template1 データベース内に定義された手続き 言語は、その後に作成された全てのデータベースに対して自動的に定義さ れます。ですので、データベース管理者はデフォルトで使用可能になる 言語を決定することができます。