Greenplum Database is based on the PostgreSQL implementation of the SQL standard.

This topic describes how to construct SQL queries in Greenplum Database.

Parent topic: Querying Data

SQL Lexicon

SQL is a standard language for accessing databases. The language consists of elements that enable data storage, retrieval, analysis, viewing, manipulation, and so on. You use SQL commands to construct queries and commands that the Greenplum Database engine understands. SQL queries consist of a sequence of commands. Commands consist of a sequence of valid tokens in correct syntax order, terminated by a semicolon (;).

For more information about SQL commands, see SQL Command Reference.

Greenplum Database uses PostgreSQL’s structure and syntax, with some exceptions. For more information about SQL rules and concepts in PostgreSQL, see “SQL Syntax” in the PostgreSQL documentation.

SQL Value Expressions

SQL value expressions consist of one or more values, symbols, operators, SQL functions, and data. The expressions compare data or perform calculations and return a value as the result. Calculations include logical, arithmetic, and set operations.

The following are value expressions:

  • An aggregate expression
  • An array constructor
  • A column reference
  • A constant or literal value
  • A correlated subquery
  • A field selection expression
  • A function call
  • A new column value in an INSERTor UPDATE
  • An operator invocation column reference
  • A positional parameter reference, in the body of a function definition or prepared statement
  • A row constructor
  • A scalar subquery
  • A search condition in a WHERE clause
  • A target list of a SELECT command
  • A type cast
  • A value expression in parentheses, useful to group sub-expressions and override precedence
  • A window expression

SQL constructs such as functions and operators are expressions but do not follow any general syntax rules. For more information about these constructs, see Using Functions and Operators.

Column References

A column reference has the form:

  1. <correlation>.<columnname>

Here, correlation is the name of a table (possibly qualified with a schema name) or an alias for a table defined with a FROM clause or one of the keywords NEW or OLD. NEW and OLD can appear only in rewrite rules, but you can use other correlation names in any SQL statement. If the column name is unique across all tables in the query, you can omit the “correlation.” part of the column reference.

Positional Parameters

Positional parameters are arguments to SQL statements or functions that you reference by their positions in a series of arguments. For example, $1 refers to the first argument, $2 to the second argument, and so on. The values of positional parameters are set from arguments external to the SQL statement or supplied when SQL functions are invoked. Some client libraries support specifying data values separately from the SQL command, in which case parameters refer to the out-of-line data values. A parameter reference has the form:

  1. $number

For example:

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

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

Subscripts

If an expression yields a value of an array type, you can extract a specific element of the array value as follows:

  1. <expression>[<subscript>]

You can extract multiple adjacent elements, called an array slice, as follows (including the brackets):

  1. <expression>[<lower_subscript>:<upper_subscript>]

Each subscript is an expression and yields an integer value.

Array expressions usually must be in parentheses, but you can omit the parentheses when the expression to be subscripted is a column reference or positional parameter. You can concatenate multiple subscripts when the original array is multidimensional. For example (including the parentheses):

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

Field Selection

If an expression yields a value of a composite type (row type), you can extract a specific field of the row as follows:

  1. <expression>.<fieldname>

The row expression usually must be in parentheses, but you can omit these parentheses when the expression to be selected from is a table reference or positional parameter. For example:

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

A qualified column reference is a special case of field selection syntax.

Operator Invocations

Operator invocations have the following possible syntaxes:

  1. <expression operator expression>(binary infix operator)
  1. <operator expression>(unary prefix operator)
  1. <expression operator>(unary postfix operator)

Where operator is an operator token, one of the key words AND, OR, or NOT, or qualified operator name in the form:

  1. OPERATOR(<schema>.<operatorname>)

Available operators and whether they are unary or binary depends on the operators that the system or user defines. For more information about built-in operators, see Built-in Functions and Operators.

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:

  1. function ([expression [, expression ... ]])

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

  1. sqrt(2)

See Summary of Built-in Functions for lists of the built-in functions by category. You can add custom functions, too.

Aggregate Expressions

