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

43.4. 式 #

<title>Expressions</title>

All expressions used in <application>PL/pgSQL</application> statements are processed using the server's main <acronym>SQL</acronym> executor. For example, when you write a <application>PL/pgSQL</application> statement like PL/pgSQL文で使用される式は全て、サーバの主SQLエグゼキュータを使用して処理されます。 例えば、以下のPL/pgSQL

IF expression THEN ...

<application>PL/pgSQL</application> will evaluate the expression by feeding a query like が記述された時、PL/pgSQL

SELECT expression

to the main SQL engine. While forming the <command>SELECT</command> command, any occurrences of <application>PL/pgSQL</application> variable names are replaced by query parameters, as discussed in detail in <xref linkend="plpgsql-var-subst"/>. This allows the query plan for the <command>SELECT</command> to be prepared just once and then reused for subsequent evaluations with different values of the variables. Thus, what really happens on first use of an expression is essentially a <command>PREPARE</command> command. For example, if we have declared two integer variables <literal>x</literal> and <literal>y</literal>, and we write を主SQLエンジンに供給して、上式を評価します。 43.11.1において詳細を説明したように、SELECTコマンドの形成においてPL/pgSQL変数名は、その都度問い合わせパラメータによって置換されます。 これにより、SELECTの問い合わせ計画は一度だけ準備することができ、その後の評価で異なった変数値を代入して再利用されます。 すなわち、式の最初の使用においては、実質的にPREPAREコマンドと同等です。 例えば、2つの整数変数xyを宣言して、

IF x < y THEN ...

what happens behind the scenes is equivalent to という条件式を記述すると背後では

PREPARE statement_name(integer, integer) AS SELECT $1 < $2;

and then this prepared statement is <command>EXECUTE</command>d for each execution of the <command>IF</command> statement, with the current values of the <application>PL/pgSQL</application> variables supplied as parameter values. Normally these details are not important to a <application>PL/pgSQL</application> user, but they are useful to know when trying to diagnose a problem. More information appears in <xref linkend="plpgsql-plan-caching"/>. と同等なプリペアドステートメントが作成されます。 そしてIF文を実行する度にPL/pgSQLの最新の変数値をパラメータ値として供給して、このプリペアドステートメントに対してEXECUTEを行います。 通常この詳細は、PL/pgSQLユーザにとって重要ではありませんが、この知識は問題点の解析に有用です。 それ以外の情報は、43.11.2に記述されています。

Since an <replaceable>expression</replaceable> is converted to a <literal>SELECT</literal> command, it can contain the same clauses that an ordinary <literal>SELECT</literal> would, except that it cannot include a top-level <literal>UNION</literal>, <literal>INTERSECT</literal>, or <literal>EXCEPT</literal> clause. Thus for example one could test whether a table is non-empty with expressionSELECTコマンドに変換されますので、通常のSELECTが含むことのできるものと同じ句を含むことができます。ただし、トップレベルのUNIONINTERSECTEXCEPT句は含むことができません。 そのため、例えば、以下によりテーブルが空でないか確かめることができます。

IF count(*) > 0 FROM my_table THEN ...

since the <replaceable>expression</replaceable> between <literal>IF</literal> and <literal>THEN</literal> is parsed as though it were <literal>SELECT count(*) &gt; 0 FROM my_table</literal>. The <literal>SELECT</literal> must produce a single column, and not more than one row. (If it produces no rows, the result is taken as NULL.) IFTHEN間のSELECT count(*) > 0 FROM my_tableであるかのように解析されるからです。 SELECTは1つの列、2つ以上でない行を生成しなければなりません。 (行を生成しないのであれば、結果はNULLとして受け付けられます。)