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

CREATE PROCEDURE

CREATE PROCEDURE <refpurpose>define a new procedure</refpurpose> — 新しいプロシージャを定義する

概要

CREATE [ OR REPLACE ] PROCEDURE
    name ( [ [ argmode ] [ argname ] argtype [ { DEFAULT | = } default_expr ] [, ...] ] )
  { LANGUAGE lang_name
    | TRANSFORM { FOR TYPE type_name } [, ... ]
    | [ EXTERNAL ] SECURITY INVOKER | [ EXTERNAL ] SECURITY DEFINER
    | SET configuration_parameter { TO value | = value | FROM CURRENT }
    | AS 'definition'
    | AS 'obj_file', 'link_symbol'
    | sql_body
  } ...

説明

<title>Description</title>

<command>CREATE PROCEDURE</command> defines a new procedure. <command>CREATE OR REPLACE PROCEDURE</command> will either create a new procedure, or replace an existing definition. To be able to define a procedure, the user must have the <literal>USAGE</literal> privilege on the language. CREATE PROCEDUREは新たなプロシージャを定義します。 CREATE OR REPLACE PROCEDUREは新たなプロシージャを作るか、既存の定義を置きかえます。 プロシージャを定義するにはユーザは言語にUSAGE権限が必要です。

If a schema name is included, then the procedure is created in the specified schema. Otherwise it is created in the current schema. The name of the new procedure must not match any existing procedure or function with the same input argument types in the same schema. However, procedures and functions of different argument types can share a name (this is called <firstterm>overloading</firstterm>). スキーマ名が含まれている場合、プロシージャは指定されたスキーマに作られます。 そうでなければ現在のスキーマに作られます。 同スキーマ内で新たなプロシージャ名と入力引数型が既存のプロシージャや関数と一致してはなりません。 しかしながら、プロシージャや関数が異なる引数型であれば同じ名前を共有できます(これはオーバーロードと呼ばれます)。

To replace the current definition of an existing procedure, use <command>CREATE OR REPLACE PROCEDURE</command>. It is not possible to change the name or argument types of a procedure this way (if you tried, you would actually be creating a new, distinct procedure). 既存プロシージャの現在の定義を置き換えるには、CREATE OR REPLACE PROCEDUREを使用してください。 この方法でプロシージャの名前や引数型を変更することはできません(試みれば、実際は新たな別プロシージャを作ることになるでしょう)。

When <command>CREATE OR REPLACE PROCEDURE</command> is used to replace an existing procedure, the ownership and permissions of the procedure do not change. All other procedure properties are assigned the values specified or implied in the command. You must own the procedure to replace it (this includes being a member of the owning role). 既存プロシージャの置き換えにCREATE OR REPLACE PROCEDUREが使われたとき、プロシージャの所有者と権限設定は変更されません。 その他全てのプロシージャ属性は、コマンドで指定された値または暗黙の値に設定されます。 プロシージャを置き換えるには所有者(所有するロールのメンバであることも含みます)でなければなりません。

The user that creates the procedure becomes the owner of the procedure. プロシージャを作ったユーザはプロシージャの所有者になります。

To be able to create a procedure, you must have <literal>USAGE</literal> privilege on the argument types. プロシージャを作るには、引数型に対してUSAGE権限を持っていなければなりません。

Refer to <xref linkend="xproc"/> for further information on writing procedures. プロシージャを書くことに関する更なる情報は38.4を参照してください。

パラメータ

<title>Parameters</title>
name

The name (optionally schema-qualified) of the procedure to create. 作成するプロシージャ名(スキーマ修飾も可)。

argmode

The mode of an argument: <literal>IN</literal>, <literal>OUT</literal>, <literal>INOUT</literal>, or <literal>VARIADIC</literal>. If omitted, the default is <literal>IN</literal>. 引数モードで、INOUTINOUT、あるいは、VARIADICのいずれかです。 省略した場合のデフォルトはINです。

argname

The name of an argument. 引数名。

argtype

