# 4.2. Value Expressions

4.2.1. Column References

4.2.2. Positional Parameters

4.2.3. Subscripts

4.2.4. Field Selection

4.2.5. Operator Invocations

4.2.6. Function Calls

4.2.7. Aggregate Expressions

4.2.8. Window Function Calls

4.2.9. Type Casts

4.2.10. Collation Expressions

4.2.11. Scalar Subqueries

4.2.12. Array Constructors

4.2.13. Row Constructors

4.2.14. Expression Evaluation Rules

Value expressions are used in a variety of contexts, such as in the target list of theSELECTcommand, as new column values inINSERTorUPDATE, or in search conditions in a number of commands. The result of a value expression is sometimes called ascalar, to distinguish it from the result of a table expression (which is a table). Value expressions are therefore also calledscalar expressions(or even simplyexpressions). The expression syntax allows the calculation of values from primitive parts using arithmetic, logical, set, and other operations.

A value expression is one of the following:

  • A constant or literal value

  • A column reference

  • A positional parameter reference, in the body of a function definition or prepared statement

  • A subscripted expression

  • A field selection expression

  • An operator invocation

  • A function call

  • An aggregate expression

  • A window function call

  • A type cast

  • A collation expression

  • A scalar subquery

  • An array constructor

  • A row constructor

  • Another value expression in parentheses (used to group subexpressions and override precedence)

    In addition to this list, there are a number of constructs that can be classified as an expression but do not follow any general syntax rules. These generally have the semantics of a function or operator and are explained in the appropriate location inChapter 9. An example is theIS NULLclause.

    We have already discussed constants inSection 4.1.2. The following sections discuss the remaining options.

# 4.2.1. Column References

A column can be referenced in the form:

correlation.columnname

*correlation*is the name of a table (possibly qualified with a schema name), or an alias for a table defined by means of aFROMclause. The correlation name and separating dot can be omitted if the column name is unique across all the tables being used in the current query. (See alsoChapter 7.)

# 4.2.2. Positional Parameters

A positional parameter reference is used to indicate a value that is supplied externally to an SQL statement. Parameters are used in SQL function definitions and in prepared queries. Some client libraries also support specifying data values separately from the SQL command string, in which case parameters are used to refer to the out-of-line data values. The form of a parameter reference is:

$number

For example, consider the definition of a function,dept, as:

CREATE FUNCTION dept(text) RETURNS dept
    AS $$ SELECT * FROM dept WHERE name = $1 $$
    LANGUAGE SQL;

Here the$1references the value of the first function argument whenever the function is invoked.

# 4.2.3. Subscripts

If an expression yields a value of an array type, then a specific element of the array value can be extracted by writing

expression[subscript]

or multiple adjacent elements (an “array slice”) can be extracted by writing

expression[lower_subscript:upper_subscript]

(Here, the brackets[ ]are meant to appear literally.) Each*subscript*is itself an expression, which will be rounded to the nearest integer value.

In general the array*expression*must be parenthesized, but the parentheses can be omitted when the expression to be subscripted is just a column reference or positional parameter. Also, multiple subscripts can be concatenated when the original array is multidimensional. For example:

mytable.arraycolumn[4]
mytable.two_d_column[17][34]
$1[10:42]
(arrayfunction(a,b))

[42]

The parentheses in the last example are required. SeeSection 8.15for more about arrays.

# 4.2.4. Field Selection

If an expression yields a value of a composite type (row type), then a specific field of the row can be extracted by writing

expression.fieldname

In general the row*expression*must be parenthesized, but the parentheses can be omitted when the expression to be selected from is just a table reference or positional parameter. For example:

mytable.mycolumn
$1.somecolumn
(rowfunction(a,b)).col3

(Thus, a qualified column reference is actually just a special case of the field selection syntax.) An important special case is extracting a field from a table column that is of a composite type:

(compositecol).somefield
(mytable.compositecol).somefield

