1ALTER SEQUENCE(7) PostgreSQL 13.3 Documentation ALTER SEQUENCE(7)
2
3
4
6 ALTER_SEQUENCE - change the definition of a sequence generator
7
9 ALTER SEQUENCE [ IF EXISTS ] name
10 [ AS data_type ]
11 [ INCREMENT [ BY ] increment ]
12 [ MINVALUE minvalue | NO MINVALUE ] [ MAXVALUE maxvalue | NO MAXVALUE ]
13 [ START [ WITH ] start ]
14 [ RESTART [ [ WITH ] restart ] ]
15 [ CACHE cache ] [ [ NO ] CYCLE ]
16 [ OWNED BY { table_name.column_name | NONE } ]
17 ALTER SEQUENCE [ IF EXISTS ] name OWNER TO { new_owner | CURRENT_USER | SESSION_USER }
18 ALTER SEQUENCE [ IF EXISTS ] name RENAME TO new_name
19 ALTER SEQUENCE [ IF EXISTS ] name SET SCHEMA new_schema
20
22 ALTER SEQUENCE changes the parameters of an existing sequence
23 generator. Any parameters not specifically set in the ALTER SEQUENCE
24 command retain their prior settings.
25
26 You must own the sequence to use ALTER SEQUENCE. To change a sequence's
27 schema, you must also have CREATE privilege on the new schema. To alter
28 the owner, you must also be a direct or indirect member of the new
29 owning role, and that role must have CREATE privilege on the sequence's
30 schema. (These restrictions enforce that altering the owner doesn't do
31 anything you couldn't do by dropping and recreating the sequence.
32 However, a superuser can alter ownership of any sequence anyway.)
33
35 name
36 The name (optionally schema-qualified) of a sequence to be altered.
37
38 IF EXISTS
39 Do not throw an error if the sequence does not exist. A notice is
40 issued in this case.
41
42 data_type
43 The optional clause AS data_type changes the data type of the
44 sequence. Valid types are smallint, integer, and bigint.
45
46 Changing the data type automatically changes the minimum and
47 maximum values of the sequence if and only if the previous minimum
48 and maximum values were the minimum or maximum value of the old
49 data type (in other words, if the sequence had been created using
50 NO MINVALUE or NO MAXVALUE, implicitly or explicitly). Otherwise,
51 the minimum and maximum values are preserved, unless new values are
52 given as part of the same command. If the minimum and maximum
53 values do not fit into the new data type, an error will be
54 generated.
55
56 increment
57 The clause INCREMENT BY increment is optional. A positive value
58 will make an ascending sequence, a negative one a descending
59 sequence. If unspecified, the old increment value will be
60 maintained.
61
62 minvalue
63 NO MINVALUE
64 The optional clause MINVALUE minvalue determines the minimum value
65 a sequence can generate. If NO MINVALUE is specified, the defaults
66 of 1 and the minimum value of the data type for ascending and
67 descending sequences, respectively, will be used. If neither option
68 is specified, the current minimum value will be maintained.
69
70 maxvalue
71 NO MAXVALUE
72 The optional clause MAXVALUE maxvalue determines the maximum value
73 for the sequence. If NO MAXVALUE is specified, the defaults of the
74 maximum value of the data type and -1 for ascending and descending
75 sequences, respectively, will be used. If neither option is
76 specified, the current maximum value will be maintained.
77
78 start
79 The optional clause START WITH start changes the recorded start
80 value of the sequence. This has no effect on the current sequence
81 value; it simply sets the value that future ALTER SEQUENCE RESTART
82 commands will use.
83
84 restart
85 The optional clause RESTART [ WITH restart ] changes the current
86 value of the sequence. This is similar to calling the setval
87 function with is_called = false: the specified value will be
88 returned by the next call of nextval. Writing RESTART with no
89 restart value is equivalent to supplying the start value that was
90 recorded by CREATE SEQUENCE or last set by ALTER SEQUENCE START
91 WITH.
92
93 In contrast to a setval call, a RESTART operation on a sequence is
94 transactional and blocks concurrent transactions from obtaining
95 numbers from the same sequence. If that's not the desired mode of
96 operation, setval should be used.
97
98 cache
99 The clause CACHE cache enables sequence numbers to be preallocated
100 and stored in memory for faster access. The minimum value is 1
101 (only one value can be generated at a time, i.e., no cache). If
102 unspecified, the old cache value will be maintained.
103
104 CYCLE
105 The optional CYCLE key word can be used to enable the sequence to
106 wrap around when the maxvalue or minvalue has been reached by an
107 ascending or descending sequence respectively. If the limit is
108 reached, the next number generated will be the minvalue or
109 maxvalue, respectively.
110
111 NO CYCLE
112 If the optional NO CYCLE key word is specified, any calls to
113 nextval after the sequence has reached its maximum value will
114 return an error. If neither CYCLE or NO CYCLE are specified, the
115 old cycle behavior will be maintained.
116
117 OWNED BY table_name.column_name
118 OWNED BY NONE
119 The OWNED BY option causes the sequence to be associated with a
120 specific table column, such that if that column (or its whole
121 table) is dropped, the sequence will be automatically dropped as
122 well. If specified, this association replaces any previously
123 specified association for the sequence. The specified table must
124 have the same owner and be in the same schema as the sequence.
125 Specifying OWNED BY NONE removes any existing association, making
126 the sequence “free-standing”.
127
128 new_owner
129 The user name of the new owner of the sequence.
130
131 new_name
132 The new name for the sequence.
133
134 new_schema
135 The new schema for the sequence.
136
138 ALTER SEQUENCE will not immediately affect nextval results in backends,
139 other than the current one, that have preallocated (cached) sequence
140 values. They will use up all cached values prior to noticing the
141 changed sequence generation parameters. The current backend will be
142 affected immediately.
143
144 ALTER SEQUENCE does not affect the currval status for the sequence.
145 (Before PostgreSQL 8.3, it sometimes did.)
146
147 ALTER SEQUENCE blocks concurrent nextval, currval, lastval, and setval
148 calls.
149
150 For historical reasons, ALTER TABLE can be used with sequences too; but
151 the only variants of ALTER TABLE that are allowed with sequences are
152 equivalent to the forms shown above.
153
155 Restart a sequence called serial, at 105:
156
157 ALTER SEQUENCE serial RESTART WITH 105;
158
160 ALTER SEQUENCE conforms to the SQL standard, except for the AS, START
161 WITH, OWNED BY, OWNER TO, RENAME TO, and SET SCHEMA clauses, which are
162 PostgreSQL extensions.
163
165 CREATE SEQUENCE (CREATE_SEQUENCE(7)), DROP SEQUENCE (DROP_SEQUENCE(7))
166
167
168
169PostgreSQL 13.3 2021 ALTER SEQUENCE(7)