The data type(s) of the procedure's arguments (optionally schema-qualified), if any. The argument types can be base, composite, or domain types, or can reference the type of a table column. プロシージャ引数があるなら、そのデータ型(スキーマ修飾も可)です。 引数型には基本型、複合型、ドメイン型、あるいは、テーブル列の型の参照が使えます。

Depending on the implementation language it might also be allowed to specify <quote>pseudo-types</quote> such as <type>cstring</type>. Pseudo-types indicate that the actual argument type is either incompletely specified, or outside the set of ordinary SQL data types. 実装言語によっては、cstringなどの擬似データ型を指定することができます。 擬似データ型は実際の引数型が完全には特定されていないか、通常のSQLデータ型の枠外にあることを示しています。

The type of a column is referenced by writing 列の型は以下のように参照されます。 table_name.column_name%TYPE. Using this feature can sometimes help make a procedure independent of changes to the definition of a table. この機能を使うことは時にプロシージャをテーブル定義の変更から独立させる助けとなります。

default_expr

An expression to be used as default value if the parameter is not specified. The expression has to be coercible to the argument type of the parameter. All input parameters following a parameter with a default value must have default values as well. パラメータが指定されなかった場合のデフォルト値として使用される式です。 この式はパラメータの引数型と変換可能でなければなりません。 デフォルト値を持つパラメータの後ろにあるパラメータはすべて、同様にデフォルト値を持たなければなりません。

lang_name

The name of the language that the procedure is implemented in. It can be <literal>sql</literal>, <literal>c</literal>, <literal>internal</literal>, or the name of a user-defined procedural language, e.g., <literal>plpgsql</literal>. The default is <literal>sql</literal> if <replaceable class="parameter">sql_body</replaceable> is specified. Enclosing the name in single quotes is deprecated and requires matching case. プロシージャを実装している言語の名前です。 このパラメータには、sqlcinternal、もしくはユーザ定義手続き言語(例:plpgsql)の名前を指定可能です。 sql_bodyが指定されていれば、デフォルトはsqlです。 名前を単一引用符で囲むのは廃止予定で、大文字小文字の一致が必要になります。

TRANSFORM { FOR TYPE type_name } [, ... ] }

Lists which transforms a call to the procedure should apply. Transforms convert between SQL types and language-specific data types; see <xref linkend="sql-createtransform"/>. Procedural language implementations usually have hardcoded knowledge of the built-in types, so those don't need to be listed here. If a procedural language implementation does not know how to handle a type and no transform is supplied, it will fall back to a default behavior for converting data types, but this depends on the implementation. プロシージャ呼び出しにどの変換を適用すべきかのリストです。 変換はSQLの型と言語独自のデータ型の間の変換を行います(CREATE TRANSFORMを参照)。 手続言語の実装では、通常、ビルトインの型についてハードコードされた知識があるので、それらをこのリストに含める必要はありません。 手続言語の実装が型の処理について定めておらず、変換が提供されない場合は、データ型変換のデフォルトの動作によることになりますが、これは実装に依存します。

[EXTERNAL] SECURITY INVOKER
[EXTERNAL] SECURITY DEFINER
<para><literal>SECURITY INVOKER</literal> indicates that the procedure is to be executed with the privileges of the user that calls it. That is the default. <literal>SECURITY DEFINER</literal> specifies that the procedure is to be executed with the privileges of the user that owns it.

SECURITY INVOKERを指定すると、プロシージャを呼び出したユーザの権限で、そのプロシージャが実行されます。 これがデフォルトです。 SECURITY DEFINERを指定すると、プロシージャを所有するユーザの権限で、そのプロシージャが実行されます。

The key word <literal>EXTERNAL</literal> is allowed for SQL conformance, but it is optional since, unlike in SQL, this feature applies to all procedures not only external ones. EXTERNALキーワードは、SQLとの互換性を保つために許されています。 しかし、SQLとは異なり、この機能は外部プロシージャだけではなくすべてのプロシージャに適用されるため、このキーワードは省略可能です。