The parentheses are required here to show thatcompositecolis a column name not a table name, or thatmytableis a table name not a schema name in the second case.

You can ask for all fields of a composite value by writing.*:

(compositecol).*

This notation behaves differently depending on context; seeSection 8.16.5for details.

# 4.2.5. Operator Invocations

There are two possible syntaxes for an operator invocation:

expression operator expression(binary infix operator)
operator expression(unary prefix operator)

where the*operator*token follows the syntax rules ofSection 4.1.3, or is one of the key wordsAND,OR, andNOT, or is a qualified operator name in the form:

OPERATOR(schema.operatorname)

Which particular operators exist and whether they are unary or binary depends on what operators have been defined by the system or the user.Chapter 9describes the built-in operators.

# 4.2.6. Function Calls

The syntax for a function call is the name of a function (possibly qualified with a schema name), followed by its argument list enclosed in parentheses:

function_name ([expression [, expression ... ]] )

For example, the following computes the square root of 2:

sqrt(2)

The list of built-in functions is inChapter 9. Other functions can be added by the user.

When issuing queries in a database where some users mistrust other users, observe security precautions fromSection 10.3when writing function calls.

The arguments can optionally have names attached. SeeSection 4.3for details.

# Note

A function that takes a single argument of composite type can optionally be called using field-selection syntax, and conversely field selection can be written in functional style. That is, the notationscol(table)andtable.colare interchangeable. This behavior is not SQL-standard but is provided in PostgreSQL because it allows use of functions to emulate “computed fields”. For more information seeSection 8.16.5.

# 4.2.7. Aggregate Expressions

Anaggregate expressionrepresents the application of an aggregate function across the rows selected by a query. An aggregate function reduces multiple inputs to a single output value, such as the sum or average of the inputs. The syntax of an aggregate expression is one of the following:

