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

46.1. PL/Python関数 #

<title>PL/Python Functions</title>

Functions in PL/Python are declared via the standard <xref linkend="sql-createfunction"/> syntax: PL/Pythonで作成された関数は標準的なCREATE FUNCTION構文で宣言されます。

CREATE FUNCTION funcname (argument-list)
  RETURNS return-type
AS $$
  # PL/Python function body
$$ LANGUAGE plpython3u;

The body of a function is simply a Python script. When the function is called, its arguments are passed as elements of the list <varname>args</varname>; named arguments are also passed as ordinary variables to the Python script. Use of named arguments is usually more readable. The result is returned from the Python code in the usual way, with <literal>return</literal> or <literal>yield</literal> (in case of a result-set statement). If you do not provide a return value, Python returns the default <symbol>None</symbol>. <application>PL/Python</application> translates Python's <symbol>None</symbol> into the SQL null value. In a procedure, the result from the Python code must be <symbol>None</symbol> (typically achieved by ending the procedure without a <literal>return</literal> statement or by using a <literal>return</literal> statement without argument); otherwise, an error will be raised. 関数本体は単なるPythonスクリプトです。 関数が呼び出されると、引数はargs[]リストの要素として渡されます。 名前付きの引数も通常の変数としてPythonスクリプトに渡されます。 通常、名前付き引数の方が可読性が高くなります。 結果は、Pythonコードから通常の方法、returnまたはyield(結果セット文の場合)で返されるものです。 戻り値を提供しない場合、PythonはデフォルトのNoneを返します。 PL/PythonはPythonのNoneをSQLのNULL値に変換します。 プロシージャでは、Pythonコードからの結果はNoneでなければなりません(典型的にはreturn文を使わずプロシージャを終了したり、return文を引数無しで使うことで達成されます)。 さもないとエラーが起きます。

For example, a function to return the greater of two integers can be defined as: たとえば、2つの整数の内大きな数を返す関数は以下のように定義することができます。

CREATE FUNCTION pymax (a integer, b integer)
  RETURNS integer
AS $$
  if a > b:
    return a
  return b
$$ LANGUAGE plpython3u;

The Python code that is given as the body of the function definition is transformed into a Python function. For example, the above results in: 関数定義の本体として提供されたPythonのコードはPythonの関数に変換されます。 例えば上の例は以下のようになります。

def __plpython_procedure_pymax_23456():
  if a > b:
    return a
  return b

assuming that 23456 is the OID assigned to the function by <productname>PostgreSQL</productname>. ここで、23456はPostgreSQLにより割り当てられたこの関数のOIDです。

The arguments are set as global variables. Because of the scoping rules of Python, this has the subtle consequence that an argument variable cannot be reassigned inside the function to the value of an expression that involves the variable name itself, unless the variable is redeclared as global in the block. For example, the following won't work: 引数はグローバル変数として設定されます。 Pythonのスコープ規則のため、これは、ブロック内でグローバルとして再宣言されていない限り、関数内で引数変数に変数名自身を含む式の値として再代入できないという難解な結果をもたらします。 例えば以下は動作しません。

CREATE FUNCTION pystrip(x text)
  RETURNS text
AS $$
  x = x.strip()  # error
  return x
$$ LANGUAGE plpython3u;

because assigning to <varname>x</varname> makes <varname>x</varname> a local variable for the entire block, and so the <varname>x</varname> on the right-hand side of the assignment refers to a not-yet-assigned local variable <varname>x</varname>, not the PL/Python function parameter. Using the <literal>global</literal> statement, this can be made to work: xへの代入は、xをブロック全体に対するローカル変数にしようとし、そして、代入の右辺のxがPL/Pythonの関数パラメータではなく、まだ割り当てられていないローカル変数xを参照するためです。 global文を使用することで、動作するようになります。

CREATE FUNCTION pystrip(x text)
  RETURNS text
AS $$
  global x
  x = x.strip()  # ok now
  return x
$$ LANGUAGE plpython3u;

But it is advisable not to rely on this implementation detail of PL/Python. It is better to treat the function parameters as read-only. しかし、PL/Pythonのこうした詳細な実装に依存しないようにすることを勧めます。 関数パラメータは読み取りのみとして扱うことを勧めます。