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

46.9. ユーティリティ関数 #

<title>Utility Functions</title>

The <literal>plpy</literal> module also provides the functions plpyモジュールでは以下の関数も提供しています。

plpy.debug(msg, **kwargs)
plpy.log(msg, **kwargs)
plpy.info(msg, **kwargs)
plpy.notice(msg, **kwargs)
plpy.warning(msg, **kwargs)
plpy.error(msg, **kwargs)
plpy.fatal(msg, **kwargs)

<function>plpy.error</function> and <function>plpy.fatal</function> actually raise a Python exception which, if uncaught, propagates out to the calling query, causing the current transaction or subtransaction to be aborted. <literal>raise plpy.Error(<replaceable>msg</replaceable>)</literal> and <literal>raise plpy.Fatal(<replaceable>msg</replaceable>)</literal> are equivalent to calling <literal>plpy.error(<replaceable>msg</replaceable>)</literal> and <literal>plpy.fatal(<replaceable>msg</replaceable>)</literal>, respectively but the <literal>raise</literal> form does not allow passing keyword arguments. The other functions only generate messages of different priority levels. Whether messages of a particular priority are reported to the client, written to the server log, or both is controlled by the <xref linkend="guc-log-min-messages"/> and <xref linkend="guc-client-min-messages"/> configuration variables. See <xref linkend="runtime-config"/> for more information. plpy.errorおよびplpy.fatalは、実際にPythonの例外を発生させます。 これが捕捉されない場合、呼び出し中の問い合わせに伝わり、その結果、現在のトランザクションもしくはサブトランザクションがアボートします。 raise plpy.Error(msg)およびraise plpy.Fatal(msg)は、それぞれplpy.error(msg)およびplpy.fatal(msg)の呼び出しと同じですが、raise形式ではキーワード引数を渡すことができません。 他の関数は異なる重要度のメッセージを生成するだけです。 log_min_messagesclient_min_messages設定変数は、特定の重要度のメッセージをクライアントに報告するか、サーバのログに書き出すか、あるいはその両方かを制御します。 詳細は第20章を参照してください。

The <replaceable>msg</replaceable> argument is given as a positional argument. For backward compatibility, more than one positional argument can be given. In that case, the string representation of the tuple of positional arguments becomes the message reported to the client. msg引数は位置引数として与えられます。 後方互換性のために、2つ以上の位置引数を与えることができます。 その場合、位置引数のタプルの文字列表現がクライアントに報告されるメッセージになります。

The following keyword-only arguments are accepted: 以下のキーワードのみの引数を受け付けます。

detail
hint
sqlstate
schema_name
table_name
column_name
datatype_name
constraint_name

The string representation of the objects passed as keyword-only arguments is used to enrich the messages reported to the client. For example: キーワードのみの引数として渡されたオブジェクトの文字列表現は、クライアントへ報告されるメッセージを豊富にするのに使われます。 例えば、

CREATE FUNCTION raise_custom_exception() RETURNS void AS $$
plpy.error("custom exception message",
           detail="some info about exception",
           hint="hint for users")
$$ LANGUAGE plpython3u;

=# SELECT raise_custom_exception();
ERROR:  plpy.Error: custom exception message
DETAIL:  some info about exception
HINT:  hint for users
CONTEXT:  Traceback (most recent call last):
  PL/Python function "raise_custom_exception", line 4, in <module>
    hint="hint for users")
PL/Python function "raise_custom_exception"

Another set of utility functions are <literal>plpy.quote_literal(<replaceable>string</replaceable>)</literal>, <literal>plpy.quote_nullable(<replaceable>string</replaceable>)</literal>, and <literal>plpy.quote_ident(<replaceable>string</replaceable>)</literal>. They are equivalent to the built-in quoting functions described in <xref linkend="functions-string"/>. They are useful when constructing ad-hoc queries. A PL/Python equivalent of dynamic SQL from <xref linkend="plpgsql-quote-literal-example"/> would be: この他のユーティリティ関数群にはplpy.quote_literal(string)plpy.quote_nullable(string)およびplpy.quote_ident(string)があります。 これらは9.4で説明する組み込みの引用符付け関数と同等です。 これらはその場限りの問い合わせを構築する時に有用です。 例 43.1の動的SQLと同等なPL/Pythonを以下に示します。

plpy.execute("UPDATE tbl SET %s = %s WHERE key = %s" % (
    plpy.quote_ident(colname),
    plpy.quote_nullable(newvalue),
    plpy.quote_literal(keyvalue)))