aggregate_name (expression [ , ... ] [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ]
aggregate_name (ALL expression [ , ... ] [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ]
aggregate_name (DISTINCT expression [ , ... ] [ order_by_clause ] ) [ FILTER ( WHERE filter_clause ) ]
aggregate_name ( * ) [ FILTER ( WHERE filter_clause ) ]
aggregate_name ( [ expression [ , ... ] ] ) WITHIN GROUP ( order_by_clause ) [ FILTER ( WHERE filter_clause ) ]

where*aggregate_nameis a previously defined aggregate (possibly qualified with a schema name) and表达是任何本身不包含聚合表达式或窗口函数调用的值表达式。可选的order_by_clause过滤子句*如下所述。

聚合表达式的第一种形式为每个输入行调用一次聚合。第二种形式与第一种形式相同,因为全部是默认值。第三种形式为在输入行中找到的每个不同的表达式值(或不同的值集,对于多个表达式)调用一次聚合。第四种形式为每个输入行调用一次聚合;由于没有指定特定的输入值,它通常只对数数(*)聚合函数。最后一种形式用于有序集聚合函数,如下所述。

大多数聚合函数忽略空输入,因此一个或多个表达式产生空值的行将被丢弃。对于所有内置聚合,除非另有说明,否则可以假定为 true。

例如,数数(*)产生输入行的总数;计数(f1)产生输入行数,其中f1是非空的,因为数数忽略空值;和计数(不同的 f1)产生不同的非空值的数量f1.

通常,输入行以未指定的顺序馈送到聚合函数。在许多情况下,这无关紧要。例如,分钟无论以何种顺序接收输入,都会产生相同的结果。但是,某些聚合函数(例如array_aggstring_agg) 产生的结果取决于输入行的顺序。当使用这样的聚合时,可选的*order_by_clause可用于指定所需的顺序。这order_by_clause*具有与查询级别相同的语法订购方式条款,如中所述第 7.5 节,除了它的表达式总是只是表达式,不能是输出列名或数字。例如:

SELECT array_agg(a ORDER BY b DESC) FROM table;

在处理多参数聚合函数时,请注意订购方式子句在所有聚合参数之后。例如,这样写:

SELECT string_agg(a, ',' ORDER BY a) FROM table;

不是这个:

SELECT string_agg(a ORDER BY a, ',') FROM table;  -- incorrect

后者在语法上是有效的,但它表示一个单参数聚合函数的调用,其中包含两个订购方式键(第二个相当没用,因为它是一个常数)。

如果清楚的被指定除了一个*order_by_clause*, 那么所有订购方式表达式必须匹配聚合的常规参数;也就是说,您不能对未包含在清楚的列表。

# 笔记

指定两者的能力清楚的订购方式在聚合函数中是 PostgreSQL 扩展。

配售订购方式在聚合的常规参数列表中,如目前所描述的,用于对通用和统计聚合的输入行进行排序时,排序是可选的。聚合函数有一个子类,称为有序集聚合为此*order_by_clause必需的*,通常是因为聚合的计算仅在其输入行的特定排序方面才有意义。有序集聚合的典型示例包括排名和百分位数计算。对于有序集聚合,order_by_clause里面写着组内 (...),如上面的最终语法替代所示。中的表达式order_by_clause每个输入行评估一次,就像常规聚合参数一样,按order_by_clause的要求,并作为输入参数提供给聚合函数。(这与非组内 order_by_clause,它不被视为聚合函数的参数。)前面的参数表达式组内,如果有的话,被称为直接论据将它们与聚合参数列在*order_by_clause*. Unlike regular aggregate arguments, direct arguments are evaluated only once per aggregate call, not once per input row. This means that they can contain variables only if those variables are grouped byGROUP BY; this restriction is the same as if the direct arguments were not inside an aggregate expression at all. Direct arguments are typically used for things like percentile fractions, which only make sense as a single value per aggregation calculation. The direct argument list can be empty; in this case, write just()not(*). (PostgreSQL will actually accept either spelling, but only the first way conforms to the SQL standard.)

An example of an ordered-set aggregate call is:

SELECT percentile_cont(0.5) WITHIN GROUP (ORDER BY income) FROM households;
 percentile_cont
### 4.2.8. Window Function Calls

[]()[]()

 A *window function call* represents the application of an aggregate-like function over some portion of the rows selected by a query. Unlike non-window aggregate calls, this is not tied to grouping of the selected rows into a single output row — each row remains separate in the query output. However the window function has access to all the rows that would be part of the current row's group according to the grouping specification (`PARTITION BY` list) of the window function call. The syntax of a window function call is one of the following:

function_name ([expression[, expression ...]])[FILTER ( WHERE filter_clause )]OVER window_name function_name ([expression[, expression ...]])[FILTER ( WHERE filter_clause )]OVER ( window_definition ) function_name ()[FILTER ( WHERE filter_clause )]OVER window_name function_name ()[FILTER ( WHERE filter_clause )]OVER ( window_definition )

 where *`window_definition`* has the syntax

[existing_window_name]PARTITION BY expression[, ...]][ORDER BY expression [ ASC | DESC | USING operator ] [ NULLS { FIRST | LAST } ] [, ...]][ frame_clause ]

 The optional *`frame_clause`* can be one of

{ RANGE | ROWS | GROUPS } frame_start[frame_exclusion]{ RANGE | ROWS | GROUPS } BETWEEN frame_start AND frame_end[frame_exclusion]

 where *`frame_start`* and *`frame_end`* can be one of

UNBOUNDED PRECEDING offset PRECEDING CURRENT ROW offset FOLLOWING UNBOUNDED FOLLOWING

 and *`frame_exclusion`* can be one of

EXCLUDE CURRENT ROW EXCLUDE GROUP EXCLUDE TIES EXCLUDE NO OTHERS

 Here, *`expression`* represents any value expression that does not itself contain window function calls.

*`window_name`* is a reference to a named window specification defined in the query's `WINDOW` clause. Alternatively, a full *`window_definition`* can be given within parentheses, using the same syntax as for defining a named window in the `WINDOW` clause; see the [SELECT](sql-select.html) reference page for details. It's worth pointing out that `OVER wname` is not exactly equivalent to `OVER (wname ...)`; the latter implies copying and modifying the window definition, and will be rejected if the referenced window specification includes a frame clause.

 The `PARTITION BY` clause groups the rows of the query into *partitions*, which are processed separately by the window function. `PARTITION BY` works similarly to a query-level `GROUP BY` clause, except that its expressions are always just expressions and cannot be output-column names or numbers. Without `PARTITION BY`, all rows produced by the query are treated as a single partition. The `ORDER BY` clause determines the order in which the rows of a partition are processed by the window function. It works similarly to a query-level `ORDER BY` clause, but likewise cannot use output-column names or numbers. Without `ORDER BY`, rows are processed in an unspecified order.

 The *`frame_clause`* specifies the set of rows constituting the *window frame*, which is a subset of the current partition, for those window functions that act on the frame instead of the whole partition. The set of rows in the frame can vary depending on which row is the current row. The frame can be specified in `RANGE`, `ROWS` or `GROUPS` mode; in each case, it runs from the *`frame_start`* to the *`frame_end`*. If *`frame_end`* is omitted, the end defaults to `CURRENT ROW`.

 A *`frame_start`* of `UNBOUNDED PRECEDING` means that the frame starts with the first row of the partition, and similarly a *`frame_end`* of `UNBOUNDED FOLLOWING` means that the frame ends with the last row of the partition.

 In `RANGE` or `GROUPS` mode, a *`frame_start`* of `CURRENT ROW` means the frame starts with the current row's first *peer* row (a row that the window's `ORDER BY` clause sorts as equivalent to the current row), while a *`frame_end`* of `CURRENT ROW` means the frame ends with the current row's last peer row. In `ROWS` mode, `CURRENT ROW` simply means the current row.

 In the *`offset`* `PRECEDING` and *`offset`* `FOLLOWING` frame options, the *`offset`* must be an expression not containing any variables, aggregate functions, or window functions. The meaning of the *`offset`* depends on the frame mode:

