2 8.16. Composite Types #
4 8.16.1. Declaration of Composite Types
5 8.16.2. Constructing Composite Values
6 8.16.3. Accessing Composite Types
7 8.16.4. Modifying Composite Types
8 8.16.5. Using Composite Types in Queries
9 8.16.6. Composite Type Input and Output Syntax
11 A composite type represents the structure of a row or record; it is
12 essentially just a list of field names and their data types. PostgreSQL
13 allows composite types to be used in many of the same ways that simple
14 types can be used. For example, a column of a table can be declared to
15 be of a composite type.
17 8.16.1. Declaration of Composite Types #
19 Here are two simple examples of defining composite types:
20 CREATE TYPE complex AS (
25 CREATE TYPE inventory_item AS (
31 The syntax is comparable to CREATE TABLE, except that only field names
32 and types can be specified; no constraints (such as NOT NULL) can
33 presently be included. Note that the AS keyword is essential; without
34 it, the system will think a different kind of CREATE TYPE command is
35 meant, and you will get odd syntax errors.
37 Having defined the types, we can use them to create tables:
38 CREATE TABLE on_hand (
43 INSERT INTO on_hand VALUES (ROW('fuzzy dice', 42, 1.99), 1000);
46 CREATE FUNCTION price_extension(inventory_item, integer) RETURNS numeric
47 AS 'SELECT $1.price * $2' LANGUAGE SQL;
49 SELECT price_extension(item, 10) FROM on_hand;
51 Whenever you create a table, a composite type is also automatically
52 created, with the same name as the table, to represent the table's row
53 type. For example, had we said:
54 CREATE TABLE inventory_item (
56 supplier_id integer REFERENCES suppliers,
57 price numeric CHECK (price > 0)
60 then the same inventory_item composite type shown above would come into
61 being as a byproduct, and could be used just as above. Note however an
62 important restriction of the current implementation: since no
63 constraints are associated with a composite type, the constraints shown
64 in the table definition do not apply to values of the composite type
65 outside the table. (To work around this, create a domain over the
66 composite type, and apply the desired constraints as CHECK constraints
69 8.16.2. Constructing Composite Values #
71 To write a composite value as a literal constant, enclose the field
72 values within parentheses and separate them by commas. You can put
73 double quotes around any field value, and must do so if it contains
74 commas or parentheses. (More details appear below.) Thus, the general
75 format of a composite constant is the following:
76 '( val1 , val2 , ... )'
79 '("fuzzy dice",42,1.99)'
81 which would be a valid value of the inventory_item type defined above.
82 To make a field be NULL, write no characters at all in its position in
83 the list. For example, this constant specifies a NULL third field:
86 If you want an empty string rather than NULL, write double quotes:
89 Here the first field is a non-NULL empty string, the third is NULL.
91 (These constants are actually only a special case of the generic type
92 constants discussed in Section 4.1.2.7. The constant is initially
93 treated as a string and passed to the composite-type input conversion
94 routine. An explicit type specification might be necessary to tell
95 which type to convert the constant to.)
97 The ROW expression syntax can also be used to construct composite
98 values. In most cases this is considerably simpler to use than the
99 string-literal syntax since you don't have to worry about multiple
100 layers of quoting. We already used this method above:
101 ROW('fuzzy dice', 42, 1.99)
104 The ROW keyword is actually optional as long as you have more than one
105 field in the expression, so these can be simplified to:
106 ('fuzzy dice', 42, 1.99)
109 The ROW expression syntax is discussed in more detail in
112 8.16.3. Accessing Composite Types #
114 To access a field of a composite column, one writes a dot and the field
115 name, much like selecting a field from a table name. In fact, it's so
116 much like selecting from a table name that you often have to use
117 parentheses to keep from confusing the parser. For example, you might
118 try to select some subfields from our on_hand example table with
120 SELECT item.name FROM on_hand WHERE item.price > 9.99;
122 This will not work since the name item is taken to be a table name, not
123 a column name of on_hand, per SQL syntax rules. You must write it like
125 SELECT (item).name FROM on_hand WHERE (item).price > 9.99;
127 or if you need to use the table name as well (for instance in a
128 multitable query), like this:
129 SELECT (on_hand.item).name FROM on_hand WHERE (on_hand.item).price > 9.99;
131 Now the parenthesized object is correctly interpreted as a reference to
132 the item column, and then the subfield can be selected from it.
134 Similar syntactic issues apply whenever you select a field from a
135 composite value. For instance, to select just one field from the result
136 of a function that returns a composite value, you'd need to write
138 SELECT (my_func(...)).field FROM ...
140 Without the extra parentheses, this will generate a syntax error.
142 The special field name * means “all fields”, as further explained in
145 8.16.4. Modifying Composite Types #
147 Here are some examples of the proper syntax for inserting and updating
148 composite columns. First, inserting or updating a whole column:
149 INSERT INTO mytab (complex_col) VALUES((1.1,2.2));
151 UPDATE mytab SET complex_col = ROW(1.1,2.2) WHERE ...;
153 The first example omits ROW, the second uses it; we could have done it
156 We can update an individual subfield of a composite column:
157 UPDATE mytab SET complex_col.r = (complex_col).r + 1 WHERE ...;
159 Notice here that we don't need to (and indeed cannot) put parentheses
160 around the column name appearing just after SET, but we do need
161 parentheses when referencing the same column in the expression to the
162 right of the equal sign.
164 And we can specify subfields as targets for INSERT, too:
165 INSERT INTO mytab (complex_col.r, complex_col.i) VALUES(1.1, 2.2);
167 Had we not supplied values for all the subfields of the column, the
168 remaining subfields would have been filled with null values.
170 8.16.5. Using Composite Types in Queries #
172 There are various special syntax rules and behaviors associated with
173 composite types in queries. These rules provide useful shortcuts, but
174 can be confusing if you don't know the logic behind them.
176 In PostgreSQL, a reference to a table name (or alias) in a query is
177 effectively a reference to the composite value of the table's current
178 row. For example, if we had a table inventory_item as shown above, we
180 SELECT c FROM inventory_item c;
182 This query produces a single composite-valued column, so we might get
185 ------------------------
186 ("fuzzy dice",42,1.99)
189 Note however that simple names are matched to column names before table
190 names, so this example works only because there is no column named c in
193 The ordinary qualified-column-name syntax table_name.column_name can be
194 understood as applying field selection to the composite value of the
195 table's current row. (For efficiency reasons, it's not actually
196 implemented that way.)
199 SELECT c.* FROM inventory_item c;
201 then, according to the SQL standard, we should get the contents of the
202 table expanded into separate columns:
203 name | supplier_id | price
204 ------------+-------------+-------
205 fuzzy dice | 42 | 1.99
209 SELECT c.name, c.supplier_id, c.price FROM inventory_item c;
211 PostgreSQL will apply this expansion behavior to any composite-valued
212 expression, although as shown above, you need to write parentheses
213 around the value that .* is applied to whenever it's not a simple table
214 name. For example, if myfunc() is a function returning a composite type
215 with columns a, b, and c, then these two queries have the same result:
216 SELECT (myfunc(x)).* FROM some_table;
217 SELECT (myfunc(x)).a, (myfunc(x)).b, (myfunc(x)).c FROM some_table;
221 PostgreSQL handles column expansion by actually transforming the first
222 form into the second. So, in this example, myfunc() would get invoked
223 three times per row with either syntax. If it's an expensive function
224 you may wish to avoid that, which you can do with a query like:
225 SELECT m.* FROM some_table, LATERAL myfunc(x) AS m;
227 Placing the function in a LATERAL FROM item keeps it from being invoked
228 more than once per row. m.* is still expanded into m.a, m.b, m.c, but
229 now those variables are just references to the output of the FROM item.
230 (The LATERAL keyword is optional here, but we show it to clarify that
231 the function is getting x from some_table.)
233 The composite_value.* syntax results in column expansion of this kind
234 when it appears at the top level of a SELECT output list, a RETURNING
235 list in INSERT/UPDATE/DELETE/MERGE, a VALUES clause, or a row
236 constructor. In all other contexts (including when nested inside one of
237 those constructs), attaching .* to a composite value does not change
238 the value, since it means “all columns” and so the same composite value
239 is produced again. For example, if somefunc() accepts a
240 composite-valued argument, these queries are the same:
241 SELECT somefunc(c.*) FROM inventory_item c;
242 SELECT somefunc(c) FROM inventory_item c;
244 In both cases, the current row of inventory_item is passed to the
245 function as a single composite-valued argument. Even though .* does
246 nothing in such cases, using it is good style, since it makes clear
247 that a composite value is intended. In particular, the parser will
248 consider c in c.* to refer to a table name or alias, not to a column
249 name, so that there is no ambiguity; whereas without .*, it is not
250 clear whether c means a table name or a column name, and in fact the
251 column-name interpretation will be preferred if there is a column named
254 Another example demonstrating these concepts is that all these queries
256 SELECT * FROM inventory_item c ORDER BY c;
257 SELECT * FROM inventory_item c ORDER BY c.*;
258 SELECT * FROM inventory_item c ORDER BY ROW(c.*);
260 All of these ORDER BY clauses specify the row's composite value,
261 resulting in sorting the rows according to the rules described in
262 Section 9.25.6. However, if inventory_item contained a column named c,
263 the first case would be different from the others, as it would mean to
264 sort by that column only. Given the column names previously shown,
265 these queries are also equivalent to those above:
266 SELECT * FROM inventory_item c ORDER BY ROW(c.name, c.supplier_id, c.price);
267 SELECT * FROM inventory_item c ORDER BY (c.name, c.supplier_id, c.price);
269 (The last case uses a row constructor with the key word ROW omitted.)
271 Another special syntactical behavior associated with composite values
272 is that we can use functional notation for extracting a field of a
273 composite value. The simple way to explain this is that the notations
274 field(table) and table.field are interchangeable. For example, these
275 queries are equivalent:
276 SELECT c.name FROM inventory_item c WHERE c.price > 1000;
277 SELECT name(c) FROM inventory_item c WHERE price(c) > 1000;
279 Moreover, if we have a function that accepts a single argument of a
280 composite type, we can call it with either notation. These queries are
282 SELECT somefunc(c) FROM inventory_item c;
283 SELECT somefunc(c.*) FROM inventory_item c;
284 SELECT c.somefunc FROM inventory_item c;
286 This equivalence between functional notation and field notation makes
287 it possible to use functions on composite types to implement “computed
288 fields”. An application using the last query above wouldn't need to be
289 directly aware that somefunc isn't a real column of the table.
293 Because of this behavior, it's unwise to give a function that takes a
294 single composite-type argument the same name as any of the fields of
295 that composite type. If there is ambiguity, the field-name
296 interpretation will be chosen if field-name syntax is used, while the
297 function will be chosen if function-call syntax is used. However,
298 PostgreSQL versions before 11 always chose the field-name
299 interpretation, unless the syntax of the call required it to be a
300 function call. One way to force the function interpretation in older
301 versions is to schema-qualify the function name, that is, write
302 schema.func(compositevalue).
304 8.16.6. Composite Type Input and Output Syntax #
306 The external text representation of a composite value consists of items
307 that are interpreted according to the I/O conversion rules for the
308 individual field types, plus decoration that indicates the composite
309 structure. The decoration consists of parentheses (( and )) around the
310 whole value, plus commas (,) between adjacent items. Whitespace outside
311 the parentheses is ignored, but within the parentheses it is considered
312 part of the field value, and might or might not be significant
313 depending on the input conversion rules for the field data type. For
317 the whitespace will be ignored if the field type is integer, but not if
320 As shown previously, when writing a composite value you can write
321 double quotes around any individual field value. You must do so if the
322 field value would otherwise confuse the composite-value parser. In
323 particular, fields containing parentheses, commas, double quotes, or
324 backslashes must be double-quoted. To put a double quote or backslash
325 in a quoted composite field value, precede it with a backslash. (Also,
326 a pair of double quotes within a double-quoted field value is taken to
327 represent a double quote character, analogously to the rules for single
328 quotes in SQL literal strings.) Alternatively, you can avoid quoting
329 and use backslash-escaping to protect all data characters that would
330 otherwise be taken as composite syntax.
332 A completely empty field value (no characters at all between the commas
333 or parentheses) represents a NULL. To write a value that is an empty
334 string rather than NULL, write "".
336 The composite output routine will put double quotes around field values
337 if they are empty strings or contain parentheses, commas, double
338 quotes, backslashes, or white space. (Doing so for white space is not
339 essential, but aids legibility.) Double quotes and backslashes embedded
340 in field values will be doubled.
344 Remember that what you write in an SQL command will first be
345 interpreted as a string literal, and then as a composite. This doubles
346 the number of backslashes you need (assuming escape string syntax is
347 used). For example, to insert a text field containing a double quote
348 and a backslash in a composite value, you'd need to write:
349 INSERT ... VALUES ('("\"\\")');
351 The string-literal processor removes one level of backslashes, so that
352 what arrives at the composite-value parser looks like ("\"\\"). In
353 turn, the string fed to the text data type's input routine becomes "\.
354 (If we were working with a data type whose input routine also treated
355 backslashes specially, bytea for example, we might need as many as
356 eight backslashes in the command to get one backslash into the stored
357 composite field.) Dollar quoting (see Section 4.1.2.4) can be used to
358 avoid the need to double backslashes.
362 The ROW constructor syntax is usually easier to work with than the
363 composite-literal syntax when writing composite values in SQL commands.
364 In ROW, individual field values are written the same way they would be
365 written when not members of a composite.