An aggregate expression applies an aggregate function across the rows that a query selects. An aggregate function performs a calculation on a set of values and returns a single value, such as the sum or average of the set of values. The syntax of an aggregate expression is one of the following:

  • aggregate_name(expression [ , ... ] ) [ FILTER ( WHERE filter_clause ) ] — operates across all input rows for which the expected result value is non-null. ALL is the default.
  • aggregate_name(ALL expression [ , ... ] ) [ FILTER ( WHERE filter_clause ) ] — operates identically to the first form because ALL is the default.
  • aggregate_name(DISTINCT expression [ , ... ] ) [ FILTER ( WHERE filter_clause ) ] — operates across all distinct non-null values of input rows.
  • aggregate_name(*) [ FILTER ( WHERE filter_clause ) ] — operates on all rows with values both null and non-null. Generally, this form is most useful for the count(*) aggregate function.

Where aggregate_name is a previously defined aggregate (possibly schema-qualified) and expression is any value expression that does not contain an aggregate expression.

For example, count(*) yields the total number of input rows, count(f1) yields the number of input rows in which f1 is non-null, andcount(distinct f1) yields the number of distinct non-null values of f1.

If FILTER is specified, then only the input rows for which the filter_clause evaluates to true are fed to the aggregate function; other rows are discarded. For example:

  1. SELECT
  2. count(*) AS unfiltered,
  3. count(*) FILTER (WHERE i < 5) AS filtered
  4. FROM generate_series(1,10) AS s(i);
  5. unfiltered | filtered
  6. ------------+----------
  7. 10 | 4
  8. (1 row)

For predefined aggregate functions, see Built-in Functions and Operators. You can also add custom aggregate functions.

Greenplum Database provides the MEDIAN aggregate function, which returns the fiftieth percentile of the PERCENTILE_CONT result and special aggregate expressions for inverse distribution functions as follows:

  1. PERCENTILE_CONT(_percentage_) WITHIN GROUP (ORDER BY _expression_)
  1. PERCENTILE_DISC(_percentage_) WITHIN GROUP (ORDER BY _expression_)

Currently you can use only these two expressions with the keyword WITHIN GROUP.

Limitations of Aggregate Expressions

The following are current limitations of the aggregate expressions:

  • Greenplum Database does not support the following keywords: ALL, DISTINCT, and OVER. See Table 5 for more details.
  • An aggregate expression can appear only in the result list or HAVING clause of a SELECT command. It is forbidden in other clauses, such as WHERE, because those clauses are logically evaluated before the results of aggregates form. This restriction applies to the query level to which the aggregate belongs.
  • When an aggregate expression appears in a subquery, the aggregate is normally evaluated over the rows of the subquery. If the aggregate’s arguments (and filter_clause if any) contain only outer-level variables, the aggregate belongs to the nearest such outer level and evaluates over the rows of that query. The aggregate expression as a whole is then an outer reference for the subquery in which it appears, and the aggregate expression acts as a constant over any one evaluation of that subquery. The restriction about appearing only in the result list or HAVING clause applies with respect to the query level at which the aggregate appears. See Scalar Subqueries and Table 3.
  • Greenplum Database does not support specifying an aggregate function as an argument to another aggregate function.
  • Greenplum Database does not support specifying a window function as an argument to an aggregate function.

Window Expressions

Window expressions allow application developers to more easily compose complex online analytical processing (OLAP) queries using standard SQL commands. For example, with window expressions, users can calculate moving averages or sums over various intervals, reset aggregations and ranks as selected column values change, and express complex ratios in simple terms.

A window expression represents the application of a window function to a window frame, which is defined with an OVER() clause. This is comparable to the type of calculation that can be done with an aggregate function and a GROUP BY clause. Unlike aggregate functions, which return a single result value for each group of rows, window functions return a result value for every row, but that value is calculated with respect to the set of rows in the window frame to which the row belongs. The OVER() clause allows dividing the rows into partitions and then further restricting the window frame by specifying which rows preceding or following the current row within its partition to include in the calculation.

Greenplum Database does not support specifying a window function as an argument to another window function.

The syntax of a window expression is:

  1. window_function ( [expression [, ...]] ) [ FILTER ( WHERE filter_clause ) ] OVER ( window_specification )