* In `ROWS` mode, the *`offset`* must yield a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of rows before or after the current row.

* In `GROUPS` mode, the *`offset`* again must yield a non-null, non-negative integer, and the option means that the frame starts or ends the specified number of *peer groups* before or after the current row's peer group, where a peer group is a set of rows that are equivalent in the `ORDER BY` ordering. (There must be an `ORDER BY` clause in the window definition to use `GROUPS` mode.)

* In `RANGE` mode, these options require that the `ORDER BY` clause specify exactly one column. The *`offset`* specifies the maximum difference between the value of that column in the current row and its value in preceding or following rows of the frame. The data type of the *`offset`* expression varies depending on the data type of the ordering column. For numeric ordering columns it is typically of the same type as the ordering column, but for datetime ordering columns it is an `interval`. For example, if the ordering column is of type `date` or `timestamp`, one could write `RANGE BETWEEN '1 day' PRECEDING AND '10 days' FOLLOWING`. The *`offset`* is still required to be non-null and non-negative, though the meaning of “non-negative” depends on its data type.

 In any case, the distance to the end of the frame is limited by the distance to the end of the partition, so that for rows near the partition ends the frame might contain fewer rows than elsewhere.

 Notice that in both `ROWS` and `GROUPS` mode, `0 PRECEDING` and `0 FOLLOWING` are equivalent to `CURRENT ROW`. This normally holds in `RANGE` mode as well, for an appropriate data-type-specific meaning of “zero”.

 The *`frame_exclusion`* option allows rows around the current row to be excluded from the frame, even if they would be included according to the frame start and frame end options. `EXCLUDE CURRENT ROW` excludes the current row from the frame. `EXCLUDE GROUP` excludes the current row and its ordering peers from the frame. `EXCLUDE TIES` excludes any peers of the current row from the frame, but not the current row itself. `EXCLUDE NO OTHERS` simply specifies explicitly the default behavior of not excluding the current row or its peers.

 The default framing option is `RANGE UNBOUNDED PRECEDING`, which is the same as `RANGE BETWEEN UNBOUNDED PRECEDING AND CURRENT ROW`. With `ORDER BY`, this sets the frame to be all rows from the partition start up through the current row's last `ORDER BY` peer. Without `ORDER BY`, this means all rows of the partition are included in the window frame, since all rows become peers of the current row.

 Restrictions are that *`frame_start`* cannot be `UNBOUNDED FOLLOWING`, *`frame_end`* cannot be `UNBOUNDED PRECEDING`, and the *`frame_end`* choice cannot appear earlier in the above list of *`frame_start`* and *`frame_end`* options than the *`frame_start`* choice does — for example `RANGE BETWEEN CURRENT ROW AND *`offset`* PRECEDING` is not allowed. But, for example, `ROWS BETWEEN 7 PRECEDING AND 8 PRECEDING` is allowed, even though it would never select any rows.

 If `FILTER` is specified, then only the input rows for which the *`filter_clause`* evaluates to true are fed to the window function; other rows are discarded. Only window functions that are aggregates accept a `FILTER` clause.

 The built-in window functions are described in [Table 9.62](functions-window.html#FUNCTIONS-WINDOW-TABLE). Other window functions can be added by the user. Also, any built-in or user-defined general-purpose or statistical aggregate can be used as a window function. (Ordered-set and hypothetical-set aggregates cannot presently be used as window functions.)

 The syntaxes using `*` are used for calling parameter-less aggregate functions as window functions, for example `count(*) OVER (PARTITION BY x ORDER BY y)`. The asterisk (`*`) is customarily not used for window-specific functions. Window-specific functions do not allow `DISTINCT` or `ORDER BY` to be used within the function argument list.

 Window function calls are permitted only in the `SELECT` list and the `ORDER BY` clause of the query.

 More information about window functions can be found in [Section 3.5](tutorial-window.html), [Section 9.22](functions-window.html), and [Section 7.2.5](queries-table-expressions.html#QUERIES-WINDOW).

### 4.2.9. Type Casts

[]()[]()[]()

 A type cast specifies a conversion from one data type to another. PostgreSQL accepts two equivalent syntaxes for type casts:

CAST ( expression AS type ) expression::type

 The `CAST` syntax conforms to SQL; the syntax with `::` is historical PostgreSQL usage.

 When a cast is applied to a value expression of a known type, it represents a run-time type conversion. The cast will succeed only if a suitable type conversion operation has been defined. Notice that this is subtly different from the use of casts with constants, as shown in [Section 4.1.2.7](sql-syntax-lexical.html#SQL-SYNTAX-CONSTANTS-GENERIC). A cast applied to an unadorned string literal represents the initial assignment of a type to a literal constant value, and so it will succeed for any type (if the contents of the string literal are acceptable input syntax for the data type).

 An explicit type cast can usually be omitted if there is no ambiguity as to the type that a value expression must produce (for example, when it is assigned to a table column); the system will automatically apply a type cast in such cases. However, automatic casting is only done for casts that are marked “OK to apply implicitly” in the system catalogs. Other casts must be invoked with explicit casting syntax. This restriction is intended to prevent surprising conversions from being applied silently.

 It is also possible to specify a type cast using a function-like syntax:

typename ( expression )

 However, this only works for types whose names are also valid as function names. For example, `double precision` cannot be used this way, but the equivalent `float8` can. Also, the names `interval`, `time`, and `timestamp` can only be used in this fashion if they are double-quoted, because of syntactic conflicts. Therefore, the use of the function-like cast syntax leads to inconsistencies and should probably be avoided.

### Note

 The function-like syntax is in fact just a function call. When one of the two standard cast syntaxes is used to do a run-time conversion, it will internally invoke a registered function to perform the conversion. By convention, these conversion functions have the same name as their output type, and thus the “function-like syntax” is nothing more than a direct invocation of the underlying conversion function. Obviously, this is not something that a portable application should rely on. For further details see [CREATE CAST](sql-createcast.html).

### 4.2.10. Collation Expressions

[]()

 The `COLLATE` clause overrides the collation of an expression. It is appended to the expression it applies to:

expr COLLATE collation

 where *`collation`* is a possibly schema-qualified identifier. The `COLLATE` clause binds tighter than operators; parentheses can be used when necessary.

 If no collation is explicitly specified, the database system either derives a collation from the columns involved in the expression, or it defaults to the default collation of the database if no column is involved in the expression.

 The two common uses of the `COLLATE` clause are overriding the sort order in an `ORDER BY` clause, for example:

SELECT a, b, c FROM tbl WHERE ... ORDER BY a COLLATE "C";

 and overriding the collation of a function or operator call that has locale-sensitive results, for example:

SELECT * FROM tbl WHERE a > 'foo' COLLATE "C";

 Note that in the latter case the `COLLATE` clause is attached to an input argument of the operator we wish to affect. It doesn't matter which argument of the operator or function call the `COLLATE` clause is attached to, because the collation that is applied by the operator or function is derived by considering all arguments, and an explicit `COLLATE` clause will override the collations of all other arguments. (Attaching non-matching `COLLATE` clauses to more than one argument, however, is an error. For more details see [Section 24.2](collation.html).) Thus, this gives the same result as the previous example:

SELECT * FROM tbl WHERE a COLLATE "C" > 'foo';

 But this is an error:

SELECT * FROM tbl WHERE (a > 'foo') COLLATE "C";

 because it attempts to apply a collation to the result of the `>` operator, which is of the non-collatable data type `boolean`.

### 4.2.11. Scalar Subqueries

[]()

 A scalar subquery is an ordinary `SELECT` query in parentheses that returns exactly one row with one column. (See [Chapter 7](queries.html) for information about writing queries.) The `SELECT` query is executed and the single returned value is used in the surrounding value expression. It is an error to use a query that returns more than one row or more than one column as a scalar subquery. (But if, during a particular execution, the subquery returns no rows, there is no error; the scalar result is taken to be null.) The subquery can refer to variables from the surrounding query, which will act as constants during any one evaluation of the subquery. See also [Section 9.23](functions-subquery.html) for other expressions involving subqueries.

 For example, the following finds the largest city population in each state:

SELECT name, (SELECT max(pop) FROM cities WHERE cities.state = states.name) FROM states;

### 4.2.12. Array Constructors

[]()[]()

 An array constructor is an expression that builds an array value using values for its member elements. A simple array constructor consists of the key word `ARRAY`, a left square bracket `[`, a list of expressions (separated by commas) for the array element values, and finally a right square bracket `]`. For example:

SELECT ARRAY[1,2,3+4]; array

# 4.2.13. Row Constructors

A row constructor is an expression that builds a row value (also called a composite value) using values for its member fields. A row constructor consists of the key wordROW, a left parenthesis, zero or more expressions (separated by commas) for the row field values, and finally a right parenthesis. For example:

SELECT ROW(1,2.5,'this is a test');

The key wordROWis optional when there is more than one expression in the list.

A row constructor can include the syntax*rowvalue*.*, which will be expanded to a list of the elements of the row value, just as occurs when the.*syntax is used at the top level of aSELECT列表(见第 8.16.5 节)。例如,如果表有列f1f2,这些是相同的:

SELECT ROW(t.*, 42) FROM t;
SELECT ROW(t.f1, t.f2, 42) FROM t;

# 笔记

在 PostgreSQL 8.2 之前,.*语法没有在行构造函数中展开,所以写行(t.*,42)创建了一个双字段行,其第一个字段是另一个行值。新行为通常更有用。如果您需要嵌套行值的旧行为,请编写内部行值而不.*, 例如行(t,42).

默认情况下,由 a 创建的值表达式是匿名记录类型。如有必要,可以将其转换为命名的复合类型——表的行类型或使用创建的复合类型创建类型为.可能需要显式转换以避免歧义。例如:

CREATE TABLE mytable(f1 int, f2 float, f3 text);

CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1' LANGUAGE SQL;

-- No cast needed since only one getf1() exists
SELECT getf1(ROW(1,2.5,'this is a test'));
 getf1
### 4.2.14. Expression Evaluation Rules

[]()

 The order of evaluation of subexpressions is not defined. In particular, the inputs of an operator or function are not necessarily evaluated left-to-right or in any other fixed order.

 Furthermore, if the result of an expression can be determined by evaluating only some parts of it, then other subexpressions might not be evaluated at all. For instance, if one wrote:

选择真或 somefunc();

 then `somefunc()` would (probably) not be called at all. The same would be the case if one wrote:

选择 somefunc() 或真;

 Note that this is not the same as the left-to-right “short-circuiting” of Boolean operators that is found in some programming languages.

 As a consequence, it is unwise to use functions with side effects as part of complex expressions. It is particularly dangerous to rely on side effects or evaluation order in `WHERE` and `HAVING` clauses, since those clauses are extensively reprocessed as part of developing an execution plan. Boolean expressions (`AND`/`OR`/`NOT` combinations) in those clauses can be reorganized in any manner allowed by the laws of Boolean algebra.

 When it is essential to force evaluation order, a `CASE` construct (see [Section 9.18](functions-conditional.html)) can be used. For example, this is an untrustworthy way of trying to avoid division by zero in a `WHERE` clause:

选择 ... 其中 x > 0 且 y/x > 1.5;

 But this is safe:

SELECT ... WHERE CASE WHEN x > 0 THEN y/x > 1.5 ELSE false END;

 A `CASE` construct used in this fashion will defeat optimization attempts, so it should only be done when necessary. (In this particular example, it would be better to sidestep the problem by writing `y > 1.5*x` instead.)

`CASE` is not a cure-all for such issues, however. One limitation of the technique illustrated above is that it does not prevent early evaluation of constant subexpressions. As described in [Section 38.7](xfunc-volatility.html), functions and operators marked `IMMUTABLE` can be evaluated when the query is planned rather than when it is executed. Thus for example

SELECT CASE WHEN x > 0 THEN x ELSE 1/0 END FROM tab;

 is likely to result in a division-by-zero failure due to the planner trying to simplify the constant subexpression, even if every row in the table has `x > 0` so that the `ELSE` arm would never be entered at run time.

 While that particular example might seem silly, related cases that don't obviously involve constants can occur in queries executed within functions, since the values of function arguments and local variables can be inserted into queries as constants for planning purposes. Within PL/pgSQL functions, for example, using an `IF`-`THEN`-`ELSE` statement to protect a risky computation is much safer than just nesting it in a `CASE` expression.

 Another limitation of the same kind is that a `CASE` cannot prevent evaluation of an aggregate expression contained within it, because aggregate expressions are computed before other expressions in a `SELECT` list or `HAVING` clause are considered. For example, the following query can cause a division-by-zero error despite seemingly having protected against it:

SELECT CASE WHEN min(employees) > 0 THEN avg(expenses / employees) END FROM 部门;

 The `min()` and `avg()` aggregates are computed concurrently over all the input rows, so if any row has `employees` equal to zero, the division-by-zero error will occur before there is any opportunity to test the result of `min()`. Instead, use a `WHERE` or `FILTER` clause to prevent problematic input rows from reaching an aggregate function in the first place.