A <literal>SECURITY DEFINER</literal> procedure cannot execute transaction control statements (for example, <command>COMMIT</command> and <command>ROLLBACK</command>, depending on the language). SECURITY DEFINERプロシージャではトランザクション制御文(言語によりますが例えばCOMMITROLLBACK)は実行できません。

configuration_parameter
value

The <literal>SET</literal> clause causes the specified configuration parameter to be set to the specified value when the procedure is entered, and then restored to its prior value when the procedure exits. <literal>SET FROM CURRENT</literal> saves the value of the parameter that is current when <command>CREATE PROCEDURE</command> is executed as the value to be applied when the procedure is entered. SET句により、プロシージャが始まった時に指定した設定パラメータを指定した値に設定し、プロシージャの終了時にそれを以前の値に戻すことができます。 SET FROM CURRENTは、CREATE PROCEDUREの実行時点でのパラメータ値を、プロシージャに入る時に適用する値として保管します。

If a <literal>SET</literal> clause is attached to a procedure, then the effects of a <command>SET LOCAL</command> command executed inside the procedure for the same variable are restricted to the procedure: the configuration parameter's prior value is still restored at procedure exit. However, an ordinary <command>SET</command> command (without <literal>LOCAL</literal>) overrides the <literal>SET</literal> clause, much as it would do for a previous <command>SET LOCAL</command> command: the effects of such a command will persist after procedure exit, unless the current transaction is rolled back. プロシージャにSET句が付いている場合、プロシージャ内部で実行されるSET LOCALコマンドの同一変数に対する効果はそのプロシージャに制限されます。 つまり、設定パラメータの前の値はプロシージャが終了する時に元に戻ります。 しかし、通常の(LOCALがない)SETコマンドはSET句を上書きします。 これは過去に行われたSET LOCALコマンドに対してもほぼ同じです。 つまり、このコマンドの効果は、現在のトランザクションがロールバックされない限り、プロシージャが終了した後も永続化されます。

If a <literal>SET</literal> clause is attached to a procedure, then that procedure cannot execute transaction control statements (for example, <command>COMMIT</command> and <command>ROLLBACK</command>, depending on the language). プロシージャにSET句が付いている場合、そのプロシージャではトランザクション制御文(言語によりますが例えばCOMMITROLLBACK)を実行できません。

See <xref linkend="sql-set"/> and <xref linkend="runtime-config"/> for more information about allowed parameter names and values. 使用できるパラメータ名と値についての詳細はSET第20章を参照してください。

definition

A string constant defining the procedure; the meaning depends on the language. It can be an internal procedure name, the path to an object file, an SQL command, or text in a procedural language. プロシージャを定義する文字列定数です。 このパラメータの意味は言語に依存します。 内部的なプロシージャ名、オブジェクトファイルへのパス、SQLコマンド、あるいは、手続き言語で記述されたテキストを指定できます。

It is often helpful to use dollar quoting (see <xref linkend="sql-syntax-dollar-quoting"/>) to write the procedure definition string, rather than the normal single quote syntax. Without dollar quoting, any single quotes or backslashes in the procedure definition must be escaped by doubling them. プロシージャを定義する文字列を記述する際に、通常の単一引用符ではなく、ドル引用符(4.1.2.4参照)を使用すると便利なことが多くあります。 ドル引用符を使用しなければ、プロシージャ定義内の単一引用符やバックスラッシュは必ず二重にしてエスケープしなければなりません。

obj_file, link_symbol

This form of the <literal>AS</literal> clause is used for dynamically loadable C language procedures when the procedure name in the C language source code is not the same as the name of the SQL procedure. The string <replaceable class="parameter">obj_file</replaceable> is the name of the shared library file containing the compiled C procedure, and is interpreted as for the <link linkend="sql-load"><command>LOAD</command></link> command. The string <replaceable class="parameter">link_symbol</replaceable> is the procedure's link symbol, that is, the name of the procedure in the C language source code. If the link symbol is omitted, it is assumed to be the same as the name of the SQL procedure being defined. この構文のAS句は、動的にロードされるC言語プロシージャにおいて、C言語のソースコード中のプロシージャ名がSQLプロシージャの名前と同じでない場合に使われます。 obj_fileという文字列はコンパイルされたCプロシージャを含む共有ライブラリファイルの名前で、LOADコマンドの場合と同じように解釈されます。 文字列link_symbolはそのプロシージャのリンクシンボル、つまり、C言語ソースコード中のプロシージャの名前です。 リンクシンボルが省略された場合、定義されるSQLプロシージャの名前と同じものであるとみなされます。

