RefLib

Reference library for AI

Back

postgresql_16:3.5.16.5 Using Composite Types in Queries

PostgreSQL 16 > The SQL Language > Data Types > Composite Types > Using Composite Types in Queries

There are various special syntax rules and behaviors associated with composite types in queries. These rules provide useful shortcuts, but can be confusing if you don't know the logic behind them.

In PostgreSQL, a reference to a table name (or alias) in a query is effectively a reference to the composite value of the table's current row. For example, if we had a table inventory_item as shown above (see postgresql_16:3.5.16.1), we could write: SELECT c FROM inventory_item c; This query produces a single composite-valued column, so we might get output like: c ------------------------ ("fuzzy dice",42,1.99) (1 row) Note however that simple names are matched to column names before table names, so this example works only because there is no column named c in the query's tables.

The ordinary qualified-column-name syntax *table_name*.*column_name* can be understood as applying field selection (see postgresql_16:3.1.2.4) to the composite value of the table's current row. (For efficiency reasons, it's not actually implemented that way.)

When we write SELECT c.* FROM inventory_item c; then, according to the SQL standard, we should get the contents of the table expanded into separate columns: name | supplier_id | price ------------+-------------+------- fuzzy dice | 42 | 1.99 (1 row) as if the query were SELECT c.name, c.supplier_id, c.price FROM inventory_item c; PostgreSQL will apply this expansion behavior to any composite-valued expression, although as shown above (see postgresql_16:3.5.16.3), you need to write parentheses around the value that .* is applied to whenever it's not a simple table name. For example, if myfunc() is a function returning a composite type with columns a, b, and c, then these two queries have the same result: SELECT (myfunc(x)).* FROM some_table; SELECT (myfunc(x)).a, (myfunc(x)).b, (myfunc(x)).c FROM some_table;

Tip

PostgreSQL handles column expansion by actually transforming the first form into the second. So, in this example, myfunc() would get invoked three times per row with either syntax. If it's an expensive function you may wish to avoid that, which you can do with a query like: SELECT m.* FROM some_table, LATERAL myfunc(x) AS m; Placing the function in a LATERAL FROM item keeps it from being invoked more than once per row. m.* is still expanded into m.a, m.b, m.c, but now those variables are just references to the output of the FROM item. (The LATERAL keyword is optional here, but we show it to clarify that the function is getting x from some_table.)

The *composite_value*.* syntax results in column expansion of this kind when it appears at the top level of a SELECT output list (see postgresql_16:3.4.3), a RETURNING list (see postgresql_16:3.3.4) in INSERT/UPDATE/DELETE, a VALUES clause (see postgresql_16:3.4.7), or a row constructor (see postgresql_16:3.1.2.13). In all other contexts (including when nested inside one of those constructs), attaching .* to a composite value does not change the value, since it means “all columns” and so the same composite value is produced again. For example, if somefunc() accepts a composite-valued argument, these queries are the same: SELECT somefunc(c.*) FROM inventory_item c; SELECT somefunc(c) FROM inventory_item c; In both cases, the current row of inventory_item is passed to the function as a single composite-valued argument. Even though .* does nothing in such cases, using it is good style, since it makes clear that a composite value is intended. In particular, the parser will consider c in c.* to refer to a table name or alias, not to a column name, so that there is no ambiguity; whereas without .*, it is not clear whether c means a table name or a column name, and in fact the column-name interpretation will be preferred if there is a column named c.

Another example demonstrating these concepts is that all these queries mean the same thing: SELECT * FROM inventory_item c ORDER BY c; SELECT * FROM inventory_item c ORDER BY c.*; SELECT * FROM inventory_item c ORDER BY ROW(c.*); All of these ORDER BY clauses specify the row's composite value, resulting in sorting the rows according to the rules described in (see postgresql_16:3.6.24.6). However, if inventory_item contained a column named c, the first case would be different from the others, as it would mean to sort by that column only. Given the column names previously shown, these queries are also equivalent to those above: SELECT * FROM inventory_item c ORDER BY ROW(c.name, c.supplier_id, c.price); SELECT * FROM inventory_item c ORDER BY (c.name, c.supplier_id, c.price); (The last case uses a row constructor with the key word ROW omitted.)

Another special syntactical behavior associated with composite values is that we can use *functional notation* for extracting a field of a composite value. The simple way to explain this is that the notations *field*(*table*) and *table*.*field* are interchangeable. For example, these queries are equivalent: SELECT c.name FROM inventory_item c WHERE c.price > 1000; SELECT name(c) FROM inventory_item c WHERE price(c) > 1000; Moreover, if we have a function that accepts a single argument of a composite type, we can call it with either notation. These queries are all equivalent: SELECT somefunc(c) FROM inventory_item c; SELECT somefunc(c.*) FROM inventory_item c; SELECT c.somefunc FROM inventory_item c;

This equivalence between functional notation and field notation makes it possible to use functions on composite types to implement “computed fields”. An application using the last query above wouldn't need to be directly aware that somefunc isn't a real column of the table.

Tip

Because of this behavior, it's unwise to give a function that takes a single composite-type argument the same name as any of the fields of that composite type. If there is ambiguity, the field-name interpretation will be chosen if field-name syntax is used, while the function will be chosen if function-call syntax is used. However, PostgreSQL versions before 11 always chose the field-name interpretation, unless the syntax of the call required it to be a function call. One way to force the function interpretation in older versions is to schema-qualify the function name, that is, write *schema*.*func*(*compositevalue*).

Back