4 ALTER TYPE — change the definition of a type
8 ALTER TYPE name OWNER TO { new_owner | CURRENT_ROLE | CURRENT_USER | SESSION_USE
10 ALTER TYPE name RENAME TO new_name
11 ALTER TYPE name SET SCHEMA new_schema
12 ALTER TYPE name RENAME ATTRIBUTE attribute_name TO new_attribute_name [ CASCADE
14 ALTER TYPE name action [, ... ]
15 ALTER TYPE name ADD VALUE [ IF NOT EXISTS ] new_enum_value [ { BEFORE | AFTER }
17 ALTER TYPE name RENAME VALUE existing_enum_value TO new_enum_value
18 ALTER TYPE name SET ( property = value [, ... ] )
20 where action is one of:
22 ADD ATTRIBUTE attribute_name data_type [ COLLATE collation ] [ CASCADE | RES
24 DROP ATTRIBUTE [ IF EXISTS ] attribute_name [ CASCADE | RESTRICT ]
25 ALTER ATTRIBUTE attribute_name [ SET DATA ] TYPE data_type [ COLLATE collati
26 on ] [ CASCADE | RESTRICT ]
30 ALTER TYPE changes the definition of an existing type. There are
34 This form changes the owner of the type.
37 This form changes the name of the type.
40 This form moves the type into another schema.
43 This form is only usable with composite types. It changes the
44 name of an individual attribute of the type.
47 This form adds a new attribute to a composite type, using the
48 same syntax as CREATE TYPE.
50 DROP ATTRIBUTE [ IF EXISTS ]
51 This form drops an attribute from a composite type. If IF EXISTS
52 is specified and the attribute does not exist, no error is
53 thrown. In this case a notice is issued instead.
55 ALTER ATTRIBUTE ... SET DATA TYPE
56 This form changes the type of an attribute of a composite type.
58 ADD VALUE [ IF NOT EXISTS ] [ BEFORE | AFTER ]
59 This form adds a new value to an enum type. The new value's
60 place in the enum's ordering can be specified as being BEFORE or
61 AFTER one of the existing values. Otherwise, the new item is
62 added at the end of the list of values.
64 If IF NOT EXISTS is specified, it is not an error if the type
65 already contains the new value: a notice is issued but no other
66 action is taken. Otherwise, an error will occur if the new value
70 This form renames a value of an enum type. The value's place in
71 the enum's ordering is not affected. An error will occur if the
72 specified value is not present or the new name is already
75 SET ( property = value [, ... ] )
76 This form is only applicable to base types. It allows adjustment
77 of a subset of the base-type properties that can be set in
78 CREATE TYPE. Specifically, these properties can be changed:
80 + RECEIVE can be set to the name of a binary input function, or
81 NONE to remove the type's binary input function. Using this
82 option requires superuser privilege.
83 + SEND can be set to the name of a binary output function, or
84 NONE to remove the type's binary output function. Using this
85 option requires superuser privilege.
86 + TYPMOD_IN can be set to the name of a type modifier input
87 function, or NONE to remove the type's type modifier input
88 function. Using this option requires superuser privilege.
89 + TYPMOD_OUT can be set to the name of a type modifier output
90 function, or NONE to remove the type's type modifier output
91 function. Using this option requires superuser privilege.
92 + ANALYZE can be set to the name of a type-specific statistics
93 collection function, or NONE to remove the type's statistics
94 collection function. Using this option requires superuser
96 + SUBSCRIPT can be set to the name of a type-specific
97 subscripting handler function, or NONE to remove the type's
98 subscripting handler function. Using this option requires
100 + STORAGE can be set to plain, extended, external, or main (see
101 Section 66.2 for more information about what these mean).
102 However, changing from plain to another setting requires
103 superuser privilege (because it requires that the type's C
104 functions all be TOAST-ready), and changing to plain from
105 another setting is not allowed at all (since the type may
106 already have TOASTed values present in the database). Note
107 that changing this option doesn't by itself change any stored
108 data, it just sets the default TOAST strategy to be used for
109 table columns created in the future. See ALTER TABLE to change
110 the TOAST strategy for existing table columns.
112 See CREATE TYPE for more details about these type properties.
113 Note that where appropriate, a change in these properties for a
114 base type will be propagated automatically to domains based on
117 The ADD ATTRIBUTE, DROP ATTRIBUTE, and ALTER ATTRIBUTE actions can be
118 combined into a list of multiple alterations to apply in parallel. For
119 example, it is possible to add several attributes and/or alter the type
120 of several attributes in a single command.
122 You must own the type to use ALTER TYPE. To change the schema of a
123 type, you must also have CREATE privilege on the new schema. To alter
124 the owner, you must be able to SET ROLE to the new owning role, and
125 that role must have CREATE privilege on the type's schema. (These
126 restrictions enforce that altering the owner doesn't do anything you
127 couldn't do by dropping and recreating the type. However, a superuser
128 can alter ownership of any type anyway.) To add an attribute or alter
129 an attribute type, you must also have USAGE privilege on the
130 attribute's data type.
135 The name (possibly schema-qualified) of an existing type to
139 The new name for the type.
142 The user name of the new owner of the type.
145 The new schema for the type.
148 The name of the attribute to add, alter, or drop.
151 The new name of the attribute to be renamed.
154 The data type of the attribute to add, or the new type of the
158 The new value to be added to an enum type's list of values, or
159 the new name to be given to an existing value. Like all enum
160 literals, it needs to be quoted.
163 The existing enum value that the new value should be added
164 immediately before or after in the enum type's sort ordering.
165 Like all enum literals, it needs to be quoted.
168 The existing enum value that should be renamed. Like all enum
169 literals, it needs to be quoted.
172 The name of a base-type property to be modified; see above for
176 Automatically propagate the operation to typed tables of the
177 type being altered, and their descendants.
180 Refuse the operation if the type being altered is the type of a
181 typed table. This is the default.
185 If ALTER TYPE ... ADD VALUE (the form that adds a new value to an enum
186 type) is executed inside a transaction block, the new value cannot be
187 used until after the transaction has been committed.
189 Comparisons involving an added enum value will sometimes be slower than
190 comparisons involving only original members of the enum type. This will
191 usually only occur if BEFORE or AFTER is used to set the new value's
192 sort position somewhere other than at the end of the list. However,
193 sometimes it will happen even though the new value is added at the end
194 (this occurs if the OID counter “wrapped around” since the original
195 creation of the enum type). The slowdown is usually insignificant; but
196 if it matters, optimal performance can be regained by dropping and
197 recreating the enum type, or by dumping and restoring the database.
201 To rename a data type:
202 ALTER TYPE electronic_mail RENAME TO email;
204 To change the owner of the type email to joe:
205 ALTER TYPE email OWNER TO joe;
207 To change the schema of the type email to customers:
208 ALTER TYPE email SET SCHEMA customers;
210 To add a new attribute to a composite type:
211 ALTER TYPE compfoo ADD ATTRIBUTE f3 int;
213 To add a new value to an enum type in a particular sort position:
214 ALTER TYPE colors ADD VALUE 'orange' AFTER 'red';
216 To rename an enum value:
217 ALTER TYPE colors RENAME VALUE 'purple' TO 'mauve';
219 To create binary I/O functions for an existing base type:
220 CREATE FUNCTION mytypesend(mytype) RETURNS bytea ...;
221 CREATE FUNCTION mytyperecv(internal, oid, integer) RETURNS mytype ...;
222 ALTER TYPE mytype SET (
229 The variants to add and drop attributes are part of the SQL standard;
230 the other variants are PostgreSQL extensions.
234 CREATE TYPE, DROP TYPE