Movatterモバイル変換


[0]ホーム

URL:



Facebook
Postgres Pro
Facebook
Downloads
41.4. Expressions
Prev UpChapter 41. PL/pgSQLSQL Procedural LanguageHome Next

41.4. Expressions#

All expressions used inPL/pgSQL statements are processed using the server's mainSQL executor. For example, when you write aPL/pgSQL statement like

IFexpression THEN ...

PL/pgSQL will evaluate the expression by feeding a query like

SELECTexpression

to the main SQL engine. While forming theSELECT command, any occurrences ofPL/pgSQL variable names are replaced by query parameters, as discussed in detail inSection 41.11.1. This allows the query plan for theSELECT 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 aPREPARE command. For example, if we have declared two integer variablesx andy, and we write

IF x < y THEN ...

what happens behind the scenes is equivalent to

PREPAREstatement_name(integer, integer) AS SELECT $1 < $2;

and then this prepared statement isEXECUTEd for each execution of theIF statement, with the current values of thePL/pgSQL variables supplied as parameter values. Normally these details are not important to aPL/pgSQL user, but they are useful to know when trying to diagnose a problem. More information appears inSection 41.11.2.

Since anexpression is converted to aSELECT command, it can contain the same clauses that an ordinarySELECT would, except that it cannot include a top-levelUNION,INTERSECT, orEXCEPT clause. Thus for example one could test whether a table is non-empty with

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

since theexpression betweenIF andTHEN is parsed as though it wereSELECT count(*) > 0 FROM my_table. TheSELECT must produce a single column, and not more than one row. (If it produces no rows, the result is taken as NULL.)


Prev Up Next
41.3. Declarations Home 41.5. Basic Statements
pdfepub
Go to PostgreSQL 17
By continuing to browse this website, you agree to the use of cookies. Go toPrivacy Policy.

[8]ページ先頭

©2009-2025 Movatter.jp