]> begriffs open source - ai-pg/blob - full-docs/src/sgml/man7/ALTER_SEQUENCE.7
WIP: toc builder
[ai-pg] / full-docs / src / sgml / man7 / ALTER_SEQUENCE.7
1 '\" t
2 .\"     Title: ALTER SEQUENCE
3 .\"    Author: The PostgreSQL Global Development Group
4 .\" Generator: DocBook XSL Stylesheets vsnapshot <http://docbook.sf.net/>
5 .\"      Date: 2025
6 .\"    Manual: PostgreSQL 18.0 Documentation
7 .\"    Source: PostgreSQL 18.0
8 .\"  Language: English
9 .\"
10 .TH "ALTER SEQUENCE" "7" "2025" "PostgreSQL 18.0" "PostgreSQL 18.0 Documentation"
11 .\" -----------------------------------------------------------------
12 .\" * Define some portability stuff
13 .\" -----------------------------------------------------------------
14 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
15 .\" http://bugs.debian.org/507673
16 .\" http://lists.gnu.org/archive/html/groff/2009-02/msg00013.html
17 .\" ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
18 .ie \n(.g .ds Aq \(aq
19 .el       .ds Aq '
20 .\" -----------------------------------------------------------------
21 .\" * set default formatting
22 .\" -----------------------------------------------------------------
23 .\" disable hyphenation
24 .nh
25 .\" disable justification (adjust text to left margin only)
26 .ad l
27 .\" -----------------------------------------------------------------
28 .\" * MAIN CONTENT STARTS HERE *
29 .\" -----------------------------------------------------------------
30 .SH "NAME"
31 ALTER_SEQUENCE \- change the definition of a sequence generator
32 .SH "SYNOPSIS"
33 .sp
34 .nf
35 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR
36     [ AS \fIdata_type\fR ]
37     [ INCREMENT [ BY ] \fIincrement\fR ]
38     [ MINVALUE \fIminvalue\fR | NO MINVALUE ] [ MAXVALUE \fImaxvalue\fR | NO MAXVALUE ]
39     [ [ NO ] CYCLE ]
40     [ START [ WITH ] \fIstart\fR ]
41     [ RESTART [ [ WITH ] \fIrestart\fR ] ]
42     [ CACHE \fIcache\fR ]
43     [ OWNED BY { \fItable_name\fR\&.\fIcolumn_name\fR | NONE } ]
44 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR SET { LOGGED | UNLOGGED }
45 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR OWNER TO { \fInew_owner\fR | CURRENT_ROLE | CURRENT_USER | SESSION_USER }
46 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR RENAME TO \fInew_name\fR
47 ALTER SEQUENCE [ IF EXISTS ] \fIname\fR SET SCHEMA \fInew_schema\fR
48 .fi
49 .SH "DESCRIPTION"
50 .PP
51 \fBALTER SEQUENCE\fR
52 changes the parameters of an existing sequence generator\&. Any parameters not specifically set in the
53 \fBALTER SEQUENCE\fR
54 command retain their prior settings\&.
55 .PP
56 You must own the sequence to use
57 \fBALTER SEQUENCE\fR\&. To change a sequence\*(Aqs schema, you must also have
58 CREATE
59 privilege on the new schema\&. To alter the owner, you must be able to
60 SET ROLE
61 to the new owning role, and that role must have
62 CREATE
63 privilege on the sequence\*(Aqs schema\&. (These restrictions enforce that altering the owner doesn\*(Aqt do anything you couldn\*(Aqt do by dropping and recreating the sequence\&. However, a superuser can alter ownership of any sequence anyway\&.)
64 .SH "PARAMETERS"
65 .PP
66 .PP
67 \fIname\fR
68 .RS 4
69 The name (optionally schema\-qualified) of a sequence to be altered\&.
70 .RE
71 .PP
72 IF EXISTS
73 .RS 4
74 Do not throw an error if the sequence does not exist\&. A notice is issued in this case\&.
75 .RE
76 .PP
77 \fIdata_type\fR
78 .RS 4
79 The optional clause
80 AS \fIdata_type\fR
81 changes the data type of the sequence\&. Valid types are
82 smallint,
83 integer, and
84 bigint\&.
85 .sp
86 Changing the data type automatically changes the minimum and maximum values of the sequence if and only if the previous minimum and maximum values were the minimum or maximum value of the old data type (in other words, if the sequence had been created using
87 NO MINVALUE
88 or
89 NO MAXVALUE, implicitly or explicitly)\&. Otherwise, the minimum and maximum values are preserved, unless new values are given as part of the same command\&. If the minimum and maximum values do not fit into the new data type, an error will be generated\&.
90 .RE
91 .PP
92 \fIincrement\fR
93 .RS 4
94 The clause
95 INCREMENT BY \fIincrement\fR
96 is optional\&. A positive value will make an ascending sequence, a negative one a descending sequence\&. If unspecified, the old increment value will be maintained\&.
97 .RE
98 .PP
99 \fIminvalue\fR
100 .br
101 NO MINVALUE
102 .RS 4
103 The optional clause
104 MINVALUE \fIminvalue\fR
105 determines the minimum value a sequence can generate\&. If
106 NO MINVALUE
107 is specified, the defaults of 1 and the minimum value of the data type for ascending and descending sequences, respectively, will be used\&. If neither option is specified, the current minimum value will be maintained\&.
108 .RE
109 .PP
110 \fImaxvalue\fR
111 .br
112 NO MAXVALUE
113 .RS 4
114 The optional clause
115 MAXVALUE \fImaxvalue\fR
116 determines the maximum value for the sequence\&. If
117 NO MAXVALUE
118 is specified, the defaults of the maximum value of the data type and \-1 for ascending and descending sequences, respectively, will be used\&. If neither option is specified, the current maximum value will be maintained\&.
119 .RE
120 .PP
121 CYCLE
122 .RS 4
123 The optional
124 CYCLE
125 key word can be used to enable the sequence to wrap around when the
126 \fImaxvalue\fR
127 or
128 \fIminvalue\fR
129 has been reached by an ascending or descending sequence respectively\&. If the limit is reached, the next number generated will be the
130 \fIminvalue\fR
131 or
132 \fImaxvalue\fR, respectively\&.
133 .RE
134 .PP
135 NO CYCLE
136 .RS 4
137 If the optional
138 NO CYCLE
139 key word is specified, any calls to
140 \fBnextval\fR
141 after the sequence has reached its maximum value will return an error\&. If neither
142 CYCLE
143 or
144 NO CYCLE
145 are specified, the old cycle behavior will be maintained\&.
146 .RE
147 .PP
148 \fIstart\fR
149 .RS 4
150 The optional clause
151 START WITH \fIstart\fR
152 changes the recorded start value of the sequence\&. This has no effect on the
153 \fIcurrent\fR
154 sequence value; it simply sets the value that future
155 \fBALTER SEQUENCE RESTART\fR
156 commands will use\&.
157 .RE
158 .PP
159 \fIrestart\fR
160 .RS 4
161 The optional clause
162 RESTART [ WITH \fIrestart\fR ]
163 changes the current value of the sequence\&. This is similar to calling the
164 \fBsetval\fR
165 function with
166 is_called
167 =
168 false: the specified value will be returned by the
169 \fInext\fR
170 call of
171 \fBnextval\fR\&. Writing
172 RESTART
173 with no
174 \fIrestart\fR
175 value is equivalent to supplying the start value that was recorded by
176 \fBCREATE SEQUENCE\fR
177 or last set by
178 \fBALTER SEQUENCE START WITH\fR\&.
179 .sp
180 In contrast to a
181 \fBsetval\fR
182 call, a
183 RESTART
184 operation on a sequence is transactional and blocks concurrent transactions from obtaining numbers from the same sequence\&. If that\*(Aqs not the desired mode of operation,
185 \fBsetval\fR
186 should be used\&.
187 .RE
188 .PP
189 \fIcache\fR
190 .RS 4
191 The clause
192 CACHE \fIcache\fR
193 enables sequence numbers to be preallocated and stored in memory for faster access\&. The minimum value is 1 (only one value can be generated at a time, i\&.e\&., no cache)\&. If unspecified, the old cache value will be maintained\&.
194 .RE
195 .PP
196 SET { LOGGED | UNLOGGED }
197 .RS 4
198 This form changes the sequence from unlogged to logged or vice\-versa (see
199 CREATE SEQUENCE (\fBCREATE_SEQUENCE\fR(7)))\&. It cannot be applied to a temporary sequence\&.
200 .RE
201 .PP
202 OWNED BY \fItable_name\fR\&.\fIcolumn_name\fR
203 .br
204 OWNED BY NONE
205 .RS 4
206 The
207 OWNED BY
208 option causes the sequence to be associated with a specific table column, such that if that column (or its whole table) is dropped, the sequence will be automatically dropped as well\&. If specified, this association replaces any previously specified association for the sequence\&. The specified table must have the same owner and be in the same schema as the sequence\&. Specifying
209 OWNED BY NONE
210 removes any existing association, making the sequence
211 \(lqfree\-standing\(rq\&.
212 .RE
213 .PP
214 \fInew_owner\fR
215 .RS 4
216 The user name of the new owner of the sequence\&.
217 .RE
218 .PP
219 \fInew_name\fR
220 .RS 4
221 The new name for the sequence\&.
222 .RE
223 .PP
224 \fInew_schema\fR
225 .RS 4
226 The new schema for the sequence\&.
227 .RE
228 .SH "NOTES"
229 .PP
230 \fBALTER SEQUENCE\fR
231 will not immediately affect
232 \fBnextval\fR
233 results in backends, other than the current one, that have preallocated (cached) sequence values\&. They will use up all cached values prior to noticing the changed sequence generation parameters\&. The current backend will be affected immediately\&.
234 .PP
235 \fBALTER SEQUENCE\fR
236 does not affect the
237 \fBcurrval\fR
238 status for the sequence\&. (Before
239 PostgreSQL
240 8\&.3, it sometimes did\&.)
241 .PP
242 \fBALTER SEQUENCE\fR
243 blocks concurrent
244 \fBnextval\fR,
245 \fBcurrval\fR,
246 \fBlastval\fR, and
247 \fBsetval\fR
248 calls\&.
249 .PP
250 For historical reasons,
251 \fBALTER TABLE\fR
252 can be used with sequences too; but the only variants of
253 \fBALTER TABLE\fR
254 that are allowed with sequences are equivalent to the forms shown above\&.
255 .SH "EXAMPLES"
256 .PP
257 Restart a sequence called
258 serial, at 105:
259 .sp
260 .if n \{\
261 .RS 4
262 .\}
263 .nf
264 ALTER SEQUENCE serial RESTART WITH 105;
265 .fi
266 .if n \{\
267 .RE
268 .\}
269 .SH "COMPATIBILITY"
270 .PP
271 \fBALTER SEQUENCE\fR
272 conforms to the
273 SQL
274 standard, except for the
275 AS,
276 START WITH,
277 OWNED BY,
278 OWNER TO,
279 RENAME TO, and
280 SET SCHEMA
281 clauses, which are
282 PostgreSQL
283 extensions\&.
284 .SH "SEE ALSO"
285 CREATE SEQUENCE (\fBCREATE_SEQUENCE\fR(7)), DROP SEQUENCE (\fBDROP_SEQUENCE\fR(7))