CREATE FUNCTION

Name

CREATE FUNCTION  --  Defines a new function 新規の関数を定義します。

Synopsis

CREATE FUNCTION name ( [ ftype [, ...] ] )
    RETURNS rtype
    AS definition
    LANGUAGE 'langname'
  

入力

name

The name of a function to create.

作成する関数の名前です。

ftype

The data type of function arguments.

関数の引数のデータ型です。

rtype

The return data type.

戻されたデータ型です。

definition

A string defining the function; the meaning depends on the language. It may be an internal function name, the path to an object file, an SQL query, or text in a procedural language.

関数を定義する文です。なにを意味するかは言語に依存します。 内部関数の名前であったり、あるオブジェクトファイルへの パスのであったり、SQL 問合せ、あるいは手続き言語の中の テキストであることもあります。

langname

may be 'C', 'sql', 'internal' or 'plname', where 'plname' is the name of a created procedural language. See CREATE LANGUAGE for details.

は 'C'、 'sql'、 'internal' または 'plname' です。ここで言う、 'plname' は作成された手続き言語の名前です。 詳しくは CREATE LANGUAGE を参照して下さい。

出力

CREATE

This is returned if the command completes successfully.

コマンドが正常に終了したときに戻ります。

説明

CREATE FUNCTION allows a Postgres user to register a function with a database. Subsequently, this user is treated as the owner of the function.

CREATE FUNCTIONPostgres ユーザが あるデータベースに関数を登録する許可を与えます。 その後、そのユーザは関数の所有者として取り扱われます。

注意事項

Refer to the chapter on functions in the PostgreSQL Programmer's Guide for further information.

より詳しくは PostgreSQL プログラマガイドの 関数についての章を参照して下さい。

Use DROP FUNCTION to drop user-defined functions.

ユーザ定義関数を削除するには DROP FUNCTION を使用します。

Postgres allows function "overloading"; that is, the same name can be used for several different functions so long as they have distinct argument types. This facility must be used with caution for INTERNAL and C-language functions, however.

Postgres では関数の "オーバロードを 行うこと"、すなわち、引数の型が別のものであるかぎり、複数の関数に 同じ名前を付けることができます。とは言っても、 INTERNAL と C 言語 関数の取り扱いには注意が必要です。

Two INTERNAL functions cannot have the same C name without causing errors at link time. To get around that, give them different C names (for example, use the argument types as part of the C names), then specify those names in the AS clause of CREATE FUNCTION. If the AS clause is left empty then CREATE FUNCTION assumes the C name of the function is the same as the SQL name.

リンク時にエラーを発生させない様にして 2 つの INTERNAL 関数に同じ C 名を付けることは出来ません。このことを予防するためには、異なった C 名を(例えば、C 名の一部として引数の型を使うといったような) 付けたうえで、CREATE FUNCTION の AS 句でそれらの 名前を指定するようにします。AS 句に何の指定もないと、 CREATE FUNCTION は 関数の C 名を SQL と同じ名前と想定してしまいます。

For dynamically-loaded C functions, the SQL name of the function must be the same as the C function name, because the AS clause is used to give the path name of the object file containing the C code. In this situation it is best not to try to overload SQL function names. It might work to load a C function that has the same C name as an internal function or another dynamically-loaded function --- or it might not. On some platforms the dynamic loader may botch the load in interesting ways if there is a conflict of C function names. So, even if it works for you today, you might regret overloading names later when you try to run the code somewhere else.

動的に読み込まれる C 関数に対して、その関数の SQL 名は C 関数名と同じでなければなりません。というのは、AS 句は C コード を含んでいるオブジェクトファイルのパス名を与えるために使用される からです。この状況において、同じ SQL 関数名をオーバロードしない ようにすることが賢明です。ある内部関数または他の動的読み込み関数 と同じ C 名を持つ C 関数を読み込ませても作動する可能性がありますが、 保証の限りではありません。 いくつかのプラットフォームで動的ローダは、C 関数名が相容れない 時に、奇妙な形で読み込みをやりそこないます。ですから、今現在 (正常に)動いていると言っても、そのコードを別のどこかで走らせようと した場合、同じ名前をオーバロードしたことを後悔するかもしれません。

使用法

To create a simple SQL function:

単純な SQL 関数を作ってみます:

CREATE FUNCTION one() RETURNS int4
    AS 'SELECT 1 AS RESULT'
    LANGUAGE 'sql';
SELECT one() AS answer;

       answer 
    ------
    1
   
  

To create a C function, calling a routine from a user-created shared library. This particular routine calculates a check digit and returns TRUE if the check digit in the function parameters is correct. It is intended for use in a CHECK contraint.

ユーザ作成の共有ライブラリからあるルーチンを呼び出す C 関数をつくります。この特別なルーチンはチェックデジット を計算して、関数パラメータのチェックデジットが正しければ TRUE を返します。CHECK の制約条件で使うことを目的として いるものです。

   CREATE FUNCTION ean_checkdigit(bpchar, bpchar) RETURNS bool
    AS '/usr1/proj/bray/sql/funcs.so' LANGUAGE 'c';
    
CREATE TABLE product (
    id        char(8) PRIMARY KEY,
    eanprefix char(8) CHECK (eanprefix ~ '[0-9]{2}-[0-9]{5}')
                      REFERENCES brandname(ean_prefix),
    eancode   char(6) CHECK (eancode ~ '[0-9]{6}'),
    CONSTRAINT ean    CHECK (ean_checkdigit(eanprefix, eancode))
);
   
  

バグ情報

A C function cannot return a set of values.

C 関数は値の集合を戻しません。

互換性

CREATE FUNCTION is a Postgres language extension.

CREATE FUNCTIONPostgres の拡張言語です。

SQL/PSM

Note: PSM stands for Persistent Stored Modules. It is a procedural language and it was originally hoped that PSM would be ratified as an official standard by late 1996. As of mid-1998, this has not yet happened, but it is hoped that PSM will eventually become a standard.

PSM は Persistent Stored Module (永続的内蔵モジュール) の省略形です。それは手続き言語で、1996 年の後半には PSM が 公式の標準として認められると当初期待されていました。 1998 年の中程になっても期待通りになっていませんが、 最終的には、標準となって欲しいと望まれます。

SQL/PSM CREATE FUNCTION has the following syntax: SQL/PSM CREATE FUNCTION は以下の構文となります。
CREATE FUNCTION name
    ( [ [ IN | OUT | INOUT ] etereable>eable> type [, ...] ] )
     RETURNS rtype
     LANGUAGE 'langname'
     ESPECIFIC routine
     SQL-statement