Where window_function is one of the functions listed in Table 4 or a user-defined window function, expression is any value expression that does not contain a window expression, and window_specification is:

  1. [window_name]
  2. [PARTITION BY expression [, ...]]
  3. [[ORDER BY expression [ASC | DESC | USING operator] [NULLS {FIRST | LAST}] [, ...]
  4. [{RANGE | ROWS}
  5. { UNBOUNDED PRECEDING
  6. | expression PRECEDING
  7. | CURRENT ROW
  8. | BETWEEN window_frame_bound AND window_frame_bound }]]
  1. and where `window_frame_bound` can be one of:
  1. UNBOUNDED PRECEDING
  2. expression PRECEDING
  3. CURRENT ROW
  4. expression FOLLOWING
  5. UNBOUNDED FOLLOWING

A window expression can appear only in the select list of a SELECT command. For example:

  1. SELECT count(*) OVER(PARTITION BY customer_id), * FROM sales;

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. In a window expression, a FILTER clause can be used only with a window_function that is an aggregate function.

In a window expression, the expression must contain an OVER clause. The OVER clause specifies the window frame—the rows to be processed by the window function. This syntactically distinguishes the function from a regular or aggregate function.

In a window aggregate function that is used in a window expression, Greenplum Database does not support a DISTINCT clause with multiple input expressions.

A window specification has the following characteristics:

  • The PARTITION BY clause defines the window partitions to which the window function is applied. If omitted, the entire result set is treated as one partition.
  • The ORDER BY clause defines the expression(s) for sorting rows within a window partition. The ORDER BY clause of a window specification is separate and distinct from the ORDER BY clause of a regular query expression. The ORDER BY clause is required for the window functions that calculate rankings, as it identifies the measure(s) for the ranking values. For OLAP aggregations, the ORDER BY clause is required to use window frames (the ROWS or RANGE clause).

Note

Columns of data types without a coherent ordering, such as time, are not good candidates for use in the ORDER BY clause of a window specification. Time, with or without a specified time zone, lacks a coherent ordering because addition and subtraction do not have the expected effects. For example, the following is not generally true: x::time < x::time + '2 hour'::interval

  • The ROWS or RANGE clause defines a window frame for aggregate (non-ranking) window functions. A window frame defines a set of rows within a window partition. When a window frame is defined, the window function computes on the contents of this moving frame rather than the fixed contents of the entire window partition. Window frames are row-based (ROWS) or value-based (RANGE).

Window Examples

The following examples demonstrate using window functions with partitions and window frames.

Example 1 – Aggregate Window Function Over a Partition

The PARTITION BY list in the OVER clause divides the rows into groups, or partitions, that have the same values as the specified expressions.

This example compares employees’ salaries with the average salaries for their departments:

  1. SELECT depname, empno, salary, avg(salary) OVER(PARTITION BY depname)
  2. FROM empsalary;
  3. depname | empno | salary | avg
  4. -----------+-------+--------+-----------------------
  5. develop | 9 | 4500 | 5020.0000000000000000
  6. develop | 10 | 5200 | 5020.0000000000000000
  7. develop | 11 | 5200 | 5020.0000000000000000
  8. develop | 7 | 4200 | 5020.0000000000000000
  9. develop | 8 | 6000 | 5020.0000000000000000
  10. personnel | 5 | 3500 | 3700.0000000000000000
  11. personnel | 2 | 3900 | 3700.0000000000000000
  12. sales | 1 | 5000 | 4866.6666666666666667
  13. sales | 3 | 4800 | 4866.6666666666666667
  14. sales | 4 | 4800 | 4866.6666666666666667
  15. (10 rows)

The first three output columns come from the table empsalary, and there is one output row for each row in the table. The fourth column is the average calculated on all rows that have the same depname value as the current row. Rows that share the same depname value constitute a partition, and there are three partitions in this example. The avg function is the same as the regular avg aggregate function, but the OVER clause causes it to be applied as a window function.

You can also put the window specification in a WINDOW clause and reference it in the select list. This example is equivalent to the previous query:

  1. SELECT depname, empno, salary, avg(salary) OVER(mywindow)
  2. FROM empsalary
  3. WINDOW mywindow AS (PARTITION BY depname);

Defining a named window is useful when the select list has multiple window functions using the same window specification.

Example 2 – Ranking Window Function With an ORDER BY Clause

An ORDER BY clause within the OVER clause controls the order in which rows are processed by window functions. The ORDER BY list for the window function does not have to match the output order of the query. This example uses the rank() window function to rank employees’ salaries within their departments:

  1. SELECT depname, empno, salary,
  2. rank() OVER (PARTITION BY depname ORDER BY salary DESC)
  3. FROM empsalary;
  4. depname | empno | salary | rank
  5. -----------+-------+--------+------
  6. develop | 8 | 6000 | 1
  7. develop | 11 | 5200 | 2
  8. develop | 10 | 5200 | 2
  9. develop | 9 | 4500 | 4
  10. develop | 7 | 4200 | 5
  11. personnel | 2 | 3900 | 1
  12. personnel | 5 | 3500 | 2
  13. sales | 1 | 5000 | 1
  14. sales | 4 | 4800 | 2
  15. sales | 3 | 4800 | 2
  16. (10 rows)
Example 3 – Aggregate Function over a Row Window Frame

A RANGE or ROWS clause defines the window frame—a set of rows within a partition—that the window function includes in the calculation. ROWS specifies a physical set of rows to process, for example all rows from the beginning of the partition to the current row.

This example calculates a running total of employee’s salaries by department using the sum() function to total rows from the start of the partition to the current row:

  1. SELECT depname, empno, salary,
  2. sum(salary) OVER (PARTITION BY depname ORDER BY salary
  3. ROWS between UNBOUNDED PRECEDING AND CURRENT ROW)
  4. FROM empsalary ORDER BY depname, sum;
  5. depname | empno | salary | sum
  6. -----------+-------+--------+-------
  7. develop | 7 | 4200 | 4200
  8. develop | 9 | 4500 | 8700
  9. develop | 11 | 5200 | 13900
  10. develop | 10 | 5200 | 19100
  11. develop | 8 | 6000 | 25100
  12. personnel | 5 | 3500 | 3500
  13. personnel | 2 | 3900 | 7400
  14. sales | 4 | 4800 | 4800
  15. sales | 3 | 4800 | 9600
  16. sales | 1 | 5000 | 14600
  17. (10 rows)
Example 4 – Aggregate Function for a Range Window Frame

RANGE specifies logical values based on values of the ORDER BY expression in the OVER clause. This example demonstrates the difference between ROWS and RANGE. The frame contains all rows with salary values less than or equal to the current row. Unlike the previous example, for employees with the same salary, the sum is the same and includes the salaries of all of those employees.

  1. SELECT depname, empno, salary,
  2. sum(salary) OVER (PARTITION BY depname ORDER BY salary
  3. RANGE between UNBOUNDED PRECEDING AND CURRENT ROW)
  4. FROM empsalary ORDER BY depname, sum;
  5. depname | empno | salary | sum
  6. -----------+-------+--------+-------
  7. develop | 7 | 4200 | 4200
  8. develop | 9 | 4500 | 8700
  9. develop | 11 | 5200 | 19100
  10. develop | 10 | 5200 | 19100
  11. develop | 8 | 6000 | 25100
  12. personnel | 5 | 3500 | 3500
  13. personnel | 2 | 3900 | 7400
  14. sales | 4 | 4800 | 9600
  15. sales | 3 | 4800 | 9600
  16. sales | 1 | 5000 | 14600
  17. (10 rows)

Type Casts

A type cast specifies a conversion from one data type to another. A cast applied to a value expression of a known type is a run-time type conversion. The cast succeeds only if a suitable type conversion is defined. This differs from the use of casts with constants. A cast applied to a string literal represents the initial assignment of a type to a literal constant value, so it succeeds for any type if the contents of the string literal are acceptable input syntax for the data type.

Greenplum Database supports three types of casts applied to a value expression:

  • Explicit cast - Greenplum Database applies a cast when you explicitly specify a cast between two data types. Greenplum Database accepts two equivalent syntaxes for explicit type casts:

    1. CAST ( expression AS type )
    2. expression::type

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

  • Assignment cast - Greenplum Database implicitly invokes a cast in assignment contexts, when assigning a value to a column of the target data type. For example, a CREATE CAST command with the AS ASSIGNMENT clause creates a cast that is applied implicitly in the assignment context. This example assignment cast assumes that tbl1.f1 is a column of type text. The INSERT command is allowed because the value is implicitly cast from the integer to text type.

    1. INSERT INTO tbl1 (f1) VALUES (42);
  • Implicit cast - Greenplum Database implicitly invokes a cast in assignment or expression contexts. For example, a CREATE CAST command with the AS IMPLICIT clause creates an implicit cast, a cast that is applied implicitly in both the assignment and expression context. This example implicit cast assumes that tbl1.c1 is a column of type int. For the calculation in the predicate, the value of c1 is implicitly cast from int to a decimal type.

    1. SELECT * FROM tbl1 WHERE tbl1.c2 = (4.3 + tbl1.c1) ;

You can usually omit an explicit type cast if there is no ambiguity about the type a value expression must produce (for example, when it is assigned to a table column); the system automatically applies a type cast. Greenplum Database implicitly applies casts only to casts defined with a cast context of assignment or explicit in the system catalogs. Other casts must be invoked with explicit casting syntax to prevent unexpected conversions from being applied without the user’s knowledge.

You can display cast information with the psql meta-command \dC. Cast information is stored in the catalog table pg_cast, and type information is stored in the catalog table pg_type.

Scalar Subqueries

A scalar subquery is a SELECT query in parentheses that returns exactly one row with one column. Do not use a SELECT query that returns multiple rows or columns as a scalar subquery. The query runs and uses the returned value in the surrounding value expression. A correlated scalar subquery contains references to the outer query block.

Correlated Subqueries

A correlated subquery (CSQ) is a SELECT query with a WHERE clause or target list that contains references to the parent outer clause. CSQs efficiently express results in terms of results of another query. Greenplum Database supports correlated subqueries that provide compatibility with many existing applications. A CSQ is a scalar or table subquery, depending on whether it returns one or multiple rows. Greenplum Database does not support correlated subqueries with skip-level correlations.

Correlated Subquery Examples

Example 1 – Scalar correlated subquery

  1. SELECT * FROM t1 WHERE t1.x
  2. > (SELECT MAX(t2.x) FROM t2 WHERE t2.y = t1.y);

Example 2 – Correlated EXISTS subquery

  1. SELECT * FROM t1 WHERE
  2. EXISTS (SELECT 1 FROM t2 WHERE t2.x = t1.x);

Greenplum Database uses one of the following methods to run CSQs:

  • Unnest the CSQ into join operations – This method is most efficient, and it is how Greenplum Database runs most CSQs, including queries from the TPC-H benchmark.
  • Run the CSQ on every row of the outer query – This method is relatively inefficient, and it is how Greenplum Database runs queries that contain CSQs in the SELECT list or are connected by OR conditions.

The following examples illustrate how to rewrite some of these types of queries to improve performance.

Example 3 - CSQ in the Select List

Original Query

  1. SELECT T1.a,
  2. (SELECT COUNT(DISTINCT T2.z) FROM t2 WHERE t1.x = t2.y) dt2
  3. FROM t1;

Rewrite this query to perform an inner join with t1 first and then perform a left join with t1 again. The rewrite applies for only an equijoin in the correlated condition.

Rewritten Query

  1. SELECT t1.a, dt2 FROM t1
  2. LEFT JOIN
  3. (SELECT t2.y AS csq_y, COUNT(DISTINCT t2.z) AS dt2
  4. FROM t1, t2 WHERE t1.x = t2.y
  5. GROUP BY t1.x)
  6. ON (t1.x = csq_y);

Example 4 - CSQs connected by OR Clauses

Original Query

  1. SELECT * FROM t1
  2. WHERE
  3. x > (SELECT COUNT(*) FROM t2 WHERE t1.x = t2.x)
  4. OR x < (SELECT COUNT(*) FROM t3 WHERE t1.y = t3.y)

Rewrite this query to separate it into two parts with a union on the OR conditions.

Rewritten Query

  1. SELECT * FROM t1
  2. WHERE x > (SELECT count(*) FROM t2 WHERE t1.x = t2.x)
  3. UNION
  4. SELECT * FROM t1
  5. WHERE x < (SELECT count(*) FROM t3 WHERE t1.y = t3.y)

To view the query plan, use EXPLAIN SELECT or EXPLAIN ANALYZE SELECT. Subplan nodes in the query plan indicate that the query will run on every row of the outer query, and the query is a candidate for rewriting. For more information about these statements, see Query Profiling.

Array Constructors

An array constructor is an expression that builds an array value from values for its member elements. A simple array constructor consists of the key word ARRAY, a left square bracket [, one or more expressions separated by commas for the array element values, and a right square bracket ]. For example,

  1. SELECT ARRAY[1,2,3+4];
  2. array
  3. ---------
  4. {1,2,7}

The array element type is the common type of its member expressions, determined using the same rules as for UNION or CASE constructs.

You can build multidimensional array values by nesting array constructors. In the inner constructors, you can omit the keyword ARRAY. For example, the following two SELECT statements produce the same result:

  1. SELECT ARRAY[ARRAY[1,2], ARRAY[3,4]];
  2. SELECT ARRAY[[1,2],[3,4]];
  3. array
  4. ---------------
  5. {{1,2},{3,4}}

Since multidimensional arrays must be rectangular, inner constructors at the same level must produce sub-arrays of identical dimensions.

Multidimensional array constructor elements are not limited to a sub-ARRAY construct; they are anything that produces an array of the proper kind. For example:

  1. CREATE TABLE arr(f1 int[], f2 int[]);
  2. INSERT INTO arr VALUES (ARRAY[[1,2],[3,4]],
  3. ARRAY[[5,6],[7,8]]);
  4. SELECT ARRAY[f1, f2, '{{9,10},{11,12}}'::int[]] FROM arr;
  5. array
  6. ------------------------------------------------
  7. {{{1,2},{3,4}},{{5,6},{7,8}},{{9,10},{11,12}}}

You can construct an array from the results of a subquery. Write the array constructor with the keyword ARRAY followed by a subquery in parentheses. For example:

  1. SELECT ARRAY(SELECT oid FROM pg_proc WHERE proname LIKE 'bytea%');
  2. ?column?
  3. -----------------------------------------------------------
  4. {2011,1954,1948,1952,1951,1244,1950,2005,1949,1953,2006,31}

The subquery must return a single column. The resulting one-dimensional array has an element for each row in the subquery result, with an element type matching that of the subquery’s output column. The subscripts of an array value built with ARRAY always begin with 1.

Row Constructors

A row constructor is an expression that builds a row value (also called a composite value) from values for its member fields. For example,

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

Row constructors have the syntax rowvalue.*, which expands to a list of the elements of the row value, as when you use the syntax .* at the top level of a SELECT list. For example, if table t has columns f1 and f2, the following queries are the same:

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

By default, the value created by a ROW expression has an anonymous record type. If necessary, it can be cast to a named composite type — either the row type of a table, or a composite type created with CREATE TYPE AS. To avoid ambiguity, you can explicitly cast the value if necessary. For example:

  1. CREATE TABLE mytable(f1 int, f2 float, f3 text);
  2. CREATE FUNCTION getf1(mytable) RETURNS int AS 'SELECT $1.f1'
  3. LANGUAGE SQL;

In the following query, you do not need to cast the value because there is only one getf1() function and therefore no ambiguity:

  1. SELECT getf1(ROW(1,2.5,'this is a test'));
  2. getf1
  3. -------
  4. 1
  5. CREATE TYPE myrowtype AS (f1 int, f2 text, f3 numeric);
  6. CREATE FUNCTION getf1(myrowtype) RETURNS int AS 'SELECT
  7. $1.f1' LANGUAGE SQL;

Now we need a cast to indicate which function to call:

  1. SELECT getf1(ROW(1,2.5,'this is a test'));
  2. ERROR: function getf1(record) is not unique
  1. SELECT getf1(ROW(1,2.5,'this is a test')::mytable);
  2. getf1
  3. -------
  4. 1
  5. SELECT getf1(CAST(ROW(11,'this is a test',2.5) AS
  6. myrowtype));
  7. getf1
  8. -------
  9. 11

You can use row constructors to build composite values to be stored in a composite-type table column or to be passed to a function that accepts a composite parameter.

Expression Evaluation Rules

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

If you can determine the result of an expression by evaluating only some parts of the expression, then other subexpressions might not be evaluated at all. For example, in the following expression:

  1. SELECT true OR somefunc();

somefunc() would probably not be called at all. The same is true in the following expression:

  1. SELECT somefunc() OR true;

This is not the same as the left-to-right evaluation order that Boolean operators enforce in some programming languages.

Do not use functions with side effects as part of complex expressions, especially in WHERE and HAVING clauses, because those clauses are extensively reprocessed when developing an execution plan. Boolean expressions (AND/OR/NOT combinations) in those clauses can be reorganized in any manner that Boolean algebra laws allow.

Use a CASE construct to force evaluation order. The following example is an untrustworthy way to avoid division by zero in a WHERE clause:

  1. SELECT ... WHERE x <> 0 AND y/x > 1.5;

The following example shows a trustworthy evaluation order:

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

This CASE construct usage defeats optimization attempts; use it only when necessary.