Composite type field access
PG 7.4+→ field typeAccesses a named field from a composite type (structured type) value. Parentheses are required when the composite is a column reference.
Signature
(composite_value).field_name → field_typeParameters
| Parameter | Type | Description |
|---|---|---|
| composite_value | composite type | A composite type value or column |
| field_name | identifier | Name of the field to access |
Examples
CREATE TYPE address AS (street text, city text, zip text);
SELECT (billing_address).city FROM customers;City from the billing_address composite columnSELECT (ROW(1, 'Alice')).f2;Alice (anonymous composite)CREATE TYPE inventory_item AS (name text, unit_price numeric, quantity int);
SELECT (item).name, (item).unit_price * (item).quantity AS line_total
FROM order_lines;Item name and computed line total from a composite-type columnUPDATE employees
SET contact = ROW((contact).phone, 'new@example.com', (contact).address)::contact_info
WHERE id = 42;Updates the email sub-field of a composite contact column while preserving other fieldsTo access a field from a composite-type column, you need parentheses: `(address_col).city`. Without them, PostgreSQL interprets it as table.column notation and may error or return unexpected results.
SELECT (shipping_address).street, (shipping_address).city, (shipping_address).zip FROM orders;Individual address fields from a composite column