diff --git a/doc/src/sgml/ref/select.sgml b/doc/src/sgml/ref/select.sgml index 80bb8adcac..1a6b8f7d15 100644 --- a/doc/src/sgml/ref/select.sgml +++ b/doc/src/sgml/ref/select.sgml @@ -35,7 +35,7 @@ PostgreSQL documentation [ WITH [ RECURSIVE ] with_query [, ...] ] SELECT [ ALL | DISTINCT [ ON ( expression [, ...] ) ] ] [ * | expression [ [ AS ] output_name ] [, ...] ] - [ FROM from_item [, ...] ] + [ FROM { from_item | join_expression } [, ...] ] [ WHERE condition ] [ GROUP BY [ ALL | DISTINCT ] grouping_element [, ...] ] [ HAVING condition ] @@ -47,6 +47,14 @@ SELECT [ ALL | DISTINCT [ ON ( expressioncount ] { ROW | ROWS } { ONLY | WITH TIES } ] [ FOR { UPDATE | NO KEY UPDATE | SHARE | KEY SHARE } [ OF table_name [, ...] ] [ NOWAIT | SKIP LOCKED ] [...] ] +where join_expression recursively joins two from_items: + + { from_item | join_expression } + { [ NATURAL ] join_type from_item + | join_type from_item ON join_condition + | join_type from_item USING ( join_column [, ...] ) + } + where from_item can be one of: [ ONLY ] table_name [ * ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] @@ -59,7 +67,6 @@ SELECT [ ALL | DISTINCT [ ON ( expressionfunction_name ( [ argument [, ...] ] ) AS ( column_definition [, ...] ) [ LATERAL ] ROWS FROM( function_name ( [ argument [, ...] ] ) [ AS ( column_definition [, ...] ) ] [, ...] ) [ WITH ORDINALITY ] [ [ AS ] alias [ ( column_alias [, ...] ) ] ] - from_item [ NATURAL ] join_type from_item [ ON join_condition | USING ( join_column [, ...] ) [ AS join_using_alias ] ] and grouping_element can be one of: @@ -108,6 +115,9 @@ TABLE [ ONLY ] table_name [ * ] virtual table.) If more than one element is specified in the FROM list, they are cross-joined together. (See below.) + A join_expression + element provides a single virtual table, called a Join Tree, + resulting from the joining of all its sub-elements @@ -390,8 +400,18 @@ TABLE [ ONLY ] table_name [ * ] - The FROM clause can contain the following - elements: + The join_expression + syntax provides an explicit way to join together exactly two sources. + However, this has a recursive nature, the first source can itself be + a join_expression. + The first pair is combined using the join conditions and the resulting + virtual relation is then joined to the third source, and so on. + he columns of this virtual relation retain their source table identity. + + + + The FROM clause can contain the following from_item + source table elements: @@ -582,6 +602,64 @@ TABLE [ ONLY ] table_name [ * ] + + LATERAL + + + The LATERAL key word can precede a + sub-SELECT FROM item. This allows the + sub-SELECT to refer to columns of FROM + items that appear before it in the FROM list. (Without + LATERAL, each sub-SELECT is + evaluated independently and so cannot cross-reference any other + FROM item.) + + + LATERAL can also precede a function-call + FROM item, but in this case it is a noise word, because + the function expression can refer to earlier FROM items + in any case. + + + + A LATERAL item can appear at top level in the + FROM list, or within a JOIN tree. In the + latter case it can also refer to any items that are on the left-hand + side of a JOIN that it is on the right-hand side of. + + + + When a FROM item contains LATERAL + cross-references, evaluation proceeds as follows: for each row of the + FROM item providing the cross-referenced column(s), or + set of rows of multiple FROM items providing the + columns, the LATERAL item is evaluated using that + row or row set's values of the columns. The resulting row(s) are + joined as usual with the rows they were computed from. This is + repeated for each row or set of rows from the column source table(s). + + + + The column source table(s) must be INNER or + LEFT joined to the LATERAL item, else + there would not be a well-defined set of rows from which to compute + each set of rows for the LATERAL item. Thus, + although a construct such as X RIGHT JOIN + LATERAL Y is syntactically valid, it is + not actually allowed for Y to reference + X. + + + + + + + + The following elements can only appear within a + join_expression + and provide the instructions on how to combine the two sources together: + + join_type @@ -713,56 +791,6 @@ TABLE [ ONLY ] table_name [ * ] - - - LATERAL - - - The LATERAL key word can precede a - sub-SELECT FROM item. This allows the - sub-SELECT to refer to columns of FROM - items that appear before it in the FROM list. (Without - LATERAL, each sub-SELECT is - evaluated independently and so cannot cross-reference any other - FROM item.) - - - LATERAL can also precede a function-call - FROM item, but in this case it is a noise word, because - the function expression can refer to earlier FROM items - in any case. - - - - A LATERAL item can appear at top level in the - FROM list, or within a JOIN tree. In the - latter case it can also refer to any items that are on the left-hand - side of a JOIN that it is on the right-hand side of. - - - - When a FROM item contains LATERAL - cross-references, evaluation proceeds as follows: for each row of the - FROM item providing the cross-referenced column(s), or - set of rows of multiple FROM items providing the - columns, the LATERAL item is evaluated using that - row or row set's values of the columns. The resulting row(s) are - joined as usual with the rows they were computed from. This is - repeated for each row or set of rows from the column source table(s). - - - - The column source table(s) must be INNER or - LEFT joined to the LATERAL item, else - there would not be a well-defined set of rows from which to compute - each set of rows for the LATERAL item. Thus, - although a construct such as X RIGHT JOIN - LATERAL Y is syntactically valid, it is - not actually allowed for Y to reference - X. - - -