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

38.6. 関数のオーバーロード #

<title>Function Overloading</title>

More than one function can be defined with the same SQL name, so long as the arguments they take are different. In other words, function names can be <firstterm>overloaded</firstterm>. Whether or not you use it, this capability entails security precautions when calling functions in databases where some users mistrust other users; see <xref linkend="typeconv-func"/>. When a query is executed, the server will determine which function to call from the data types and the number of the provided arguments. Overloading can also be used to simulate functions with a variable number of arguments, up to a finite maximum number. 使用する引数が異なるのであれば、同じSQL名の関数を1つ以上定義できます。 つまり、関数名はオーバーロードが可能です。 使うかどうかに関わりなく、この能力は、あるユーザが他のユーザを信用しないデータベースで関数を呼び出す時に、セキュリティの事前の対策を必要とします。10.3を参照してください。 問い合わせが実行された時、サーバは与えられた引数のデータ型と数によって呼び出すべき関数を決定します。 またオーバーロードは可変長引数を取る関数を有限の最大数までシミュレートするためにも使用できます。

When creating a family of overloaded functions, one should be careful not to create ambiguities. For instance, given the functions: オーバーロード関数を作成する時、曖昧さが発生しないように注意しなければなりません。 例えば、以下のような関数を考えてみます。

CREATE FUNCTION test(int, real) RETURNS ...
CREATE FUNCTION test(smallint, double precision) RETURNS ...

it is not immediately clear which function would be called with some trivial input like <literal>test(1, 1.5)</literal>. The currently implemented resolution rules are described in <xref linkend="typeconv"/>, but it is unwise to design a system that subtly relies on this behavior. test(1, 1.5)のような平凡な入力でも、どちらの関数を呼び出すのかはすぐには明確ではありません。 現在実装されている解決規則は第10章にて説明していますが、この動作に巧妙に依存するようにシステムを設計することは推奨しません。

A function that takes a single argument of a composite type should generally not have the same name as any attribute (field) of that type. Recall that <literal><replaceable>attribute</replaceable>(<replaceable>table</replaceable>)</literal> is considered equivalent to <literal><replaceable>table</replaceable>.<replaceable>attribute</replaceable></literal>. In the case that there is an ambiguity between a function on a composite type and an attribute of the composite type, the attribute will always be used. It is possible to override that choice by schema-qualifying the function name (that is, <literal><replaceable>schema</replaceable>.<replaceable>func</replaceable>(<replaceable>table</replaceable>) </literal>) but it's better to avoid the problem by not choosing conflicting names. 一般的に、1つの複合型の引数を取る関数は、その型の属性(フィールド)と同じ名前を持ってはいけません。 attribute(table)table.attributeと等価とみなされることを思い出してください。 複合型に対する関数と複合型の属性との間に曖昧さがあるような場合、属性の方が常に使用されます。 この振舞いは関数名をスキーマで修飾する(つまり、schema.func(table))ことにより変更できますが、競合する名前を使用しないことで問題を防ぐ方が良いでしょう。

Another possible conflict is between variadic and non-variadic functions. For instance, it is possible to create both <literal>foo(numeric)</literal> and <literal>foo(VARIADIC numeric[])</literal>. In this case it is unclear which one should be matched to a call providing a single numeric argument, such as <literal>foo(10.1)</literal>. The rule is that the function appearing earlier in the search path is used, or if the two functions are in the same schema, the non-variadic one is preferred. 可変長引数を取る関数と可変長引数を取らない関数の間に、他にも競合する可能性があります。 例えば、foo(numeric)foo(VARIADIC numeric[])の両方を作成可能です。 この場合、単一の数値引数を取った呼び出し、例えばfoo(10.1)をどちらに一致するものとすべきか不明瞭です。 検索パスのより前にある関数が使われる、もし2つの関数が同一スキーマにあれば可変長引数を取らない関数が優先されるというのが、この場合の規則です。

When overloading C-language functions, there is an additional constraint: The C name of each function in the family of overloaded functions must be different from the C names of all other functions, either internal or dynamically loaded. If this rule is violated, the behavior is not portable. You might get a run-time linker error, or one of the functions will get called (usually the internal one). The alternative form of the <literal>AS</literal> clause for the SQL <command>CREATE FUNCTION</command> command decouples the SQL function name from the function name in the C source code. For instance: C言語関数をオーバーロードする場合、さらに制限があります。 オーバーロードされた関数群内の各関数のCの名前は、内部か動的ロードされたかに関係なく他のすべての関数のCの名前と異なる必要があります。 この規則に反した場合は、この動作は移植性がありません。 実行時リンカエラーになるかもしれませんし、関数群のどれか(たいていは内部関数)が呼び出されるかもしれません。 CREATE FUNCTION SQLコマンドの別形式のAS句は、SQL関数名とCソースコード内の関数名とを分離します。 以下に例を示します。

CREATE FUNCTION test(int) RETURNS int
    AS 'filename', 'test_1arg'
    LANGUAGE C;
CREATE FUNCTION test(int, int) RETURNS int
    AS 'filename', 'test_2arg'
    LANGUAGE C;

The names of the C functions here reflect one of many possible conventions. ここでのC関数の名前は多くの取り得る規約の1つを反映しています。