When repeated <command>CREATE PROCEDURE</command> calls refer to the same object file, the file is only loaded once per session. To unload and reload the file (perhaps during development), start a new session. 同じオブジェクトファイルを参照するCREATE PROCEDURE呼び出しが繰り返される場合、ファイルがセッションにつき一度だけロードされます。 (おそらく開発中に)ファイルのアンロードと再ロードを行うには、新たなセッションを開始してください。

sql_body

The body of a <literal>LANGUAGE SQL</literal> procedure. This should be a block LANGUAGE SQLプロシージャの本体です。 これは以下のようなブロックでなければなりません。

BEGIN ATOMIC
  statement;
  statement;
  ...
  statement;
END

This is similar to writing the text of the procedure body as a string constant (see <replaceable>definition</replaceable> above), but there are some differences: This form only works for <literal>LANGUAGE SQL</literal>, the string constant form works for all languages. This form is parsed at procedure definition time, the string constant form is parsed at execution time; therefore this form cannot support polymorphic argument types and other constructs that are not resolvable at procedure definition time. This form tracks dependencies between the procedure and objects used in the procedure body, so <literal>DROP ... CASCADE</literal> will work correctly, whereas the form using string literals may leave dangling procedures. Finally, this form is more compatible with the SQL standard and other SQL implementations. これは文字列定数としてプロシージャ本体を書くのと似ていますが(上記のdefinitionを参照してください)、いくつか違いがあります。 この形式はLANGUAGE SQLに対してのみ機能し、文字列定数の形式はすべての言語に対して機能します。 この形式はプロシージャ定義時に解析され、文字列定数の形式は実行時に解析されます。 そのため、この形式は多様引数型やプロシージャ定義時に解決できないその他の構文をサポートできません。 この形式はプロシージャとプロシージャ本体の中で使われているオブジェクトの間の依存関係を追跡しますので、DROP ... CASCADEは正しく動作しますが、一方、文字列定数を使う形式は宙に浮いたプロシージャを放置するかもしれません。 最後に、この形式は標準SQLや他のSQL実装とより互換性があります。

注釈

<title>Notes</title>

See <xref linkend="sql-createfunction"/> for more details on function creation that also apply to procedures. プロシージャにも該当する関数の作成についての詳細はCREATE FUNCTIONを参照してください。

Use <xref linkend="sql-call"/> to execute a procedure. プロシージャを実行するにはCALLを使います。

<title>Examples</title>

CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
AS $$
INSERT INTO tbl VALUES (a);
INSERT INTO tbl VALUES (b);
$$;

or または

CREATE PROCEDURE insert_data(a integer, b integer)
LANGUAGE SQL
BEGIN ATOMIC
  INSERT INTO tbl VALUES (a);
  INSERT INTO tbl VALUES (b);
END;

and call like this: そして、以下のように呼び出します。

CALL insert_data(1, 2);

互換性

<title>Compatibility</title>

A <command>CREATE PROCEDURE</command> command is defined in the SQL standard. The <productname>PostgreSQL</productname> implementation can be used in a compatible way but has many extensions. For details see also <xref linkend="sql-createfunction"/>. CREATE PROCEDUREコマンドは標準SQLで定義されています。 PostgreSQLの実装は互換性のある方法で使うことはできますが、多くの拡張があります。 詳しくはCREATE FUNCTIONも参照してください。

関連項目

<title>See Also</title> ALTER PROCEDURE, DROP PROCEDURE, CALL, CREATE FUNCTION